Skip to content

Commit fd54825

Browse files
feature: add ffi api ngx_stream_lua_ffi_socket_tcp_getfd.
1 parent 7999496 commit fd54825

File tree

3 files changed

+133
-2
lines changed

3 files changed

+133
-2
lines changed

src/ngx_stream_lua_socket_tcp.c

Lines changed: 31 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -271,6 +271,8 @@ static char ngx_stream_lua_ssl_session_metatable_key;
271271
#endif
272272

273273
#define ngx_stream_lua_tcp_socket_metatable_literal_key "__tcp_cosocket_mt"
274+
#define ngx_stream_lua_raw_req_socket_metatable_literal_key \
275+
"__tcp_raw_req_cosocket_mt"
274276

275277

276278
void
@@ -308,7 +310,7 @@ ngx_stream_lua_inject_socket_tcp_api(ngx_log_t *log, lua_State *L)
308310
/* {{{raw req socket object metatable */
309311
lua_pushlightuserdata(L, ngx_stream_lua_lightudata_mask(
310312
raw_req_socket_metatable_key));
311-
lua_createtable(L, 0 /* narr */, 9 /* nrec */);
313+
lua_createtable(L, 0 /* narr */, 10 /* nrec */);
312314

313315
lua_pushcfunction(L, ngx_stream_lua_socket_tcp_receive);
314316
lua_setfield(L, -2, "receive");
@@ -344,6 +346,12 @@ ngx_stream_lua_inject_socket_tcp_api(ngx_log_t *log, lua_State *L)
344346
lua_pushvalue(L, -1);
345347
lua_setfield(L, -2, "__index");
346348

349+
lua_rawset(L, LUA_REGISTRYINDEX);
350+
351+
lua_pushliteral(L, ngx_stream_lua_raw_req_socket_metatable_literal_key);
352+
lua_pushlightuserdata(L, ngx_stream_lua_lightudata_mask(
353+
raw_req_socket_metatable_key));
354+
lua_rawget(L, LUA_REGISTRYINDEX);
347355
lua_rawset(L, LUA_REGISTRYINDEX);
348356
/* }}} */
349357

@@ -6759,4 +6767,26 @@ ngx_stream_lua_cleanup_conn_pools(lua_State *L)
67596767
lua_pop(L, 1);
67606768
}
67616769

6770+
6771+
int
6772+
ngx_stream_lua_ffi_socket_tcp_getfd(ngx_stream_lua_request_t *r,
6773+
ngx_stream_lua_socket_tcp_upstream_t *u, const char **errmsg)
6774+
{
6775+
int fd;
6776+
6777+
*errmsg = NULL;
6778+
6779+
if (u == NULL || u->peer.connection == NULL) {
6780+
*errmsg = "closed";
6781+
return -1;
6782+
}
6783+
6784+
fd = u->peer.connection->fd;
6785+
if (fd == -1) {
6786+
*errmsg = "faked connection";
6787+
}
6788+
6789+
return fd;
6790+
}
6791+
67626792
/* vi:set ft=c ts=4 sw=4 et fdm=marker: */

t/058-tcp-socket.t

Lines changed: 67 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ use Test::Nginx::Socket::Lua::Stream;
44

55
repeat_each(2);
66

7-
plan tests => repeat_each() * 224;
7+
plan tests => repeat_each() * 227;
88

99
our $HtmlDir = html_dir;
1010

@@ -3601,3 +3601,69 @@ connected: 1
36013601
setkeepalive: 1
36023602
--- no_error_log
36033603
[error]
3604+
3605+
3606+
3607+
=== TEST 69: getfd()
3608+
--- stream_server_config
3609+
content_by_lua_block {
3610+
local sock = ngx.socket.tcp()
3611+
local ok, err = sock:connect("127.0.0.1", $TEST_NGINX_SERVER_PORT)
3612+
if not ok then
3613+
ngx.say("failed to connect: ", err)
3614+
return
3615+
end
3616+
3617+
ngx.say("connected: ", ok)
3618+
ngx.say("fd: ", sock:getfd())
3619+
3620+
local req = "GET /foo HTTP/1.0\r\nHost: localhost\r\nConnection: close\r\n\r\n"
3621+
-- req = "OK"
3622+
3623+
local bytes, err = sock:send(req)
3624+
if not bytes then
3625+
ngx.say("failed to send request: ", err)
3626+
return
3627+
end
3628+
3629+
ngx.say("request sent: ", bytes)
3630+
3631+
while true do
3632+
local line, err, part = sock:receive()
3633+
if line then
3634+
ngx.say("received: ", line)
3635+
3636+
else
3637+
ngx.say("failed to receive a line: ", err, " [", part, "]")
3638+
break
3639+
end
3640+
end
3641+
3642+
ok, err = sock:close()
3643+
ngx.say("close: ", ok, " ", err)
3644+
}
3645+
3646+
--- config
3647+
server_tokens off;
3648+
3649+
location /foo {
3650+
content_by_lua_block { ngx.say("foo") }
3651+
more_clear_headers Date;
3652+
}
3653+
3654+
--- stream_response eval
3655+
qr{connected: 1
3656+
fd: \d+
3657+
request sent: 57
3658+
received: HTTP/1.1 200 OK
3659+
received: Server: nginx
3660+
received: Content-Type: text/plain
3661+
received: Content-Length: 4
3662+
received: Connection: close
3663+
received:
3664+
received: foo
3665+
failed to receive a line: closed \[\]
3666+
close: 1 nil
3667+
}
3668+
--- no_error_log
3669+
[error]

t/067-req-socket.t

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1000,3 +1000,38 @@ bad argument #2 to '?' (bad max argument)
10001000
send data after read side timeout
10011001
--- error_log
10021002
receiveany unexpected err: timeout
1003+
1004+
1005+
1006+
=== TEST 17: getfd()
1007+
--- stream_server_config
1008+
content_by_lua_block {
1009+
local sock, err = ngx.req.socket()
1010+
if sock then
1011+
ngx.say("got the request socket")
1012+
else
1013+
ngx.say("failed to get the request socket: ", err)
1014+
return
1015+
end
1016+
1017+
ngx.say("fd: ", sock:getfd())
1018+
for i = 1, 3 do
1019+
local data, err, part = sock:receive(5)
1020+
if data then
1021+
ngx.say("received: ", data)
1022+
else
1023+
ngx.say("failed to receive: ", err, " [", part, "]")
1024+
end
1025+
end
1026+
}
1027+
--- stream_request chomp
1028+
hello world! my
1029+
--- stream_response eval
1030+
qr/got the request socket
1031+
fd: \d+
1032+
received: hello
1033+
received: worl
1034+
received: d! my
1035+
/
1036+
--- no_error_log
1037+
[error]

0 commit comments

Comments
 (0)