From be7a04d57571137758445046072726535fbe043b Mon Sep 17 00:00:00 2001 From: Bryan Moyles Date: Sun, 22 Jul 2018 15:35:06 -0400 Subject: [PATCH 1/3] Set Content-Length when writing responses to close client connections. --- src/api_server.cc | 36 ++++++++++++++++++++++++++---------- 1 file changed, 26 insertions(+), 10 deletions(-) diff --git a/src/api_server.cc b/src/api_server.cc index 275eddaf..5e2c2e00 100644 --- a/src/api_server.cc +++ b/src/api_server.cc @@ -124,10 +124,14 @@ void MetadataApiServer::HandleMonitoredResource( LOG(INFO) << "Found resource for " << id << ": " << resource; } conn->set_status(HttpServer::connection::ok); + + std::string response = resource.ToJSON()->ToString(); + conn->set_headers(std::map({ {"Content-Type", "application/json"}, + {"Content-Length", std::to_string(response.length())}, })); - conn->write(resource.ToJSON()->ToString()); + conn->write(response); } catch (const std::out_of_range& e) { // TODO: This could be considered log spam. // As we add more resource mappings, these will become less and less @@ -136,14 +140,18 @@ void MetadataApiServer::HandleMonitoredResource( LOG(WARNING) << "No matching resource for " << id; } conn->set_status(HttpServer::connection::not_found); - conn->set_headers(std::map({ - {"Content-Type", "application/json"}, - })); json::value json_response = json::object({ {"status_code", json::number(404)}, {"error", json::string("Not found")}, }); - conn->write(json_response->ToString()); + + std::string response = json_response->ToString(); + + conn->set_headers(std::map({ + {"Content-Type", "application/json"}, + {"Content-Length", std::to_string(response.length())}, + })); + conn->write(response); } } @@ -159,21 +167,29 @@ void MetadataApiServer::HandleHealthz( LOG(INFO) << "/healthz returning 200"; } conn->set_status(HttpServer::connection::ok); + + std::string response = "healthy"; + conn->set_headers(std::map({ {"Content-Type", "text/plain"}, + {"Content-Length", std::to_string(response.length())}, })); - conn->write("healthy"); + conn->write(response); } else { LOG(WARNING) << "/healthz returning 500; unhealthy components: " << boost::algorithm::join(unhealthy_components, ", "); conn->set_status(HttpServer::connection::internal_server_error); + + std::string response = "unhealthy components:\n"; + for (const auto& component : unhealthy_components) { + response.append(component + "\n"); + } + conn->set_headers(std::map({ {"Content-Type", "text/plain"}, + {"Content-Length", std::to_string(response.length())}, })); - conn->write("unhealthy components:\n"); - for (const auto& component : unhealthy_components) { - conn->write(component + "\n"); - } + conn->write(response); } } From 0b67cd2212935511140baa42ccb1aba8e6eef502 Mon Sep 17 00:00:00 2001 From: Bryan Moyles Date: Sun, 22 Jul 2018 16:15:31 -0400 Subject: [PATCH 2/3] Address feedback. --- src/api_server.cc | 15 +++++++++------ 1 file changed, 9 insertions(+), 6 deletions(-) diff --git a/src/api_server.cc b/src/api_server.cc index 5e2c2e00..50097927 100644 --- a/src/api_server.cc +++ b/src/api_server.cc @@ -129,7 +129,7 @@ void MetadataApiServer::HandleMonitoredResource( conn->set_headers(std::map({ {"Content-Type", "application/json"}, - {"Content-Length", std::to_string(response.length())}, + {"Content-Length", std::to_string(response.size())}, })); conn->write(response); } catch (const std::out_of_range& e) { @@ -149,7 +149,7 @@ void MetadataApiServer::HandleMonitoredResource( conn->set_headers(std::map({ {"Content-Type", "application/json"}, - {"Content-Length", std::to_string(response.length())}, + {"Content-Length", std::to_string(response.size())}, })); conn->write(response); } @@ -172,7 +172,7 @@ void MetadataApiServer::HandleHealthz( conn->set_headers(std::map({ {"Content-Type", "text/plain"}, - {"Content-Length", std::to_string(response.length())}, + {"Content-Length", std::to_string(response.size())}, })); conn->write(response); } else { @@ -180,14 +180,17 @@ void MetadataApiServer::HandleHealthz( << boost::algorithm::join(unhealthy_components, ", "); conn->set_status(HttpServer::connection::internal_server_error); - std::string response = "unhealthy components:\n"; + std::ostringstream response_stream; + response_stream << "unhealthy components:\n"; for (const auto& component : unhealthy_components) { - response.append(component + "\n"); + response_stream << component << "\n"; } + std::string response = response_stream.str(); + conn->set_headers(std::map({ {"Content-Type", "text/plain"}, - {"Content-Length", std::to_string(response.length())}, + {"Content-Length", std::to_string(response.size())}, })); conn->write(response); } From c1b1e30106bef0cb4e2ad252cd1db88b0111e0f4 Mon Sep 17 00:00:00 2001 From: Bryan Moyles Date: Sun, 22 Jul 2018 17:00:04 -0400 Subject: [PATCH 3/3] Address feedback. --- src/api_server.cc | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/src/api_server.cc b/src/api_server.cc index 50097927..cb720e1b 100644 --- a/src/api_server.cc +++ b/src/api_server.cc @@ -180,8 +180,7 @@ void MetadataApiServer::HandleHealthz( << boost::algorithm::join(unhealthy_components, ", "); conn->set_status(HttpServer::connection::internal_server_error); - std::ostringstream response_stream; - response_stream << "unhealthy components:\n"; + std::ostringstream response_stream("unhealthy components:\n"); for (const auto& component : unhealthy_components) { response_stream << component << "\n"; }