Skip to content

Commit 173546b

Browse files
author
alonbg
committed
through error on ngx.flush and ngx.echo in udp context + tests. style.
1 parent 92d3b01 commit 173546b

4 files changed

+60
-61
lines changed

src/ngx_stream_lua_output.c

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -61,7 +61,7 @@ ngx_stream_lua_ngx_echo(lua_State *L, unsigned newline)
6161
}
6262

6363
if (s->connection->type == SOCK_DGRAM) {
64-
return luaL_error(L, "api call is not supported under udp transport");
64+
return luaL_error(L, "not supported in udp requests");
6565
}
6666

6767
ctx = ngx_stream_get_module_ctx(s, ngx_stream_lua_module);
@@ -480,6 +480,10 @@ ngx_stream_lua_ngx_flush(lua_State *L)
480480

481481
s = ngx_stream_lua_get_session(L);
482482

483+
if (s->connection->type == SOCK_DGRAM) {
484+
return luaL_error(L, "not supported in udp requests");
485+
}
486+
483487
wait = 1; /* always wait */
484488

485489
ctx = ngx_stream_get_module_ctx(s, ngx_stream_lua_module);
@@ -567,6 +571,10 @@ ngx_stream_lua_ngx_eof(lua_State *L)
567571
return luaL_error(L, "no session object found");
568572
}
569573

574+
if (s->connection->type == SOCK_DGRAM) {
575+
return luaL_error(L, "not supported in udp requests");
576+
}
577+
570578
if (lua_gettop(L) != 0) {
571579
return luaL_error(L, "no argument is expected");
572580
}

src/ngx_stream_lua_socket_udp.c

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1096,7 +1096,6 @@ ngx_stream_lua_socket_udp_receive(lua_State *L)
10961096
ngx_stream_lua_srv_conf_t *lscf;
10971097

10981098
nargs = lua_gettop(L);
1099-
11001099
if (nargs != 1) {
11011100
return luaL_error(L, "expecting 1 arguments "
11021101
"(including the object), but got %d", nargs);
@@ -1167,9 +1166,10 @@ ngx_stream_lua_socket_udp_receive(lua_State *L)
11671166
ngx_stream_lua_socket_udp_handle_success(s, u);
11681167
u->connected = 1;
11691168
rc = NGX_OK;
1170-
} else {
1169+
1170+
} else {
11711171
rc = ngx_stream_lua_socket_udp_read(s, u);
1172-
}
1172+
}
11731173

11741174
if (rc == NGX_ERROR) {
11751175
dd("read failed: %d", (int) u->ft_type);
@@ -1354,7 +1354,6 @@ ngx_stream_lua_socket_udp_read(ngx_stream_session_t *s,
13541354
ssize_t n;
13551355

13561356
c = u->udp_connection.connection;
1357-
13581357
rev = c->read;
13591358

13601359
ngx_log_debug1(NGX_LOG_DEBUG_STREAM, c->log, 0,

t/138-req-udp-socket.t

Lines changed: 44 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -59,7 +59,7 @@ received: hello world! my
5959

6060
--- dgram_response
6161
--- error_log
62-
api call is not supported under udp transport
62+
not supported in udp requests
6363

6464

6565

@@ -73,11 +73,50 @@ api call is not supported under udp transport
7373

7474
--- dgram_response
7575
--- error_log
76-
api call is not supported under udp transport
76+
not supported in udp requests
7777

7878

7979

80-
=== TEST 4: receive (bad arg number)
80+
=== TEST 4: ngx.eof after ngx.req.udp_socket(true)
81+
--- dgram_server_config
82+
content_by_lua_block {
83+
local sock, err = ngx.req.udp_socket(true)
84+
if not sock then
85+
ngx.log(ngx.ERR, "failed to get raw request socket: ", err)
86+
return ngx.exit(ngx.ERROR)
87+
end
88+
assert(sock:send(""))
89+
ngx.eof()
90+
}
91+
92+
--- config
93+
server_tokens off;
94+
95+
--- dgram_response
96+
--- error_log
97+
not supported in udp requests
98+
99+
100+
101+
=== TEST 5: ngx.flush after ngx.udp_req.socket(true)
102+
--- dgram_server_config
103+
content_by_lua_block {
104+
local sock, err = ngx.req.udp_socket(true)
105+
if not sock then
106+
ngx.log(ngx.ERR, "failed to get raw request socket: ", err)
107+
return ngx.exit(ngx.ERROR)
108+
end
109+
assert(sock:send(""))
110+
ngx.flush()
111+
}
112+
113+
--- dgram_response
114+
--- error_log
115+
not supported in udp requests
116+
117+
118+
119+
=== TEST 6: receive (bad arg number)
81120
--- dgram_server_config
82121
content_by_lua_block {
83122
local sock, err = ngx.req.udp_socket()
@@ -91,7 +130,7 @@ expecting 1 arguments (including the object), but got 2
91130

92131

93132

94-
=== TEST 5: failing reread after reading timeout happens
133+
=== TEST 7: failing reread after reading timeout happens
95134
--- SKIP
96135
--- dgram_server_config
97136
content_by_lua_block {
@@ -117,7 +156,7 @@ stream lua udp socket read timed out
117156

118157

119158

120-
=== TEST 6: req socket GC'd
159+
=== TEST 8: req socket GC'd
121160
--- dgram_server_config
122161
content_by_lua_block {
123162
do

t/140-raw-req-udp-socket.t

Lines changed: 4 additions & 51 deletions
Original file line numberDiff line numberDiff line change
@@ -106,54 +106,7 @@ ok
106106
107107
108108
109-
=== TEST 4: ngx.eof after ngx.req.udp_socket(true)
110-
--- dgram_server_config
111-
content_by_lua_block {
112-
local sock, err = ngx.req.udp_socket(true)
113-
if not sock then
114-
ngx.log(ngx.ERR, "failed to get raw request socket: ", err)
115-
return ngx.exit(ngx.ERROR)
116-
end
117-
assert(sock:send(""))
118-
local ok, err = ngx.eof()
119-
if not ok then
120-
ngx.log(ngx.ERR, "failed to eof: ", err)
121-
return
122-
end
123-
}
124-
125-
--- config
126-
server_tokens off;
127-
128-
--- dgram_response
129-
--- no_error_log
130-
[error]
131-
132-
133-
134-
=== TEST 5: ngx.flush after ngx.udp_req.socket(true)
135-
--- dgram_server_config
136-
content_by_lua_block {
137-
local sock, err = ngx.req.udp_socket(true)
138-
if not sock then
139-
ngx.log(ngx.ERR, "failed to get raw request socket: ", err)
140-
return ngx.exit(ngx.ERROR)
141-
end
142-
assert(sock:send(""))
143-
local ok, err = ngx.flush()
144-
if not ok then
145-
ngx.log(ngx.ERR, "failed to flush: ", err)
146-
return
147-
end
148-
}
149-
150-
--- dgram_response
151-
--- no_error_log
152-
[error]
153-
154-
155-
156-
=== TEST 6: receive timeout
109+
=== TEST 4: receive timeout
157110
--- SKIP
158111
--- dgram_server_config
159112
content_by_lua_block {
@@ -179,7 +132,7 @@ stream lua udp socket read timed out
179132
180133
181134
182-
=== TEST 7: on_abort called during ngx.sleep()
135+
=== TEST 5: on_abort called during ngx.sleep()
183136
--- dgram_server_config
184137
lua_check_client_abort on;
185138
@@ -227,7 +180,7 @@ mysock handler aborted
227180
228181
229182
230-
=== TEST 8: on_abort called during sock:receive()
183+
=== TEST 6: on_abort called during sock:receive()
231184
--- dgram_server_config
232185
lua_check_client_abort on;
233186
@@ -278,7 +231,7 @@ mysock handler aborted
278231
279232
280233
281-
=== TEST 9: request body not read yet
234+
=== TEST 7: request body not read yet
282235
--- dgram_server_config
283236
content_by_lua_block {
284237
local sock, err = ngx.req.udp_socket(true)

0 commit comments

Comments
 (0)