Fix header ordering

This commit is contained in:
PabloMK7 2023-10-04 22:03:35 +02:00
parent 5fb455425c
commit 0689a4a22f

View File

@ -137,8 +137,8 @@ static ssize_t WriteHeaders(httplib::Stream& strm,
return write_len; return write_len;
} }
static size_t HeaderWriteHandler(std::vector<Context::RequestHeader>& pending_headers, static size_t HandleHeaderWrite(std::vector<Context::RequestHeader>& pending_headers,
httplib::Stream& strm, httplib::Headers& httplib_headers) { httplib::Stream& strm, httplib::Headers& httplib_headers) {
std::vector<Context::RequestHeader> final_headers; std::vector<Context::RequestHeader> final_headers;
std::vector<Context::RequestHeader>::iterator it_p; std::vector<Context::RequestHeader>::iterator it_p;
httplib::Headers::iterator it_h; httplib::Headers::iterator it_h;
@ -148,6 +148,7 @@ static size_t HeaderWriteHandler(std::vector<Context::RequestHeader>& pending_he
[&str](Context::RequestHeader& rh) { return rh.name == str; }); [&str](Context::RequestHeader& rh) { return rh.name == str; });
}; };
// Watch out for header ordering!!
// First: Host // First: Host
it_p = find_pending_header("Host"); it_p = find_pending_header("Host");
if (it_p != pending_headers.end()) { if (it_p != pending_headers.end()) {
@ -160,14 +161,13 @@ static size_t HeaderWriteHandler(std::vector<Context::RequestHeader>& pending_he
} }
} }
// Second: Content-Type (optional, ignore httplib) // Second, user defined headers
it_p = find_pending_header("Content-Type"); // Third, Content-Type (optional, appended by MakeRequest)
if (it_p != pending_headers.end()) { for (const auto& header : pending_headers) {
final_headers.push_back(Context::RequestHeader(it_p->name, it_p->value)); final_headers.push_back(header);
pending_headers.erase(it_p);
} }
// Third: Content-Length // Fourth: Content-Length
it_p = find_pending_header("Content-Length"); it_p = find_pending_header("Content-Length");
if (it_p != pending_headers.end()) { if (it_p != pending_headers.end()) {
final_headers.push_back(Context::RequestHeader(it_p->name, it_p->value)); final_headers.push_back(Context::RequestHeader(it_p->name, it_p->value));
@ -179,11 +179,6 @@ static size_t HeaderWriteHandler(std::vector<Context::RequestHeader>& pending_he
} }
} }
// Finally, user defined headers
for (const auto& header : pending_headers) {
final_headers.push_back(header);
}
return WriteHeaders(strm, final_headers); return WriteHeaders(strm, final_headers);
}; };
@ -212,12 +207,15 @@ void Context::MakeRequest() {
return true; return true;
}; };
// Watch out for header ordering!!
auto header_writter = [&pending_headers](httplib::Stream& strm, auto header_writter = [&pending_headers](httplib::Stream& strm,
httplib::Headers& httplib_headers) { httplib::Headers& httplib_headers) {
return HeaderWriteHandler(pending_headers, strm, httplib_headers); return HandleHeaderWrite(pending_headers, strm, httplib_headers);
}; };
for (const auto& header : headers) {
pending_headers.push_back(header);
}
if (!post_data.empty()) { if (!post_data.empty()) {
pending_headers.push_back( pending_headers.push_back(
Context::RequestHeader("Content-Type", "application/x-www-form-urlencoded")); Context::RequestHeader("Content-Type", "application/x-www-form-urlencoded"));
@ -225,10 +223,6 @@ void Context::MakeRequest() {
boost::replace_all(request.body, "*", "%2A"); boost::replace_all(request.body, "*", "%2A");
} }
for (const auto& header : headers) {
pending_headers.push_back(header);
}
if (!post_data_raw.empty()) { if (!post_data_raw.empty()) {
request.body = post_data_raw; request.body = post_data_raw;
} }