2006-02-21 12:28:42 +00:00
|
|
|
/* <!-- copyright */
|
|
|
|
/*
|
2006-09-21 15:31:24 +00:00
|
|
|
* aria2 - The high speed download utility
|
2006-02-21 12:28:42 +00:00
|
|
|
*
|
|
|
|
* Copyright (C) 2006 Tatsuhiro Tsujikawa
|
|
|
|
*
|
|
|
|
* This program is free software; you can redistribute it and/or modify
|
|
|
|
* it under the terms of the GNU General Public License as published by
|
|
|
|
* the Free Software Foundation; either version 2 of the License, or
|
|
|
|
* (at your option) any later version.
|
|
|
|
*
|
|
|
|
* This program is distributed in the hope that it will be useful,
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
* GNU General Public License for more details.
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU General Public License
|
|
|
|
* along with this program; if not, write to the Free Software
|
2010-01-05 16:01:46 +00:00
|
|
|
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
|
2006-09-21 15:31:24 +00:00
|
|
|
*
|
|
|
|
* In addition, as a special exception, the copyright holders give
|
|
|
|
* permission to link the code of portions of this program with the
|
|
|
|
* OpenSSL library under certain conditions as described in each
|
|
|
|
* individual source file, and distribute linked combinations
|
|
|
|
* including the two.
|
|
|
|
* You must obey the GNU General Public License in all respects
|
|
|
|
* for all of the code used other than OpenSSL. If you modify
|
|
|
|
* file(s) with this exception, you may extend this exception to your
|
|
|
|
* version of the file(s), but you are not obligated to do so. If you
|
|
|
|
* do not wish to do so, delete this exception statement from your
|
|
|
|
* version. If you delete this exception statement from all source
|
|
|
|
* files in the program, then also delete it here.
|
2006-02-21 12:28:42 +00:00
|
|
|
*/
|
|
|
|
/* copyright --> */
|
|
|
|
#include "FtpInitiateConnectionCommand.h"
|
2008-11-04 14:08:26 +00:00
|
|
|
|
|
|
|
#include <map>
|
|
|
|
|
2007-10-11 16:58:24 +00:00
|
|
|
#include "DownloadEngine.h"
|
|
|
|
#include "Option.h"
|
|
|
|
#include "Request.h"
|
2006-02-21 12:28:42 +00:00
|
|
|
#include "FtpNegotiationCommand.h"
|
2008-02-08 15:53:45 +00:00
|
|
|
#include "HttpRequest.h"
|
|
|
|
#include "Segment.h"
|
2006-02-21 12:28:42 +00:00
|
|
|
#include "HttpRequestCommand.h"
|
|
|
|
#include "FtpTunnelRequestCommand.h"
|
|
|
|
#include "DlAbortEx.h"
|
2008-02-08 15:53:45 +00:00
|
|
|
#include "Logger.h"
|
2006-02-21 12:28:42 +00:00
|
|
|
#include "message.h"
|
2006-02-21 14:00:58 +00:00
|
|
|
#include "prefs.h"
|
2007-10-17 16:26:51 +00:00
|
|
|
#include "HttpConnection.h"
|
2008-02-08 15:53:45 +00:00
|
|
|
#include "Socket.h"
|
2009-06-28 10:37:15 +00:00
|
|
|
#include "DownloadContext.h"
|
2010-03-21 07:05:49 +00:00
|
|
|
#include "util.h"
|
2010-05-21 12:22:04 +00:00
|
|
|
#include "AuthConfigFactory.h"
|
|
|
|
#include "AuthConfig.h"
|
2008-02-08 15:53:45 +00:00
|
|
|
|
|
|
|
namespace aria2 {
|
2006-02-21 12:28:42 +00:00
|
|
|
|
2008-05-08 11:18:36 +00:00
|
|
|
FtpInitiateConnectionCommand::FtpInitiateConnectionCommand
|
2010-03-20 14:30:36 +00:00
|
|
|
(cuid_t cuid,
|
2010-02-28 12:30:11 +00:00
|
|
|
const SharedHandle<Request>& req,
|
2009-06-23 15:35:45 +00:00
|
|
|
const SharedHandle<FileEntry>& fileEntry,
|
2008-05-08 11:18:36 +00:00
|
|
|
RequestGroup* requestGroup,
|
|
|
|
DownloadEngine* e)
|
2009-06-23 15:35:45 +00:00
|
|
|
:InitiateConnectionCommand(cuid, req, fileEntry, requestGroup, e) {}
|
2006-02-21 12:28:42 +00:00
|
|
|
|
2008-05-08 11:18:36 +00:00
|
|
|
FtpInitiateConnectionCommand::~FtpInitiateConnectionCommand() {}
|
2006-02-21 12:28:42 +00:00
|
|
|
|
2008-05-08 11:18:36 +00:00
|
|
|
Command* FtpInitiateConnectionCommand::createNextCommand
|
2009-07-02 15:18:13 +00:00
|
|
|
(const std::string& hostname, const std::string& addr, uint16_t port,
|
2010-02-28 12:30:11 +00:00
|
|
|
const std::vector<std::string>& resolvedAddresses,
|
2008-11-04 14:08:26 +00:00
|
|
|
const SharedHandle<Request>& proxyRequest)
|
2008-05-08 11:18:36 +00:00
|
|
|
{
|
2006-02-21 12:28:42 +00:00
|
|
|
Command* command;
|
2008-11-04 14:08:26 +00:00
|
|
|
if(!proxyRequest.isNull()) {
|
2008-11-05 12:30:22 +00:00
|
|
|
std::map<std::string, std::string> options;
|
2010-05-21 12:22:04 +00:00
|
|
|
SharedHandle<SocketCore> pooledSocket;
|
2009-02-07 13:46:08 +00:00
|
|
|
std::string proxyMethod = resolveProxyMethod(req->getProtocol());
|
2010-05-21 12:22:04 +00:00
|
|
|
if(proxyMethod == V_GET) {
|
|
|
|
pooledSocket = e->popPooledSocket(req->getHost(), req->getPort());
|
|
|
|
} else {
|
|
|
|
pooledSocket = e->popPooledSocket
|
|
|
|
(options, req->getHost(), req->getPort(),
|
|
|
|
e->getAuthConfigFactory()->createAuthConfig
|
|
|
|
(req, getOption().get())->getUser());
|
|
|
|
}
|
2008-11-05 12:30:22 +00:00
|
|
|
if(pooledSocket.isNull()) {
|
2010-03-21 07:05:49 +00:00
|
|
|
if(logger->info()) {
|
|
|
|
logger->info(MSG_CONNECTING_TO_SERVER,
|
|
|
|
util::itos(cuid).c_str(), addr.c_str(), port);
|
|
|
|
}
|
2008-11-05 12:30:22 +00:00
|
|
|
socket.reset(new SocketCore());
|
2009-07-02 15:18:13 +00:00
|
|
|
socket->establishConnection(addr, port);
|
2008-11-05 12:30:22 +00:00
|
|
|
|
2009-02-07 13:46:08 +00:00
|
|
|
if(proxyMethod == V_GET) {
|
2010-01-05 16:01:46 +00:00
|
|
|
// Use GET for FTP via HTTP proxy.
|
|
|
|
req->setMethod(Request::METHOD_GET);
|
|
|
|
SharedHandle<HttpConnection> hc
|
|
|
|
(new HttpConnection(cuid, socket, getOption().get()));
|
|
|
|
|
|
|
|
HttpRequestCommand* c =
|
|
|
|
new HttpRequestCommand(cuid, req, _fileEntry,
|
|
|
|
_requestGroup, hc, e, socket);
|
|
|
|
c->setConnectedAddr(hostname, addr, port);
|
|
|
|
c->setProxyRequest(proxyRequest);
|
|
|
|
command = c;
|
2009-02-07 13:46:08 +00:00
|
|
|
} else if(proxyMethod == V_TUNNEL) {
|
2010-01-05 16:01:46 +00:00
|
|
|
FtpTunnelRequestCommand* c =
|
|
|
|
new FtpTunnelRequestCommand(cuid, req, _fileEntry,
|
|
|
|
_requestGroup, e,
|
|
|
|
proxyRequest, socket);
|
|
|
|
c->setConnectedAddr(hostname, addr, port);
|
|
|
|
command = c;
|
2008-11-05 12:30:22 +00:00
|
|
|
} else {
|
2010-01-05 16:01:46 +00:00
|
|
|
// TODO
|
|
|
|
throw DL_ABORT_EX("ERROR");
|
2008-11-05 12:30:22 +00:00
|
|
|
}
|
2006-02-21 12:28:42 +00:00
|
|
|
} else {
|
2009-02-07 13:46:08 +00:00
|
|
|
if(proxyMethod == V_TUNNEL) {
|
2010-01-05 16:01:46 +00:00
|
|
|
command =
|
|
|
|
new FtpNegotiationCommand(cuid, req, _fileEntry,
|
|
|
|
_requestGroup, e, pooledSocket,
|
|
|
|
FtpNegotiationCommand::SEQ_SEND_CWD,
|
|
|
|
options["baseWorkingDir"]);
|
2009-02-07 13:46:08 +00:00
|
|
|
} else if(proxyMethod == V_GET) {
|
2010-01-05 16:01:46 +00:00
|
|
|
// Use GET for FTP via HTTP proxy.
|
|
|
|
req->setMethod(Request::METHOD_GET);
|
|
|
|
SharedHandle<HttpConnection> hc
|
|
|
|
(new HttpConnection(cuid, pooledSocket, getOption().get()));
|
|
|
|
|
|
|
|
HttpRequestCommand* c =
|
|
|
|
new HttpRequestCommand(cuid, req, _fileEntry,
|
|
|
|
_requestGroup, hc, e, pooledSocket);
|
|
|
|
c->setProxyRequest(proxyRequest);
|
|
|
|
command = c;
|
2008-11-05 12:30:22 +00:00
|
|
|
} else {
|
2010-01-05 16:01:46 +00:00
|
|
|
// TODO
|
|
|
|
throw DL_ABORT_EX("ERROR");
|
2008-11-05 12:30:22 +00:00
|
|
|
}
|
2006-02-21 12:28:42 +00:00
|
|
|
}
|
|
|
|
} else {
|
2008-09-24 17:01:57 +00:00
|
|
|
std::map<std::string, std::string> options;
|
2008-05-09 14:44:28 +00:00
|
|
|
SharedHandle<SocketCore> pooledSocket =
|
2010-05-21 12:22:04 +00:00
|
|
|
e->popPooledSocket(options, resolvedAddresses, req->getPort(),
|
|
|
|
e->getAuthConfigFactory()->createAuthConfig
|
|
|
|
(req, getOption().get())->getUser());
|
2008-05-09 14:44:28 +00:00
|
|
|
if(pooledSocket.isNull()) {
|
2010-03-21 07:05:49 +00:00
|
|
|
if(logger->info()) {
|
|
|
|
logger->info(MSG_CONNECTING_TO_SERVER,
|
|
|
|
util::itos(cuid).c_str(), addr.c_str(), port);
|
|
|
|
}
|
2008-05-17 09:15:14 +00:00
|
|
|
socket.reset(new SocketCore());
|
2009-07-02 15:18:13 +00:00
|
|
|
socket->establishConnection(addr, port);
|
|
|
|
FtpNegotiationCommand* c =
|
2010-01-05 16:01:46 +00:00
|
|
|
new FtpNegotiationCommand(cuid, req, _fileEntry,
|
|
|
|
_requestGroup, e, socket);
|
2009-07-02 15:18:13 +00:00
|
|
|
c->setConnectedAddr(hostname, addr, port);
|
|
|
|
command = c;
|
2008-05-09 14:44:28 +00:00
|
|
|
} else {
|
2008-09-24 17:01:57 +00:00
|
|
|
command =
|
2010-01-05 16:01:46 +00:00
|
|
|
new FtpNegotiationCommand(cuid, req, _fileEntry,
|
|
|
|
_requestGroup, e, pooledSocket,
|
|
|
|
FtpNegotiationCommand::SEQ_SEND_CWD,
|
|
|
|
options["baseWorkingDir"]);
|
2008-05-09 14:44:28 +00:00
|
|
|
}
|
2006-02-21 12:28:42 +00:00
|
|
|
}
|
2008-05-08 11:18:36 +00:00
|
|
|
return command;
|
2006-02-21 12:28:42 +00:00
|
|
|
}
|
|
|
|
|
2008-02-08 15:53:45 +00:00
|
|
|
} // namespace aria2
|