From ebf9cc8679c89d9330dbc0fe3d3e72ecd9c51b2b Mon Sep 17 00:00:00 2001 From: "Yichun Zhang (agentzh)" Date: Wed, 2 Oct 2013 17:10:48 -0700 Subject: [PATCH] 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. --- src/ngx_http_lua_socket_tcp.c | 8 ++++--- t/116-raw-req-socket.t | 42 ++++++++++++++++++++++++++++++++++- 2 files changed, 46 insertions(+), 4 deletions(-) diff --git a/src/ngx_http_lua_socket_tcp.c b/src/ngx_http_lua_socket_tcp.c index d27edbb60a..c997b63ba4 100644 --- a/src/ngx_http_lua_socket_tcp.c +++ b/src/ngx_http_lua_socket_tcp.c @@ -3150,9 +3150,11 @@ ngx_http_lua_req_socket(lua_State *L) return 2; #else if (!r->request_body) { - lua_pushnil(L); - lua_pushliteral(L, "requesty body not read yet"); - return 2; + rb = ngx_pcalloc(r->pool, sizeof(ngx_http_request_body_t)); + if (rb == NULL) { + return luaL_error(L, "out of memory"); + } + r->request_body = rb; } if (c->buffered) { diff --git a/t/116-raw-req-socket.t b/t/116-raw-req-socket.t index e0d2de22a7..ff321888d8 100644 --- a/t/116-raw-req-socket.t +++ b/t/116-raw-req-socket.t @@ -5,7 +5,7 @@ use t::TestNginxLua; repeat_each(2); -plan tests => repeat_each() * 29; +plan tests => repeat_each() * 33; our $HtmlDir = html_dir; @@ -695,3 +695,43 @@ msg: 1: received: hello, wo --- no_error_log [error] + + +=== TEST 13: request body not read yet +--- config + server_tokens off; + location = /t { + content_by_lua ' + local sock, err = ngx.req.socket(true) + if not sock then + ngx.log(ngx.ERR, "server: failed to get raw req socket: ", err) + return + end + + local data, err = sock:receive(5) + if not data then + ngx.log(ngx.ERR, "failed to receive: ", err) + return + end + + local ok, err = sock:send("HTTP/1.1 200 OK\\r\\nContent-Length: 5\\r\\n\\r\\n" .. data) + if not ok then + ngx.log(ngx.ERR, "failed to send: ", err) + return + end + '; + } + +--- raw_request eval +"GET /t HTTP/1.0\r +Host: localhost\r +Content-Length: 5\r +\r +hello" +--- response_headers +Content-Length: 5 +--- response_body chop +hello +--- no_error_log +[error] +