HttpHeaderProcessor.cc 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163
  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. 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_ += std::string(&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_EX("Too large http header");
  67. }
  68. }
  69. bool HttpHeaderProcessor::eoh() const
  70. {
  71. if(buf_.find("\r\n\r\n") == std::string::npos &&
  72. buf_.find("\n\n") == std::string::npos) {
  73. return false;
  74. } else {
  75. return true;
  76. }
  77. }
  78. size_t HttpHeaderProcessor::getPutBackDataLength() const
  79. {
  80. std::string::size_type delimpos = std::string::npos;
  81. if((delimpos = buf_.find("\r\n\r\n")) != std::string::npos) {
  82. return buf_.size()-(delimpos+4);
  83. } else if((delimpos = buf_.find("\n\n")) != std::string::npos) {
  84. return buf_.size()-(delimpos+2);
  85. } else {
  86. return 0;
  87. }
  88. }
  89. void HttpHeaderProcessor::clear()
  90. {
  91. buf_.erase();
  92. }
  93. SharedHandle<HttpHeader> HttpHeaderProcessor::getHttpResponseHeader()
  94. {
  95. std::string::size_type delimpos = std::string::npos;
  96. if(((delimpos = buf_.find("\r\n")) == std::string::npos &&
  97. (delimpos = buf_.find("\n")) == std::string::npos) ||
  98. delimpos < 12) {
  99. throw DL_RETRY_EX(EX_NO_STATUS_HEADER);
  100. }
  101. int32_t statusCode;
  102. if(!util::parseIntNoThrow(statusCode, buf_.substr(9, 3))) {
  103. throw DL_RETRY_EX("Status code could not be parsed as integer.");
  104. }
  105. HttpHeaderHandle httpHeader(new HttpHeader());
  106. httpHeader->setVersion(buf_.substr(0, 8));
  107. httpHeader->setStatusCode(statusCode);
  108. std::istringstream strm(buf_);
  109. // TODO 1st line(HTTP/1.1 200...) is also send to HttpHeader, but it should
  110. // not.
  111. httpHeader->fill(strm);
  112. return httpHeader;
  113. }
  114. SharedHandle<HttpHeader> HttpHeaderProcessor::getHttpRequestHeader()
  115. {
  116. // The minimum case of the first line is:
  117. // GET / HTTP/1.x
  118. // At least 14bytes before \r\n or \n.
  119. std::string::size_type delimpos = std::string::npos;
  120. if(((delimpos = buf_.find("\r\n")) == std::string::npos &&
  121. (delimpos = buf_.find("\n")) == std::string::npos) ||
  122. delimpos < 14) {
  123. throw DL_RETRY_EX(EX_NO_STATUS_HEADER);
  124. }
  125. std::vector<std::string> firstLine;
  126. util::split(buf_.substr(0, delimpos), std::back_inserter(firstLine)," ",true);
  127. if(firstLine.size() != 3) {
  128. throw DL_ABORT_EX("Malformed HTTP request header.");
  129. }
  130. SharedHandle<HttpHeader> httpHeader(new HttpHeader());
  131. httpHeader->setMethod(firstLine[0]);
  132. httpHeader->setRequestPath(firstLine[1]);
  133. httpHeader->setVersion(firstLine[2]);
  134. std::istringstream strm(buf_.substr(delimpos));
  135. httpHeader->fill(strm);
  136. return httpHeader;
  137. }
  138. std::string HttpHeaderProcessor::getHeaderString() const
  139. {
  140. std::string::size_type delimpos = std::string::npos;
  141. if((delimpos = buf_.find("\r\n\r\n")) == std::string::npos &&
  142. (delimpos = buf_.find("\n\n")) == std::string::npos) {
  143. return buf_;
  144. } else {
  145. return buf_.substr(0, delimpos);
  146. }
  147. }
  148. } // namespace aria2