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