Request.cc 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118
  1. /* <!-- copyright */
  2. /*
  3. * aria2 - a simple utility for downloading files faster
  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., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
  20. */
  21. /* copyright --> */
  22. #include "Request.h"
  23. #include "Util.h"
  24. #include "FeatureConfig.h"
  25. Request::Request():port(0), tryCount(0), isTorrent(false) {
  26. seg.sp = 0;
  27. seg.ep = 0;
  28. seg.ds = 0;
  29. seg.finish = false;
  30. cookieBox = new CookieBox();
  31. }
  32. Request::~Request() {
  33. delete cookieBox;
  34. }
  35. bool Request::setUrl(const string& url) {
  36. this->url = url;
  37. return parseUrl(url);
  38. }
  39. bool Request::resetUrl() {
  40. previousUrl = referer;
  41. return setUrl(url);
  42. }
  43. bool Request::redirectUrl(const string& url) {
  44. previousUrl = currentUrl;
  45. return parseUrl(url);
  46. }
  47. bool Request::parseUrl(const string& url) {
  48. string tempUrl;
  49. string::size_type sharpIndex = url.find("#");
  50. if(sharpIndex != string::npos) {
  51. if(FeatureConfig::getInstance()->isSupported("metalink") &&
  52. url.find(METALINK_MARK) == sharpIndex) {
  53. tempUrl = url.substr(sharpIndex+strlen(METALINK_MARK));
  54. } else {
  55. tempUrl = url.substr(0, sharpIndex);
  56. }
  57. } else {
  58. tempUrl = url;
  59. }
  60. currentUrl = tempUrl;
  61. string query;
  62. host = "";
  63. port = 0;
  64. dir = "";
  65. file = "";
  66. if(tempUrl.find_first_not_of(SAFE_CHARS) != string::npos) {
  67. return false;
  68. }
  69. string::size_type startQueryIndex = tempUrl.find("?");
  70. if(startQueryIndex != string::npos) {
  71. query = tempUrl.substr(startQueryIndex);
  72. tempUrl.erase(startQueryIndex);
  73. }
  74. string::size_type hp = tempUrl.find("://");
  75. if(hp == string::npos) return false;
  76. protocol = tempUrl.substr(0, hp);
  77. int defPort;
  78. if((defPort = FeatureConfig::getInstance()->getDefaultPort(protocol)) == 0) {
  79. return false;
  80. }
  81. hp += 3;
  82. if(tempUrl.size() <= hp) return false;
  83. string::size_type hep = tempUrl.find("/", hp);
  84. if(hep == string::npos) {
  85. hep = tempUrl.size();
  86. }
  87. pair<string, string> hostAndPort;
  88. Util::split(hostAndPort, tempUrl.substr(hp, hep-hp), ':');
  89. host = hostAndPort.first;
  90. if(hostAndPort.second != "") {
  91. port = (int)strtol(hostAndPort.second.c_str(), NULL, 10);
  92. if(!(0 < port && port <= 65535)) {
  93. return false;
  94. }
  95. } else {
  96. // If port is not specified, then we set it to default port of its protocol..
  97. port = defPort;
  98. }
  99. string::size_type direp = tempUrl.find_last_of("/");
  100. if(direp == string::npos || direp <= hep) {
  101. dir = "/";
  102. direp = hep;
  103. } else {
  104. dir = tempUrl.substr(hep, direp-hep);
  105. }
  106. if(tempUrl.size() > direp+1) {
  107. file = tempUrl.substr(direp+1);
  108. }
  109. file += query;
  110. return true;
  111. }