Request.cc 9.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325
  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(_url);
  100. }
  101. bool Request::resetUrl() {
  102. _previousUrl = _referer;
  103. _supportsPersistentConnection = true;
  104. return parseUrl(_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. std::string redirectedUrl;
  115. if(url.find("://") == std::string::npos) {
  116. // rfc2616 requires absolute URI should be provided by Location header
  117. // field, but some servers don't obey this rule.
  118. if(util::startsWith(url, "/")) {
  119. // abosulute path
  120. redirectedUrl = strconcat(_protocol, "://", _host, url);
  121. } else {
  122. // relative path
  123. redirectedUrl = strconcat(_protocol, "://", _host, _dir, "/", url);
  124. }
  125. } else {
  126. redirectedUrl = url;
  127. }
  128. return parseUrl(redirectedUrl);
  129. }
  130. bool Request::parseUrl(const std::string& srcUrl) {
  131. const std::string url = urlencode(removeFragment(srcUrl));
  132. _currentUrl = url;
  133. _host = A2STR::NIL;
  134. _port = 0;
  135. _dir = A2STR::NIL;
  136. _file = A2STR::NIL;
  137. _query = A2STR::NIL;
  138. _username = A2STR::NIL;
  139. _password = A2STR::NIL;
  140. _hasPassword = false;
  141. _ipv6LiteralAddress = false;
  142. // http://user:password@aria2.sourceforge.net:80/dir/file?query
  143. // | || || | | |
  144. // | || hostLast| | | |
  145. // | || portFirst| | |
  146. // authorityFirst || authorityLast | |
  147. // || | | |
  148. // userInfoLast | | |
  149. // | | | |
  150. // hostPortFirst | | |
  151. // | | |
  152. // dirFirst dirLast|
  153. // |
  154. // queryFirst
  155. // find query part
  156. std::string::const_iterator queryFirst = url.begin();
  157. for(; queryFirst != url.end(); ++queryFirst) {
  158. if(*queryFirst == '?') break;
  159. }
  160. _query = std::string(queryFirst, url.end());
  161. // find protocol
  162. std::string::size_type protocolOffset = url.find("://");
  163. if(protocolOffset == std::string::npos) return false;
  164. _protocol = std::string(url.begin(), url.begin()+protocolOffset);
  165. uint16_t defPort;
  166. if((defPort = FeatureConfig::getInstance()->getDefaultPort(_protocol)) == 0) {
  167. return false;
  168. }
  169. // find authority
  170. std::string::const_iterator authorityFirst = url.begin()+protocolOffset+3;
  171. std::string::const_iterator authorityLast = authorityFirst;
  172. for(; authorityLast != queryFirst; ++authorityLast) {
  173. if(*authorityLast == '/') break;
  174. }
  175. if(authorityFirst == authorityLast) {
  176. // No authority found
  177. return false;
  178. }
  179. // find userinfo(username and password) in authority if they exist
  180. std::string::const_iterator userInfoLast = authorityFirst;
  181. std::string::const_iterator hostPortFirst = authorityFirst;
  182. for(; userInfoLast != authorityLast; ++userInfoLast) {
  183. if(*userInfoLast == '@') {
  184. hostPortFirst = userInfoLast;
  185. ++hostPortFirst;
  186. std::string::const_iterator userLast = authorityFirst;
  187. for(; userLast != userInfoLast; ++userLast) {
  188. if(*userLast == ':') {
  189. _password = util::urldecode(std::string(userLast+1, userInfoLast));
  190. _hasPassword = true;
  191. break;
  192. }
  193. }
  194. _username = util::urldecode(std::string(authorityFirst, userLast));
  195. break;
  196. }
  197. }
  198. std::string::const_iterator hostLast = hostPortFirst;
  199. std::string::const_iterator portFirst = authorityLast;
  200. if(*hostPortFirst == '[') {
  201. // Detected IPv6 literal address in square brackets
  202. for(; hostLast != authorityLast; ++hostLast) {
  203. if(*hostLast == ']') {
  204. ++hostLast;
  205. if(hostLast == authorityLast) {
  206. _ipv6LiteralAddress = true;
  207. } else {
  208. if(*hostLast == ':') {
  209. portFirst = hostLast;
  210. ++portFirst;
  211. _ipv6LiteralAddress = true;
  212. }
  213. }
  214. break;
  215. }
  216. }
  217. if(!_ipv6LiteralAddress) {
  218. return false;
  219. }
  220. } else {
  221. for(; hostLast != authorityLast; ++hostLast) {
  222. if(*hostLast == ':') {
  223. portFirst = hostLast;
  224. ++portFirst;
  225. break;
  226. }
  227. }
  228. }
  229. if(hostPortFirst == hostLast) {
  230. // No host
  231. return false;
  232. }
  233. if(portFirst == authorityLast) {
  234. // If port is not specified, then we set it to default port of
  235. // its protocol..
  236. _port = defPort;
  237. } else {
  238. uint32_t tempPort;
  239. if(util::parseUIntNoThrow(tempPort, std::string(portFirst, authorityLast))){
  240. if(65535 < tempPort) {
  241. return false;
  242. }
  243. _port = tempPort;
  244. } else {
  245. return false;
  246. }
  247. }
  248. if(_ipv6LiteralAddress) {
  249. _host = std::string(hostPortFirst+1, hostLast-1);
  250. } else {
  251. _host = std::string(hostPortFirst, hostLast);
  252. }
  253. // find directory and file part
  254. std::string::const_iterator dirLast = authorityLast;
  255. for(std::string::const_iterator i = authorityLast;
  256. i != queryFirst; ++i) {
  257. if(*i == '/') {
  258. dirLast = i;
  259. }
  260. }
  261. if(dirLast != queryFirst) {
  262. _file = std::string(dirLast+1, queryFirst);
  263. }
  264. // Erase duplicated slashes.
  265. std::string::const_iterator dirFirst = authorityLast;
  266. for(; dirFirst != dirLast; ++dirFirst) {
  267. if(*dirFirst != '/') {
  268. --dirFirst;
  269. break;
  270. }
  271. }
  272. for(; dirLast != dirFirst; --dirLast) {
  273. if(*dirLast != '/') {
  274. ++dirLast;
  275. break;
  276. }
  277. }
  278. if(dirFirst == dirLast) {
  279. _dir = A2STR::SLASH_C;
  280. } else {
  281. _dir = std::string(dirFirst, dirLast);
  282. }
  283. return true;
  284. }
  285. void Request::resetRedirectCount()
  286. {
  287. _redirectCount = 0;
  288. }
  289. void Request::setMaxPipelinedRequest(unsigned int num)
  290. {
  291. _maxPipelinedRequest = num;
  292. }
  293. const SharedHandle<PeerStat>& Request::initPeerStat()
  294. {
  295. // Use host and protocol in original URI, because URI selector
  296. // selects URI based on original URI, not redirected one.
  297. Request origReq;
  298. origReq.setUrl(_url);
  299. _peerStat.reset(new PeerStat(0, origReq.getHost(), origReq.getProtocol()));
  300. return _peerStat;
  301. }
  302. } // namespace aria2