diff --git a/test/http/parser.cpp b/test/http/parser.cpp index 2694f1cbd4..e5d2e08097 100644 --- a/test/http/parser.cpp +++ b/test/http/parser.cpp @@ -829,6 +829,28 @@ BOOST_AUTO_TEST_CASE( plain_http_response ) { BOOST_CHECK( r.get_body() == "\n\n
\nThor
\n" ); } +/*BOOST_AUTO_TEST_CASE( parse_istream ) { + websocketpp::http::parser::response r; + + std::stringstream s; + + s << "HTTP/1.1 200 OK\r\nDate: Thu, 10 May 2012 11:59:25 GMT\r\nServer: Apache/2.2.21 (Unix) mod_ssl/2.2.21 OpenSSL/0.9.8r DAV/2 PHP/5.3.8 with Suhosin-Patch\r\nLast-Modified: Tue, 30 Mar 2010 17:41:28 GMT\r\nETag: \"16799d-55-4830823a78200\"\r\nAccept-Ranges: bytes\r\nContent-Length: 85\r\nVary: Accept-Encoding\r\nContent-Type: text/html\r\n\r\n\n\n\nThor
\n"; + + bool exception = false; + size_t pos = 0; + + try { + pos += r.consume(s); + } catch (std::exception &e) { + exception = true; + std::cout << e.what() << std::endl; + } + + BOOST_CHECK( exception == false ); + BOOST_CHECK( pos == 405 ); + BOOST_CHECK( r.headers_ready() == true ); +}*/ + BOOST_AUTO_TEST_CASE( write_request_basic ) { websocketpp::http::parser::request r; diff --git a/websocketpp/http/impl/response.hpp b/websocketpp/http/impl/response.hpp index 0882fd8bf8..31d1c3afa3 100644 --- a/websocketpp/http/impl/response.hpp +++ b/websocketpp/http/impl/response.hpp @@ -131,6 +131,46 @@ inline size_t response::consume(const char *buf, size_t len) { } } +inline size_t response::consume(std::istream & s) { + char buf[512]; + size_t bytes_read; + size_t bytes_processed; + size_t total = 0; + + while (s.good()) { + s.getline(buf,512); + bytes_read = s.gcount(); + + if (s.fail() || s.eof()) { + bytes_processed = this->consume(buf,bytes_read); + total += bytes_processed; + + if (bytes_processed != bytes_read) { + // problem + break; + } + } else if (s.bad()) { + // problem + break; + } else { + // the delimiting newline was found. Replace the trailing null with + // the newline that was discarded, since our raw consume function + // expects the newline to be be there. + buf[bytes_read] = '\n'; + bytes_read++; + bytes_processed = this->consume(buf,bytes_read); + total += bytes_processed; + + if (bytes_processed != bytes_read) { + // problem + break; + } + } + } + + return total; +} + inline bool response::parse_complete(std::istream& s) { // parse a complete header (ie \r\n\r\n MUST be in the input stream) std::string response; diff --git a/websocketpp/http/request.hpp b/websocketpp/http/request.hpp index 6d6f08d305..47c05034cb 100644 --- a/websocketpp/http/request.hpp +++ b/websocketpp/http/request.hpp @@ -47,6 +47,9 @@ namespace parser { */ class request : public parser { public: + typedef request type; + typedef lib::shared_ptr