Skip to content

Commit 2d734cb

Browse files
committed
feature(socket.tcp): allow second parameter is nil and default zero
1 parent 6789886 commit 2d734cb

File tree

5 files changed

+110
-5
lines changed

5 files changed

+110
-5
lines changed

README.markdown

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7227,7 +7227,7 @@ See also [ngx.socket.udp](#ngxsocketudp).
72277227
tcpsock:connect
72287228
---------------
72297229

7230-
**syntax:** *ok, err = tcpsock:connect(host, port, options_table?)*
7230+
**syntax:** *ok, err = tcpsock:connect(host, port?, options_table?)*
72317231

72327232
**syntax:** *ok, err = tcpsock:connect("unix:/path/to/unix-domain.socket", options_table?)*
72337233

@@ -7246,6 +7246,8 @@ Both IP addresses and domain names can be specified as the `host` argument. In c
72467246

72477247
If the nameserver returns multiple IP addresses for the host name, this method will pick up one randomly.
72487248

7249+
`port` is optional and accept nil(for compatibility with host is a unix domain), and it default value is zero.
7250+
72497251
In case of error, the method returns `nil` followed by a string describing the error. In case of success, the method returns `1`.
72507252

72517253
Here is an example for connecting to a TCP server:

doc/HttpLuaModule.wiki

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6115,7 +6115,7 @@ See also [[#ngx.socket.udp|ngx.socket.udp]].
61156115
61166116
== tcpsock:connect ==
61176117
6118-
'''syntax:''' ''ok, err = tcpsock:connect(host, port, options_table?)''
6118+
'''syntax:''' ''ok, err = tcpsock:connect(host, port?, options_table?)''
61196119
61206120
'''syntax:''' ''ok, err = tcpsock:connect("unix:/path/to/unix-domain.socket", options_table?)''
61216121
@@ -6133,6 +6133,8 @@ Both IP addresses and domain names can be specified as the <code>host</code> arg
61336133
61346134
If the nameserver returns multiple IP addresses for the host name, this method will pick up one randomly.
61356135
6136+
<code>port</code> is optional and accept nil(for compatibility with host is a unix domain), and it default value is zero.
6137+
61366138
In case of error, the method returns <code>nil</code> followed by a string describing the error. In case of success, the method returns <code>1</code>.
61376139
61386140
Here is an example for connecting to a TCP server:

src/ngx_http_lua_socket_tcp.c

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -974,7 +974,7 @@ ngx_http_lua_socket_tcp_connect(lua_State *L)
974974
n--;
975975
}
976976

977-
if (n == 3) {
977+
if (n == 3 && !lua_isnil(L, 3)) {
978978
port = luaL_checkinteger(L, 3);
979979

980980
if (port < 0 || port > 65535) {
@@ -991,7 +991,7 @@ ngx_http_lua_socket_tcp_connect(lua_State *L)
991991

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

994-
} else { /* n == 2 */
994+
} else { /* n == 2 || lua_isnil(L, 3) */
995995
port = 0;
996996
}
997997

t/058-tcp-socket.t

Lines changed: 34 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() * 222;
7+
plan tests => repeat_each() * 225;
88

99
our $HtmlDir = html_dir;
1010

@@ -4158,3 +4158,36 @@ orld
41584158
[error]
41594159
--- error_log
41604160
lua tcp socket calling receiveany() method to read at most 7 bytes
4161+
4162+
4163+
4164+
=== TEST 70: port is optional and accept nil, default is 0
4165+
--- config
4166+
server_tokens off;
4167+
location = /t {
4168+
set $port $TEST_NGINX_SERVER_PORT;
4169+
content_by_lua_block {
4170+
local sock = ngx.socket.tcp()
4171+
sock:settimeout(500)
4172+
local ok, err = sock:connect("127.0.0.1")
4173+
if ok then
4174+
ngx.say("connect success")
4175+
return
4176+
end
4177+
4178+
local ok, err = sock:connect("127.0.0.1", nil)
4179+
if ok then
4180+
ngx.say("connect success")
4181+
return
4182+
end
4183+
4184+
ngx.say("ok")
4185+
}
4186+
}
4187+
4188+
--- request
4189+
GET /t
4190+
--- response_body
4191+
ok
4192+
--- error_log
4193+
connect to 127.0.0.1:0

t/059-unix-socket.t

Lines changed: 68 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -201,3 +201,71 @@ received:
201201
received: foo
202202
failed to receive a line: closed
203203
close: 1 nil
204+
205+
206+
207+
=== TEST 5: second parameter is nil
208+
--- http_config
209+
server {
210+
listen unix:$TEST_NGINX_HTML_DIR/nginx.sock;
211+
default_type 'text/plain';
212+
213+
server_tokens off;
214+
location /foo {
215+
content_by_lua 'ngx.say("foo")';
216+
more_clear_headers Date;
217+
}
218+
}
219+
--- config
220+
location /test {
221+
content_by_lua '
222+
local sock = ngx.socket.tcp()
223+
local ok, err = sock:connect("unix:$TEST_NGINX_HTML_DIR/nginx.sock", nil)
224+
if not ok then
225+
ngx.say("failed to connect: ", err)
226+
return
227+
end
228+
229+
ngx.say("connected: ", ok)
230+
231+
local req = "GET /foo HTTP/1.0\\r\\nHost: localhost\\r\\nConnection: close\\r\\n\\r\\n"
232+
-- req = "OK"
233+
234+
local bytes, err = sock:send(req)
235+
if not bytes then
236+
ngx.say("failed to send request: ", err)
237+
return
238+
end
239+
240+
ngx.say("request sent: ", bytes)
241+
242+
while true do
243+
print("calling receive")
244+
local line, err = sock:receive()
245+
if line then
246+
ngx.say("received: ", line)
247+
248+
else
249+
ngx.say("failed to receive a line: ", err)
250+
break
251+
end
252+
end
253+
254+
ok, err = sock:close()
255+
ngx.say("close: ", ok, " ", err)
256+
';
257+
}
258+
--- request
259+
GET /test
260+
--- response_body
261+
connected: 1
262+
request sent: 57
263+
received: HTTP/1.1 200 OK
264+
received: Server: nginx
265+
received: Content-Type: text/plain
266+
received: Content-Length: 4
267+
received: Connection: close
268+
received:
269+
received: foo
270+
failed to receive a line: closed
271+
close: 1 nil

0 commit comments

Comments
 (0)