renamed streambuf to content_buffer

This commit is contained in:
eidheim 2014-08-12 07:47:27 +02:00
commit 38ca7e5575
2 changed files with 16 additions and 14 deletions

View file

@ -23,9 +23,9 @@ namespace SimpleWeb {
std::unordered_map<std::string, std::string> header; std::unordered_map<std::string, std::string> header;
private: private:
boost::asio::streambuf streambuf; boost::asio::streambuf content_buffer;
Response(): content(&streambuf) {}; Response(): content(&content_buffer) {};
}; };
//TODO add header parameters //TODO add header parameters
@ -60,14 +60,14 @@ namespace SimpleWeb {
boost::asio::write(*socket, write_buffer); 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); parse_response_header(response, response->content);
if(response->header.count("Content-Length")>0) { 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)); 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") { else if(response->header.count("Transfer-Encoding")>0 && response->header["Transfer-Encoding"]=="chunked") {
@ -77,17 +77,17 @@ namespace SimpleWeb {
size_t length; size_t length;
std::string buffer; std::string buffer;
do { 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; std::string line;
getline(response->content, line); getline(response->content, line);
bytes_transferred-=line.size()+1; bytes_transferred-=line.size()+1;
line.pop_back(); line.pop_back();
length=stoull(line, 0, 16); 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) { 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)); boost::asio::transfer_exactly(2+length-num_additional_bytes));
} }
@ -100,7 +100,7 @@ namespace SimpleWeb {
response->content.get(); response->content.get();
} while(length>0); } 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(); response_content_output_stream << content.rdbuf();
} }
} }
@ -182,6 +182,8 @@ namespace SimpleWeb {
public: public:
Client(const std::string& server_port_path) : ClientBase<HTTP>::ClientBase(server_port_path, 80) { Client(const std::string& server_port_path) : ClientBase<HTTP>::ClientBase(server_port_path, 80) {
socket=std::make_shared<HTTP>(asio_io_service); socket=std::make_shared<HTTP>(asio_io_service);
boost::asio::ip::tcp::no_delay option(true);
socket->set_option(option);
}; };
private: private:

View file

@ -23,9 +23,9 @@ namespace SimpleWeb {
std::smatch path_match; std::smatch path_match;
private: private:
Request(): content(&streambuf) {} Request(): content(&content_buffer) {}
boost::asio::streambuf streambuf; boost::asio::streambuf content_buffer;
}; };
typedef std::map<std::string, std::unordered_map<std::string, typedef std::map<std::string, std::unordered_map<std::string,
@ -95,7 +95,7 @@ namespace SimpleWeb {
if(timeout_request>0) if(timeout_request>0)
timer=set_timeout_on_socket(socket, timeout_request); 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) { [this, socket, request, timer](const boost::system::error_code& ec, size_t bytes_transferred) {
if(timeout_request>0) if(timeout_request>0)
timer->cancel(); timer->cancel();
@ -104,7 +104,7 @@ namespace SimpleWeb {
//"After a successful async_read_until operation, the streambuf may contain additional data beyond the delimiter" //"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 //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). //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); parse_request(request, request->content);
@ -115,7 +115,7 @@ namespace SimpleWeb {
if(timeout_content>0) if(timeout_content>0)
timer=set_timeout_on_socket(socket, timeout_content); 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), boost::asio::transfer_exactly(stoull(request->header["Content-Length"])-num_additional_bytes),
[this, socket, request, timer] [this, socket, request, timer]
(const boost::system::error_code& ec, size_t bytes_transferred) { (const boost::system::error_code& ec, size_t bytes_transferred) {