Skip to content

Commit fc03bfe

Browse files
committed
bugfix: now ngx.req.socket(raw) returns proper error when there is some other "light thread" reading the request body.
1 parent ebf9cc8 commit fc03bfe

File tree

2 files changed

+54
-2
lines changed

2 files changed

+54
-2
lines changed

src/ngx_http_lua_socket_tcp.c

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3149,7 +3149,15 @@ ngx_http_lua_req_socket(lua_State *L)
31493149
lua_pushliteral(L, "nginx version too old");
31503150
return 2;
31513151
#else
3152-
if (!r->request_body) {
3152+
if (r->request_body) {
3153+
if (r->request_body->rest > 0) {
3154+
lua_pushnil(L);
3155+
lua_pushliteral(L, "pending request body reading in some "
3156+
"other thread");
3157+
return 2;
3158+
}
3159+
3160+
} else {
31533161
rb = ngx_pcalloc(r->pool, sizeof(ngx_http_request_body_t));
31543162
if (rb == NULL) {
31553163
return luaL_error(L, "out of memory");

t/116-raw-req-socket.t

Lines changed: 45 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ use t::TestNginxLua;
55

66
repeat_each(2);
77

8-
plan tests => repeat_each() * 33;
8+
plan tests => repeat_each() * 36;
99

1010
our $HtmlDir = html_dir;
1111

@@ -735,3 +735,47 @@ hello
735735
--- no_error_log
736736
[error]
737737
738+
739+
740+
=== TEST 14: pending request body reading
741+
--- config
742+
server_tokens off;
743+
location = /t {
744+
content_by_lua '
745+
ngx.thread.spawn(function ()
746+
ngx.req.read_body()
747+
end)
748+
749+
local sock, err = ngx.req.socket(true)
750+
if not sock then
751+
ngx.log(ngx.WARN, "server: failed to get raw req socket: ", err)
752+
return ngx.exit(444)
753+
end
754+
755+
local data, err = sock:receive(5)
756+
if not data then
757+
ngx.log(ngx.ERR, "failed to receive: ", err)
758+
return
759+
end
760+
761+
local ok, err = sock:send("HTTP/1.1 200 OK\\r\\nContent-Length: 5\\r\\n\\r\\n" .. data)
762+
if not ok then
763+
ngx.log(ngx.ERR, "failed to send: ", err)
764+
return
765+
end
766+
';
767+
}
768+
769+
--- raw_request eval
770+
"GET /t HTTP/1.0\r
771+
Host: localhost\r
772+
Content-Length: 5\r
773+
\r
774+
hell"
775+
--- ignore_response
776+
--- no_error_log
777+
[error]
778+
[alert]
779+
--- error_log
780+
server: failed to get raw req socket: pending request body reading in some other thread
781+

0 commit comments

Comments
 (0)