Skip to content

Commit

Permalink
bugfix: ngx.req.socket() did not honor the "Expect: 100-continue" req…
Browse files Browse the repository at this point in the history
…uest header and could hang. thanks Matthieu Tourne for the patch in pull request #107.
  • Loading branch information
agentzh committed May 15, 2012
1 parent 21450b7 commit 754d547
Show file tree
Hide file tree
Showing 2 changed files with 84 additions and 0 deletions.
49 changes: 49 additions & 0 deletions src/ngx_http_lua_socket.c
Expand Up @@ -93,6 +93,7 @@ static ngx_int_t ngx_http_lua_socket_add_input_buffer(ngx_http_request_t *r,
ngx_http_lua_socket_upstream_t *u);
static ngx_int_t ngx_http_lua_socket_insert_buffer(ngx_http_request_t *r,
ngx_http_lua_socket_upstream_t *u, u_char *pat, size_t prefix);
static ngx_int_t ngx_http_lua_test_expect(ngx_http_request_t *r);


enum {
Expand Down Expand Up @@ -2686,6 +2687,12 @@ ngx_http_lua_req_socket(lua_State *L)
return 2;
}

if (ngx_http_lua_test_expect(r) != NGX_OK) {
lua_pushnil(L);
lua_pushliteral(L, "test expect failed");
return 2;
}

/* prevent other request body reader from running */

rb = ngx_pcalloc(r->pool, sizeof(ngx_http_request_body_t));
Expand Down Expand Up @@ -3554,3 +3561,45 @@ static ngx_int_t ngx_http_lua_socket_insert_buffer(ngx_http_request_t *r,
return NGX_OK;
}


static ngx_int_t
ngx_http_lua_test_expect(ngx_http_request_t *r)
{
ngx_int_t n;
ngx_str_t *expect;

if (r->expect_tested
|| r->headers_in.expect == NULL
|| r->http_version < NGX_HTTP_VERSION_11)
{
return NGX_OK;
}

r->expect_tested = 1;

expect = &r->headers_in.expect->value;

if (expect->len != sizeof("100-continue") - 1
|| ngx_strncasecmp(expect->data, (u_char *) "100-continue",
sizeof("100-continue") - 1)
!= 0)
{
return NGX_OK;
}

ngx_log_debug0(NGX_LOG_DEBUG_HTTP, r->connection->log, 0,
"send 100 Continue");

n = r->connection->send(r->connection,
(u_char *) "HTTP/1.1 100 Continue" CRLF CRLF,
sizeof("HTTP/1.1 100 Continue" CRLF CRLF) - 1);

if (n == sizeof("HTTP/1.1 100 Continue" CRLF CRLF) - 1) {
return NGX_OK;
}

/* we assume that such small packet should be send successfully */

return NGX_ERROR;
}

35 changes: 35 additions & 0 deletions t/067-req-socket.t
Expand Up @@ -486,3 +486,38 @@ done
--- no_error_log
[error]



=== TEST 7: Expect & 100 Continue
--- config
location /t {
content_by_lua '
local sock, err = ngx.req.socket()
if sock then
ngx.say("got the request socket")
else
ngx.say("failed to get the request socket: ", err)
return
end
for i = 1, 3 do
local data, err, part = sock:receive(5)
if data then
ngx.say("received: ", data)
else
ngx.say("failed to receive: ", err, " [", part, "]")
end
end
';
}
--- request
POST /t
hello world
--- more_headers
Expect: 100-Continue
--- error_code: 100
--- response_body_like chomp
\breceived: hello\b.*?\breceived: worl\b
--- no_error_log
[error]

0 comments on commit 754d547

Please sign in to comment.