Skip to content

Commit

Permalink
feature: tcpsock:connect(): allows the options_table argument being nil.
Browse files Browse the repository at this point in the history
Signed-off-by: Yichun Zhang (agentzh) <agentzh@gmail.com>
  • Loading branch information
doujiang24 authored and agentzh committed Dec 29, 2016
1 parent 5fdbb56 commit 3978310
Show file tree
Hide file tree
Showing 2 changed files with 57 additions and 1 deletion.
6 changes: 6 additions & 0 deletions src/ngx_http_lua_socket_tcp.c
Expand Up @@ -501,6 +501,12 @@ ngx_http_lua_socket_tcp_connect(lua_State *L)
n--; n--;
} }


/* the fourth argument is not a table */
if (n == 4) {
lua_pop(L, 1);
n--;
}

if (n == 3) { if (n == 3) {
port = luaL_checkinteger(L, 3); port = luaL_checkinteger(L, 3);


Expand Down
52 changes: 51 additions & 1 deletion t/058-tcp-socket.t
Expand Up @@ -4,7 +4,7 @@ use Test::Nginx::Socket::Lua;


repeat_each(2); repeat_each(2);


plan tests => repeat_each() * 187; plan tests => repeat_each() * 190;


our $HtmlDir = html_dir; our $HtmlDir = html_dir;


Expand Down Expand Up @@ -3640,3 +3640,53 @@ failed to receive a line: closed []
close: 1 nil close: 1 nil
--- error_log --- error_log
lua http cleanup reuse lua http cleanup reuse



=== TEST 60: options_table is nil
--- config
location /t {
set $port $TEST_NGINX_MEMCACHED_PORT;

content_by_lua_block {
local sock = ngx.socket.tcp()
local port = ngx.var.port

local ok, err = sock:connect("127.0.0.1", port, nil)
if not ok then
ngx.say("failed to connect: ", err)
return
end

ngx.say("connected: ", ok)

local req = "flush_all\r\n"

local bytes, err = sock:send(req)
if not bytes then
ngx.say("failed to send request: ", err)
return
end
ngx.say("request sent: ", bytes)

local line, err, part = sock:receive()
if line then
ngx.say("received: ", line)

else
ngx.say("failed to receive a line: ", err, " [", part, "]")
end

ok, err = sock:close()
ngx.say("close: ", ok, " ", err)
}
}
--- request
GET /t
--- response_body
connected: 1
request sent: 11
received: OK
close: 1 nil
--- no_error_log
[error]

0 comments on commit 3978310

Please sign in to comment.