asio problem with detecting http header end
Hello, I use Boost.ASIO for receiving Twitter JSON data. I use this code: boost::asio::write( p_socket, l_request ); boost::asio::streambuf l_response; boost::asio::read_until(p_socket, l_response, "\r\n\r\n"); So in my optional the l_response variable should be read the full HTTP header. But if I write it to the std::cout I get: HTTP/1.1 200 OK Date: Fri, 29 Jul 2011 20:08:37 GMT Server: hi Cache-Control: max-age=15, must-revalidate, max-age=300 Expires: Fri, 29 Jul 2011 20:13:37 GMT Content-Type: application/json;charset=utf-8 Content-Length: 9763 Vary: Accept-Encoding X-Varnish: 1045274856 Age: 0 Via: 1.1 varnish X-Cache-Svr: smf1-aba-35-sr2.prod.twitter.com X-Cache: MISS Connection: close {"completed_in":0.025,"max_id":97033383951081472,"max_id_str":"97033383951081472","next_page":"?page=2&max_id=9703338395108147 I can change the "until read parameter" to \n, Connection: close or something else, but the read_until method does not stop the reading at the correct chars. Same code on a Wikipedia call runs without problems. Can anyone help me please to receive the correct header? Thanks Phil
I use Boost.ASIO for receiving Twitter JSON data. I use this code: boost::asio::write( p_socket, l_request );
boost::asio::streambuf l_response;
boost::asio::read_until(p_socket, l_response, "\r\n\r\n"); So in my optional the l_response variable should be read the full HTTP header. But if I write it to the std::cout I get: HTTP/1.1 200 OK Date: Fri, 29 Jul 2011 20:08:37 GMT Server: hi Cache-Control: max-age=15, must-revalidate, max-age=300 Expires: Fri, 29 Jul 2011 20:13:37 GMT Content-Type: application/json;charset=utf-8 Content-Length: 9763 Vary: Accept-Encoding X-Varnish: 1045274856 Age: 0 Via: 1.1 varnish X-Cache-Svr: smf1-aba-35-sr2.prod.twitter.com X-Cache: MISS Connection: close {"completed_in":0.025,"max_id":97033383951081472,"max_id_str":"97033383951081472","next_page":"?page=2&max_id=9703338395108147 I can change the "until read parameter" to \n, Connection: close or something else, but the read_until method does not stop the reading at the correct chars.
Read the manual: http://www.boost.org/doc/libs/1_47_0/doc/html/boost_asio/reference/read_unti... read_until is just a convenience function that reads *some* data until it finds your substring.
Il 07/30/2011 08:39 PM, Igor R ha scritto:
I use Boost.ASIO for receiving Twitter JSON data. I use this code: boost::asio::write( p_socket, l_request );
boost::asio::streambuf l_response;
boost::asio::read_until(p_socket, l_response, "\r\n\r\n");
Read the manual: http://www.boost.org/doc/libs/1_47_0/doc/html/boost_asio/reference/read_unti...
I would also suggest that you look to the http server examples in the documentation. The problem of reading an http response, as your client should do, is no different from reading an http request as the server in the examples does (also parsing it is little different, thus reading the examples would benefit you for this). As an aside, let me remark that your approach was flawed also from the http point of view. Think what would happen if the server, for any reason whatsoever, sent you a message without a body... The empty line is intended as a _separator_, not as a _termiantor_ i.e. if there is no body there is no CRLF alone on its line, because there is nothing to separate! Thus in that case your call will lock forever waiting for a sequence which would never come (well, it would not lock truly forever as the server would close the connection ad you'll end up with an unexpected connection error, but you still would have problems) -- Leo Cacciari Aliae nationes servitutem pati possunt populi romani est propria libertas
On 2011-07-30 20:39:05 +0200, Igor R said:
I use Boost.ASIO for receiving Twitter JSON data. I use this code: boost::asio::write( p_socket, l_request );
boost::asio::streambuf l_response;
boost::asio::read_until(p_socket, l_response, "\r\n\r\n"); So in my optional the l_response variable should be read the full HTTP header. But if I write it to the std::cout I get: HTTP/1.1 200 OK Date: Fri, 29 Jul 2011 20:08:37 GMT Server: hi Cache-Control: max-age=15, must-revalidate, max-age=300 Expires: Fri, 29 Jul 2011 20:13:37 GMT Content-Type: application/json;charset=utf-8 Content-Length: 9763 Vary: Accept-Encoding X-Varnish: 1045274856 Age: 0 Via: 1.1 varnish X-Cache-Svr: smf1-aba-35-sr2.prod.twitter.com X-Cache: MISS Connection: close {"completed_in":0.025,"max_id":97033383951081472,"max_id_str":"97033383951081472","next_page":"?page=2&max_id=9703338395108147
I can change the "until read parameter" to \n, Connection: close or something else, but the read_until method does not stop the reading at the correct chars.
Read the manual: http://www.boost.org/doc/libs/1_47_0/doc/html/boost_asio/reference/read_unti...
read_until is just a convenience function that reads *some* data until it finds your substring.
Thanks, I have solved with clearing the buffer. I extract the hedaer with getline and run my working with the rest
participants (4)
-
Igor R
-
Kraus Philipp
-
Leo Cacciari
-
Philipp Kraus