Skip to content

Commit 3978310

Browse files
doujiang24agentzh
authored andcommitted
feature: tcpsock:connect(): allows the options_table argument being nil.
Signed-off-by: Yichun Zhang (agentzh) <agentzh@gmail.com>
1 parent 5fdbb56 commit 3978310

File tree

2 files changed

+57
-1
lines changed

2 files changed

+57
-1
lines changed

src/ngx_http_lua_socket_tcp.c

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -501,6 +501,12 @@ ngx_http_lua_socket_tcp_connect(lua_State *L)
501501
n--;
502502
}
503503

504+
/* the fourth argument is not a table */
505+
if (n == 4) {
506+
lua_pop(L, 1);
507+
n--;
508+
}
509+
504510
if (n == 3) {
505511
port = luaL_checkinteger(L, 3);
506512

t/058-tcp-socket.t

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

55
repeat_each(2);
66

7-
plan tests => repeat_each() * 187;
7+
plan tests => repeat_each() * 190;
88

99
our $HtmlDir = html_dir;
1010

@@ -3640,3 +3640,53 @@ failed to receive a line: closed []
36403640
close: 1 nil
36413641
--- error_log
36423642
lua http cleanup reuse
3643+
3644+
3645+
3646+
=== TEST 60: options_table is nil
3647+
--- config
3648+
location /t {
3649+
set $port $TEST_NGINX_MEMCACHED_PORT;
3650+
3651+
content_by_lua_block {
3652+
local sock = ngx.socket.tcp()
3653+
local port = ngx.var.port
3654+
3655+
local ok, err = sock:connect("127.0.0.1", port, nil)
3656+
if not ok then
3657+
ngx.say("failed to connect: ", err)
3658+
return
3659+
end
3660+
3661+
ngx.say("connected: ", ok)
3662+
3663+
local req = "flush_all\r\n"
3664+
3665+
local bytes, err = sock:send(req)
3666+
if not bytes then
3667+
ngx.say("failed to send request: ", err)
3668+
return
3669+
end
3670+
ngx.say("request sent: ", bytes)
3671+
3672+
local line, err, part = sock:receive()
3673+
if line then
3674+
ngx.say("received: ", line)
3675+
3676+
else
3677+
ngx.say("failed to receive a line: ", err, " [", part, "]")
3678+
end
3679+
3680+
ok, err = sock:close()
3681+
ngx.say("close: ", ok, " ", err)
3682+
}
3683+
}
3684+
--- request
3685+
GET /t
3686+
--- response_body
3687+
connected: 1
3688+
request sent: 11
3689+
received: OK
3690+
close: 1 nil
3691+
--- no_error_log
3692+
[error]

0 commit comments

Comments
 (0)