From 8de7cff9f41aa4778619a426f1983e68bdcea1bb Mon Sep 17 00:00:00 2001 From: Tatsuhiro Tsujikawa Date: Sun, 8 Jun 2008 13:12:24 +0000 Subject: [PATCH] 2008-06-08 Tatsuhiro Tsujikawa * src/HttpHeaderProcessor.cc: Rewritten using std::string as a buffer. * src/HttpHeaderProcessor.h: Rewritten using std::string as a buffer. * src/HttpConnection.cc: Included missing sstream. --- ChangeLog | 6 +++++ src/HttpConnection.cc | 1 + src/HttpHeaderProcessor.cc | 50 ++++++++++++++++++-------------------- src/HttpHeaderProcessor.h | 4 +-- 4 files changed, 33 insertions(+), 28 deletions(-) diff --git a/ChangeLog b/ChangeLog index bae318ab..de3ac175 100644 --- a/ChangeLog +++ b/ChangeLog @@ -1,3 +1,9 @@ +2008-06-08 Tatsuhiro Tsujikawa + + * src/HttpHeaderProcessor.cc: Rewritten using std::string as a buffer. + * src/HttpHeaderProcessor.h: Rewritten using std::string as a buffer. + * src/HttpConnection.cc: Included missing sstream. + 2008-06-08 Tatsuhiro Tsujikawa Added --bt-max-open-files option. diff --git a/src/HttpConnection.cc b/src/HttpConnection.cc index dbda02be..3676f5a5 100644 --- a/src/HttpConnection.cc +++ b/src/HttpConnection.cc @@ -48,6 +48,7 @@ #include "Logger.h" #include "Socket.h" #include "Option.h" +#include namespace aria2 { diff --git a/src/HttpHeaderProcessor.cc b/src/HttpHeaderProcessor.cc index bf35d697..9b2ef0e0 100644 --- a/src/HttpHeaderProcessor.cc +++ b/src/HttpHeaderProcessor.cc @@ -39,6 +39,7 @@ #include "DlRetryEx.h" #include "DlAbortEx.h" #include "A2STR.h" +#include namespace aria2 { @@ -49,28 +50,26 @@ HttpHeaderProcessor::~HttpHeaderProcessor() {} void HttpHeaderProcessor::update(const unsigned char* data, size_t length) { checkHeaderLimit(length); - strm.write(reinterpret_cast(data), length); + _buf += std::string(&data[0], &data[length]); } void HttpHeaderProcessor::update(const std::string& data) { checkHeaderLimit(data.size()); - strm << data; + _buf += data; } void HttpHeaderProcessor::checkHeaderLimit(size_t incomingLength) { - strm.seekg(0, std::ios::end); - if((size_t)strm.tellg()+incomingLength > _limit) { + if(_buf.size()+incomingLength > _limit) { throw DlAbortEx("Too large http header"); } - strm.clear(); } bool HttpHeaderProcessor::eoh() const { - std::string str = strm.str(); - if(str.find("\r\n\r\n") == std::string::npos && str.find("\n\n") == std::string::npos) { + if(_buf.find("\r\n\r\n") == std::string::npos && + _buf.find("\n\n") == std::string::npos) { return false; } else { return true; @@ -79,12 +78,11 @@ bool HttpHeaderProcessor::eoh() const size_t HttpHeaderProcessor::getPutBackDataLength() const { - const std::string& str = strm.str(); std::string::size_type delimpos = std::string::npos; - if((delimpos = str.find("\r\n\r\n")) != std::string::npos) { - return str.size()-(delimpos+4); - } else if((delimpos = str.find("\n\n")) != std::string::npos) { - return str.size()-(delimpos+2); + if((delimpos = _buf.find("\r\n\r\n")) != std::string::npos) { + return _buf.size()-(delimpos+4); + } else if((delimpos = _buf.find("\n\n")) != std::string::npos) { + return _buf.size()-(delimpos+2); } else { return 0; } @@ -92,35 +90,35 @@ size_t HttpHeaderProcessor::getPutBackDataLength() const void HttpHeaderProcessor::clear() { - strm.str(A2STR::NIL); + _buf.erase(); } SharedHandle HttpHeaderProcessor::getHttpResponseHeader() { - strm.seekg(0, std::ios::beg); - std::string line; - getline(strm, line); - // check HTTP status value - if(line.size() <= 12) { + std::string::size_type delimpos = std::string::npos; + if(((delimpos = _buf.find("\r\n")) == std::string::npos && + (delimpos = _buf.find("\n")) == std::string::npos) || + delimpos < 12) { throw DlRetryEx(EX_NO_STATUS_HEADER); } HttpHeaderHandle httpHeader(new HttpHeader()); - httpHeader->setResponseStatus(line.substr(9, 3)); - httpHeader->setVersion(line.substr(0, 8)); + httpHeader->setVersion(_buf.substr(0, 8)); + httpHeader->setResponseStatus(_buf.substr(9, 3)); + std::istringstream strm(_buf); + // TODO 1st line(HTTP/1.1 200...) is also send to HttpHeader, but it should + // not. httpHeader->fill(strm); - strm.clear(); return httpHeader; } std::string HttpHeaderProcessor::getHeaderString() const { - std::string str = strm.str(); std::string::size_type delimpos = std::string::npos; - if((delimpos = str.find("\r\n\r\n")) != std::string::npos || - (delimpos = str.find("\n\n")) != std::string::npos) { - return str.substr(0, delimpos); + if((delimpos = _buf.find("\r\n\r\n")) == std::string::npos && + (delimpos = _buf.find("\n\n")) == std::string::npos) { + return _buf; } else { - return str; + return _buf.substr(0, delimpos); } } diff --git a/src/HttpHeaderProcessor.h b/src/HttpHeaderProcessor.h index e4bdc9f1..1e0a703a 100644 --- a/src/HttpHeaderProcessor.h +++ b/src/HttpHeaderProcessor.h @@ -38,7 +38,7 @@ #include "common.h" #include "SharedHandle.h" #include -#include +#include namespace aria2 { @@ -46,7 +46,7 @@ class HttpHeader; class HttpHeaderProcessor { private: - std::stringstream strm; + std::string _buf; size_t _limit; void checkHeaderLimit(size_t incomingLength);