Request.cc 9.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322
  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 "StringFormat.h"
  40. #include "A2STR.h"
  41. #include "a2functional.h"
  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. _maxPipelinedRequest(1),
  55. _method(METHOD_GET),
  56. _hasPassword(false),
  57. _ipv6LiteralAddress(false)
  58. {}
  59. static std::string removeFragment(const std::string& url)
  60. {
  61. std::string::size_type sharpIndex = url.find("#");
  62. if(sharpIndex == std::string::npos) {
  63. return url;
  64. } else {
  65. return url.substr(0, sharpIndex);
  66. }
  67. }
  68. static bool isHexNumber(const char c)
  69. {
  70. return ('0' <= c && c <= '9') || ('A' <= c && c <= 'F') ||
  71. ('a' <= c && c <= 'f');
  72. }
  73. static std::string urlencode(const std::string& src)
  74. {
  75. std::string result = src;
  76. if(src.empty()) {
  77. return result;
  78. }
  79. result += " ";
  80. for(int index = src.size()-1; index >= 0; --index) {
  81. const unsigned char c = result[index];
  82. // '/' is not urlencoded because src is expected to be a path.
  83. if(!util::inRFC3986ReservedChars(c) && !util::inRFC3986UnreservedChars(c)) {
  84. if(c == '%') {
  85. if(!isHexNumber(result[index+1]) || !isHexNumber(result[index+2])) {
  86. result.replace(index, 1, "%25");
  87. }
  88. } else {
  89. result.replace(index, 1, StringFormat("%%%02X", c).str());
  90. }
  91. }
  92. }
  93. result.erase(result.size()-2);
  94. return result;
  95. }
  96. bool Request::setUrl(const std::string& url) {
  97. _supportsPersistentConnection = true;
  98. _url = url;
  99. return parseUrl(urlencode(removeFragment(_url)));
  100. }
  101. bool Request::resetUrl() {
  102. _previousUrl = _referer;
  103. _supportsPersistentConnection = true;
  104. return parseUrl(urlencode(removeFragment(_url)));
  105. }
  106. void Request::setReferer(const std::string& url)
  107. {
  108. _referer = _previousUrl = urlencode(removeFragment(url));
  109. }
  110. bool Request::redirectUrl(const std::string& url) {
  111. _previousUrl = A2STR::NIL;
  112. _supportsPersistentConnection = true;
  113. ++_redirectCount;
  114. if(url.find("://") == std::string::npos) {
  115. // rfc2616 requires absolute URI should be provided by Location header
  116. // field, but some servers don't obey this rule.
  117. if(util::startsWith(url, "/")) {
  118. // abosulute path
  119. return parseUrl(strconcat(_protocol, "://", _host, url));
  120. } else {
  121. // relative path
  122. return parseUrl(strconcat(_protocol, "://", _host, _dir, "/", url));
  123. }
  124. } else {
  125. return parseUrl(url);
  126. }
  127. }
  128. bool Request::parseUrl(const std::string& url) {
  129. _currentUrl = url;
  130. _host = A2STR::NIL;
  131. _port = 0;
  132. _dir = A2STR::NIL;
  133. _file = A2STR::NIL;
  134. _query = A2STR::NIL;
  135. _username = A2STR::NIL;
  136. _password = A2STR::NIL;
  137. _hasPassword = false;
  138. _ipv6LiteralAddress = false;
  139. // http://user:password@aria2.sourceforge.net:80/dir/file?query
  140. // | || || | | |
  141. // | || hostLast| | | |
  142. // | || portFirst| | |
  143. // authorityFirst || authorityLast | |
  144. // || | | |
  145. // userInfoLast | | |
  146. // | | | |
  147. // hostPortFirst | | |
  148. // | | |
  149. // dirFirst dirLast|
  150. // |
  151. // queryFirst
  152. // find query part
  153. std::string::const_iterator queryFirst = url.begin();
  154. for(; queryFirst != url.end(); ++queryFirst) {
  155. if(*queryFirst == '?') break;
  156. }
  157. _query = std::string(queryFirst, url.end());
  158. // find protocol
  159. std::string::size_type protocolOffset = url.find("://");
  160. if(protocolOffset == std::string::npos) return false;
  161. _protocol = std::string(url.begin(), url.begin()+protocolOffset);
  162. uint16_t defPort;
  163. if((defPort = FeatureConfig::getInstance()->getDefaultPort(_protocol)) == 0) {
  164. return false;
  165. }
  166. // find authority
  167. std::string::const_iterator authorityFirst = url.begin()+protocolOffset+3;
  168. std::string::const_iterator authorityLast = authorityFirst;
  169. for(; authorityLast != queryFirst; ++authorityLast) {
  170. if(*authorityLast == '/') break;
  171. }
  172. if(authorityFirst == authorityLast) {
  173. // No authority found
  174. return false;
  175. }
  176. // find userinfo(username and password) in authority if they exist
  177. std::string::const_iterator userInfoLast = authorityFirst;
  178. std::string::const_iterator hostPortFirst = authorityFirst;
  179. for(; userInfoLast != authorityLast; ++userInfoLast) {
  180. if(*userInfoLast == '@') {
  181. hostPortFirst = userInfoLast;
  182. ++hostPortFirst;
  183. std::string::const_iterator userLast = authorityFirst;
  184. for(; userLast != userInfoLast; ++userLast) {
  185. if(*userLast == ':') {
  186. _password = util::urldecode(std::string(userLast+1, userInfoLast));
  187. _hasPassword = true;
  188. break;
  189. }
  190. }
  191. _username = util::urldecode(std::string(authorityFirst, userLast));
  192. break;
  193. }
  194. }
  195. std::string::const_iterator hostLast = hostPortFirst;
  196. std::string::const_iterator portFirst = authorityLast;
  197. if(*hostPortFirst == '[') {
  198. // Detected IPv6 literal address in square brackets
  199. for(; hostLast != authorityLast; ++hostLast) {
  200. if(*hostLast == ']') {
  201. ++hostLast;
  202. if(hostLast == authorityLast) {
  203. _ipv6LiteralAddress = true;
  204. } else {
  205. if(*hostLast == ':') {
  206. portFirst = hostLast;
  207. ++portFirst;
  208. _ipv6LiteralAddress = true;
  209. }
  210. }
  211. break;
  212. }
  213. }
  214. if(!_ipv6LiteralAddress) {
  215. return false;
  216. }
  217. } else {
  218. for(; hostLast != authorityLast; ++hostLast) {
  219. if(*hostLast == ':') {
  220. portFirst = hostLast;
  221. ++portFirst;
  222. break;
  223. }
  224. }
  225. }
  226. if(hostPortFirst == hostLast) {
  227. // No host
  228. return false;
  229. }
  230. if(portFirst == authorityLast) {
  231. // If port is not specified, then we set it to default port of
  232. // its protocol..
  233. _port = defPort;
  234. } else {
  235. uint32_t tempPort;
  236. if(util::parseUIntNoThrow(tempPort, std::string(portFirst, authorityLast))){
  237. if(65535 < tempPort) {
  238. return false;
  239. }
  240. _port = tempPort;
  241. } else {
  242. return false;
  243. }
  244. }
  245. if(_ipv6LiteralAddress) {
  246. _host = std::string(hostPortFirst+1, hostLast-1);
  247. } else {
  248. _host = std::string(hostPortFirst, hostLast);
  249. }
  250. // find directory and file part
  251. std::string::const_iterator dirLast = authorityLast;
  252. for(std::string::const_iterator i = authorityLast;
  253. i != queryFirst; ++i) {
  254. if(*i == '/') {
  255. dirLast = i;
  256. }
  257. }
  258. if(dirLast != queryFirst) {
  259. _file = std::string(dirLast+1, queryFirst);
  260. }
  261. // Erase duplicated slashes.
  262. std::string::const_iterator dirFirst = authorityLast;
  263. for(; dirFirst != dirLast; ++dirFirst) {
  264. if(*dirFirst != '/') {
  265. --dirFirst;
  266. break;
  267. }
  268. }
  269. for(; dirLast != dirFirst; --dirLast) {
  270. if(*dirLast != '/') {
  271. ++dirLast;
  272. break;
  273. }
  274. }
  275. if(dirFirst == dirLast) {
  276. _dir = A2STR::SLASH_C;
  277. } else {
  278. _dir = std::string(dirFirst, dirLast);
  279. }
  280. return true;
  281. }
  282. void Request::resetRedirectCount()
  283. {
  284. _redirectCount = 0;
  285. }
  286. void Request::setMaxPipelinedRequest(unsigned int num)
  287. {
  288. _maxPipelinedRequest = num;
  289. }
  290. const SharedHandle<PeerStat>& Request::initPeerStat()
  291. {
  292. // Use host and protocol in original URI, because URI selector
  293. // selects URI based on original URI, not redirected one.
  294. Request origReq;
  295. origReq.setUrl(_url);
  296. _peerStat.reset(new PeerStat(0, origReq.getHost(), origReq.getProtocol()));
  297. return _peerStat;
  298. }
  299. } // namespace aria2