Skip to content

Commit

Permalink
feature: now the raw request cosocket returned by ngx.req.socket() no…
Browse files Browse the repository at this point in the history
… 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.
  • Loading branch information
agentzh committed Oct 3, 2013
1 parent 3740962 commit ebf9cc8
Show file tree
Hide file tree
Showing 2 changed files with 46 additions and 4 deletions.
8 changes: 5 additions & 3 deletions src/ngx_http_lua_socket_tcp.c
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down
42 changes: 41 additions & 1 deletion t/116-raw-req-socket.t
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ use t::TestNginxLua;

repeat_each(2);

plan tests => repeat_each() * 29;
plan tests => repeat_each() * 33;

our $HtmlDir = html_dir;

Expand Down Expand Up @@ -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]

0 comments on commit ebf9cc8

Please sign in to comment.