Request.cc 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210
  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::PROTO_HTTP("http");
  48. const std::string Request::PROTO_HTTPS("https");
  49. const std::string Request::PROTO_FTP("ftp");
  50. Request::Request():
  51. port_(0), tryCount_(0),
  52. redirectCount_(0),
  53. supportsPersistentConnection_(true),
  54. keepAliveHint_(false),
  55. pipeliningHint_(false),
  56. maxPipelinedRequest_(1),
  57. method_(METHOD_GET),
  58. hasPassword_(false),
  59. ipv6LiteralAddress_(false),
  60. removalRequested_(false),
  61. connectedPort_(0),
  62. wakeTime_(global::wallclock)
  63. {}
  64. Request::~Request() {}
  65. namespace {
  66. std::string removeFragment(const std::string& uri)
  67. {
  68. std::string::size_type sharpIndex = uri.find("#");
  69. if(sharpIndex == std::string::npos) {
  70. return uri;
  71. } else {
  72. return uri.substr(0, sharpIndex);
  73. }
  74. }
  75. } // namespace
  76. namespace {
  77. std::string percentEncode(const std::string& src)
  78. {
  79. std::string result;
  80. for(std::string::const_iterator i = src.begin(), eoi = src.end(); i != eoi;
  81. ++i) {
  82. // Non-Printable ASCII and non-ASCII chars + some ASCII chars.
  83. unsigned char c = *i;
  84. if(in(c, 0x00u, 0x20u) || c >= 0x7fu ||
  85. // Chromium escapes following characters. Firefox4 escapes
  86. // more.
  87. c == '"' || c == '<' || c == '>') {
  88. result += fmt("%%%02X", c);
  89. } else {
  90. result += c;
  91. }
  92. }
  93. return result;
  94. }
  95. } // namespace
  96. bool Request::setUri(const std::string& uri) {
  97. supportsPersistentConnection_ = true;
  98. uri_ = uri;
  99. return parseUri(uri_);
  100. }
  101. bool Request::resetUri() {
  102. previousUri_ = referer_;
  103. supportsPersistentConnection_ = true;
  104. setConnectedAddrInfo(A2STR::NIL, A2STR::NIL, 0);
  105. return parseUri(uri_);
  106. }
  107. void Request::setReferer(const std::string& uri)
  108. {
  109. referer_ = previousUri_ = percentEncode(removeFragment(uri));
  110. }
  111. bool Request::redirectUri(const std::string& uri) {
  112. supportsPersistentConnection_ = true;
  113. ++redirectCount_;
  114. std::string redirectedUri;
  115. if(uri.find("://") == std::string::npos) {
  116. // rfc2616 requires absolute URI should be provided by Location header
  117. // field, but some servers don't obey this rule.
  118. if(util::startsWith(uri, "/")) {
  119. // abosulute path
  120. redirectedUri = strconcat(protocol_, "://", host_, uri);
  121. } else {
  122. // relative path
  123. redirectedUri = strconcat(protocol_, "://", host_, dir_, "/", uri);
  124. }
  125. } else {
  126. redirectedUri = uri;
  127. }
  128. return parseUri(redirectedUri);
  129. }
  130. bool Request::parseUri(const std::string& srcUri) {
  131. currentUri_ = percentEncode(removeFragment(srcUri));
  132. uri::UriStruct us;
  133. if(uri::parse(us, currentUri_)) {
  134. protocol_.swap(us.protocol);
  135. host_.swap(us.host);
  136. port_ = us.port;
  137. dir_.swap(us.dir);
  138. file_.swap(us.file);
  139. query_.swap(us.query);
  140. username_.swap(us.username);
  141. password_.swap(us.password);
  142. hasPassword_ = us.hasPassword;
  143. ipv6LiteralAddress_ = us.ipv6LiteralAddress;
  144. return true;
  145. } else {
  146. return false;
  147. }
  148. }
  149. void Request::resetRedirectCount()
  150. {
  151. redirectCount_ = 0;
  152. }
  153. void Request::setMaxPipelinedRequest(unsigned int num)
  154. {
  155. maxPipelinedRequest_ = num;
  156. }
  157. const SharedHandle<PeerStat>& Request::initPeerStat()
  158. {
  159. // Use host and protocol in original URI, because URI selector
  160. // selects URI based on original URI, not redirected one.
  161. uri::UriStruct us;
  162. bool v = uri::parse(us, uri_);
  163. assert(v);
  164. peerStat_.reset(new PeerStat(0, us.host, us.protocol));
  165. return peerStat_;
  166. }
  167. std::string Request::getURIHost() const
  168. {
  169. if(isIPv6LiteralAddress()) {
  170. return strconcat("[", getHost(), "]");
  171. } else {
  172. return getHost();
  173. }
  174. }
  175. void Request::setMethod(const std::string& method)
  176. {
  177. method_ = method;
  178. }
  179. void Request::setConnectedAddrInfo
  180. (const std::string& hostname, const std::string& addr, uint16_t port)
  181. {
  182. connectedHostname_ = hostname;
  183. connectedAddr_ = addr;
  184. connectedPort_ = port;
  185. }
  186. } // namespace aria2