eidheim / Simple-Web-Server

A very simple, fast, multithreaded, platform independent HTTP and HTTPS server and client library implemented using C++11 and Boost.Asio. Created to be an easy way to make REST resources available from C++ applications.
MIT License
2.62k stars 758 forks source link

Client is not reading complete response if neither Conten-Length nor Transfer-Encoding are set in header #112

Closed hytano closed 7 years ago

hytano commented 7 years ago

I am trying to access a HTTP server which is responding with HTTP 1.1.

The client is not returning the full response from the server.

The options "Content-Lenght" and "Transfer-Encoding" are NOT present in the server's responses. But the option "Connection : close" is present. As I understand HTTP the option "Connection : close" should be enough to tell the client to read everything until the connection is closed by the server and treat all this data as part of the response.

If I insert the following code in std::shared_ptr<Response> request_read() everything works fine:

else if((header_it=response->header.find("Transfer-Encoding"))!=response->header.end() && header_it->second=="chunked") {
    request_read_chunked(response, chunked_streambuf);
} else if((header_it=response->header.find("Connection"))!=response->header.end() && header_it->second=="close") {
        auto timer=get_timeout_timer();
        boost::asio::async_read(*socket, response->content_buffer, boost::asio::transfer_at_least(1), [this, timer](const boost::system::error_code& ec, size_t /*bytes_transferred*/) {
        if(timer)
                timer->cancel();
        if(ec) {
            std::lock_guard<std::mutex> lock(socket_mutex);
            this->socket=nullptr;
            throw boost::system::system_error(ec);
        }
    });
 }
eidheim commented 7 years ago

Thank you, I'll look at this Tomorrow hopefully.

eidheim commented 7 years ago

I did some changes to your code in the above commit, but thank you again for reporting this issue.