/* */ #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_(21/*lines*/*8190/*per line*/) {} // The above values come from Apache's documentation // http://httpd.apache.org/docs/2.2/en/mod/core.html: See // LimitRequestFieldSize and LimitRequestLine directive. Also the // page states that the number of request fields rarely exceeds 20. // aria2 uses this class in both client and server side. 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); } int32_t statusCode; if(!util::parseIntNoThrow(statusCode, buf_.substr(9, 3))) { throw DL_RETRY_EX("Status code could not be parsed as integer."); } HttpHeaderHandle httpHeader(new HttpHeader()); httpHeader->setVersion(buf_.substr(0, 8)); httpHeader->setStatusCode(statusCode); 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