HttpHeaderProcessor.cc 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166
  1. /* <!-- copyright */
  2. /*
  3. * aria2 - The high speed download utility
  4. *
  5. * Copyright (C) 2006 Tatsuhiro Tsujikawa
  6. *
  7. * This program is free software; you can redistribute it and/or modify
  8. * it under the terms of the GNU General Public License as published by
  9. * the Free Software Foundation; either version 2 of the License, or
  10. * (at your option) any later version.
  11. *
  12. * This program is distributed in the hope that it will be useful,
  13. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  14. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  15. * GNU General Public License for more details.
  16. *
  17. * You should have received a copy of the GNU General Public License
  18. * along with this program; if not, write to the Free Software
  19. * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
  20. *
  21. * In addition, as a special exception, the copyright holders give
  22. * permission to link the code of portions of this program with the
  23. * OpenSSL library under certain conditions as described in each
  24. * individual source file, and distribute linked combinations
  25. * including the two.
  26. * You must obey the GNU General Public License in all respects
  27. * for all of the code used other than OpenSSL. If you modify
  28. * file(s) with this exception, you may extend this exception to your
  29. * version of the file(s), but you are not obligated to do so. If you
  30. * do not wish to do so, delete this exception statement from your
  31. * version. If you delete this exception statement from all source
  32. * files in the program, then also delete it here.
  33. */
  34. /* copyright --> */
  35. #include "HttpHeaderProcessor.h"
  36. #include <sstream>
  37. #include <vector>
  38. #include "HttpHeader.h"
  39. #include "message.h"
  40. #include "util.h"
  41. #include "DlRetryEx.h"
  42. #include "DlAbortEx.h"
  43. #include "A2STR.h"
  44. #include "error_code.h"
  45. namespace aria2 {
  46. HttpHeaderProcessor::HttpHeaderProcessor():
  47. limit_(21/*lines*/*8190/*per line*/) {}
  48. // The above values come from Apache's documentation
  49. // http://httpd.apache.org/docs/2.2/en/mod/core.html: See
  50. // LimitRequestFieldSize and LimitRequestLine directive. Also the
  51. // page states that the number of request fields rarely exceeds 20.
  52. // aria2 uses this class in both client and server side.
  53. HttpHeaderProcessor::~HttpHeaderProcessor() {}
  54. void HttpHeaderProcessor::update(const unsigned char* data, size_t length)
  55. {
  56. checkHeaderLimit(length);
  57. buf_ += std::string(&data[0], &data[length]);
  58. }
  59. void HttpHeaderProcessor::update(const std::string& data)
  60. {
  61. checkHeaderLimit(data.size());
  62. buf_ += data;
  63. }
  64. void HttpHeaderProcessor::checkHeaderLimit(size_t incomingLength)
  65. {
  66. if(buf_.size()+incomingLength > limit_) {
  67. throw DL_ABORT_EX2("Too large http header",
  68. error_code::HTTP_PROTOCOL_ERROR);
  69. }
  70. }
  71. bool HttpHeaderProcessor::eoh() const
  72. {
  73. if(buf_.find("\r\n\r\n") == std::string::npos &&
  74. buf_.find("\n\n") == std::string::npos) {
  75. return false;
  76. } else {
  77. return true;
  78. }
  79. }
  80. size_t HttpHeaderProcessor::getPutBackDataLength() const
  81. {
  82. std::string::size_type delimpos = std::string::npos;
  83. if((delimpos = buf_.find("\r\n\r\n")) != std::string::npos) {
  84. return buf_.size()-(delimpos+4);
  85. } else if((delimpos = buf_.find("\n\n")) != std::string::npos) {
  86. return buf_.size()-(delimpos+2);
  87. } else {
  88. return 0;
  89. }
  90. }
  91. void HttpHeaderProcessor::clear()
  92. {
  93. buf_.erase();
  94. }
  95. SharedHandle<HttpHeader> HttpHeaderProcessor::getHttpResponseHeader()
  96. {
  97. std::string::size_type delimpos = std::string::npos;
  98. if(((delimpos = buf_.find("\r\n")) == std::string::npos &&
  99. (delimpos = buf_.find("\n")) == std::string::npos) ||
  100. delimpos < 12) {
  101. throw DL_RETRY_EX(EX_NO_STATUS_HEADER);
  102. }
  103. int32_t statusCode;
  104. if(!util::parseIntNoThrow(statusCode, buf_.substr(9, 3))) {
  105. throw DL_RETRY_EX("Status code could not be parsed as integer.");
  106. }
  107. HttpHeaderHandle httpHeader(new HttpHeader());
  108. httpHeader->setVersion(buf_.substr(0, 8));
  109. httpHeader->setStatusCode(statusCode);
  110. std::istringstream strm(buf_);
  111. // TODO 1st line(HTTP/1.1 200...) is also send to HttpHeader, but it should
  112. // not.
  113. httpHeader->fill(strm);
  114. return httpHeader;
  115. }
  116. SharedHandle<HttpHeader> HttpHeaderProcessor::getHttpRequestHeader()
  117. {
  118. // The minimum case of the first line is:
  119. // GET / HTTP/1.x
  120. // At least 14bytes before \r\n or \n.
  121. std::string::size_type delimpos = std::string::npos;
  122. if(((delimpos = buf_.find("\r\n")) == std::string::npos &&
  123. (delimpos = buf_.find("\n")) == std::string::npos) ||
  124. delimpos < 14) {
  125. throw DL_RETRY_EX(EX_NO_STATUS_HEADER);
  126. }
  127. std::vector<std::string> firstLine;
  128. util::split(buf_.substr(0, delimpos), std::back_inserter(firstLine)," ",true);
  129. if(firstLine.size() != 3) {
  130. throw DL_ABORT_EX2("Malformed HTTP request header.",
  131. error_code::HTTP_PROTOCOL_ERROR);
  132. }
  133. SharedHandle<HttpHeader> httpHeader(new HttpHeader());
  134. httpHeader->setMethod(firstLine[0]);
  135. httpHeader->setRequestPath(firstLine[1]);
  136. httpHeader->setVersion(firstLine[2]);
  137. std::istringstream strm(buf_.substr(delimpos));
  138. httpHeader->fill(strm);
  139. return httpHeader;
  140. }
  141. std::string HttpHeaderProcessor::getHeaderString() const
  142. {
  143. std::string::size_type delimpos = std::string::npos;
  144. if((delimpos = buf_.find("\r\n\r\n")) == std::string::npos &&
  145. (delimpos = buf_.find("\n\n")) == std::string::npos) {
  146. return buf_;
  147. } else {
  148. return buf_.substr(0, delimpos);
  149. }
  150. }
  151. } // namespace aria2