Request.cc 9.6 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. 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. supportsPersistentConnection_ = true;
  113. ++redirectCount_;
  114. std::string redirectedUri;
  115. if(uri.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(uri, "/")) {
  119. // abosulute path
  120. redirectedUri = strconcat(protocol_, "://", host_, uri);
  121. } else {
  122. // relative path
  123. redirectedUri = strconcat(protocol_, "://", host_, dir_, "/", uri);
  124. }
  125. } else {
  126. redirectedUri = uri;
  127. }
  128. return parseUri(redirectedUri);
  129. }
  130. bool Request::parseUri(const std::string& srcUri) {
  131. const std::string uri = percentEncode(removeFragment(srcUri));
  132. currentUri_ = uri;
  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 = uri.begin();
  157. for(; queryFirst != uri.end(); ++queryFirst) {
  158. if(*queryFirst == '?') break;
  159. }
  160. query_ = std::string(queryFirst, uri.end());
  161. // find protocol
  162. std::string::size_type protocolOffset = uri.find("://");
  163. if(protocolOffset == std::string::npos) return false;
  164. protocol_ = std::string(uri.begin(), uri.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 = uri.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 = authorityLast;
  181. std::string::const_iterator hostPortFirst = authorityFirst;
  182. for(; userInfoLast != authorityFirst-1; --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::percentDecode(std::string(userLast+1,userInfoLast));
  190. hasPassword_ = true;
  191. break;
  192. }
  193. }
  194. username_ = util::percentDecode(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.setUri(uri_);
  299. peerStat_.reset(new PeerStat(0, origReq.getHost(), origReq.getProtocol()));
  300. return peerStat_;
  301. }
  302. } // namespace aria2