Request.cc 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222
  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 "Util.h"
  37. #include "FeatureConfig.h"
  38. #include "RecoverableException.h"
  39. #include "StringFormat.h"
  40. #include "A2STR.h"
  41. #include <utility>
  42. namespace aria2 {
  43. const std::string Request::METHOD_GET = "GET";
  44. const std::string Request::METHOD_HEAD = "HEAD";
  45. const std::string Request::PROTO_HTTP("http");
  46. const std::string Request::PROTO_HTTPS("https");
  47. const std::string Request::PROTO_FTP("ftp");
  48. Request::Request():
  49. port(0), tryCount(0),
  50. _redirectCount(0),
  51. _supportsPersistentConnection(true),
  52. _keepAliveHint(false),
  53. _pipeliningHint(false),
  54. method(METHOD_GET)
  55. {}
  56. Request::~Request() {}
  57. bool Request::setUrl(const std::string& url) {
  58. this->url = url;
  59. return parseUrl(url);
  60. }
  61. bool Request::resetUrl() {
  62. previousUrl = referer;
  63. return parseUrl(url);
  64. }
  65. bool Request::redirectUrl(const std::string& url) {
  66. previousUrl = A2STR::NIL;
  67. _supportsPersistentConnection = true;
  68. ++_redirectCount;
  69. if(url.find("://") == std::string::npos) {
  70. // rfc2616 requires absolute URI should be provided by Location header
  71. // field, but some servers don't obey this rule.
  72. if(Util::startsWith(url, "/")) {
  73. // abosulute path
  74. return parseUrl(protocol+"://"+host+url);
  75. } else {
  76. // relative path
  77. return parseUrl(protocol+"://"+host+dir+"/"+url);
  78. }
  79. } else {
  80. return parseUrl(url);
  81. }
  82. }
  83. bool Request::parseUrl(const std::string& url) {
  84. std::string tempUrl;
  85. std::string::size_type sharpIndex = url.find("#");
  86. if(sharpIndex != std::string::npos) {
  87. urlencode(tempUrl, url.substr(0, sharpIndex));
  88. } else {
  89. urlencode(tempUrl, url);
  90. }
  91. currentUrl = tempUrl;
  92. std::string query;
  93. host = A2STR::NIL;
  94. port = 0;
  95. dir = A2STR::NIL;
  96. file = A2STR::NIL;
  97. _query = A2STR::NIL;
  98. _username = A2STR::NIL;
  99. _password = A2STR::NIL;
  100. // find query part
  101. std::string queryTemp;
  102. std::string::size_type startQueryIndex = tempUrl.find("?");
  103. if(startQueryIndex != std::string::npos) {
  104. queryTemp = tempUrl.substr(startQueryIndex);
  105. tempUrl.erase(startQueryIndex);
  106. }
  107. // find protocol
  108. std::string::size_type hp = tempUrl.find("://");
  109. if(hp == std::string::npos) return false;
  110. protocol = tempUrl.substr(0, hp);
  111. uint16_t defPort;
  112. if((defPort = FeatureConfig::getInstance()->getDefaultPort(protocol)) == 0) {
  113. return false;
  114. }
  115. hp += 3;
  116. // find host part
  117. if(tempUrl.size() <= hp) return false;
  118. std::string::size_type hep = tempUrl.find("/", hp);
  119. if(hep == std::string::npos) {
  120. hep = tempUrl.size();
  121. }
  122. std::string hostPart = tempUrl.substr(hp, hep-hp);
  123. // find username and password in host part if they exist
  124. std::string::size_type atmarkp = hostPart.find_last_of("@");
  125. if(atmarkp != std::string::npos) {
  126. std::string authPart = hostPart.substr(0, atmarkp);
  127. std::pair<std::string, std::string> userPass =
  128. Util::split(authPart, A2STR::COLON_C);
  129. _username = Util::urldecode(userPass.first);
  130. _password = Util::urldecode(userPass.second);
  131. hostPart.erase(0, atmarkp+1);
  132. }
  133. std::pair<std::string, std::string> hostAndPort;
  134. Util::split(hostAndPort, hostPart, ':');
  135. host = hostAndPort.first;
  136. if(hostAndPort.second != A2STR::NIL) {
  137. try {
  138. port = Util::parseInt(hostAndPort.second);
  139. } catch(RecoverableException& e) {
  140. return false;
  141. }
  142. } else {
  143. // If port is not specified, then we set it to default port of its protocol..
  144. port = defPort;
  145. }
  146. // find directory and file part
  147. std::string::size_type direp = tempUrl.find_last_of("/");
  148. if(direp == std::string::npos || direp <= hep) {
  149. dir = A2STR::SLASH_C;
  150. direp = hep;
  151. } else {
  152. std::string rawDir = tempUrl.substr(hep, direp-hep);
  153. std::string::size_type p = rawDir.find_first_not_of("/");
  154. if(p != std::string::npos) {
  155. rawDir.erase(0, p-1);
  156. }
  157. p = rawDir.find_last_not_of("/");
  158. if(p != std::string::npos) {
  159. rawDir.erase(p+1);
  160. }
  161. dir = rawDir;
  162. }
  163. if(tempUrl.size() > direp+1) {
  164. file = tempUrl.substr(direp+1);
  165. }
  166. _query = queryTemp;
  167. return true;
  168. }
  169. bool Request::isHexNumber(const char c) const
  170. {
  171. return ('0' <= c && c <= '9') || ('A' <= c && c <= 'F') || ('a' <= c && c <= 'f');
  172. }
  173. void Request::urlencode(std::string& result, const std::string& src) const
  174. {
  175. if(src.empty()) {
  176. result = A2STR::NIL;
  177. return;
  178. }
  179. size_t lastIndex = src.size()-1;
  180. result = src+" ";
  181. size_t index = lastIndex;
  182. while(index-- > 0) {
  183. const unsigned char c = result[index];
  184. // '/' is not urlencoded because src is expected to be a path.
  185. if(Util::shouldUrlencode(c)) {
  186. if(c == '%') {
  187. if(!isHexNumber(result[index+1]) || !isHexNumber(result[index+2])) {
  188. result.replace(index, 1, "%25");
  189. }
  190. } else {
  191. result.replace(index, 1, StringFormat("%%%02x", c).str());
  192. }
  193. }
  194. }
  195. result.erase(result.size()-2);
  196. }
  197. void Request::resetRedirectCount()
  198. {
  199. _redirectCount = 0;
  200. }
  201. unsigned int Request::getRedirectCount() const
  202. {
  203. return _redirectCount;
  204. }
  205. } // namespace aria2