Skip to content

Commit c4b86fb

Browse files
feature: add ngx.req.get_original_addr.
1 parent c7c72c0 commit c4b86fb

File tree

3 files changed

+37
-0
lines changed

3 files changed

+37
-0
lines changed

lib/resty/core/request.lua

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,8 @@ local ngx_lua_ffi_req_start_time
4242
if subsystem == "stream" then
4343
ffi.cdef[[
4444
double ngx_stream_lua_ffi_req_start_time(ngx_stream_lua_request_t *r);
45+
int ngx_stream_lua_ffi_req_dst_addr(ngx_stream_lua_request_t *r, char *buf,
46+
size_t *buf_size, char *errbuf, size_t *errbuf_size);
4547
]]
4648

4749
ngx_lua_ffi_req_start_time = C.ngx_stream_lua_ffi_req_start_time
@@ -66,6 +68,24 @@ end
6668

6769

6870
if subsystem == "stream" then
71+
local ERR_BUF_SIZE = 256
72+
73+
local buf = get_string_buf(128, 1)
74+
local buf_sizep = ffi_new("size_t[1]")
75+
76+
function ngx.req.get_original_addr()
77+
local r = get_request()
78+
local err = get_string_buf(ERR_BUF_SIZE)
79+
local errlenp = get_size_ptr()
80+
buf_sizep[0] = 128
81+
local rc = C.ngx_stream_lua_ffi_req_dst_addr(r, buf, buf_sizep, err, errlenp)
82+
if tonumber(rc) ~= 0 then
83+
return nil, tostring(err, errlenp[0])
84+
end
85+
86+
return ffi_str(buf, buf_sizep[0])
87+
end
88+
6989
return _M
7090
end
7191

t/process-type-hup.t

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -86,3 +86,4 @@ init_worker_by_lua(nginx.conf:48):6: process type: privileged
8686
--- no_error_log
8787
[error]
8888
--- skip_nginx: 4: < 1.11.2
89+
--- wait: 0.2

t/stream/request.t

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,3 +37,19 @@ qr/\[TRACE\s+\d+ content_by_lua\(nginx\.conf:\d+\):3 loop\]/
3737
[error]
3838
bad argument type
3939
stitch
40+
41+
42+
43+
=== TEST 2: get dst address
44+
--- stream_server_config
45+
content_by_lua_block {
46+
local dst_addr = ngx.req.get_original_addr()
47+
ngx.say("origin addr: ", dst_addr)
48+
}
49+
--- stream_response eval
50+
qr/127.0.0.1:\d+/
51+
--- no_error_log
52+
[error]
53+
[alert]
54+
[crit]
55+
[crit2]

0 commit comments

Comments
 (0)