I’m just writing a little client side HTTP application. It just sends a GET Request to an IP Camera and then receives a Screenshot in jpeg format.
Now for the implementation of HTTP I am using Boost Asio. So for the first try I oriented pretty much at the sync_client example Boost Asio Sync HTTP Client Example.
Now mainly I’m a bit worried by the separation of Headers and Data.
First I get the first line of the response:
boost::asio::streambuf response;
boost::asio::read_until(*m_Socket, response, "\r\n");
std::istream response_stream(&response);
std::string http_version;
response_stream >> http_version;
if (!response_stream || http_version.substr(0, 5) != "HTTP/")
{
std::cout << "Invalid response\n";
return;
}
uint32_t status_code;
response_stream >> status_code;
std::string status_message;
std::getline(response_stream, status_message);
if (status_code != 200) // 200 = status code OK
{
std::cout << "Response returned with status code " << status_code << "\n";
return;
}
Now until here everything is clear to me.I’m reading until first new line and then check the stuff in my buffer.
Now I’m trying to read the second part of the header:
boost::asio::read_until(*m_Socket, response, "\r\n\r\n");
std::string header;
while (std::getline(response_stream, header) && (header != "\r"))
{
std::cout << header << "\n";
}
std::cout << "\n";
Now to this I have some questions:
-
The while loop is searching until there is a blank line ( the only line where a
\rstands all by itself ). Now if I assume that a new line is defined by\r, why do I use\r\n\r\natboost::asio::read_until? I mean I would expect wether the one or the other, but both? -
If I call the
boost::asio::read_untilmethod with\r\ras delimiter it throws anEnd of Fileexception. This stands in contrary to what my while loop is searching since this is looking for a\r\r( since it looks line after line, and every line closes with a\r)
So as you can see Im quite worried how to divide stuff inside my header. It’s getting even worser because the boost::asio::read_until call does always read further than the delmiter ( this is actually OK, since it’s mentioned in the documentation ), but still it kinda always has the same trail of data ( from the actualy jpeg ), with the same length following.
Maybe someone could enlighten me?
‘\r’ is the carriage return (CR) character and ‘\n’ is the line-feed (LF) character. HTTP message lines are terminated by “\r\n” (CRLF).
From “HTTP The Definitive Guide”:
What seems to be throwing you off is that some line I/O functions (in this case std::getline()) automatically strip the trailing ‘\n’ so you are only seeing the preceding ‘\r’. I think what you should be doing, is looking for a blank line (rather than a line with only ‘\r’). And a line that is only ‘\r’ should be considered a blank line.