Skip to content

Allow port nil #2

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
33 changes: 30 additions & 3 deletions src/ngx_http_lua_socket_tcp.c
Original file line number Diff line number Diff line change
Expand Up @@ -970,8 +970,16 @@ ngx_http_lua_socket_tcp_connect(lua_State *L)
n--;
}

if (n == 3) {
port = luaL_checkinteger(L, 3);
/* most popular suit: host:port */
if (n == 3 && lua_isnumber(L, 3)) {

/* Hit the following parameter combination:
* sock:connect("127.0.0.1", port)
* sock:connect("127.0.0.1", port, opts)
* sock:connect("unix:/path", port)
* sock:connect("unix:/path", port, opts) */

port = (int) lua_tointeger(L, 3);

if (port < 0 || port > 65535) {
lua_pushnil(L);
Expand All @@ -987,8 +995,27 @@ ngx_http_lua_socket_tcp_connect(lua_State *L)

dd("socket key: %s", lua_tostring(L, -1));

} else { /* n == 2 */
} else if (len >= 5 && ngx_strncasecmp(p, (u_char *) "unix:", 5) == 0) {

/* Hit the following parameter combination:
* sock:connect("unix:/path")
* sock:connect("unix:/path", nil)
* sock:connect("unix:/path", opts)
* sock:connect("unix:/path", nil, opts) */

port = 0;

} else {

/* Ban the following parameter combination:
* sock:connect("127.0.0.1")
* sock:connect("127.0.0.1", nil)
* sock:connect("127.0.0.1", opts)
* sock:connect("127.0.0.1", nil, opts) */

lua_pushnil(L);
lua_pushfstring(L, "missing the port number");
return 2;
}

if (!custom_pool) {
Expand Down
2 changes: 1 addition & 1 deletion t/023-rewrite/unix-socket.t
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,7 @@ qr{\[crit\] .*? connect\(\) to unix:/tmp/nosuchfile\.sock failed}
--- request
GET /test
--- response_body
failed to connect: failed to parse host name "/tmp/test-nginx.sock": invalid host
failed to connect: missing the port number



Expand Down
46 changes: 43 additions & 3 deletions t/058-tcp-socket.t
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ use Test::Nginx::Socket::Lua;

repeat_each(2);

plan tests => repeat_each() * 228;
plan tests => repeat_each() * 231;

our $HtmlDir = html_dir;

Expand Down Expand Up @@ -3029,7 +3029,7 @@ qr/runtime error: content_by_lua\(nginx\.conf:\d+\):16: bad request/
--- user_files
>>> myfoo.lua
local sock = ngx.socket.tcp()
local ok, err = sock:connect("agentzh.org")
local ok, err = sock:connect("agentzh.org", 12345)
if not ok then
ngx.log(ngx.ERR, "failed to connect: ", err)
return
Expand Down Expand Up @@ -3059,7 +3059,7 @@ runtime error: attempt to yield across C-call boundary
end
local function err()
local sock = ngx.socket.tcp()
local ok, err = sock:connect("agentzh.org")
local ok, err = sock:connect("agentzh.org", 12345)
if not ok then
ngx.log(ngx.ERR, "failed to connect: ", err)
return
Expand Down Expand Up @@ -4327,3 +4327,43 @@ failed to receive a line: closed []
close: 1 nil
--- no_error_log
[error]



=== TEST 72: port is not number
--- config
server_tokens off;
location = /t {
set $port $TEST_NGINX_SERVER_PORT;
content_by_lua_block {
local sock = ngx.socket.tcp()
sock:settimeout(500)

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

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

local ok, err = sock:connect("127.0.0.1", {})
if not ok then
ngx.say("connect failed: ", err)
end

ngx.say("finish")
}
}

--- request
GET /t
--- response_body
connect failed: missing the port number
connect failed: missing the port number
connect failed: missing the port number
finish
--- no_error_log
[error]
138 changes: 137 additions & 1 deletion t/059-unix-socket.t
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@ qr{\[crit\] .*? connect\(\) to unix:/tmp/nosuchfile\.sock failed}
--- request
GET /test
--- response_body
failed to connect: failed to parse host name "/tmp/test-nginx.sock": invalid host
failed to connect: missing the port number



Expand Down Expand Up @@ -201,3 +201,139 @@ received:
received: foo
failed to receive a line: closed
close: 1 nil



=== TEST 5: port will be ignored
--- http_config
server {
listen unix:$TEST_NGINX_HTML_DIR/nginx.sock;
default_type 'text/plain';

server_tokens off;
location /foo {
content_by_lua 'ngx.say("foo")';
more_clear_headers Date;
}
}
--- config
location /test {
content_by_lua '
local sock = ngx.socket.tcp()
local ok, err = sock:connect("unix:$TEST_NGINX_HTML_DIR/nginx.sock", 80)
if not ok then
ngx.say("failed to connect: ", err)
return
end

ngx.say("connected: ", ok)

local req = "GET /foo HTTP/1.0\\r\\nHost: localhost\\r\\nConnection: close\\r\\n\\r\\n"
-- req = "OK"

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

ngx.say("request sent: ", bytes)

while true do
print("calling receive")
local line, err = sock:receive()
if line then
ngx.say("received: ", line)

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

ok, err = sock:close()
ngx.say("close: ", ok, " ", err)
';
}
--- request
GET /test
--- response_body
connected: 1
request sent: 57
received: HTTP/1.1 200 OK
received: Server: nginx
received: Content-Type: text/plain
received: Content-Length: 4
received: Connection: close
received:
received: foo
failed to receive a line: closed
close: 1 nil



=== TEST 6: second parameter is nil
--- http_config
server {
listen unix:$TEST_NGINX_HTML_DIR/nginx.sock;
default_type 'text/plain';

server_tokens off;
location /foo {
content_by_lua 'ngx.say("foo")';
more_clear_headers Date;
}
}
--- config
location /test {
content_by_lua '
local sock = ngx.socket.tcp()
local ok, err = sock:connect("unix:$TEST_NGINX_HTML_DIR/nginx.sock", nil)
if not ok then
ngx.say("failed to connect: ", err)
return
end

ngx.say("connected: ", ok)

local req = "GET /foo HTTP/1.0\\r\\nHost: localhost\\r\\nConnection: close\\r\\n\\r\\n"
-- req = "OK"

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

ngx.say("request sent: ", bytes)

while true do
print("calling receive")
local line, err = sock:receive()
if line then
ngx.say("received: ", line)

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

ok, err = sock:close()
ngx.say("close: ", ok, " ", err)
';
}
--- request
GET /test
--- response_body
connected: 1
request sent: 57
received: HTTP/1.1 200 OK
received: Server: nginx
received: Content-Type: text/plain
received: Content-Length: 4
received: Connection: close
received:
received: foo
failed to receive a line: closed
close: 1 nil
62 changes: 61 additions & 1 deletion t/068-socket-keepalive.t
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ use Test::Nginx::Socket::Lua;

#repeat_each(2);

plan tests => repeat_each() * (blocks() * 4 + 31);
plan tests => repeat_each() * (blocks() * 4 + 34);

our $HtmlDir = html_dir;

Expand Down Expand Up @@ -2956,3 +2956,63 @@ GET /t
lua tcp socket abort queueing
--- response_body
ok



=== TEST 53: custom pools in third parameters for unix domain socket
--- http_config eval
"
lua_package_path '$::HtmlDir/?.lua;./?.lua;;';
server {
listen unix:$::HtmlDir/nginx.sock;
default_type 'text/plain';

server_tokens off;
location /foo {
echo foo;
more_clear_headers Date;
}
}
"
--- config
location /t {
set $port $TEST_NGINX_MEMCACHED_PORT;
content_by_lua '
local test = require "test"
local path = "$TEST_NGINX_HTML_DIR/nginx.sock";
test.go(path, "A")
test.go(path, "B")
';
}
--- user_files
>>> test.lua
module("test", package.seeall)

function go(path, pool)
local sock = ngx.socket.tcp()
local ok, err = sock:connect("unix:" .. path, nil, {pool = pool})
if not ok then
ngx.say("failed to connect: ", err)
return
end

ngx.say("connected: ", ok, ", reused: ", sock:getreusedtimes())

local ok, err = sock:setkeepalive()
if not ok then
ngx.say("failed to set reusable: ", err)
end
end
--- request
GET /t
--- response_body
connected: 1, reused: 0
connected: 1, reused: 0
--- no_error_log eval
["[error]",
"lua tcp socket keepalive: free connection pool for ",
"lua tcp socket get keepalive peer: using connection"
]
--- error_log
lua tcp socket keepalive create connection pool for key "A"
lua tcp socket keepalive create connection pool for key "B"