Request.cc 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191
  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 "FeatureConfig.h"
  40. #include "StringFormat.h"
  41. #include "A2STR.h"
  42. #include "a2functional.h"
  43. #include "uri.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. {}
  63. namespace {
  64. std::string removeFragment(const std::string& uri)
  65. {
  66. std::string::size_type sharpIndex = uri.find("#");
  67. if(sharpIndex == std::string::npos) {
  68. return uri;
  69. } else {
  70. return uri.substr(0, sharpIndex);
  71. }
  72. }
  73. } // namespace
  74. namespace {
  75. std::string percentEncode(const std::string& src)
  76. {
  77. std::string result = src;
  78. if(src.empty()) {
  79. return result;
  80. }
  81. result += " ";
  82. for(int index = src.size()-1; index >= 0; --index) {
  83. const unsigned char c = result[index];
  84. // '/' is not percent encoded because src is expected to be a path.
  85. if(!util::inRFC3986ReservedChars(c) && !util::inRFC3986UnreservedChars(c)) {
  86. if(c == '%') {
  87. if(!util::isHexDigit(result[index+1]) ||
  88. !util::isHexDigit(result[index+2])) {
  89. result.replace(index, 1, "%25");
  90. }
  91. } else {
  92. result.replace(index, 1, StringFormat("%%%02X", c).str());
  93. }
  94. }
  95. }
  96. result.erase(result.size()-2);
  97. return result;
  98. }
  99. } // namespace
  100. bool Request::setUri(const std::string& uri) {
  101. supportsPersistentConnection_ = true;
  102. uri_ = uri;
  103. return parseUri(uri_);
  104. }
  105. bool Request::resetUri() {
  106. previousUri_ = referer_;
  107. supportsPersistentConnection_ = true;
  108. setConnectedAddrInfo(A2STR::NIL, A2STR::NIL, 0);
  109. return parseUri(uri_);
  110. }
  111. void Request::setReferer(const std::string& uri)
  112. {
  113. referer_ = previousUri_ = percentEncode(removeFragment(uri));
  114. }
  115. bool Request::redirectUri(const std::string& uri) {
  116. supportsPersistentConnection_ = true;
  117. ++redirectCount_;
  118. std::string redirectedUri;
  119. if(uri.find("://") == std::string::npos) {
  120. // rfc2616 requires absolute URI should be provided by Location header
  121. // field, but some servers don't obey this rule.
  122. if(util::startsWith(uri, "/")) {
  123. // abosulute path
  124. redirectedUri = strconcat(protocol_, "://", host_, uri);
  125. } else {
  126. // relative path
  127. redirectedUri = strconcat(protocol_, "://", host_, dir_, "/", uri);
  128. }
  129. } else {
  130. redirectedUri = uri;
  131. }
  132. return parseUri(redirectedUri);
  133. }
  134. bool Request::parseUri(const std::string& srcUri) {
  135. currentUri_ = percentEncode(removeFragment(srcUri));
  136. uri::UriStruct us;
  137. if(uri::parse(us, currentUri_)) {
  138. protocol_.swap(us.protocol);
  139. host_.swap(us.host);
  140. port_ = us.port;
  141. dir_.swap(us.dir);
  142. file_.swap(us.file);
  143. query_.swap(us.query);
  144. username_.swap(us.username);
  145. password_.swap(us.password);
  146. hasPassword_ = us.hasPassword;
  147. ipv6LiteralAddress_ = us.ipv6LiteralAddress;
  148. return true;
  149. } else {
  150. return false;
  151. }
  152. }
  153. void Request::resetRedirectCount()
  154. {
  155. redirectCount_ = 0;
  156. }
  157. void Request::setMaxPipelinedRequest(unsigned int num)
  158. {
  159. maxPipelinedRequest_ = num;
  160. }
  161. const SharedHandle<PeerStat>& Request::initPeerStat()
  162. {
  163. // Use host and protocol in original URI, because URI selector
  164. // selects URI based on original URI, not redirected one.
  165. uri::UriStruct us;
  166. bool v = uri::parse(us, uri_);
  167. assert(v);
  168. peerStat_.reset(new PeerStat(0, us.host, us.protocol));
  169. return peerStat_;
  170. }
  171. } // namespace aria2