Skip to content

fix: when EXPECTING_BODY nil will hand request #332

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

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
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
2 changes: 1 addition & 1 deletion lib/resty/http.lua
Original file line number Diff line number Diff line change
Expand Up @@ -706,7 +706,7 @@ function _M.send_request(self, params)
headers["Content-Length"] = length

elseif body == nil and EXPECTING_BODY[str_upper(params.method)] then
headers["Content-Length"] = 0
return nil, "Request body is nil but " .. str_upper(params.method) .. " method expects a body. Use an empty string \"\" if you want to send an empty body."

elseif body ~= nil then
headers["Content-Length"] = #tostring(body)
Expand Down
38 changes: 36 additions & 2 deletions t/06-simpleinterface.t
Original file line number Diff line number Diff line change
Expand Up @@ -269,15 +269,15 @@ nil closed
lua tcp socket read timed out


=== TEST 7: Content-Length is set on POST/PUT/PATCH requests when body is absent
=== TEST 7: Content-Length is set on POST/PUT/PATCH requests when body is explicitly empty
--- http_config eval: $::HttpConfig
--- config
location = /a {
content_by_lua '
for i, method in ipairs({ "POST", "PUT", "PATCH" }) do
local http = require "resty.http"
local httpc = http.new()
local res, err = httpc:request_uri("http://127.0.0.1:"..ngx.var.server_port.."/b", { method = method })
local res, err = httpc:request_uri("http://127.0.0.1:"..ngx.var.server_port.."/b", { method = method, body = "" })

if not res then
ngx.log(ngx.ERR, err)
Expand Down Expand Up @@ -336,3 +336,37 @@ Content-Length: nil
[error]
[warn]


=== TEST 9: Error when body is nil for POST/PUT/PATCH requests
--- http_config eval: $::HttpConfig
--- config
location = /a {
content_by_lua '
for i, method in ipairs({ "POST", "PUT", "PATCH" }) do
local http = require "resty.http"
local httpc = http.new()
local res, err = httpc:request_uri("http://127.0.0.1:"..ngx.var.server_port.."/b", { method = method })

if not res then
ngx.say(method, ": ", err)
else
ngx.say(method, ": success")
end
end
';
}
location = /b {
content_by_lua '
ngx.say("Should not reach here")
';
}
--- request
GET /a
--- response_body
POST: Request body is nil but POST method expects a body. Use an empty string "" if you want to send an empty body.
PUT: Request body is nil but PUT method expects a body. Use an empty string "" if you want to send an empty body.
PATCH: Request body is nil but PATCH method expects a body. Use an empty string "" if you want to send an empty body.
--- no_error_log
[error]
[warn]

Loading