diff --git a/.travis.yml b/.travis.yml index ff8d82049..bceca5404 100644 --- a/.travis.yml +++ b/.travis.yml @@ -71,7 +71,7 @@ install: - git clone https://github.com/openresty/set-misc-nginx-module.git ../set-misc-nginx-module - git clone https://github.com/openresty/mockeagain.git - git clone https://github.com/openresty/test-nginx.git - - git clone https://github.com/openresty/stream-lua-nginx-module.git ../stream-lua-nginx-module + - git clone -b port-ngx-lua-v0.10.15-changes https://github.com/thibaultcha/stream-lua-nginx-module.git ../stream-lua-nginx-module script: - cd luajit2/ diff --git a/lib/resty/core.lua b/lib/resty/core.lua index 57c9d175d..449f847b0 100644 --- a/lib/resty/core.lua +++ b/lib/resty/core.lua @@ -3,22 +3,22 @@ local subsystem = ngx.config.subsystem +require "resty.core.ctx" +require "resty.core.var" +require "resty.core.worker" require "resty.core.regex" require "resty.core.shdict" require "resty.core.time" require "resty.core.misc" +require "resty.core.hash" +require "resty.core.uri" +require "resty.core.exit" +require "resty.core.base64" if subsystem == 'http' then - require "resty.core.uri" - require "resty.core.hash" - require "resty.core.base64" - require "resty.core.exit" - require "resty.core.var" - require "resty.core.ctx" require "resty.core.request" require "resty.core.response" - require "resty.core.worker" require "resty.core.phase" require "resty.core.ndk" end diff --git a/lib/resty/core/base64.lua b/lib/resty/core/base64.lua index 981424cc7..8a0e463b4 100644 --- a/lib/resty/core/base64.lua +++ b/lib/resty/core/base64.lua @@ -1,21 +1,28 @@ -- Copyright (C) Yichun Zhang (agentzh) -local ffi = require 'ffi' +local ffi = require "ffi" local base = require "resty.core.base" -local ffi_string = ffi.string + local C = ffi.C +local ffi_string = ffi.string local ngx = ngx local type = type -local tostring = tostring local error = error +local floor = math.floor +local tostring = tostring local get_string_buf = base.get_string_buf local get_size_ptr = base.get_size_ptr -local floor = math.floor +local subsystem = ngx.config.subsystem -ffi.cdef[[ +local ngx_lua_ffi_encode_base64 +local ngx_lua_ffi_decode_base64 + + +if subsystem == "http" then + ffi.cdef[[ size_t ngx_http_lua_ffi_encode_base64(const unsigned char *src, size_t len, unsigned char *dst, int no_padding); @@ -23,7 +30,25 @@ ffi.cdef[[ int ngx_http_lua_ffi_decode_base64(const unsigned char *src, size_t len, unsigned char *dst, size_t *dlen); -]] + ]] + + ngx_lua_ffi_encode_base64 = C.ngx_http_lua_ffi_encode_base64 + ngx_lua_ffi_decode_base64 = C.ngx_http_lua_ffi_decode_base64 + +elseif subsystem == "stream" then + ffi.cdef[[ + size_t ngx_stream_lua_ffi_encode_base64(const unsigned char *src, + size_t len, unsigned char *dst, + int no_padding); + + int ngx_stream_lua_ffi_decode_base64(const unsigned char *src, + size_t len, unsigned char *dst, + size_t *dlen); + ]] + + ngx_lua_ffi_encode_base64 = C.ngx_stream_lua_ffi_encode_base64 + ngx_lua_ffi_decode_base64 = C.ngx_stream_lua_ffi_decode_base64 +end local function base64_encoded_length(len, no_padding) @@ -57,8 +82,7 @@ ngx.encode_base64 = function (s, no_padding) local dlen = base64_encoded_length(slen, no_padding_bool) local dst = get_string_buf(dlen) - local r_dlen = C.ngx_http_lua_ffi_encode_base64(s, slen, dst, - no_padding_int) + local r_dlen = ngx_lua_ffi_encode_base64(s, slen, dst, no_padding_int) -- if dlen ~= r_dlen then error("discrepancy in len") end return ffi_string(dst, r_dlen) end @@ -78,7 +102,7 @@ ngx.decode_base64 = function (s) -- print("dlen: ", tonumber(dlen)) local dst = get_string_buf(dlen) local pdlen = get_size_ptr() - local ok = C.ngx_http_lua_ffi_decode_base64(s, slen, dst, pdlen) + local ok = ngx_lua_ffi_decode_base64(s, slen, dst, pdlen) if ok == 0 then return nil end diff --git a/lib/resty/core/ctx.lua b/lib/resty/core/ctx.lua index e348a7eb4..0683aaaf9 100644 --- a/lib/resty/core/ctx.lua +++ b/lib/resty/core/ctx.lua @@ -1,28 +1,47 @@ -- Copyright (C) Yichun Zhang (agentzh) -local ffi = require 'ffi' -local debug = require 'debug' +local ffi = require "ffi" +local debug = require "debug" local base = require "resty.core.base" local misc = require "resty.core.misc" +local C = ffi.C local register_getter = misc.register_ngx_magic_key_getter local register_setter = misc.register_ngx_magic_key_setter local registry = debug.getregistry() local new_tab = base.new_tab local ref_in_table = base.ref_in_table local get_request = base.get_request -local C = ffi.C local FFI_NO_REQ_CTX = base.FFI_NO_REQ_CTX local FFI_OK = base.FFI_OK local error = error +local subsystem = ngx.config.subsystem + + +local ngx_lua_ffi_get_ctx_ref +local ngx_lua_ffi_set_ctx_ref -ffi.cdef[[ -int ngx_http_lua_ffi_get_ctx_ref(ngx_http_request_t *r); -int ngx_http_lua_ffi_set_ctx_ref(ngx_http_request_t *r, int ref); -]] +if subsystem == "http" then + ffi.cdef[[ + int ngx_http_lua_ffi_get_ctx_ref(ngx_http_request_t *r); + int ngx_http_lua_ffi_set_ctx_ref(ngx_http_request_t *r, int ref); + ]] + + ngx_lua_ffi_get_ctx_ref = C.ngx_http_lua_ffi_get_ctx_ref + ngx_lua_ffi_set_ctx_ref = C.ngx_http_lua_ffi_set_ctx_ref + +elseif subsystem == "stream" then + ffi.cdef[[ + int ngx_stream_lua_ffi_get_ctx_ref(ngx_stream_lua_request_t *r); + int ngx_stream_lua_ffi_set_ctx_ref(ngx_stream_lua_request_t *r, int ref); + ]] + + ngx_lua_ffi_get_ctx_ref = C.ngx_stream_lua_ffi_get_ctx_ref + ngx_lua_ffi_set_ctx_ref = C.ngx_stream_lua_ffi_set_ctx_ref +end local _M = { @@ -37,7 +56,7 @@ local function get_ctx_table() error("no request found") end - local ctx_ref = C.ngx_http_lua_ffi_get_ctx_ref(r) + local ctx_ref = ngx_lua_ffi_get_ctx_ref(r) if ctx_ref == FFI_NO_REQ_CTX then error("no request ctx found") end @@ -46,7 +65,7 @@ local function get_ctx_table() if ctx_ref < 0 then local ctx = new_tab(0, 4) ctx_ref = ref_in_table(ctxs, ctx) - if C.ngx_http_lua_ffi_set_ctx_ref(r, ctx_ref) ~= FFI_OK then + if ngx_lua_ffi_set_ctx_ref(r, ctx_ref) ~= FFI_OK then return nil end return ctx @@ -63,7 +82,7 @@ local function set_ctx_table(ctx) error("no request found") end - local ctx_ref = C.ngx_http_lua_ffi_get_ctx_ref(r) + local ctx_ref = ngx_lua_ffi_get_ctx_ref(r) if ctx_ref == FFI_NO_REQ_CTX then error("no request ctx found") end @@ -71,7 +90,7 @@ local function set_ctx_table(ctx) local ctxs = registry.ngx_lua_ctx_tables if ctx_ref < 0 then ctx_ref = ref_in_table(ctxs, ctx) - C.ngx_http_lua_ffi_set_ctx_ref(r, ctx_ref) + ngx_lua_ffi_set_ctx_ref(r, ctx_ref) return end ctxs[ctx_ref] = ctx diff --git a/lib/resty/core/exit.lua b/lib/resty/core/exit.lua index f9f00f791..30a7b613c 100644 --- a/lib/resty/core/exit.lua +++ b/lib/resty/core/exit.lua @@ -1,22 +1,40 @@ -- Copyright (C) Yichun Zhang (agentzh) -local ffi = require 'ffi' -local ffi_string = ffi.string +local ffi = require "ffi" +local base = require "resty.core.base" + + local C = ffi.C +local ffi_string = ffi.string local ngx = ngx local error = error -local base = require "resty.core.base" local get_string_buf = base.get_string_buf local get_size_ptr = base.get_size_ptr local get_request = base.get_request local co_yield = coroutine._yield +local subsystem = ngx.config.subsystem -ffi.cdef[[ +local ngx_lua_ffi_exit + + +if subsystem == "http" then + ffi.cdef[[ int ngx_http_lua_ffi_exit(ngx_http_request_t *r, int status, unsigned char *err, size_t *errlen); -]] + ]] + + ngx_lua_ffi_exit = C.ngx_http_lua_ffi_exit + +elseif subsystem == "stream" then + ffi.cdef[[ + int ngx_stream_lua_ffi_exit(ngx_stream_lua_request_t *r, int status, + unsigned char *err, size_t *errlen); + ]] + + ngx_lua_ffi_exit = C.ngx_stream_lua_ffi_exit +end local ERR_BUF_SIZE = 128 @@ -31,7 +49,7 @@ ngx.exit = function (rc) error("no request found") end errlen[0] = ERR_BUF_SIZE - rc = C.ngx_http_lua_ffi_exit(r, rc, err, errlen) + rc = ngx_lua_ffi_exit(r, rc, err, errlen) if rc == 0 then -- print("yielding...") return co_yield() diff --git a/lib/resty/core/hash.lua b/lib/resty/core/hash.lua index de97270c3..4a09b009d 100644 --- a/lib/resty/core/hash.lua +++ b/lib/resty/core/hash.lua @@ -1,18 +1,27 @@ -- Copyright (C) Yichun Zhang (agentzh) -local ffi = require 'ffi' -local ffi_string = ffi.string -local ffi_new = ffi.new +local ffi = require "ffi" +local base = require "resty.core.base" + + local C = ffi.C +local ffi_new = ffi.new +local ffi_string = ffi.string local ngx = ngx local type = type -local tostring = tostring local error = error -local base = require "resty.core.base" +local tostring = tostring +local subsystem = ngx.config.subsystem + + +local ngx_lua_ffi_md5 +local ngx_lua_ffi_md5_bin +local ngx_lua_ffi_sha1_bin -ffi.cdef[[ +if subsystem == "http" then + ffi.cdef[[ void ngx_http_lua_ffi_md5_bin(const unsigned char *src, size_t len, unsigned char *dst); @@ -21,7 +30,28 @@ ffi.cdef[[ int ngx_http_lua_ffi_sha1_bin(const unsigned char *src, size_t len, unsigned char *dst); -]] + ]] + + ngx_lua_ffi_md5 = C.ngx_http_lua_ffi_md5 + ngx_lua_ffi_md5_bin = C.ngx_http_lua_ffi_md5_bin + ngx_lua_ffi_sha1_bin = C.ngx_http_lua_ffi_sha1_bin + +elseif subsystem == "stream" then + ffi.cdef[[ + void ngx_stream_lua_ffi_md5_bin(const unsigned char *src, size_t len, + unsigned char *dst); + + void ngx_stream_lua_ffi_md5(const unsigned char *src, size_t len, + unsigned char *dst); + + int ngx_stream_lua_ffi_sha1_bin(const unsigned char *src, size_t len, + unsigned char *dst); + ]] + + ngx_lua_ffi_md5 = C.ngx_stream_lua_ffi_md5 + ngx_lua_ffi_md5_bin = C.ngx_stream_lua_ffi_md5_bin + ngx_lua_ffi_sha1_bin = C.ngx_stream_lua_ffi_sha1_bin +end local MD5_DIGEST_LEN = 16 @@ -35,7 +65,7 @@ ngx.md5_bin = function (s) s = tostring(s) end end - C.ngx_http_lua_ffi_md5_bin(s, #s, md5_buf) + ngx_lua_ffi_md5_bin(s, #s, md5_buf) return ffi_string(md5_buf, MD5_DIGEST_LEN) end @@ -51,7 +81,7 @@ ngx.md5 = function (s) s = tostring(s) end end - C.ngx_http_lua_ffi_md5(s, #s, md5_hex_buf) + ngx_lua_ffi_md5(s, #s, md5_hex_buf) return ffi_string(md5_hex_buf, MD5_HEX_DIGEST_LEN) end @@ -67,7 +97,7 @@ ngx.sha1_bin = function (s) s = tostring(s) end end - local ok = C.ngx_http_lua_ffi_sha1_bin(s, #s, sha_buf) + local ok = ngx_lua_ffi_sha1_bin(s, #s, sha_buf) if ok == 0 then error("SHA-1 support missing in Nginx") end diff --git a/lib/resty/core/misc.lua b/lib/resty/core/misc.lua index 8caab2633..ff7954a2b 100644 --- a/lib/resty/core/misc.lua +++ b/lib/resty/core/misc.lua @@ -6,107 +6,98 @@ local ffi = require "ffi" local os = require "os" -local FFI_OK = base.FFI_OK -local FFI_NO_REQ_CTX = base.FFI_NO_REQ_CTX -local FFI_BAD_CONTEXT = base.FFI_BAD_CONTEXT -local get_string_buf = base.get_string_buf -local get_string_buf_size = base.get_string_buf_size -local get_size_ptr = base.get_size_ptr -local new_tab = base.new_tab local C = ffi.C local ffi_new = ffi.new local ffi_str = ffi.string -local setmetatable = setmetatable -local rawget = rawget -local rawset = rawset local ngx = ngx -local get_request = base.get_request local type = type local error = error +local rawget = rawget +local rawset = rawset local tonumber = tonumber +local setmetatable = setmetatable +local FFI_OK = base.FFI_OK +local FFI_NO_REQ_CTX = base.FFI_NO_REQ_CTX +local FFI_BAD_CONTEXT = base.FFI_BAD_CONTEXT +local new_tab = base.new_tab +local get_request = base.get_request +local get_size_ptr = base.get_size_ptr +local get_string_buf = base.get_string_buf +local get_string_buf_size = base.get_string_buf_size local subsystem = ngx.config.subsystem -local _M +local ngx_lua_ffi_get_resp_status local ngx_lua_ffi_get_conf_env +local ngx_magic_key_getters +local ngx_magic_key_setters -if subsystem == 'http' then - _M = new_tab(0, 3) +local _M = new_tab(0, 3) +local ngx_mt = new_tab(0, 2) - local ngx_magic_key_getters = new_tab(0, 4) - local ngx_magic_key_setters = new_tab(0, 2) +if subsystem == "http" then + ngx_magic_key_getters = new_tab(0, 4) + ngx_magic_key_setters = new_tab(0, 2) - - local function register_getter(key, func) - ngx_magic_key_getters[key] = func - end - _M.register_ngx_magic_key_getter = register_getter +elseif subsystem == "stream" then + ngx_magic_key_getters = new_tab(0, 2) + ngx_magic_key_setters = new_tab(0, 1) +end - local function register_setter(key, func) - ngx_magic_key_setters[key] = func - end - _M.register_ngx_magic_key_setter = register_setter +local function register_getter(key, func) + ngx_magic_key_getters[key] = func +end +_M.register_ngx_magic_key_getter = register_getter - local mt = new_tab(0, 2) +local function register_setter(key, func) + ngx_magic_key_setters[key] = func +end +_M.register_ngx_magic_key_setter = register_setter - mt.__index = function (tb, key) - local f = ngx_magic_key_getters[key] - if f then - return f() - end - return rawget(tb, key) +ngx_mt.__index = function (tb, key) + local f = ngx_magic_key_getters[key] + if f then + return f() end + return rawget(tb, key) +end - mt.__newindex = function (tb, key, ctx) - local f = ngx_magic_key_setters[key] - if f then - return f(ctx) - end - return rawset(tb, key, ctx) +ngx_mt.__newindex = function (tb, key, ctx) + local f = ngx_magic_key_setters[key] + if f then + return f(ctx) end + return rawset(tb, key, ctx) +end - setmetatable(ngx, mt) +setmetatable(ngx, ngx_mt) +if subsystem == "http" then ffi.cdef[[ -int ngx_http_lua_ffi_get_resp_status(ngx_http_request_t *r); -int ngx_http_lua_ffi_set_resp_status(ngx_http_request_t *r, int r); -int ngx_http_lua_ffi_is_subrequest(ngx_http_request_t *r); -int ngx_http_lua_ffi_headers_sent(ngx_http_request_t *r); -int ngx_http_lua_ffi_get_conf_env(const unsigned char *name, - unsigned char **env_buf, size_t *name_len); + int ngx_http_lua_ffi_get_resp_status(ngx_http_request_t *r); + int ngx_http_lua_ffi_set_resp_status(ngx_http_request_t *r, int r); + int ngx_http_lua_ffi_is_subrequest(ngx_http_request_t *r); + int ngx_http_lua_ffi_headers_sent(ngx_http_request_t *r); + int ngx_http_lua_ffi_get_conf_env(const unsigned char *name, + unsigned char **env_buf, + size_t *name_len); ]] + ngx_lua_ffi_get_resp_status = C.ngx_http_lua_ffi_get_resp_status ngx_lua_ffi_get_conf_env = C.ngx_http_lua_ffi_get_conf_env -- ngx.status - local function get_status() - local r = get_request() - - if not r then - error("no request found") - end - - local rc = C.ngx_http_lua_ffi_get_resp_status(r) - - if rc == FFI_BAD_CONTEXT then - error("API disabled in the current context", 2) - end - - return rc - end - register_getter("status", get_status) - local function set_status(status) local r = get_request() @@ -132,6 +123,7 @@ int ngx_http_lua_ffi_get_conf_env(const unsigned char *name, -- ngx.is_subrequest + local function is_subreq() local r = get_request() @@ -152,6 +144,7 @@ int ngx_http_lua_ffi_get_conf_env(const unsigned char *name, -- ngx.headers_sent + local function headers_sent() local r = get_request() @@ -173,20 +166,40 @@ int ngx_http_lua_ffi_get_conf_env(const unsigned char *name, end register_getter("headers_sent", headers_sent) -elseif subsystem == 'stream' then - _M = new_tab(0, 1) - - +elseif subsystem == "stream" then ffi.cdef[[ -int ngx_stream_lua_ffi_get_conf_env(const unsigned char *name, - unsigned char **env_buf, size_t *name_len); + int ngx_stream_lua_ffi_get_resp_status(ngx_stream_lua_request_t *r); + int ngx_stream_lua_ffi_get_conf_env(const unsigned char *name, + unsigned char **env_buf, + size_t *name_len); ]] - + ngx_lua_ffi_get_resp_status = C.ngx_stream_lua_ffi_get_resp_status ngx_lua_ffi_get_conf_env = C.ngx_stream_lua_ffi_get_conf_env end +-- ngx.status + + +local function get_status() + local r = get_request() + + if not r then + error("no request found") + end + + local rc = ngx_lua_ffi_get_resp_status(r) + + if rc == FFI_BAD_CONTEXT then + error("API disabled in the current context", 2) + end + + return rc +end +register_getter("status", get_status) + + do local _getenv = os.getenv local env_ptr = ffi_new("unsigned char *[1]") diff --git a/lib/resty/core/uri.lua b/lib/resty/core/uri.lua index af0e65572..27522e4d2 100644 --- a/lib/resty/core/uri.lua +++ b/lib/resty/core/uri.lua @@ -1,17 +1,26 @@ -- Copyright (C) Yichun Zhang (agentzh) -local ffi = require 'ffi' -local ffi_string = ffi.string +local ffi = require "ffi" +local base = require "resty.core.base" + + local C = ffi.C +local ffi_string = ffi.string local ngx = ngx local type = type local tostring = tostring -local base = require "resty.core.base" local get_string_buf = base.get_string_buf +local subsystem = ngx.config.subsystem + +local ngx_lua_ffi_escape_uri +local ngx_lua_ffi_unescape_uri +local ngx_lua_ffi_uri_escaped_length -ffi.cdef[[ + +if subsystem == "http" then + ffi.cdef[[ size_t ngx_http_lua_ffi_uri_escaped_length(const unsigned char *src, size_t len); @@ -20,7 +29,28 @@ ffi.cdef[[ size_t ngx_http_lua_ffi_unescape_uri(const unsigned char *src, size_t len, unsigned char *dst); -]] + ]] + + ngx_lua_ffi_escape_uri = C.ngx_http_lua_ffi_escape_uri + ngx_lua_ffi_unescape_uri = C.ngx_http_lua_ffi_unescape_uri + ngx_lua_ffi_uri_escaped_length = C.ngx_http_lua_ffi_uri_escaped_length + +elseif subsystem == "stream" then + ffi.cdef[[ + size_t ngx_stream_lua_ffi_uri_escaped_length(const unsigned char *src, + size_t len); + + void ngx_stream_lua_ffi_escape_uri(const unsigned char *src, size_t len, + unsigned char *dst); + + size_t ngx_stream_lua_ffi_unescape_uri(const unsigned char *src, + size_t len, unsigned char *dst); + ]] + + ngx_lua_ffi_escape_uri = C.ngx_stream_lua_ffi_escape_uri + ngx_lua_ffi_unescape_uri = C.ngx_stream_lua_ffi_unescape_uri + ngx_lua_ffi_uri_escaped_length = C.ngx_stream_lua_ffi_uri_escaped_length +end ngx.escape_uri = function (s) @@ -32,13 +62,13 @@ ngx.escape_uri = function (s) end end local slen = #s - local dlen = C.ngx_http_lua_ffi_uri_escaped_length(s, slen) + local dlen = ngx_lua_ffi_uri_escaped_length(s, slen) -- print("dlen: ", tonumber(dlen)) if dlen == slen then return s end local dst = get_string_buf(dlen) - C.ngx_http_lua_ffi_escape_uri(s, slen, dst) + ngx_lua_ffi_escape_uri(s, slen, dst) return ffi_string(dst, dlen) end @@ -54,7 +84,7 @@ ngx.unescape_uri = function (s) local slen = #s local dlen = slen local dst = get_string_buf(dlen) - dlen = C.ngx_http_lua_ffi_unescape_uri(s, slen, dst) + dlen = ngx_lua_ffi_unescape_uri(s, slen, dst) return ffi_string(dst, dlen) end diff --git a/lib/resty/core/var.lua b/lib/resty/core/var.lua index ff2fa158e..ea9c7635a 100644 --- a/lib/resty/core/var.lua +++ b/lib/resty/core/var.lua @@ -1,20 +1,27 @@ -- Copyright (C) Yichun Zhang (agentzh) -local ffi = require 'ffi' +local ffi = require "ffi" local base = require "resty.core.base" + +local C = ffi.C local ffi_new = ffi.new local ffi_str = ffi.string -local C = ffi.C local type = type +local error = error +local tostring = tostring +local setmetatable = setmetatable local get_request = base.get_request local get_string_buf = base.get_string_buf local get_size_ptr = base.get_size_ptr local new_tab = base.new_tab -local error = error -local tostring = tostring -local setmetatable = setmetatable +local subsystem = ngx.config.subsystem + + +local ngx_lua_ffi_var_get +local ngx_lua_ffi_var_set + local ERR_BUF_SIZE = 256 @@ -22,7 +29,8 @@ local ERR_BUF_SIZE = 256 ngx.var = new_tab(0, 0) -ffi.cdef[[ +if subsystem == "http" then + ffi.cdef[[ int ngx_http_lua_ffi_var_get(ngx_http_request_t *r, const char *name_data, size_t name_len, char *lowcase_buf, int capture_id, char **value, size_t *value_len, char **err); @@ -31,7 +39,26 @@ ffi.cdef[[ const unsigned char *name_data, size_t name_len, unsigned char *lowcase_buf, const unsigned char *value, size_t value_len, unsigned char *errbuf, size_t *errlen); -]] + ]] + + ngx_lua_ffi_var_get = C.ngx_http_lua_ffi_var_get + ngx_lua_ffi_var_set = C.ngx_http_lua_ffi_var_set + +elseif subsystem == "stream" then + ffi.cdef[[ + int ngx_stream_lua_ffi_var_get(ngx_stream_lua_request_t *r, + const char *name_data, size_t name_len, char *lowcase_buf, + int capture_id, char **value, size_t *value_len, char **err); + + int ngx_stream_lua_ffi_var_set(ngx_stream_lua_request_t *r, + const unsigned char *name_data, size_t name_len, + unsigned char *lowcase_buf, const unsigned char *value, + size_t value_len, unsigned char *errbuf, size_t *errlen); + ]] + + ngx_lua_ffi_var_get = C.ngx_stream_lua_ffi_var_get + ngx_lua_ffi_var_set = C.ngx_stream_lua_ffi_var_set +end local value_ptr = ffi_new("unsigned char *[1]") @@ -47,8 +74,8 @@ local function var_get(self, name) local value_len = get_size_ptr() local rc if type(name) == "number" then - rc = C.ngx_http_lua_ffi_var_get(r, nil, 0, nil, name, value_ptr, - value_len, errmsg) + rc = ngx_lua_ffi_var_get(r, nil, 0, nil, name, value_ptr, value_len, + errmsg) else if type(name) ~= "string" then @@ -58,8 +85,8 @@ local function var_get(self, name) local name_len = #name local lowcase_buf = get_string_buf(name_len) - rc = C.ngx_http_lua_ffi_var_get(r, name, name_len, lowcase_buf, 0, - value_ptr, value_len, errmsg) + rc = ngx_lua_ffi_var_get(r, name, name_len, lowcase_buf, 0, value_ptr, + value_len, errmsg) end -- ngx.log(ngx.WARN, "rc = ", rc) @@ -104,8 +131,8 @@ local function var_set(self, name, value) end local errbuf = lowcase_buf + name_len - local rc = C.ngx_http_lua_ffi_var_set(r, name, name_len, lowcase_buf, - value, value_len, errbuf, errlen) + local rc = ngx_lua_ffi_var_set(r, name, name_len, lowcase_buf, value, + value_len, errbuf, errlen) -- ngx.log(ngx.WARN, "rc = ", rc) diff --git a/lib/resty/core/worker.lua b/lib/resty/core/worker.lua index 150e32f6c..c336debdb 100644 --- a/lib/resty/core/worker.lua +++ b/lib/resty/core/worker.lua @@ -1,37 +1,64 @@ -- Copyright (C) Yichun Zhang (agentzh) -local ffi = require 'ffi' +local ffi = require "ffi" local base = require "resty.core.base" local C = ffi.C local new_tab = base.new_tab +local subsystem = ngx.config.subsystem + + +local ngx_lua_ffi_worker_id +local ngx_lua_ffi_worker_pid +local ngx_lua_ffi_worker_count +local ngx_lua_ffi_worker_exiting ngx.worker = new_tab(0, 4) -ffi.cdef[[ -int ngx_http_lua_ffi_worker_pid(void); -int ngx_http_lua_ffi_worker_exiting(void); -int ngx_http_lua_ffi_worker_id(void); -int ngx_http_lua_ffi_worker_count(void); -]] +if subsystem == "http" then + ffi.cdef[[ + int ngx_http_lua_ffi_worker_id(void); + int ngx_http_lua_ffi_worker_pid(void); + int ngx_http_lua_ffi_worker_count(void); + int ngx_http_lua_ffi_worker_exiting(void); + ]] + + ngx_lua_ffi_worker_id = C.ngx_http_lua_ffi_worker_id + ngx_lua_ffi_worker_pid = C.ngx_http_lua_ffi_worker_pid + ngx_lua_ffi_worker_count = C.ngx_http_lua_ffi_worker_count + ngx_lua_ffi_worker_exiting = C.ngx_http_lua_ffi_worker_exiting + +elseif subsystem == "stream" then + ffi.cdef[[ + int ngx_stream_lua_ffi_worker_id(void); + int ngx_stream_lua_ffi_worker_pid(void); + int ngx_stream_lua_ffi_worker_count(void); + int ngx_stream_lua_ffi_worker_exiting(void); + ]] + + ngx_lua_ffi_worker_id = C.ngx_stream_lua_ffi_worker_id + ngx_lua_ffi_worker_pid = C.ngx_stream_lua_ffi_worker_pid + ngx_lua_ffi_worker_count = C.ngx_stream_lua_ffi_worker_count + ngx_lua_ffi_worker_exiting = C.ngx_stream_lua_ffi_worker_exiting +end function ngx.worker.exiting() - return C.ngx_http_lua_ffi_worker_exiting() ~= 0 + return ngx_lua_ffi_worker_exiting() ~= 0 end function ngx.worker.pid() - return C.ngx_http_lua_ffi_worker_pid() + return ngx_lua_ffi_worker_pid() end function ngx.worker.id() - local id = C.ngx_http_lua_ffi_worker_id() + local id = ngx_lua_ffi_worker_id() if id < 0 then return nil end @@ -41,7 +68,7 @@ end function ngx.worker.count() - return C.ngx_http_lua_ffi_worker_count() + return ngx_lua_ffi_worker_count() end diff --git a/t/stream/os-getenv.t b/t/stream/os-getenv.t index 5816f37ab..d41e52241 100644 --- a/t/stream/os-getenv.t +++ b/t/stream/os-getenv.t @@ -2,7 +2,7 @@ use lib '.'; use t::TestCore::Stream; -plan tests => repeat_each() * (blocks() * 2 + 2); +plan tests => repeat_each() * (blocks() * 2 + 1); $ENV{TEST_NGINX_BAR} = 'world'; $ENV{TEST_NGINX_LUA_PACKAGE_PATH} = "$t::TestCore::Stream::lua_package_path"; @@ -154,81 +154,38 @@ in content:\s+ === TEST 8: os.getenv() overwrite is reverted in worker phases ---- stream_config - lua_package_path "$TEST_NGINX_LUA_PACKAGE_PATH"; - lua_load_resty_core off; - - init_by_lua_block { - package.loaded.os_getenv = os.getenv - require "resty.core" - package.loaded.is_os_getenv = os.getenv == package.loaded.os_getenv - } ---- stream_server_config - content_by_lua_block { - os.getenv("") - - ngx.say("in init: ", package.loaded.is_os_getenv, "\n", - "in content: ", os.getenv == package.loaded.os_getenv) - } ---- stream_response -in init: false -in content: true - - - -=== TEST 9: os.getenv() can be localized before loading resty.core --- main_config env FOO=hello; --- stream_config lua_package_path "$TEST_NGINX_LUA_PACKAGE_PATH"; - lua_load_resty_core off; init_by_lua_block { - package.loaded.os_getenv = os.getenv - require "resty.core" - - do - local getenv = os.getenv - - package.loaded.f = function () - ngx.log(ngx.NOTICE, "FOO: ", getenv("FOO")) - end - end - - package.loaded.f() - - package.loaded.is_os_getenv = os.getenv == package.loaded.os_getenv + package.loaded.init_os_getenv = os.getenv } --- stream_server_config content_by_lua_block { - package.loaded.f() - package.loaded.f() + ngx.say("FOO=", os.getenv("FOO")) - ngx.say("in init: ", package.loaded.is_os_getenv, "\n", - "in content: ", os.getenv == package.loaded.os_getenv) + if os.getenv ~= package.loaded.init_os_getenv then + ngx.say("os.getenv() overwrite was reverted") + + else + ngx.say("os.getenv() overwrite was not reverted") + end } --- stream_response -in init: false -in content: true ---- grep_error_log eval -qr/FOO: [a-z]+/ ---- grep_error_log_out -FOO: hello -FOO: hello -FOO: hello +FOO=hello +os.getenv() overwrite was reverted -=== TEST 10: os.getenv() can be localized after loading resty.core +=== TEST 9: os.getenv() can be localized after loading resty.core --- main_config env FOO=hello; --- stream_config lua_package_path "$TEST_NGINX_LUA_PACKAGE_PATH"; - lua_load_resty_core off; init_by_lua_block { - package.loaded.os_getenv = os.getenv - do local getenv = os.getenv @@ -248,15 +205,18 @@ env FOO=hello; package.loaded.f() package.loaded.f() - ngx.say("in init: ", package.loaded.is_os_getenv, "\n", - "in content: ", os.getenv == package.loaded.os_getenv) + if os.getenv ~= package.loaded.init_os_getenv then + ngx.say("os.getenv() overwrite was reverted") + + else + ngx.say("os.getenv() overwrite was not reverted") + end } --- stream_response -in init: false -in content: false +os.getenv() overwrite was reverted --- grep_error_log eval qr/FOO: [a-z]+/ --- grep_error_log_out -FOO: nil +FOO: hello FOO: hello FOO: hello