Skip to content

Commit 53519cd

Browse files
spacewanderagentzh
authored andcommitted
feature: allowed sending boolean and nil values in cosockets.
Signed-off-by: Yichun Zhang (agentzh) <agentzh@gmail.com>
1 parent 6d4475a commit 53519cd

File tree

4 files changed

+191
-2
lines changed

4 files changed

+191
-2
lines changed

src/ngx_http_lua_socket_tcp.c

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2462,6 +2462,20 @@ ngx_http_lua_socket_tcp_send(lua_State *L)
24622462
len = ngx_http_lua_calc_strlen_in_table(L, 2, 2, 1 /* strict */);
24632463
break;
24642464

2465+
case LUA_TNIL:
2466+
len = sizeof("nil") - 1;
2467+
break;
2468+
2469+
case LUA_TBOOLEAN:
2470+
if (lua_toboolean(L, 2)) {
2471+
len = sizeof("true") - 1;
2472+
2473+
} else {
2474+
len = sizeof("false") - 1;
2475+
}
2476+
2477+
break;
2478+
24652479
default:
24662480
msg = lua_pushfstring(L, "string, number, boolean, nil, "
24672481
"or array table expected, got %s",
@@ -2497,6 +2511,29 @@ ngx_http_lua_socket_tcp_send(lua_State *L)
24972511
b->last = ngx_http_lua_copy_str_in_table(L, -1, b->last);
24982512
break;
24992513

2514+
case LUA_TNIL:
2515+
*b->last++ = 'n';
2516+
*b->last++ = 'i';
2517+
*b->last++ = 'l';
2518+
break;
2519+
2520+
case LUA_TBOOLEAN:
2521+
if (lua_toboolean(L, 2)) {
2522+
*b->last++ = 't';
2523+
*b->last++ = 'r';
2524+
*b->last++ = 'u';
2525+
*b->last++ = 'e';
2526+
2527+
} else {
2528+
*b->last++ = 'f';
2529+
*b->last++ = 'a';
2530+
*b->last++ = 'l';
2531+
*b->last++ = 's';
2532+
*b->last++ = 'e';
2533+
}
2534+
2535+
break;
2536+
25002537
default:
25012538
return luaL_error(L, "impossible to reach here");
25022539
}

src/ngx_http_lua_socket_udp.c

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -845,6 +845,20 @@ ngx_http_lua_socket_udp_send(lua_State *L)
845845
len = ngx_http_lua_calc_strlen_in_table(L, 2, 2, 1 /* strict */);
846846
break;
847847

848+
case LUA_TNIL:
849+
len = sizeof("nil") - 1;
850+
break;
851+
852+
case LUA_TBOOLEAN:
853+
if (lua_toboolean(L, 2)) {
854+
len = sizeof("true") - 1;
855+
856+
} else {
857+
len = sizeof("false") - 1;
858+
}
859+
860+
break;
861+
848862
default:
849863
msg = lua_pushfstring(L, "string, number, boolean, nil, "
850864
"or array table expected, got %s",
@@ -867,6 +881,32 @@ ngx_http_lua_socket_udp_send(lua_State *L)
867881
(void) ngx_http_lua_copy_str_in_table(L, 2, query.data);
868882
break;
869883

884+
case LUA_TNIL:
885+
p = query.data;
886+
*p++ = 'n';
887+
*p++ = 'i';
888+
*p++ = 'l';
889+
break;
890+
891+
case LUA_TBOOLEAN:
892+
p = query.data;
893+
894+
if (lua_toboolean(L, 2)) {
895+
*p++ = 't';
896+
*p++ = 'r';
897+
*p++ = 'u';
898+
*p++ = 'e';
899+
900+
} else {
901+
*p++ = 'f';
902+
*p++ = 'a';
903+
*p++ = 'l';
904+
*p++ = 's';
905+
*p++ = 'e';
906+
}
907+
908+
break;
909+
870910
default:
871911
return luaL_error(L, "impossible to reach here");
872912
}

t/058-tcp-socket.t

Lines changed: 63 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() * 196;
7+
plan tests => repeat_each() * 199;
88

99
our $HtmlDir = html_dir;
1010

@@ -3746,3 +3746,65 @@ GET /t
37463746
failed to connect: bad port number: 65536
37473747
--- no_error_log
37483748
[error]
3749+
3750+
3751+
3752+
=== TEST 63: send boolean and nil
3753+
--- config
3754+
location /t {
3755+
set $port $TEST_NGINX_SERVER_PORT;
3756+
3757+
content_by_lua_block {
3758+
local sock = ngx.socket.tcp()
3759+
local port = ngx.var.port
3760+
local ok, err = sock:connect("127.0.0.1", port)
3761+
if not ok then
3762+
ngx.say("failed to connect: ", err)
3763+
return
3764+
end
3765+
3766+
local function send(data)
3767+
local bytes, err = sock:send(data)
3768+
if not bytes then
3769+
ngx.say("failed to send request: ", err)
3770+
return
3771+
end
3772+
end
3773+
3774+
local req = "GET /foo HTTP/1.0\r\nHost: localhost\r\nConnection: close\r\nTest: "
3775+
send(req)
3776+
send(true)
3777+
send(false)
3778+
send(nil)
3779+
send("\r\n\r\n")
3780+
3781+
while true do
3782+
local line, err, part = sock:receive()
3783+
if line then
3784+
ngx.say("received: ", line)
3785+
else
3786+
break
3787+
end
3788+
end
3789+
3790+
ok, err = sock:close()
3791+
}
3792+
}
3793+
3794+
location /foo {
3795+
server_tokens off;
3796+
more_clear_headers Date;
3797+
echo $http_test;
3798+
}
3799+
3800+
--- request
3801+
GET /t
3802+
--- response_body
3803+
received: HTTP/1.1 200 OK
3804+
received: Server: nginx
3805+
received: Content-Type: text/plain
3806+
received: Connection: close
3807+
received:
3808+
received: truefalsenil
3809+
--- no_error_log
3810+
[error]

t/087-udp-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() * (3 * blocks() + 13);
7+
plan tests => repeat_each() * (3 * blocks() + 14);
88

99
our $HtmlDir = html_dir;
1010

@@ -1124,3 +1124,53 @@ GET /t
11241124
failed to connect: bad port number: 65536
11251125
--- no_error_log
11261126
[error]
1127+
1128+
1129+
1130+
=== TEST 21: send boolean and nil
1131+
--- config
1132+
server_tokens off;
1133+
location /t {
1134+
set $port $TEST_NGINX_MEMCACHED_PORT;
1135+
1136+
content_by_lua_block {
1137+
local socket = ngx.socket
1138+
local udp = socket.udp()
1139+
local port = ngx.var.port
1140+
udp:settimeout(1000) -- 1 sec
1141+
1142+
local ok, err = udp:setpeername("127.0.0.1", ngx.var.port)
1143+
if not ok then
1144+
ngx.say("failed to connect: ", err)
1145+
return
1146+
end
1147+
1148+
local function send(data)
1149+
local bytes, err = udp:send(data)
1150+
if not bytes then
1151+
ngx.say("failed to send: ", err)
1152+
return
1153+
end
1154+
ngx.say("sent ok")
1155+
end
1156+
1157+
send(true)
1158+
send(false)
1159+
send(nil)
1160+
}
1161+
}
1162+
--- request
1163+
GET /t
1164+
--- response_body
1165+
sent ok
1166+
sent ok
1167+
sent ok
1168+
--- no_error_log
1169+
[error]
1170+
--- grep_error_log eval
1171+
qr/send: fd:\d+ \d+ of \d+/
1172+
--- grep_error_log_out eval
1173+
qr/send: fd:\d+ 4 of 4
1174+
send: fd:\d+ 5 of 5
1175+
send: fd:\d+ 3 of 3/
1176+
--- log_level: debug

0 commit comments

Comments
 (0)