/* */ #include "HttpHeaderProcessor.h" #include #include #include "HttpHeader.h" #include "message.h" #include "util.h" #include "DlRetryEx.h" #include "DlAbortEx.h" #include "A2STR.h" namespace aria2 { HttpHeaderProcessor::HttpHeaderProcessor():_limit(4096) {} HttpHeaderProcessor::~HttpHeaderProcessor() {} void HttpHeaderProcessor::update(const unsigned char* data, size_t length) { checkHeaderLimit(length); _buf += std::string(&data[0], &data[length]); } void HttpHeaderProcessor::update(const std::string& data) { checkHeaderLimit(data.size()); _buf += data; } void HttpHeaderProcessor::checkHeaderLimit(size_t incomingLength) { if(_buf.size()+incomingLength > _limit) { throw DL_ABORT_EX("Too large http header"); } } bool HttpHeaderProcessor::eoh() const { if(_buf.find("\r\n\r\n") == std::string::npos && _buf.find("\n\n") == std::string::npos) { return false; } else { return true; } } size_t HttpHeaderProcessor::getPutBackDataLength() const { std::string::size_type delimpos = std::string::npos; 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; } } void HttpHeaderProcessor::clear() { _buf.erase(); } SharedHandle HttpHeaderProcessor::getHttpResponseHeader() { 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 DL_RETRY_EX(EX_NO_STATUS_HEADER); } HttpHeaderHandle httpHeader(new HttpHeader()); 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); return httpHeader; } SharedHandle HttpHeaderProcessor::getHttpRequestHeader() { // The minimum case of the first line is: // GET / HTTP/1.x // At least 14bytes before \r\n or \n. 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 < 14) { throw DL_RETRY_EX(EX_NO_STATUS_HEADER); } std::vector firstLine; util::split(_buf.substr(0, delimpos), std::back_inserter(firstLine)," ",true); if(firstLine.size() != 3) { throw DL_ABORT_EX("Malformed HTTP request header."); } SharedHandle httpHeader(new HttpHeader()); httpHeader->setMethod(firstLine[0]); httpHeader->setRequestPath(firstLine[1]); httpHeader->setVersion(firstLine[2]); std::istringstream strm(_buf.substr(delimpos)); httpHeader->fill(strm); return httpHeader; } std::string HttpHeaderProcessor::getHeaderString() const { std::string::size_type delimpos = std::string::npos; if((delimpos = _buf.find("\r\n\r\n")) == std::string::npos && (delimpos = _buf.find("\n\n")) == std::string::npos) { return _buf; } else { return _buf.substr(0, delimpos); } } } // namespace aria2