Skip to content

fix 100 response with headers #287

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 3 commits into from
Mar 9, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 12 additions & 7 deletions lib/resty/http.lua
Original file line number Diff line number Diff line change
Expand Up @@ -635,18 +635,23 @@ end
local function _handle_continue(sock, body)
local status, version, reason, err = _receive_status(sock) --luacheck: no unused
if not status then
return nil, nil, err
return nil, nil, nil, err
end

-- Only send body if we receive a 100 Continue
if status == 100 then
local ok, err = sock:receive("*l") -- Read carriage return
-- Read headers
local headers, err = _receive_headers(sock)
if not headers then
return nil, nil, nil, err
end

local ok, err = _send_body(sock, body)
if not ok then
return nil, nil, err
return nil, nil, nil, err
end
_send_body(sock, body)
end
return status, version, err
return status, version, reason, err
end


Expand Down Expand Up @@ -764,11 +769,11 @@ function _M.read_response(self, params)
-- If we expect: continue, we need to handle this, sending the body if allowed.
-- If we don't get 100 back, then status is the actual status.
if params.headers["Expect"] == "100-continue" then
local _status, _version, _err = _handle_continue(sock, params.body)
local _status, _version, _reason, _err = _handle_continue(sock, params.body)
if not _status then
return nil, _err
elseif _status ~= 100 then
status, version, err = _status, _version, _err -- luacheck: no unused
status, version, reason, err = _status, _version, _reason, _err -- luacheck: no unused
end
end

Expand Down
154 changes: 150 additions & 4 deletions t/03-requestbody.t
Original file line number Diff line number Diff line change
Expand Up @@ -205,7 +205,153 @@ Expectation Failed
[warn]


=== TEST 5: Non string request bodies are converted with correct length
=== TEST 5: Return 100 Continue with headers
--- http_config eval: $::HttpConfig
--- config
location = /a {
content_by_lua_block {
local http = require "resty.http"
local httpc = http.new()
httpc:connect({
scheme = "http",
host = "127.0.0.1",
port = ngx.var.server_port
})

local res, err = httpc:request{
body = "a=1&b=2&c=3",
path = "/b",
headers = {
["Expect"] = "100-continue",
["Content-Type"] = "application/x-www-form-urlencoded",
}
}

if not res then
ngx.log(ngx.ERR, "httpc:request failed: ", err)
end

ngx.say(res.status)
ngx.say(res:read_body())
httpc:close()
}
}
location = /b {
content_by_lua_block {
local len = ngx.req.get_headers()["Content-Length"]

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

-- with additional header
local ok, err = sock:send("HTTP/1.1 100 Continue\r\nConnection: keep-alive\r\n\r\n")
if not ok then
ngx.log(ngx.ERR, "failed to send 100 response: ", err)
end

local data, err = sock:receive(len)
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\n" ..
"Content-Length: " .. len .. "\r\n" ..
"Content-Type: application/x-www-form-urlencoded\r\n\r\n" ..
data)
if not ok then
ngx.log(ngx.ERR, "failed to send 200 response: ", err)
return
end
}
}
--- request
GET /a
--- response_body
200
a=1&b=2&c=3
--- no_error_log
[error]
[warn]


=== TEST 6: Return 100 Continue without headers
--- http_config eval: $::HttpConfig
--- config
location = /a {
content_by_lua_block {
local http = require "resty.http"
local httpc = http.new()
httpc:connect({
scheme = "http",
host = "127.0.0.1",
port = ngx.var.server_port
})

local res, err = httpc:request{
body = "a=1&b=2&c=3",
path = "/b",
headers = {
["Expect"] = "100-continue",
["Content-Type"] = "application/x-www-form-urlencoded",
}
}

if not res then
ngx.log(ngx.ERR, "httpc:request failed: ", err)
end

ngx.say(res.status)
ngx.say(res:read_body())
httpc:close()
}
}
location = /b {
content_by_lua_block {
local len = ngx.req.get_headers()["Content-Length"]

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

-- without additional headers
local ok, err = sock:send("HTTP/1.1 100 Continue\r\n\r\n")
if not ok then
ngx.log(ngx.ERR, "failed to send 100 response: ", err)
end

local data, err = sock:receive(len)
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\n" ..
"Content-Length: " .. len .. "\r\n" ..
"Content-Type: application/x-www-form-urlencoded\r\n\r\n" ..
data)
if not ok then
ngx.log(ngx.ERR, "failed to send 200 response: ", err)
return
end
}
}
--- request
GET /a
--- response_body
200
a=1&b=2&c=3
--- no_error_log
[error]
[warn]


=== TEST 7: Non string request bodies are converted with correct length
--- http_config eval: $::HttpConfig
--- config
location = /a {
Expand Down Expand Up @@ -247,7 +393,7 @@ mix123edtable
[warn]


=== TEST 6: Request body as iterator
=== TEST 8: Request body as iterator
--- http_config eval: $::HttpConfig
--- config
location = /a {
Expand Down Expand Up @@ -284,7 +430,7 @@ foobar
[warn]


=== TEST 7: Request body as iterator, errors with missing length
=== TEST 9: Request body as iterator, errors with missing length
--- http_config eval: $::HttpConfig
--- config
location = /a {
Expand Down Expand Up @@ -319,7 +465,7 @@ Request body is a function but a length or chunked encoding is not specified
[warn]


=== TEST 8: Request body as iterator with chunked encoding
=== TEST 10: Request body as iterator with chunked encoding
--- http_config eval: $::HttpConfig
--- config
location = /a {
Expand Down