Skip to content

Commit ebf9cc8

Browse files
committed
feature: now the raw request cosocket returned by ngx.req.socket() no longer requires the request body to be read already, which means that one can use this cosocket to read the raw request body data as well. thanks aviramc for the original patch.
1 parent 3740962 commit ebf9cc8

File tree

2 files changed

+46
-4
lines changed

2 files changed

+46
-4
lines changed

src/ngx_http_lua_socket_tcp.c

+5-3
Original file line numberDiff line numberDiff line change
@@ -3150,9 +3150,11 @@ ngx_http_lua_req_socket(lua_State *L)
31503150
return 2;
31513151
#else
31523152
if (!r->request_body) {
3153-
lua_pushnil(L);
3154-
lua_pushliteral(L, "requesty body not read yet");
3155-
return 2;
3153+
rb = ngx_pcalloc(r->pool, sizeof(ngx_http_request_body_t));
3154+
if (rb == NULL) {
3155+
return luaL_error(L, "out of memory");
3156+
}
3157+
r->request_body = rb;
31563158
}
31573159

31583160
if (c->buffered) {

t/116-raw-req-socket.t

+41-1
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() * 29;
8+
plan tests => repeat_each() * 33;
99

1010
our $HtmlDir = html_dir;
1111

@@ -695,3 +695,43 @@ msg: 1: received: hello, wo
695695
--- no_error_log
696696
[error]
697697
698+
699+
700+
=== TEST 13: request body not read yet
701+
--- config
702+
server_tokens off;
703+
location = /t {
704+
content_by_lua '
705+
local sock, err = ngx.req.socket(true)
706+
if not sock then
707+
ngx.log(ngx.ERR, "server: failed to get raw req socket: ", err)
708+
return
709+
end
710+
711+
local data, err = sock:receive(5)
712+
if not data then
713+
ngx.log(ngx.ERR, "failed to receive: ", err)
714+
return
715+
end
716+
717+
local ok, err = sock:send("HTTP/1.1 200 OK\\r\\nContent-Length: 5\\r\\n\\r\\n" .. data)
718+
if not ok then
719+
ngx.log(ngx.ERR, "failed to send: ", err)
720+
return
721+
end
722+
';
723+
}
724+
725+
--- raw_request eval
726+
"GET /t HTTP/1.0\r
727+
Host: localhost\r
728+
Content-Length: 5\r
729+
\r
730+
hello"
731+
--- response_headers
732+
Content-Length: 5
733+
--- response_body chop
734+
hello
735+
--- no_error_log
736+
[error]
737+

0 commit comments

Comments
 (0)