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; }; diff --git a/libs/network/example/http/async_server_file_upload.cpp b/libs/network/example/http/async_server_file_upload.cpp index 8224d7680..ab8b16aa2 100644 --- a/libs/network/example/http/async_server_file_upload.cpp +++ b/libs/network/example/http/async_server_file_upload.cpp @@ -189,8 +189,9 @@ 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"} }; @@ -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 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("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..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 @@ -126,12 +126,15 @@ void process_request(work_queue& queue) { std::this_thread::sleep_for(std::chrono::seconds(10)); 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..2ca8c49bf 100644 --- a/libs/network/example/http/hello_world_server.cpp +++ b/libs/network/example/http/hello_world_server.cpp @@ -31,12 +31,16 @@ struct hello_world { data << "Hello, " << ip << ':' << port << '!'; 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); } };