diff options
| author | Tatsuhiro Tsujikawa <tatsuhiro.t@gmail.com> | 2008-04-21 10:48:11 +0000 |
|---|---|---|
| committer | Tatsuhiro Tsujikawa <tatsuhiro.t@gmail.com> | 2008-04-21 10:48:11 +0000 |
| commit | e1fa62e6c7ce3617b8bb72b4ffe63f505cc0fdda (patch) | |
| tree | 4e68de6b8c0fe23eb681dd5937b0e4409d6dbccf /src | |
| parent | 3eb74629cbd0f01113bc581d486a8ec95633b4ad (diff) | |
2008-04-21 Tatsuhiro Tsujikawa <tujikawa at rednoah dot com>
Now HTTP status and version are a member variable of HttpHeader.
HTTP status is processed as a string, not integer.
* src/AbstractProxyResponseCommand.cc
* src/HttpConnection.cc
* src/HttpHeader.cc
* src/HttpHeader.h
* src/HttpHeaderProcessor.cc
* src/HttpHeaderProcessor.h
* src/HttpResponse.cc
* src/HttpResponse.h
* test/HttpHeaderProcessorTest.cc
* test/HttpResponseTest.cc
Diffstat (limited to 'src')
| -rw-r--r-- | src/AbstractProxyResponseCommand.cc | 2 | ||||
| -rw-r--r-- | src/HttpConnection.cc | 7 | ||||
| -rw-r--r-- | src/HttpHeader.cc | 35 | ||||
| -rw-r--r-- | src/HttpHeader.h | 18 | ||||
| -rw-r--r-- | src/HttpHeaderProcessor.cc | 20 | ||||
| -rw-r--r-- | src/HttpHeaderProcessor.h | 6 | ||||
| -rw-r--r-- | src/HttpResponse.cc | 24 | ||||
| -rw-r--r-- | src/HttpResponse.h | 11 |
8 files changed, 84 insertions, 39 deletions
diff --git a/src/AbstractProxyResponseCommand.cc b/src/AbstractProxyResponseCommand.cc index e27ff382..e7a620be 100644 --- a/src/AbstractProxyResponseCommand.cc +++ b/src/AbstractProxyResponseCommand.cc @@ -65,7 +65,7 @@ bool AbstractProxyResponseCommand::executeInternal() { e->commands.push_back(this); return false; } - if(httpResponse->getStatus() != 200) { + if(httpResponse->getResponseStatus() != "200") { throw new DlRetryEx(EX_PROXY_CONNECTION_FAILED); } e->commands.push_back(getNextCommand()); diff --git a/src/HttpConnection.cc b/src/HttpConnection.cc index e624a040..02a9e600 100644 --- a/src/HttpConnection.cc +++ b/src/HttpConnection.cc @@ -134,15 +134,14 @@ HttpResponseHandle HttpConnection::receiveResponse() // OK, we got all headers. logger->info(MSG_RECEIVE_RESPONSE, cuid, proc->getHeaderString().c_str()); - std::pair<std::string, HttpHeaderHandle> httpStatusHeader = proc->getHttpStatusHeader(); - if(Util::toLower(httpStatusHeader.second->getFirst("Connection")).find("close") != std::string::npos) { + SharedHandle<HttpHeader> httpHeader = proc->getHttpResponseHeader(); + if(Util::toLower(httpHeader->getFirst("Connection")).find("close") != std::string::npos) { entry->getHttpRequest()->getRequest()->setKeepAlive(false); } HttpResponseHandle httpResponse(new HttpResponse()); httpResponse->setCuid(cuid); - httpResponse->setStatus(Util::parseInt(httpStatusHeader.first)); - httpResponse->setHttpHeader(httpStatusHeader.second); + httpResponse->setHttpHeader(httpHeader); httpResponse->setHttpRequest(entry->getHttpRequest()); outstandingHttpRequests.pop_front(); diff --git a/src/HttpHeader.cc b/src/HttpHeader.cc index 9a9ad225..5a80665e 100644 --- a/src/HttpHeader.cc +++ b/src/HttpHeader.cc @@ -35,6 +35,7 @@ #include "HttpHeader.h" #include "Range.h" #include "Util.h" +#include <istream> namespace aria2 { @@ -120,4 +121,38 @@ RangeHandle HttpHeader::getRange() const return SharedHandle<Range>(new Range(startByte, endByte, entityLength)); } +const std::string& HttpHeader::getResponseStatus() const +{ + return _responseStatus; +} + +void HttpHeader::setResponseStatus(const std::string& responseStatus) +{ + _responseStatus = responseStatus; +} + +const std::string& HttpHeader::getVersion() const +{ + return _version; +} + +void HttpHeader::setVersion(const std::string& version) +{ + _version = version; +} + +void HttpHeader::fill(std::istream& in) +{ + std::string line; + while(std::getline(in, line)) { + line = Util::trim(line); + if(line.empty()) { + break; + } + std::pair<std::string, std::string> hp; + Util::split(hp, line, ':'); + put(hp.first, hp.second); + } +} + } // namespace aria2 diff --git a/src/HttpHeader.h b/src/HttpHeader.h index 6349a9b7..87c7c82c 100644 --- a/src/HttpHeader.h +++ b/src/HttpHeader.h @@ -40,6 +40,7 @@ #include <map> #include <deque> #include <string> +#include <iosfwd> namespace aria2 { @@ -48,6 +49,13 @@ class Range; class HttpHeader { private: std::multimap<std::string, std::string> table; + + // for HTTP response header only + // response status, e.g. "200" + std::string _responseStatus; + + // HTTP version, e.g. HTTP/1.1 + std::string _version; public: HttpHeader() {} ~HttpHeader() {} @@ -60,6 +68,16 @@ public: uint64_t getFirstAsULLInt(const std::string& name) const; SharedHandle<Range> getRange() const; + + const std::string& getResponseStatus() const; + + void setResponseStatus(const std::string& responseStatus); + + const std::string& getVersion() const; + + void setVersion(const std::string& version); + + void fill(std::istream& in); }; typedef SharedHandle<HttpHeader> HttpHeaderHandle; diff --git a/src/HttpHeaderProcessor.cc b/src/HttpHeaderProcessor.cc index 254bb737..b16d7f90 100644 --- a/src/HttpHeaderProcessor.cc +++ b/src/HttpHeaderProcessor.cc @@ -77,7 +77,7 @@ bool HttpHeaderProcessor::eoh() const size_t HttpHeaderProcessor::getPutBackDataLength() const { - std::string str = strm.str(); + 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); @@ -93,7 +93,7 @@ void HttpHeaderProcessor::clear() strm.str(""); } -std::pair<std::string, HttpHeaderHandle> HttpHeaderProcessor::getHttpStatusHeader() +SharedHandle<HttpHeader> HttpHeaderProcessor::getHttpResponseHeader() { strm.seekg(0, std::ios::beg); std::string line; @@ -102,19 +102,11 @@ std::pair<std::string, HttpHeaderHandle> HttpHeaderProcessor::getHttpStatusHeade if(line.size() <= 12) { throw new DlRetryEx(EX_NO_STATUS_HEADER); } - std::string status = line.substr(9, 3); HttpHeaderHandle httpHeader(new HttpHeader()); - while(getline(strm, line)) { - line = Util::trim(line); - if(line.empty()) { - break; - } - std::pair<std::string, std::string> hp; - Util::split(hp, line, ':'); - httpHeader->put(hp.first, hp.second); - } - - return std::pair<std::string, HttpHeaderHandle>(status, httpHeader); + httpHeader->setResponseStatus(line.substr(9, 3)); + httpHeader->setVersion(line.substr(0, 8)); + httpHeader->fill(strm); + return httpHeader; } std::string HttpHeaderProcessor::getHeaderString() const diff --git a/src/HttpHeaderProcessor.h b/src/HttpHeaderProcessor.h index fc6814d3..e4bdc9f1 100644 --- a/src/HttpHeaderProcessor.h +++ b/src/HttpHeaderProcessor.h @@ -70,7 +70,11 @@ public: */ size_t getPutBackDataLength() const; - std::pair<std::string, SharedHandle<HttpHeader> > getHttpStatusHeader(); + /** + * Processes the recieved header as a http response header and returns + * HttpHeader object. + */ + SharedHandle<HttpHeader> getHttpResponseHeader(); std::string getHeaderString() const; diff --git a/src/HttpResponse.cc b/src/HttpResponse.cc index 05946e27..90cc528c 100644 --- a/src/HttpResponse.cc +++ b/src/HttpResponse.cc @@ -50,7 +50,6 @@ namespace aria2 { HttpResponse::HttpResponse():cuid(0), - status(0), logger(LogFactory::getInstance()) {} @@ -58,19 +57,19 @@ HttpResponse::~HttpResponse() {} void HttpResponse::validateResponse() const { - if(status == 401) { + const std::string& status = getResponseStatus(); + if(status == "401") { throw new DlAbortEx(EX_AUTH_FAILED); } - if(status == 404) { + if(status == "404") { throw new DlAbortEx(MSG_RESOURCE_NOT_FOUND); } - if(status >= 400) { - throw new DlAbortEx(EX_BAD_STATUS, status); + if(status >= "400") { + throw new DlAbortEx(EX_BAD_STATUS, Util::parseUInt(status)); } - if(status >= 300) { + if(status >= "300") { if(!httpHeader->defined("Location")) { - throw new DlAbortEx(EX_LOCATION_HEADER_REQUIRED, - status); + throw new DlAbortEx(EX_LOCATION_HEADER_REQUIRED, Util::parseUInt(status)); } } else { if(!httpHeader->defined("Transfer-Encoding")) { @@ -114,7 +113,8 @@ void HttpResponse::retrieveCookie() bool HttpResponse::isRedirect() const { - return 300 <= status && status < 400 && httpHeader->defined("Location"); + const std::string& status = getResponseStatus(); + return "300" <= status && status < "400" && httpHeader->defined("Location"); } void HttpResponse::processRedirect() @@ -195,4 +195,10 @@ SharedHandle<HttpRequest> HttpResponse::getHttpRequest() const return httpRequest; } +// TODO return std::string +const std::string& HttpResponse::getResponseStatus() const +{ + return httpHeader->getResponseStatus(); +} + } // namespace aria2 diff --git a/src/HttpResponse.h b/src/HttpResponse.h index 57cfdd83..7ec54db9 100644 --- a/src/HttpResponse.h +++ b/src/HttpResponse.h @@ -49,7 +49,6 @@ class Logger; class HttpResponse { private: int32_t cuid; - unsigned int status; SharedHandle<HttpRequest> httpRequest; SharedHandle<HttpHeader> httpHeader; const Logger* logger; @@ -95,15 +94,7 @@ public: SharedHandle<HttpHeader> getHttpHeader() const; - void setStatus(unsigned int status) - { - this->status = status; - } - - unsigned int getStatus() const - { - return status; - } + const std::string& getResponseStatus() const; void setHttpRequest(const SharedHandle<HttpRequest>& httpRequest); |
