Request.cc 6.6 KB

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