1
0
mirror of synced 2025-04-21 22:25:55 +03:00

Code cleanup

This commit is contained in:
yhirose 2020-12-29 09:39:19 -05:00
parent 871d8d67b0
commit 2c07ec4600

View File

@ -4240,7 +4240,7 @@ inline bool Server::write_response_core(Stream &strm, bool close_connection,
std::string boundary; std::string boundary;
if (need_apply_ranges) { apply_ranges(req, res, content_type, boundary); } if (need_apply_ranges) { apply_ranges(req, res, content_type, boundary); }
// Headers // Preapre additional headers
if (close_connection || req.get_header_value("Connection") == "close") { if (close_connection || req.get_header_value("Connection") == "close") {
res.set_header("Connection", "close"); res.set_header("Connection", "close");
} else { } else {
@ -4266,18 +4266,22 @@ inline bool Server::write_response_core(Stream &strm, bool close_connection,
detail::BufferStream bstrm; detail::BufferStream bstrm;
// Response line // Response line and headers
if (!bstrm.write_format("HTTP/1.1 %d %s\r\n", res.status, {
detail::status_message(res.status))) { detail::BufferStream bstrm;
return false;
if (!bstrm.write_format("HTTP/1.1 %d %s\r\n", res.status,
detail::status_message(res.status))) {
return false;
}
if (!detail::write_headers(bstrm, res, Headers())) { return false; }
// Flush buffer
auto &data = bstrm.get_buffer();
strm.write(data.data(), data.size());
} }
if (!detail::write_headers(bstrm, res, Headers())) { return false; }
// Flush buffer
auto &data = bstrm.get_buffer();
strm.write(data.data(), data.size());
// Body // Body
auto ret = true; auto ret = true;
if (req.method != "HEAD") { if (req.method != "HEAD") {
@ -5264,14 +5268,7 @@ inline bool ClientImpl::write_content_with_provider(Stream &strm,
inline bool ClientImpl::write_request(Stream &strm, const Request &req, inline bool ClientImpl::write_request(Stream &strm, const Request &req,
bool close_connection, Error &error) { bool close_connection, Error &error) {
detail::BufferStream bstrm; // Prepare additonal headers
// Request line
const auto &path = detail::encode_url(req.path);
bstrm.write_format("%s %s HTTP/1.1\r\n", req.method.c_str(), path.c_str());
// Additonal headers
Headers headers; Headers headers;
if (close_connection) { headers.emplace("Connection", "close"); } if (close_connection) { headers.emplace("Connection", "close"); }
@ -5341,13 +5338,21 @@ inline bool ClientImpl::write_request(Stream &strm, const Request &req,
proxy_bearer_token_auth_token_, true)); proxy_bearer_token_auth_token_, true));
} }
detail::write_headers(bstrm, req, headers); // Request line and headers
{
detail::BufferStream bstrm;
// Flush buffer const auto &path = detail::encode_url(req.path);
auto &data = bstrm.get_buffer(); bstrm.write_format("%s %s HTTP/1.1\r\n", req.method.c_str(), path.c_str());
if (!detail::write_data(strm, data.data(), data.size())) {
error = Error::Write; detail::write_headers(bstrm, req, headers);
return false;
// Flush buffer
auto &data = bstrm.get_buffer();
if (!detail::write_data(strm, data.data(), data.size())) {
error = Error::Write;
return false;
}
} }
// Body // Body