Request.cc 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101
  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. Request::Request():port(0), tryCount(0), isTorrent(false) {
  25. defaultPorts["http"] = 80;
  26. #ifdef HAVE_LIBSSL
  27. // for SSL
  28. defaultPorts["https"] = 443;
  29. #endif // HAVE_LIBSSL
  30. defaultPorts["ftp"] = 21;
  31. seg.sp = 0;
  32. seg.ep = 0;
  33. seg.ds = 0;
  34. seg.finish = false;
  35. cookieBox = new CookieBox();
  36. }
  37. Request::~Request() {}
  38. bool Request::setUrl(string url) {
  39. this->url = url;
  40. return parseUrl(url);
  41. }
  42. bool Request::resetUrl() {
  43. previousUrl = referer;
  44. return setUrl(url);
  45. }
  46. bool Request::redirectUrl(string url) {
  47. previousUrl = currentUrl;
  48. return parseUrl(url);
  49. }
  50. bool Request::parseUrl(string url) {
  51. currentUrl = url;
  52. host = "";
  53. port = 0;
  54. dir = "";
  55. file = "";
  56. if(url.find_first_not_of(SAFE_CHARS) != string::npos) {
  57. return false;
  58. }
  59. string::size_type hp = url.find("://");
  60. if(hp == string::npos) return false;
  61. protocol = url.substr(0, hp);
  62. int defPort;
  63. if((defPort = defaultPorts[protocol]) == 0) {
  64. return false;
  65. }
  66. hp += 3;
  67. if(url.size() <= hp) return false;
  68. string::size_type hep = url.find("/", hp);
  69. if(hep == string::npos) {
  70. hep = url.size();
  71. }
  72. pair<string, string> hostAndPort;
  73. Util::split(hostAndPort, url.substr(hp, hep-hp), ':');
  74. host = hostAndPort.first;
  75. if(hostAndPort.second != "") {
  76. port = (int)strtol(hostAndPort.second.c_str(), NULL, 10);
  77. if(!(0 < port && port <= 65535)) {
  78. return false;
  79. }
  80. } else {
  81. // If port is not specified, then we set it to default port of its protocol..
  82. port = defPort;
  83. }
  84. string::size_type direp = url.find_last_of("/");
  85. if(direp == string::npos || direp <= hep) {
  86. dir = "/";
  87. direp = hep;
  88. } else {
  89. dir = url.substr(hep, direp-hep);
  90. }
  91. if(url.size() > direp+1) {
  92. file = url.substr(direp+1);
  93. }
  94. return true;
  95. }