From 18cc913358baf330a05390a6a93a810614b54840 Mon Sep 17 00:00:00 2001 From: Arun Chandrasekaran Date: Sat, 13 Jan 2018 10:06:04 -0800 Subject: [PATCH 1/3] example: send response content-length as part of http header from server to client (issue #681) --- .../example/http/async_server_file_upload.cpp | 18 ++++++++++++------ ...ello_world_async_server_with_work_queue.cpp | 7 +++++-- .../example/http/hello_world_server.cpp | 8 ++++++-- 3 files changed, 23 insertions(+), 10 deletions(-) diff --git a/libs/network/example/http/async_server_file_upload.cpp b/libs/network/example/http/async_server_file_upload.cpp index 8224d7680..a30d3095c 100644 --- a/libs/network/example/http/async_server_file_upload.cpp +++ b/libs/network/example/http/async_server_file_upload.cpp @@ -191,6 +191,7 @@ struct connection_handler { void operator()(server::request const& req, const server::connection_ptr& conn) { static std::map headers = { {"Connection","close"}, + {"Content-Length", "0"}, {"Content-Type", "text/plain"} }; @@ -206,24 +207,29 @@ struct connection_handler { // Wait until the data transfer is done by the IO threads uploader->wait_for_completion(); - // Respond to the client - conn->set_status(server::connection::ok); - conn->set_headers(headers); auto end = std::chrono::high_resolution_clock::now(); std::chrono::duration diff = end - start; std::ostringstream stm; stm << "Took " << diff.count() << " milliseconds for the transfer." << std::endl; - conn->write(stm.str()); + auto body = stm.str(); + // Respond to the client + headers["Content-Length"] = std::to_string(body.size()); + conn->set_status(server::connection::ok); + conn->set_headers(headers); + conn->write(body); } catch (const file_uploader_exception& e) { + const std::string err = e.what(); + headers["Content-Length"] = std::to_string(err.size()); conn->set_status(server::connection::bad_request); conn->set_headers(headers); - const std::string err = e.what(); conn->write(err); } } else { + static std::string body("Only path allowed is /upload"); + headers["Content-Length"] = std::to_string(body.size()); conn->set_status(server::connection::bad_request); conn->set_headers(headers); - conn->write("Only path allowed is /upload."); + conn->write(body); } } }; diff --git a/libs/network/example/http/hello_world_async_server_with_work_queue.cpp b/libs/network/example/http/hello_world_async_server_with_work_queue.cpp index 0c1bbe613..f7b09c391 100644 --- a/libs/network/example/http/hello_world_async_server_with_work_queue.cpp +++ b/libs/network/example/http/hello_world_async_server_with_work_queue.cpp @@ -125,13 +125,16 @@ void process_request(work_queue& queue) { // some heavy work! std::this_thread::sleep_for(std::chrono::seconds(10)); - std::map headers = { + static std::map headers = { + {"Content-Length", "0"}, {"Content-Type", "text/plain"}, }; + std::string body("Hello, world!"); + headers["Content-Length"] = std::to_string(body.size()); request->conn->set_status(server::connection::ok); request->conn->set_headers(headers); - request->conn->write("Hello, world!"); + request->conn->write(body); } std::this_thread::sleep_for(std::chrono::microseconds(1000)); diff --git a/libs/network/example/http/hello_world_server.cpp b/libs/network/example/http/hello_world_server.cpp index b5150c8bc..8895f6681 100644 --- a/libs/network/example/http/hello_world_server.cpp +++ b/libs/network/example/http/hello_world_server.cpp @@ -30,13 +30,17 @@ struct hello_world { std::ostringstream data; data << "Hello, " << ip << ':' << port << '!'; - std::map headers = { + static std::map headers = { + {"Content-Length", "0"}, {"Content-Type", "text/plain"}, }; + auto body = data.str(); + headers["Content-Length"] = std::to_string(body.size()); + connection->set_status(server::connection::ok); connection->set_headers(headers); - connection->write(data.str()); + connection->write(body); } }; From 7d7024246a73b3ee99e9582333bcebfdef115b1b Mon Sep 17 00:00:00 2001 From: Arun Chandrasekaran Date: Sat, 13 Jan 2018 10:51:21 -0800 Subject: [PATCH 2/3] fix build warnings with member initialization order in async_message.hpp --- boost/network/protocol/http/message/async_message.hpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/boost/network/protocol/http/message/async_message.hpp b/boost/network/protocol/http/message/async_message.hpp index bfcb00dab..8fd86a0a0 100644 --- a/boost/network/protocol/http/message/async_message.hpp +++ b/boost/network/protocol/http/message/async_message.hpp @@ -145,10 +145,10 @@ struct async_message { destination_; mutable boost::shared_future status_; mutable boost::shared_future headers_; + mutable boost::optional retrieved_headers_; mutable headers_container_type added_headers; mutable std::set removed_headers; mutable boost::shared_future body_; - mutable boost::optional retrieved_headers_; friend struct boost::network::http::impl::ready_wrapper; }; From 7a0cbe77333e2e342e9d71f47c7eadc3a7512c5e Mon Sep 17 00:00:00 2001 From: Arun Chandrasekaran Date: Fri, 19 Jan 2018 19:51:17 -0800 Subject: [PATCH 3/3] Implemented review comments. --- libs/network/example/http/async_server_file_upload.cpp | 6 +++--- .../http/hello_world_async_server_with_work_queue.cpp | 2 +- libs/network/example/http/hello_world_server.cpp | 2 +- 3 files changed, 5 insertions(+), 5 deletions(-) diff --git a/libs/network/example/http/async_server_file_upload.cpp b/libs/network/example/http/async_server_file_upload.cpp index a30d3095c..ab8b16aa2 100644 --- a/libs/network/example/http/async_server_file_upload.cpp +++ b/libs/network/example/http/async_server_file_upload.cpp @@ -189,7 +189,7 @@ struct connection_handler { /// @param [in] conn Connection object /// void operator()(server::request const& req, const server::connection_ptr& conn) { - static std::map headers = { + std::map headers = { {"Connection","close"}, {"Content-Length", "0"}, {"Content-Type", "text/plain"} @@ -225,8 +225,8 @@ struct connection_handler { conn->write(err); } } else { - static std::string body("Only path allowed is /upload"); - headers["Content-Length"] = std::to_string(body.size()); + static constexpr char body[] = "Only path allowed is /upload"; + headers["Content-Length"] = std::to_string(sizeof(body)); conn->set_status(server::connection::bad_request); conn->set_headers(headers); conn->write(body); diff --git a/libs/network/example/http/hello_world_async_server_with_work_queue.cpp b/libs/network/example/http/hello_world_async_server_with_work_queue.cpp index f7b09c391..e7581347a 100644 --- a/libs/network/example/http/hello_world_async_server_with_work_queue.cpp +++ b/libs/network/example/http/hello_world_async_server_with_work_queue.cpp @@ -125,7 +125,7 @@ void process_request(work_queue& queue) { // some heavy work! std::this_thread::sleep_for(std::chrono::seconds(10)); - static std::map headers = { + std::map headers = { {"Content-Length", "0"}, {"Content-Type", "text/plain"}, }; diff --git a/libs/network/example/http/hello_world_server.cpp b/libs/network/example/http/hello_world_server.cpp index 8895f6681..2ca8c49bf 100644 --- a/libs/network/example/http/hello_world_server.cpp +++ b/libs/network/example/http/hello_world_server.cpp @@ -30,7 +30,7 @@ struct hello_world { std::ostringstream data; data << "Hello, " << ip << ':' << port << '!'; - static std::map headers = { + std::map headers = { {"Content-Length", "0"}, {"Content-Type", "text/plain"}, };