From 30c91944c293cb163700fa9b943e0e5423d93f5a Mon Sep 17 00:00:00 2001 From: Bryan Moyles Date: Wed, 28 Mar 2018 14:57:18 +0200 Subject: [PATCH 1/5] Set headers when writing responses to properly return status code. --- src/api_server.cc | 9 +++++++++ 1 file changed, 9 insertions(+) diff --git a/src/api_server.cc b/src/api_server.cc index c673854e..c3c13473 100644 --- a/src/api_server.cc +++ b/src/api_server.cc @@ -47,7 +47,12 @@ void MetadataApiServer::Handler::operator()(const HttpServer::request& request, if (config_.VerboseLogging()) { LOG(INFO) << "Found resource for " << id << ": " << resource; } + std::map headers = { + {"Content-Type", "application/json"}, + }; + conn->set_status(HttpServer::connection::ok); + conn->set_headers(headers); conn->write(resource.ToJSON()->ToString()); } catch (const std::out_of_range& e) { // TODO: This could be considered log spam. @@ -56,7 +61,11 @@ void MetadataApiServer::Handler::operator()(const HttpServer::request& request, if (config_.VerboseLogging()) { LOG(WARNING) << "No matching resource for " << id; } + std::map headers = { + {"Content-Type", "text/plain"}, + }; conn->set_status(HttpServer::connection::not_found); + conn->set_headers(headers); } } } From 550b5e29cb0578c056cddced5702585f22bccb4a Mon Sep 17 00:00:00 2001 From: Bryan Moyles Date: Thu, 29 Mar 2018 15:53:34 +0200 Subject: [PATCH 2/5] Inline headers. --- src/api_server.cc | 15 ++++++--------- 1 file changed, 6 insertions(+), 9 deletions(-) diff --git a/src/api_server.cc b/src/api_server.cc index c3c13473..d34a0374 100644 --- a/src/api_server.cc +++ b/src/api_server.cc @@ -47,12 +47,10 @@ void MetadataApiServer::Handler::operator()(const HttpServer::request& request, if (config_.VerboseLogging()) { LOG(INFO) << "Found resource for " << id << ": " << resource; } - std::map headers = { - {"Content-Type", "application/json"}, - }; - conn->set_status(HttpServer::connection::ok); - conn->set_headers(headers); + conn->set_headers(std::map({ + {"Content-Type", "application/json"}, + })); conn->write(resource.ToJSON()->ToString()); } catch (const std::out_of_range& e) { // TODO: This could be considered log spam. @@ -61,11 +59,10 @@ void MetadataApiServer::Handler::operator()(const HttpServer::request& request, if (config_.VerboseLogging()) { LOG(WARNING) << "No matching resource for " << id; } - std::map headers = { - {"Content-Type", "text/plain"}, - }; conn->set_status(HttpServer::connection::not_found); - conn->set_headers(headers); + conn->set_headers(std::map({ + {"Content-Type", "text/plain"}, + })); } } } From 3a039003fe0b6f8d48ea131be2dd1e0fab7404f1 Mon Sep 17 00:00:00 2001 From: Bryan Moyles Date: Thu, 29 Mar 2018 18:27:18 +0200 Subject: [PATCH 3/5] Return a JSON payload as a part of the 404 response. --- src/api_server.cc | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/src/api_server.cc b/src/api_server.cc index d34a0374..2fbe4cc9 100644 --- a/src/api_server.cc +++ b/src/api_server.cc @@ -63,6 +63,11 @@ void MetadataApiServer::Handler::operator()(const HttpServer::request& request, conn->set_headers(std::map({ {"Content-Type", "text/plain"}, })); + json::value json_response = json::object({ + {"status_code", json::int(404)}, + {"error", json::string("Not found")}, + }); + conn->write(json_response->ToString()); } } } From 87b28d7fa380bda4dedf0d7b48c936c98c567a88 Mon Sep 17 00:00:00 2001 From: Bryan Moyles Date: Thu, 29 Mar 2018 18:56:12 +0200 Subject: [PATCH 4/5] Use json::number as json::int doesn't exist. --- src/api_server.cc | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/api_server.cc b/src/api_server.cc index 2fbe4cc9..f4322661 100644 --- a/src/api_server.cc +++ b/src/api_server.cc @@ -64,7 +64,7 @@ void MetadataApiServer::Handler::operator()(const HttpServer::request& request, {"Content-Type", "text/plain"}, })); json::value json_response = json::object({ - {"status_code", json::int(404)}, + {"status_code", json::number(404)}, {"error", json::string("Not found")}, }); conn->write(json_response->ToString()); From 676cef0b7ec65f5ffc565d90ff922a49b70f2480 Mon Sep 17 00:00:00 2001 From: Bryan Moyles Date: Thu, 29 Mar 2018 18:58:18 +0200 Subject: [PATCH 5/5] Set Content-Type to application/json for 404 responses. --- src/api_server.cc | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/api_server.cc b/src/api_server.cc index f4322661..dc11d5dd 100644 --- a/src/api_server.cc +++ b/src/api_server.cc @@ -61,7 +61,7 @@ void MetadataApiServer::Handler::operator()(const HttpServer::request& request, } conn->set_status(HttpServer::connection::not_found); conn->set_headers(std::map({ - {"Content-Type", "text/plain"}, + {"Content-Type", "application/json"}, })); json::value json_response = json::object({ {"status_code", json::number(404)},