Skip to content

feature: updated to support ngx_stream_lua without CFunction APIs. #269

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 9 commits into from
Closed
2 changes: 1 addition & 1 deletion .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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/
Expand Down
14 changes: 7 additions & 7 deletions lib/resty/core.lua
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
42 changes: 33 additions & 9 deletions lib/resty/core/base64.lua
Original file line number Diff line number Diff line change
@@ -1,29 +1,54 @@
-- 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);

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)
Expand Down Expand Up @@ -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
Expand All @@ -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
Expand Down
41 changes: 30 additions & 11 deletions lib/resty/core/ctx.lua
Original file line number Diff line number Diff line change
@@ -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 = {
Expand All @@ -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
Expand All @@ -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
Expand All @@ -63,15 +82,15 @@ 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

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
Expand Down
30 changes: 24 additions & 6 deletions lib/resty/core/exit.lua
Original file line number Diff line number Diff line change
@@ -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
Expand All @@ -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()
Expand Down
50 changes: 40 additions & 10 deletions lib/resty/core/hash.lua
Original file line number Diff line number Diff line change
@@ -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);

Expand All @@ -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
Expand All @@ -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

Expand All @@ -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

Expand All @@ -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
Expand Down
Loading