1
Fork 0
mirror of https://github.com/RGBCube/serenity synced 2025-05-31 05:48:12 +00:00

LibWeb: Add keep-alive in response headers if it present in request

This fix addresses issue where, if request headers contain
"Connection: keep-alive", we keep socket open without letting client
know that we support keep-alive connections. This can result in a
large number of open and unused sockets.
This commit is contained in:
Aliaksandr Kalenik 2023-03-20 02:26:07 +03:00 committed by Tim Flynn
parent 5b5a76eadd
commit 4f496e97fe

View file

@ -266,6 +266,10 @@ ErrorOr<void, Client::WrappedError> Client::handle_request(JsonValue body)
ErrorOr<void, Client::WrappedError> Client::send_success_response(JsonValue result)
{
bool keep_alive = false;
if (auto it = m_request->headers().find_if([](auto& header) { return header.name.equals_ignoring_ascii_case("Connection"sv); }); !it.is_end())
keep_alive = it->value.trim_whitespace().equals_ignoring_ascii_case("keep-alive"sv);
result = make_success_response(move(result));
auto content = result.serialized<StringBuilder>();
@ -275,6 +279,8 @@ ErrorOr<void, Client::WrappedError> Client::send_success_response(JsonValue resu
builder.append("X-Frame-Options: SAMEORIGIN\r\n"sv);
builder.append("X-Content-Type-Options: nosniff\r\n"sv);
builder.append("Pragma: no-cache\r\n"sv);
if (keep_alive)
builder.append("Connection: keep-alive\r\n"sv);
builder.append("Content-Type: application/json; charset=utf-8\r\n"sv);
builder.appendff("Content-Length: {}\r\n", content.length());
builder.append("\r\n"sv);
@ -287,10 +293,6 @@ ErrorOr<void, Client::WrappedError> Client::send_success_response(JsonValue resu
content = content.substring_view(bytes_sent);
}
bool keep_alive = false;
if (auto it = m_request->headers().find_if([](auto& header) { return header.name.equals_ignoring_ascii_case("Connection"sv); }); !it.is_end())
keep_alive = it->value.trim_whitespace().equals_ignoring_ascii_case("keep-alive"sv);
if (!keep_alive)
die();