initial work on istream input for http parsers

This commit is contained in:
Peter Thorson
2013-04-26 08:55:36 -05:00
parent 1bbb0cba7f
commit 64eb09db06
4 changed files with 70 additions and 0 deletions

View File

@@ -829,6 +829,28 @@ BOOST_AUTO_TEST_CASE( plain_http_response ) {
BOOST_CHECK( r.get_body() == "<!doctype html>\n<html>\n<head>\n<title>Thor</title>\n</head>\n<body> \n<p>Thor</p>\n</body>" );
}
/*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<!doctype html>\n<html>\n<head>\n<title>Thor</title>\n</head>\n<body> \n<p>Thor</p>\n</body>";
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;

View File

@@ -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;

View File

@@ -47,6 +47,9 @@ namespace parser {
*/
class request : public parser {
public:
typedef request type;
typedef lib::shared_ptr<type> ptr;
typedef parser::attribute_list attribute_list;
typedef parser::parameter_list parameter_list;

View File

@@ -53,6 +53,9 @@ namespace parser {
*/
class response : public parser {
public:
typedef response type;
typedef lib::shared_ptr<type> ptr;
response()
: m_read(0)
, m_buf(new std::string())
@@ -81,6 +84,8 @@ public:
*/
size_t consume(const char *buf, size_t len);
size_t consume(std::istream & s);
/// Returns true if the response is ready.
/**
* @note will never return true if the content length header is not present