Request.cc 9.6 KB

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