Request.cc 7.2 KB

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