Request.cc 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186
  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. #include "CookieBox.h"
  40. #include "RecoverableException.h"
  41. #include <utility>
  42. namespace aria2 {
  43. const std::string Request::METHOD_GET = "get";
  44. const std::string Request::METHOD_HEAD = "head";
  45. Request::Request():port(0), tryCount(0), keepAlive(false), method(METHOD_GET),
  46. cookieBox(CookieBoxFactorySingletonHolder::instance()->createNewInstance()) {}
  47. Request::~Request() {}
  48. bool Request::setUrl(const std::string& url) {
  49. this->url = url;
  50. return parseUrl(url);
  51. }
  52. bool Request::resetUrl() {
  53. previousUrl = referer;
  54. return setUrl(url);
  55. }
  56. bool Request::redirectUrl(const std::string& url) {
  57. previousUrl = "";
  58. keepAlive = false;
  59. return parseUrl(url);
  60. }
  61. bool Request::parseUrl(const std::string& url) {
  62. std::string tempUrl;
  63. std::string::size_type sharpIndex = url.find("#");
  64. if(sharpIndex != std::string::npos) {
  65. tempUrl = urlencode(url.substr(0, sharpIndex));
  66. } else {
  67. tempUrl = urlencode(url);
  68. }
  69. currentUrl = tempUrl;
  70. std::string query;
  71. host = "";
  72. port = 0;
  73. dir = "";
  74. file = "";
  75. _username = "";
  76. _password = "";
  77. // find query part
  78. std::string::size_type startQueryIndex = tempUrl.find("?");
  79. if(startQueryIndex != std::string::npos) {
  80. query = tempUrl.substr(startQueryIndex);
  81. tempUrl.erase(startQueryIndex);
  82. }
  83. // find protocol
  84. std::string::size_type hp = tempUrl.find("://");
  85. if(hp == std::string::npos) return false;
  86. protocol = tempUrl.substr(0, hp);
  87. int32_t defPort;
  88. if((defPort = FeatureConfig::getInstance()->getDefaultPort(protocol)) == 0) {
  89. return false;
  90. }
  91. hp += 3;
  92. // find host part
  93. if(tempUrl.size() <= hp) return false;
  94. std::string::size_type hep = tempUrl.find("/", hp);
  95. if(hep == std::string::npos) {
  96. hep = tempUrl.size();
  97. }
  98. std::string hostPart = tempUrl.substr(hp, hep-hp);
  99. // find username and password in host part if they exist
  100. std::string::size_type atmarkp = hostPart.find_last_of("@");
  101. if(atmarkp != std::string::npos) {
  102. std::string authPart = hostPart.substr(0, atmarkp);
  103. std::pair<std::string, std::string> userPass = Util::split(authPart, ":");
  104. _username = Util::urldecode(userPass.first);
  105. _password = Util::urldecode(userPass.second);
  106. hostPart.erase(0, atmarkp+1);
  107. }
  108. std::pair<std::string, std::string> hostAndPort;
  109. Util::split(hostAndPort, hostPart, ':');
  110. host = hostAndPort.first;
  111. if(hostAndPort.second != "") {
  112. try {
  113. port = Util::parseInt(hostAndPort.second);
  114. if(!(0 < port && port <= 65535)) {
  115. return false;
  116. }
  117. } catch(RecoverableException* e) {
  118. delete e;
  119. return false;
  120. }
  121. } else {
  122. // If port is not specified, then we set it to default port of its protocol..
  123. port = defPort;
  124. }
  125. // find directory and file part
  126. std::string::size_type direp = tempUrl.find_last_of("/");
  127. if(direp == std::string::npos || direp <= hep) {
  128. dir = "/";
  129. direp = hep;
  130. } else {
  131. std::string rawDir = tempUrl.substr(hep, direp-hep);
  132. std::string::size_type p = rawDir.find_first_not_of("/");
  133. if(p != std::string::npos) {
  134. rawDir.erase(0, p-1);
  135. }
  136. p = rawDir.find_last_not_of("/");
  137. if(p != std::string::npos) {
  138. rawDir.erase(p+1);
  139. }
  140. dir = rawDir;
  141. }
  142. if(tempUrl.size() > direp+1) {
  143. file = tempUrl.substr(direp+1);
  144. }
  145. file += query;
  146. return true;
  147. }
  148. bool Request::isHexNumber(const char c) const
  149. {
  150. return '0' <= c && c <= '9' || 'A' <= c && c <= 'F' || 'a' <= c && c <= 'f';
  151. }
  152. std::string Request::urlencode(const std::string& src) const
  153. {
  154. int32_t lastIndex = src.size()-1;
  155. std::string result = src+" ";
  156. for(int32_t index = lastIndex; index >= 0; --index) {
  157. const char c = result[index];
  158. // '/' is not urlencoded because src is expected to be a path.
  159. if(Util::shouldUrlencode(c)) {
  160. if(c == '%') {
  161. if(!isHexNumber(result[index+1]) || !isHexNumber(result[index+2])) {
  162. result.replace(index, 1, "%25");
  163. }
  164. } else {
  165. char temp[4];
  166. sprintf(temp, "%%%02x", c);
  167. temp[3] = '\0';
  168. result.replace(index, 1, temp);
  169. }
  170. }
  171. }
  172. return result.substr(0, result.size()-2);
  173. }
  174. } // namespace aria2