Request.cc 9.6 KB

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