From ea7ac38ed7694dc1d867d5c7137eb76a86ace8cc Mon Sep 17 00:00:00 2001 From: Peter Thorson Date: Sat, 27 Apr 2013 13:55:25 -0500 Subject: [PATCH] bugfixes and tests for new http istream consume --- test/http/parser.cpp | 14 +++++++------- websocketpp/http/constants.hpp | 3 +++ websocketpp/http/impl/response.hpp | 9 ++++----- 3 files changed, 14 insertions(+), 12 deletions(-) diff --git a/test/http/parser.cpp b/test/http/parser.cpp index e5d2e08097..49604268fe 100644 --- a/test/http/parser.cpp +++ b/test/http/parser.cpp @@ -829,7 +829,7 @@ BOOST_AUTO_TEST_CASE( plain_http_response ) { BOOST_CHECK( r.get_body() == "\n\n\nThor\n\n \n

Thor

\n" ); } -/*BOOST_AUTO_TEST_CASE( parse_istream ) { +BOOST_AUTO_TEST_CASE( parse_istream ) { websocketpp::http::parser::response r; std::stringstream s; @@ -846,10 +846,11 @@ BOOST_AUTO_TEST_CASE( plain_http_response ) { std::cout << e.what() << std::endl; } - BOOST_CHECK( exception == false ); - BOOST_CHECK( pos == 405 ); - BOOST_CHECK( r.headers_ready() == true ); -}*/ + BOOST_CHECK_EQUAL( exception, false ); + BOOST_CHECK_EQUAL( pos, 405 ); + BOOST_CHECK_EQUAL( r.headers_ready(), true ); + BOOST_CHECK_EQUAL( r.ready(), true ); +} BOOST_AUTO_TEST_CASE( write_request_basic ) { websocketpp::http::parser::request r; @@ -888,6 +889,5 @@ BOOST_AUTO_TEST_CASE( write_request_with_body ) { r.replace_header("Content-Type","application/x-www-form-urlencoded"); r.set_body("licenseID=string&content=string¶msXML=string"); - std::cout << r.raw() << std::endl; BOOST_CHECK( r.raw() == raw ); -} \ No newline at end of file +} diff --git a/websocketpp/http/constants.hpp b/websocketpp/http/constants.hpp index e952777143..dfdf1dabed 100644 --- a/websocketpp/http/constants.hpp +++ b/websocketpp/http/constants.hpp @@ -39,6 +39,9 @@ namespace http { // Maximum size in bytes before rejecting an HTTP header as too big. const size_t max_header_size = 16000; + // Number of bytes to use for temporary istream read buffers + const size_t istream_buffer = 512; + // invalid HTTP token characters // 0x00 - 0x32, 0x7f-0xff // ( ) < > @ , ; : \ " / [ ] ? = { } diff --git a/websocketpp/http/impl/response.hpp b/websocketpp/http/impl/response.hpp index 31d1c3afa3..1d3f2204f3 100644 --- a/websocketpp/http/impl/response.hpp +++ b/websocketpp/http/impl/response.hpp @@ -132,15 +132,15 @@ inline size_t response::consume(const char *buf, size_t len) { } inline size_t response::consume(std::istream & s) { - char buf[512]; + char buf[istream_buffer]; size_t bytes_read; size_t bytes_processed; size_t total = 0; while (s.good()) { - s.getline(buf,512); + s.getline(buf,istream_buffer); bytes_read = s.gcount(); - + if (s.fail() || s.eof()) { bytes_processed = this->consume(buf,bytes_read); total += bytes_processed; @@ -156,8 +156,7 @@ inline size_t response::consume(std::istream & s) { // 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++; + buf[bytes_read-1] = '\n'; bytes_processed = this->consume(buf,bytes_read); total += bytes_processed;