HttpHeaderProcessor.cc 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173
  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 <vector>
  37. #include "HttpHeader.h"
  38. #include "message.h"
  39. #include "util.h"
  40. #include "DlRetryEx.h"
  41. #include "DlAbortEx.h"
  42. #include "A2STR.h"
  43. #include "error_code.h"
  44. namespace aria2 {
  45. HttpHeaderProcessor::HttpHeaderProcessor():
  46. limit_(21/*lines*/*8190/*per line*/) {}
  47. // The above values come from Apache's documentation
  48. // http://httpd.apache.org/docs/2.2/en/mod/core.html: See
  49. // LimitRequestFieldSize and LimitRequestLine directive. Also the
  50. // page states that the number of request fields rarely exceeds 20.
  51. // aria2 uses this class in both client and server side.
  52. HttpHeaderProcessor::~HttpHeaderProcessor() {}
  53. void HttpHeaderProcessor::update(const unsigned char* data, size_t length)
  54. {
  55. checkHeaderLimit(length);
  56. buf_.append(&data[0], &data[length]);
  57. }
  58. void HttpHeaderProcessor::update(const std::string& data)
  59. {
  60. checkHeaderLimit(data.size());
  61. buf_ += data;
  62. }
  63. void HttpHeaderProcessor::checkHeaderLimit(size_t incomingLength)
  64. {
  65. if(buf_.size()+incomingLength > limit_) {
  66. throw DL_ABORT_EX2("Too large http header",
  67. error_code::HTTP_PROTOCOL_ERROR);
  68. }
  69. }
  70. bool HttpHeaderProcessor::eoh() const
  71. {
  72. if(buf_.find("\r\n\r\n") == std::string::npos &&
  73. buf_.find("\n\n") == std::string::npos) {
  74. return false;
  75. } else {
  76. return true;
  77. }
  78. }
  79. size_t HttpHeaderProcessor::getPutBackDataLength() const
  80. {
  81. std::string::size_type delimpos = std::string::npos;
  82. if((delimpos = buf_.find("\r\n\r\n")) != std::string::npos) {
  83. return buf_.size()-(delimpos+4);
  84. } else if((delimpos = buf_.find("\n\n")) != std::string::npos) {
  85. return buf_.size()-(delimpos+2);
  86. } else {
  87. return 0;
  88. }
  89. }
  90. void HttpHeaderProcessor::clear()
  91. {
  92. buf_.erase();
  93. }
  94. SharedHandle<HttpHeader> HttpHeaderProcessor::getHttpResponseHeader()
  95. {
  96. std::string::size_type delimpos = std::string::npos;
  97. if(((delimpos = buf_.find("\r\n")) == std::string::npos &&
  98. (delimpos = buf_.find("\n")) == std::string::npos) ||
  99. delimpos < 12) {
  100. throw DL_RETRY_EX(EX_NO_STATUS_HEADER);
  101. }
  102. int32_t statusCode;
  103. if(!util::parseIntNoThrow(statusCode,
  104. std::string(buf_.begin()+9, buf_.begin()+12))) {
  105. throw DL_RETRY_EX("Status code could not be parsed as integer.");
  106. }
  107. HttpHeaderHandle httpHeader(new HttpHeader());
  108. httpHeader->setVersion(buf_.begin(), buf_.begin()+8);
  109. httpHeader->setStatusCode(statusCode);
  110. // TODO 1st line(HTTP/1.1 200...) is also send to HttpHeader, but it should
  111. // not.
  112. if((delimpos = buf_.find("\r\n\r\n")) == std::string::npos &&
  113. (delimpos = buf_.find("\n\n")) == std::string::npos) {
  114. delimpos = buf_.size();
  115. }
  116. httpHeader->fill(buf_.begin(), buf_.begin()+delimpos);
  117. return httpHeader;
  118. }
  119. SharedHandle<HttpHeader> HttpHeaderProcessor::getHttpRequestHeader()
  120. {
  121. // The minimum case of the first line is:
  122. // GET / HTTP/1.x
  123. // At least 14bytes before \r\n or \n.
  124. std::string::size_type delimpos = std::string::npos;
  125. if(((delimpos = buf_.find("\r\n")) == std::string::npos &&
  126. (delimpos = buf_.find("\n")) == std::string::npos) ||
  127. delimpos < 14) {
  128. throw DL_RETRY_EX(EX_NO_STATUS_HEADER);
  129. }
  130. std::vector<Scip> firstLine;
  131. util::splitIter(buf_.begin(), buf_.begin()+delimpos,
  132. std::back_inserter(firstLine), ' ', true);
  133. if(firstLine.size() != 3) {
  134. throw DL_ABORT_EX2("Malformed HTTP request header.",
  135. error_code::HTTP_PROTOCOL_ERROR);
  136. }
  137. SharedHandle<HttpHeader> httpHeader(new HttpHeader());
  138. httpHeader->setMethod(firstLine[0].first, firstLine[0].second);
  139. httpHeader->setRequestPath(firstLine[1].first, firstLine[1].second);
  140. httpHeader->setVersion(firstLine[2].first, firstLine[2].second);
  141. if((delimpos = buf_.find("\r\n\r\n")) == std::string::npos &&
  142. (delimpos = buf_.find("\n\n")) == std::string::npos) {
  143. delimpos = buf_.size();
  144. }
  145. httpHeader->fill(buf_.begin(), buf_.begin()+delimpos);
  146. return httpHeader;
  147. }
  148. std::string HttpHeaderProcessor::getHeaderString() const
  149. {
  150. std::string::size_type delimpos = std::string::npos;
  151. if((delimpos = buf_.find("\r\n\r\n")) == std::string::npos &&
  152. (delimpos = buf_.find("\n\n")) == std::string::npos) {
  153. return buf_;
  154. } else {
  155. return buf_.substr(0, delimpos);
  156. }
  157. }
  158. } // namespace aria2