Skip to content

Commit 3e4cb6f

Browse files
authored
Merge pull request #185 from bungle/feat/content-length
Add Content-Length: 0 header on POST/PUT/PATCH when the body is absent
2 parents 23053ba + 70f7dd1 commit 3e4cb6f

File tree

2 files changed

+83
-2
lines changed

2 files changed

+83
-2
lines changed

lib/resty/http.lua

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,13 @@ local HOP_BY_HOP_HEADERS = {
5555
}
5656

5757

58+
local EXPECTING_BODY = {
59+
POST = true,
60+
PUT = true,
61+
PATCH = true,
62+
}
63+
64+
5865
-- Reimplemented coroutine.wrap, returning "nil, err" if the coroutine cannot
5966
-- be resumed. This protects user code from inifite loops when doing things like
6067
-- repeat
@@ -603,8 +610,13 @@ function _M.send_request(self, params)
603610
end
604611

605612
-- Ensure minimal headers are set
606-
if type(body) == 'string' and not headers["Content-Length"] then
607-
headers["Content-Length"] = #body
613+
614+
if not headers["Content-Length"] then
615+
if type(body) == 'string' then
616+
headers["Content-Length"] = #body
617+
elseif body == nil and EXPECTING_BODY[str_upper(params.method)] then
618+
headers["Content-Length"] = 0
619+
end
608620
end
609621
if not headers["Host"] then
610622
if (str_sub(self.host, 1, 5) == "unix:") then

t/06-simpleinterface.t

Lines changed: 69 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -267,3 +267,72 @@ GET /a
267267
nil closed
268268
--- error_log
269269
lua tcp socket read timed out
270+
271+
272+
=== TEST 7: Content-Length is set on POST/PUT/PATCH requests when body is absent
273+
--- http_config eval: $::HttpConfig
274+
--- config
275+
location = /a {
276+
content_by_lua '
277+
for i, method in ipairs({ "POST", "PUT", "PATCH" }) do
278+
local http = require "resty.http"
279+
local httpc = http.new()
280+
local res, err = httpc:request_uri("http://127.0.0.1:"..ngx.var.server_port.."/b", { method = method })
281+
282+
if not res then
283+
ngx.log(ngx.ERR, err)
284+
end
285+
286+
if i == 1 then
287+
ngx.status = res.status
288+
end
289+
290+
ngx.print(res.body)
291+
end
292+
';
293+
}
294+
location = /b {
295+
content_by_lua '
296+
ngx.say(ngx.req.get_method(), " Content-Length: ", ngx.req.get_headers()["Content-Length"])
297+
';
298+
}
299+
--- request
300+
GET /a
301+
--- response_body
302+
POST Content-Length: 0
303+
PUT Content-Length: 0
304+
PATCH Content-Length: 0
305+
--- no_error_log
306+
[error]
307+
[warn]
308+
309+
310+
=== TEST 8: Content-Length is not set on GET requests when body is absent
311+
--- http_config eval: $::HttpConfig
312+
--- config
313+
location = /a {
314+
content_by_lua '
315+
local http = require "resty.http"
316+
local httpc = http.new()
317+
local res, err = httpc:request_uri("http://127.0.0.1:"..ngx.var.server_port.."/b")
318+
319+
if not res then
320+
ngx.log(ngx.ERR, err)
321+
end
322+
ngx.status = res.status
323+
ngx.print(res.body)
324+
';
325+
}
326+
location = /b {
327+
content_by_lua '
328+
ngx.say("Content-Length: ", type(ngx.req.get_headers()["Content-Length"]))
329+
';
330+
}
331+
--- request
332+
GET /a
333+
--- response_body
334+
Content-Length: nil
335+
--- no_error_log
336+
[error]
337+
[warn]
338+

0 commit comments

Comments
 (0)