Request.cc 6.2 KB

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