Skip to content

Commit

Permalink
Fixed request crash with empty body and unexpected header Content-Type
Browse files Browse the repository at this point in the history
  • Loading branch information
a1div0 authored and 0x501D committed May 15, 2023
1 parent e914200 commit 6be78ef
Show file tree
Hide file tree
Showing 3 changed files with 32 additions and 3 deletions.
5 changes: 5 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,11 @@ All notable changes to this project will be documented in this file.
The format is based on [Keep a Changelog](http://keepachangelog.com/en/1.0.0/)
and this project adheres to [Semantic Versioning](http://semver.org/spec/v2.0.0.html).

## [Unreleased]

### Fixed
- Fixed request crash with empty body and unexpected header Content-Type (#189)

## [1.5.0] - 2023-03-29

### Added
Expand Down
10 changes: 7 additions & 3 deletions http/server.lua
Original file line number Diff line number Diff line change
Expand Up @@ -208,21 +208,25 @@ local function request_content_type(self)
end

local function post_param(self, name)
if self:content_type() == 'multipart/form-data' then
local body = self:read_cached()

if body == '' then
rawset(self, 'post_params', {})
elseif self:content_type() == 'multipart/form-data' then
-- TODO: do that!
rawset(self, 'post_params', {})
elseif self:content_type() == 'application/json' then
local params = self:json()
rawset(self, 'post_params', params)
elseif self:content_type() == 'application/x-www-form-urlencoded' then
local params = lib.params(self:read_cached())
local params = lib.params(body)
local pres = {}
for k, v in pairs(params) do
pres[ uri_unescape(k) ] = uri_unescape(v, true)
end
rawset(self, 'post_params', pres)
else
local params = lib.params(self:read_cached())
local params = lib.params(body)
local pres = {}
for k, v in pairs(params) do
pres[ uri_unescape(k) ] = uri_unescape(v)
Expand Down
20 changes: 20 additions & 0 deletions test/integration/http_server_requests_test.lua
Original file line number Diff line number Diff line change
Expand Up @@ -426,3 +426,23 @@ g.test_get_dot_slash = function()
local r = http_client.get(helpers.base_uri .. '/dot_slash.')
t.assert_equals(r.status, 200)
end

g.test_unwanted_content_type = function()
local httpd = g.httpd
httpd:route({
path = '/unwanted-content-type'
}, function(req)
local response = req:render{ json = req:param() }
response.status = 200
return response
end)

local opt = {
headers = {
['Content-Type'] = 'application/json'
}
}
local r = http_client.get(helpers.base_uri .. '/unwanted-content-type', opt)
t.assert_equals(r.status, 200)
t.assert_equals(r.body, '[]')
end

0 comments on commit 6be78ef

Please sign in to comment.