diff --git a/client_http.hpp b/client_http.hpp index 7391b0d..79a493f 100644 --- a/client_http.hpp +++ b/client_http.hpp @@ -23,9 +23,9 @@ namespace SimpleWeb { std::unordered_map header; private: - boost::asio::streambuf streambuf; + boost::asio::streambuf content_buffer; - Response(): content(&streambuf) {}; + Response(): content(&content_buffer) {}; }; //TODO add header parameters @@ -60,14 +60,14 @@ namespace SimpleWeb { boost::asio::write(*socket, write_buffer); - size_t bytes_transferred = boost::asio::read_until(*socket, response->streambuf, "\r\n\r\n"); + size_t bytes_transferred = boost::asio::read_until(*socket, response->content_buffer, "\r\n\r\n"); - size_t num_additional_bytes=response->streambuf.size()-bytes_transferred; + size_t num_additional_bytes=response->content_buffer.size()-bytes_transferred; parse_response_header(response, response->content); if(response->header.count("Content-Length")>0) { - boost::asio::read(*socket, response->streambuf, + boost::asio::read(*socket, response->content_buffer, boost::asio::transfer_exactly(stoull(response->header["Content-Length"])-num_additional_bytes)); } else if(response->header.count("Transfer-Encoding")>0 && response->header["Transfer-Encoding"]=="chunked") { @@ -77,17 +77,17 @@ namespace SimpleWeb { size_t length; std::string buffer; do { - size_t bytes_transferred = boost::asio::read_until(*socket, response->streambuf, "\r\n"); + size_t bytes_transferred = boost::asio::read_until(*socket, response->content_buffer, "\r\n"); std::string line; getline(response->content, line); bytes_transferred-=line.size()+1; line.pop_back(); length=stoull(line, 0, 16); - size_t num_additional_bytes=response->streambuf.size()-bytes_transferred; + size_t num_additional_bytes=response->content_buffer.size()-bytes_transferred; if((2+length)>num_additional_bytes) { - boost::asio::read(*socket, response->streambuf, + boost::asio::read(*socket, response->content_buffer, boost::asio::transfer_exactly(2+length-num_additional_bytes)); } @@ -100,7 +100,7 @@ namespace SimpleWeb { response->content.get(); } while(length>0); - std::ostream response_content_output_stream(&response->streambuf); + std::ostream response_content_output_stream(&response->content_buffer); response_content_output_stream << content.rdbuf(); } } @@ -182,6 +182,8 @@ namespace SimpleWeb { public: Client(const std::string& server_port_path) : ClientBase::ClientBase(server_port_path, 80) { socket=std::make_shared(asio_io_service); + boost::asio::ip::tcp::no_delay option(true); + socket->set_option(option); }; private: diff --git a/server_http.hpp b/server_http.hpp index 53c3b54..bba616f 100644 --- a/server_http.hpp +++ b/server_http.hpp @@ -23,9 +23,9 @@ namespace SimpleWeb { std::smatch path_match; private: - Request(): content(&streambuf) {} + Request(): content(&content_buffer) {} - boost::asio::streambuf streambuf; + boost::asio::streambuf content_buffer; }; typedef std::map0) timer=set_timeout_on_socket(socket, timeout_request); - boost::asio::async_read_until(*socket, request->streambuf, "\r\n\r\n", + boost::asio::async_read_until(*socket, request->content_buffer, "\r\n\r\n", [this, socket, request, timer](const boost::system::error_code& ec, size_t bytes_transferred) { if(timeout_request>0) timer->cancel(); @@ -104,7 +104,7 @@ namespace SimpleWeb { //"After a successful async_read_until operation, the streambuf may contain additional data beyond the delimiter" //The chosen solution is to extract lines from the stream directly when parsing the header. What is left of the //streambuf (maybe some bytes of the content) is appended to in the async_read-function below (for retrieving content). - size_t num_additional_bytes=request->streambuf.size()-bytes_transferred; + size_t num_additional_bytes=request->content_buffer.size()-bytes_transferred; parse_request(request, request->content); @@ -115,7 +115,7 @@ namespace SimpleWeb { if(timeout_content>0) timer=set_timeout_on_socket(socket, timeout_content); - boost::asio::async_read(*socket, request->streambuf, + boost::asio::async_read(*socket, request->content_buffer, boost::asio::transfer_exactly(stoull(request->header["Content-Length"])-num_additional_bytes), [this, socket, request, timer] (const boost::system::error_code& ec, size_t bytes_transferred) {