FtpInitiateConnectionCommand.cc 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384
  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 "FtpInitiateConnectionCommand.h"
  23. #include "FtpNegotiationCommand.h"
  24. #include "HttpRequestCommand.h"
  25. #include "FtpTunnelRequestCommand.h"
  26. #include "DlAbortEx.h"
  27. #include "message.h"
  28. FtpInitiateConnectionCommand::FtpInitiateConnectionCommand(int cuid, Request* req, DownloadEngine* e):AbstractCommand(cuid, req, e) {}
  29. FtpInitiateConnectionCommand::~FtpInitiateConnectionCommand() {}
  30. bool FtpInitiateConnectionCommand::executeInternal(Segment segment) {
  31. if(!e->segmentMan->downloadStarted) {
  32. e->segmentMan->filename = req->getFile();
  33. bool segFileExists = e->segmentMan->segmentFileExists();
  34. if(segFileExists) {
  35. e->segmentMan->load();
  36. e->diskWriter->openExistingFile(e->segmentMan->getFilePath());
  37. e->segmentMan->downloadStarted = true;
  38. } else {
  39. e->diskWriter->initAndOpenFile(e->segmentMan->getFilePath());
  40. }
  41. }
  42. socket = new Socket();
  43. Command* command;
  44. if(useHttpProxy()) {
  45. e->logger->info(MSG_CONNECTING_TO_SERVER, cuid,
  46. e->option->get("http_proxy_host").c_str(),
  47. e->option->getAsInt("http_proxy_port"));
  48. socket->establishConnection(e->option->get("http_proxy_host"),
  49. e->option->getAsInt("http_proxy_port"));
  50. if(useHttpProxyGet()) {
  51. command = new HttpRequestCommand(cuid, req, e, socket);
  52. } else if(useHttpProxyConnect()) {
  53. command = new FtpTunnelRequestCommand(cuid, req, e, socket);
  54. } else {
  55. // TODO
  56. throw new DlAbortEx("ERROR");
  57. }
  58. } else {
  59. e->logger->info(MSG_CONNECTING_TO_SERVER, cuid, req->getHost().c_str(),
  60. req->getPort());
  61. socket->establishConnection(req->getHost(), req->getPort());
  62. command = new FtpNegotiationCommand(cuid, req, e, socket);
  63. }
  64. e->commands.push(command);
  65. return true;
  66. }
  67. bool FtpInitiateConnectionCommand::useHttpProxy() const {
  68. return e->option->defined("http_proxy_enabled") &&
  69. e->option->get("http_proxy_enabled") == "true";
  70. }
  71. bool FtpInitiateConnectionCommand::useHttpProxyGet() const {
  72. return useHttpProxy() && e->option->get("ftp_via_http_proxy") == "get";
  73. }
  74. bool FtpInitiateConnectionCommand::useHttpProxyConnect() const {
  75. return useHttpProxy() && e->option->get("ftp_via_http_proxy") == "tunnel";
  76. }