Skip to content

Commit 3e981e8

Browse files
feature: add sock:getfd().
1 parent eac2a10 commit 3e981e8

File tree

1 file changed

+32
-0
lines changed

1 file changed

+32
-0
lines changed

lib/resty/core/socket.lua

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -63,6 +63,10 @@ ngx_http_lua_ffi_socket_tcp_get_sslhandshake_result(ngx_http_request_t *r,
6363

6464
void
6565
ngx_http_lua_ffi_ssl_free_session(void *sess);
66+
67+
int
68+
ngx_http_lua_ffi_socket_tcp_getfd(ngx_http_request_t *r,
69+
ngx_http_lua_socket_tcp_upstream_t *u, char **errmsg);
6670
]]
6771

6872
ngx_lua_ffi_socket_tcp_getoption = C.ngx_http_lua_ffi_socket_tcp_getoption
@@ -293,12 +297,40 @@ local function sslhandshake(cosocket, reused_session, server_name, ssl_verify,
293297
end
294298

295299

300+
local function getfd(cosocket)
301+
if not cosocket then
302+
error("ngx.socket getfd: expecting the cosocket object, but seen none")
303+
end
304+
305+
local r = get_request()
306+
if not r then
307+
error("no request found")
308+
end
309+
310+
local u = get_tcp_socket(cosocket)
311+
312+
local fd = C.ngx_http_lua_ffi_socket_tcp_getfd(r, u, errmsg)
313+
if (fd < 0) then
314+
return nil, ffi_str(errmsg[0])
315+
end
316+
317+
return fd;
318+
end
319+
320+
296321
do
297322
local method_table = registry.__tcp_cosocket_mt
298323
method_table.getoption = getoption
299324
method_table.setoption = setoption
300325
method_table.setclientcert = setclientcert
301326
method_table.sslhandshake = sslhandshake
327+
method_table.getfd = getfd
328+
329+
method_table = registry.__tcp_req_cosocket_mt
330+
method_table.getfd = getfd
331+
332+
method_table = registry.__tcp_raw_req_cosocket_mt
333+
method_table.getfd = getfd
302334
end
303335

304336
elseif subsystem == 'stream' then

0 commit comments

Comments
 (0)