Request.cc 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202
  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 "Request.h"
  36. #include <cassert>
  37. #include <utility>
  38. #include "util.h"
  39. #include "fmt.h"
  40. #include "A2STR.h"
  41. #include "uri.h"
  42. #include "PeerStat.h"
  43. #include "wallclock.h"
  44. namespace aria2 {
  45. const std::string Request::METHOD_GET = "GET";
  46. const std::string Request::METHOD_HEAD = "HEAD";
  47. const std::string Request::DEFAULT_FILE = "index.html";
  48. Request::Request()
  49. : method_(METHOD_GET),
  50. tryCount_(0),
  51. redirectCount_(0),
  52. supportsPersistentConnection_(true),
  53. keepAliveHint_(false),
  54. pipeliningHint_(false),
  55. maxPipelinedRequest_(1),
  56. removalRequested_(false),
  57. connectedPort_(0),
  58. wakeTime_(global::wallclock())
  59. {
  60. }
  61. Request::~Request() {}
  62. namespace {
  63. std::string removeFragment(const std::string& uri)
  64. {
  65. std::string::size_type sharpIndex = uri.find("#");
  66. if (sharpIndex == std::string::npos) {
  67. return uri;
  68. }
  69. else {
  70. return uri.substr(0, sharpIndex);
  71. }
  72. }
  73. } // namespace
  74. bool Request::setUri(const std::string& uri)
  75. {
  76. supportsPersistentConnection_ = true;
  77. uri_ = uri;
  78. return parseUri(uri_);
  79. }
  80. bool Request::resetUri()
  81. {
  82. supportsPersistentConnection_ = true;
  83. setConnectedAddrInfo(A2STR::NIL, A2STR::NIL, 0);
  84. return parseUri(uri_);
  85. }
  86. void Request::setReferer(const std::string& uri)
  87. {
  88. referer_ = removeFragment(uri);
  89. }
  90. bool Request::redirectUri(const std::string& uri)
  91. {
  92. supportsPersistentConnection_ = true;
  93. ++redirectCount_;
  94. if (uri.empty()) {
  95. return false;
  96. }
  97. std::string redirectedUri;
  98. if (util::startsWith(uri, "//")) {
  99. // Network-path reference (according to RFC 3986, Section 4.2)
  100. // Just complement current protocol.
  101. redirectedUri = getProtocol();
  102. redirectedUri += ":";
  103. redirectedUri += uri;
  104. }
  105. else {
  106. std::string::size_type schemeEnd = uri.find("://");
  107. bool absUri;
  108. if (schemeEnd == std::string::npos) {
  109. absUri = false;
  110. }
  111. else {
  112. absUri = true;
  113. // Check that scheme is acceptable one.
  114. for (size_t i = 0; i < schemeEnd; ++i) {
  115. char c = uri[i];
  116. if (!util::isAlpha(c) && !util::isDigit(c) && c != '+' && c != '-' &&
  117. c != '.') {
  118. absUri = false;
  119. break;
  120. }
  121. }
  122. }
  123. if (absUri) {
  124. redirectedUri = uri;
  125. }
  126. else {
  127. // rfc2616 requires absolute URI should be provided by Location header
  128. // field, but some servers don't obey this rule.
  129. // UPDATE: draft-ietf-httpbis-p2-semantics-18 now allows this.
  130. redirectedUri = uri::joinUri(currentUri_, uri);
  131. }
  132. }
  133. return parseUri(redirectedUri);
  134. }
  135. bool Request::parseUri(const std::string& srcUri)
  136. {
  137. currentUri_ = removeFragment(srcUri);
  138. uri::UriStruct us;
  139. if (uri::parse(us, currentUri_)) {
  140. us_.swap(us);
  141. return true;
  142. }
  143. else {
  144. return false;
  145. }
  146. }
  147. void Request::resetRedirectCount() { redirectCount_ = 0; }
  148. void Request::setMaxPipelinedRequest(int num) { maxPipelinedRequest_ = num; }
  149. const std::shared_ptr<PeerStat>& Request::initPeerStat()
  150. {
  151. // Use host and protocol in original URI, because URI selector
  152. // selects URI based on original URI, not redirected one.
  153. uri_split_result us;
  154. int v = uri_split(&us, uri_.c_str());
  155. assert(v == 0);
  156. std::string host = uri::getFieldString(us, USR_HOST, uri_.c_str());
  157. std::string protocol = uri::getFieldString(us, USR_SCHEME, uri_.c_str());
  158. peerStat_ = std::make_shared<PeerStat>(0, host, protocol);
  159. return peerStat_;
  160. }
  161. std::string Request::getURIHost() const
  162. {
  163. if (isIPv6LiteralAddress()) {
  164. std::string s = "[";
  165. s += getHost();
  166. s += "]";
  167. return s;
  168. }
  169. else {
  170. return getHost();
  171. }
  172. }
  173. void Request::setMethod(const std::string& method) { method_ = method; }
  174. void Request::setConnectedAddrInfo(const std::string& hostname,
  175. const std::string& addr, uint16_t port)
  176. {
  177. connectedHostname_ = hostname;
  178. connectedAddr_ = addr;
  179. connectedPort_ = port;
  180. }
  181. } // namespace aria2