Request.cc 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147
  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 "Util.h"
  37. #include "FeatureConfig.h"
  38. #include "CookieBoxFactory.h"
  39. const string Request::METHOD_GET = "get";
  40. const string Request::METHOD_HEAD = "head";
  41. Request::Request():port(0), tryCount(0), keepAlive(true), method(METHOD_GET),
  42. _httpAuthResolver(0),
  43. _httpProxyAuthResolver(0),
  44. _ftpAuthResolver(0),
  45. cookieBox(CookieBoxFactorySingletonHolder::instance()->createNewInstance()) {}
  46. Request::~Request() {}
  47. bool Request::setUrl(const string& url) {
  48. this->url = url;
  49. return parseUrl(url);
  50. }
  51. bool Request::resetUrl() {
  52. previousUrl = referer;
  53. return setUrl(url);
  54. }
  55. bool Request::redirectUrl(const string& url) {
  56. previousUrl = currentUrl;
  57. return parseUrl(url);
  58. }
  59. bool Request::parseUrl(const string& url) {
  60. string tempUrl;
  61. string::size_type sharpIndex = url.find("#");
  62. if(sharpIndex != string::npos) {
  63. if(FeatureConfig::getInstance()->isSupported("metalink") &&
  64. url.find(METALINK_MARK) == sharpIndex) {
  65. tempUrl = url.substr(sharpIndex+strlen(METALINK_MARK));
  66. } else {
  67. tempUrl = url.substr(0, sharpIndex);
  68. }
  69. } else {
  70. tempUrl = url;
  71. }
  72. currentUrl = tempUrl;
  73. string query;
  74. host = "";
  75. port = 0;
  76. dir = "";
  77. file = "";
  78. if(tempUrl.find_first_not_of(SAFE_CHARS) != string::npos) {
  79. return false;
  80. }
  81. string::size_type startQueryIndex = tempUrl.find("?");
  82. if(startQueryIndex != string::npos) {
  83. query = tempUrl.substr(startQueryIndex);
  84. tempUrl.erase(startQueryIndex);
  85. }
  86. string::size_type hp = tempUrl.find("://");
  87. if(hp == string::npos) return false;
  88. protocol = tempUrl.substr(0, hp);
  89. int defPort;
  90. if((defPort = FeatureConfig::getInstance()->getDefaultPort(protocol)) == 0) {
  91. return false;
  92. }
  93. hp += 3;
  94. if(tempUrl.size() <= hp) return false;
  95. string::size_type hep = tempUrl.find("/", hp);
  96. if(hep == string::npos) {
  97. hep = tempUrl.size();
  98. }
  99. pair<string, string> hostAndPort;
  100. Util::split(hostAndPort, tempUrl.substr(hp, hep-hp), ':');
  101. host = hostAndPort.first;
  102. if(hostAndPort.second != "") {
  103. port = (int)strtol(hostAndPort.second.c_str(), NULL, 10);
  104. if(!(0 < port && port <= 65535)) {
  105. return false;
  106. }
  107. } else {
  108. // If port is not specified, then we set it to default port of its protocol..
  109. port = defPort;
  110. }
  111. string::size_type direp = tempUrl.find_last_of("/");
  112. if(direp == string::npos || direp <= hep) {
  113. dir = "/";
  114. direp = hep;
  115. } else {
  116. dir = tempUrl.substr(hep, direp-hep);
  117. }
  118. if(tempUrl.size() > direp+1) {
  119. file = tempUrl.substr(direp+1);
  120. }
  121. file += query;
  122. return true;
  123. }
  124. AuthConfigHandle Request::resolveHttpAuthConfig()
  125. {
  126. return _httpAuthResolver->resolveAuthConfig(getHost());
  127. }
  128. AuthConfigHandle Request::resolveFtpAuthConfig()
  129. {
  130. return _ftpAuthResolver->resolveAuthConfig(getHost());
  131. }
  132. AuthConfigHandle Request::resolveHttpProxyAuthConfig()
  133. {
  134. return _httpProxyAuthResolver->resolveAuthConfig(getHost());
  135. }