Request.cc 6.7 KB

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