|
| 1 | +-- A module for sharing ngx.ctx between subrequests. |
| 2 | +-- Original work by Alex Zhang (openresty/lua-nginx-module/issues/1057) |
| 3 | +-- updated by 3scale/apicast. |
| 4 | +-- |
| 5 | +-- Copyright (c) 2016 3scale Inc. |
| 6 | +-- Licensed under the Apache License, Version 2.0. |
| 7 | +-- License text: See LICENSE |
| 8 | +-- |
| 9 | +-- Modifications by Kong Inc. |
| 10 | +-- * updated module functions signatures |
| 11 | +-- * made module function idempotent |
| 12 | +-- * replaced thrown errors with warn logs |
| 13 | +-- * allow passing of context |
| 14 | +-- * updated to work with new 1.19.x apis |
| 15 | + |
| 16 | +local ffi = require "ffi" |
| 17 | +local base = require "resty.core.base" |
| 18 | + |
| 19 | + |
| 20 | +local C = ffi.C |
| 21 | +local ngx = ngx |
| 22 | +local debug = require "debug" |
| 23 | +local tonumber = tonumber |
| 24 | +local registry = debug.getregistry() |
| 25 | +local subsystem = ngx.config.subsystem |
| 26 | + |
| 27 | + |
| 28 | +local ngx_lua_ffi_get_ctx_ref |
| 29 | +if subsystem == "http" then |
| 30 | + ngx_lua_ffi_get_ctx_ref = C.ngx_http_lua_ffi_get_ctx_ref |
| 31 | +elseif subsystem == "stream" then |
| 32 | + ngx_lua_ffi_get_ctx_ref = C.ngx_stream_lua_ffi_get_ctx_ref |
| 33 | +end |
| 34 | + |
| 35 | + |
| 36 | +local in_ssl_phase = ffi.new("int[1]") |
| 37 | +local ssl_ctx_ref = ffi.new("int[1]") |
| 38 | + |
| 39 | + |
| 40 | +local FFI_NO_REQ_CTX = base.FFI_NO_REQ_CTX |
| 41 | + |
| 42 | + |
| 43 | +local _M = { |
| 44 | + _VERSION = base.version |
| 45 | +} |
| 46 | + |
| 47 | +function _M.stash_ref(ctx) |
| 48 | + local r = base.get_request() |
| 49 | + if not r then |
| 50 | + ngx.log(ngx.WARN, "could not stash ngx.ctx ref: no request found") |
| 51 | + return |
| 52 | + end |
| 53 | + |
| 54 | + do |
| 55 | + local ctx_ref = ngx.var.ctx_ref |
| 56 | + if not ctx_ref or ctx_ref ~= "" then |
| 57 | + return |
| 58 | + end |
| 59 | + |
| 60 | + if not ctx then |
| 61 | + local _ = ngx.ctx -- load context if not previously loaded |
| 62 | + end |
| 63 | + end |
| 64 | + local ctx_ref = ngx_lua_ffi_get_ctx_ref(r, in_ssl_phase, ssl_ctx_ref) |
| 65 | + if ctx_ref == FFI_NO_REQ_CTX then |
| 66 | + ngx.log(ngx.WARN, "could not stash ngx.ctx ref: no ctx found") |
| 67 | + return |
| 68 | + end |
| 69 | + |
| 70 | + ngx.var.ctx_ref = ctx_ref |
| 71 | +end |
| 72 | + |
| 73 | + |
| 74 | +function _M.apply_ref() |
| 75 | + local r = base.get_request() |
| 76 | + if not r then |
| 77 | + ngx.log(ngx.WARN, "could not apply ngx.ctx: no request found") |
| 78 | + return |
| 79 | + end |
| 80 | + |
| 81 | + local ctx_ref = ngx.var.ctx_ref |
| 82 | + if not ctx_ref or ctx_ref == "" then |
| 83 | + return |
| 84 | + end |
| 85 | + |
| 86 | + ctx_ref = tonumber(ctx_ref) |
| 87 | + if not ctx_ref then |
| 88 | + return |
| 89 | + end |
| 90 | + |
| 91 | + local orig_ctx = registry.ngx_lua_ctx_tables[ctx_ref] |
| 92 | + if not orig_ctx then |
| 93 | + ngx.log(ngx.WARN, "could not apply ngx.ctx: no ctx found") |
| 94 | + return |
| 95 | + end |
| 96 | + |
| 97 | + ngx.ctx = orig_ctx |
| 98 | + ngx.var.ctx_ref = "" |
| 99 | +end |
| 100 | + |
| 101 | + |
| 102 | +return _M |
0 commit comments