Skip to content

Commit

Permalink
Assign a higher precedence to the manually set Content-Type
Browse files Browse the repository at this point in the history
  • Loading branch information
timschumi committed May 15, 2024
1 parent 40b3b39 commit 220fa28
Show file tree
Hide file tree
Showing 3 changed files with 119 additions and 1 deletion.
5 changes: 4 additions & 1 deletion src/HTTPRequest.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -175,7 +175,10 @@ bool HTTPRequest::run()
// Do we have a request body?
if (!this->body.empty()) {
post_body = this->body;
this->headers["Content-Type"] = type;

// The manually set Content-Type always takes precedence over the `type` parameter.
// std::map::insert() ignores operations if the respective key already exists.
this->headers.insert(std::make_pair("Content-Type", type));
} else {
post_body = this->build_query_string();
}
Expand Down
107 changes: 107 additions & 0 deletions tests/chttp.lua
Original file line number Diff line number Diff line change
Expand Up @@ -154,5 +154,112 @@ return {
})
end
},
{
name = "Content-Type for GET request",
async = true,
timeout = 1,
func = function()
CHTTP({
method = "GET",
url = "http://127.0.0.1:5000/echo_content_type",
success = function(code, body, headers)
expect(code).to.equal(404)
done()
end,
failed = function(err)
error("HTTP request failed: " .. err)
done()
end
})
end
},
{
name = "Content-Type for POST request",
async = true,
timeout = 1,
func = function()
CHTTP({
method = "POST",
url = "http://127.0.0.1:5000/echo_content_type",
success = function(code, body, headers)
expect(code).to.equal(200)
expect(body).to.equal("application/x-www-form-urlencoded")
done()
end,
failed = function(err)
error("HTTP request failed: " .. err)
done()
end
})
end
},
{
name = "Content-Type for POST request with body",
async = true,
timeout = 1,
func = function()
CHTTP({
method = "POST",
url = "http://127.0.0.1:5000/echo_content_type",
body = "Hello world!",
success = function(code, body, headers)
expect(code).to.equal(200)
expect(body).to.equal("text/plain; charset=utf-8")
done()
end,
failed = function(err)
error("HTTP request failed: " .. err)
done()
end
})
end
},
{
name = "Content-Type for POST request with header override",
async = true,
timeout = 1,
func = function()
CHTTP({
method = "POST",
url = "http://127.0.0.1:5000/echo_content_type",
headers = {
["Content-Type"] = "application/octet-stream",
},
success = function(code, body, headers)
expect(code).to.equal(200)
expect(body).to.equal("application/octet-stream")
done()
end,
failed = function(err)
error("HTTP request failed: " .. err)
done()
end
})
end
},
{
name = "Content-Type for POST request with body and header override",
async = true,
timeout = 1,
func = function()
CHTTP({
method = "POST",
url = "http://127.0.0.1:5000/echo_content_type",
headers = {
["Content-Type"] = "application/octet-stream",
},
body = "Hello world!",
success = function(code, body, headers)
expect(code).to.equal(200)
expect(body).to.equal("application/octet-stream")
done()
end,
failed = function(err)
error("HTTP request failed: " .. err)
done()
end
})
end
},
}
}
8 changes: 8 additions & 0 deletions tests/server.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,14 @@ def response_normal():
return b"Hello World!", 200


@app.route("/echo_content_type", methods=["GET", "POST"])
def echo_content_type():
if "Content-Type" not in flask.request.headers:
return b"", 404

return flask.request.headers["Content-Type"], 200


@app.route("/response_null_byte_in_body", methods=["GET"])
def response_null_byte_in_body():
return b"Hello World\0!", 200
Expand Down

0 comments on commit 220fa28

Please sign in to comment.