Request.cc 3.9 KB

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