Skip to content

Commit 6be78ef

Browse files
a1div00x501D
authored andcommitted
Fixed request crash with empty body and unexpected header Content-Type
1 parent e914200 commit 6be78ef

File tree

3 files changed

+32
-3
lines changed

3 files changed

+32
-3
lines changed

CHANGELOG.md

+5
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,11 @@ All notable changes to this project will be documented in this file.
44
The format is based on [Keep a Changelog](http://keepachangelog.com/en/1.0.0/)
55
and this project adheres to [Semantic Versioning](http://semver.org/spec/v2.0.0.html).
66

7+
## [Unreleased]
8+
9+
### Fixed
10+
- Fixed request crash with empty body and unexpected header Content-Type (#189)
11+
712
## [1.5.0] - 2023-03-29
813

914
### Added

http/server.lua

+7-3
Original file line numberDiff line numberDiff line change
@@ -208,21 +208,25 @@ local function request_content_type(self)
208208
end
209209

210210
local function post_param(self, name)
211-
if self:content_type() == 'multipart/form-data' then
211+
local body = self:read_cached()
212+
213+
if body == '' then
214+
rawset(self, 'post_params', {})
215+
elseif self:content_type() == 'multipart/form-data' then
212216
-- TODO: do that!
213217
rawset(self, 'post_params', {})
214218
elseif self:content_type() == 'application/json' then
215219
local params = self:json()
216220
rawset(self, 'post_params', params)
217221
elseif self:content_type() == 'application/x-www-form-urlencoded' then
218-
local params = lib.params(self:read_cached())
222+
local params = lib.params(body)
219223
local pres = {}
220224
for k, v in pairs(params) do
221225
pres[ uri_unescape(k) ] = uri_unescape(v, true)
222226
end
223227
rawset(self, 'post_params', pres)
224228
else
225-
local params = lib.params(self:read_cached())
229+
local params = lib.params(body)
226230
local pres = {}
227231
for k, v in pairs(params) do
228232
pres[ uri_unescape(k) ] = uri_unescape(v)

test/integration/http_server_requests_test.lua

+20
Original file line numberDiff line numberDiff line change
@@ -426,3 +426,23 @@ g.test_get_dot_slash = function()
426426
local r = http_client.get(helpers.base_uri .. '/dot_slash.')
427427
t.assert_equals(r.status, 200)
428428
end
429+
430+
g.test_unwanted_content_type = function()
431+
local httpd = g.httpd
432+
httpd:route({
433+
path = '/unwanted-content-type'
434+
}, function(req)
435+
local response = req:render{ json = req:param() }
436+
response.status = 200
437+
return response
438+
end)
439+
440+
local opt = {
441+
headers = {
442+
['Content-Type'] = 'application/json'
443+
}
444+
}
445+
local r = http_client.get(helpers.base_uri .. '/unwanted-content-type', opt)
446+
t.assert_equals(r.status, 200)
447+
t.assert_equals(r.body, '[]')
448+
end

0 commit comments

Comments
 (0)