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"
|
2010-11-20 08:21:36 +00:00
|
|
|
#include "LogFactory.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"
|
2012-09-28 14:27:46 +00:00
|
|
|
#include "SocketCore.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"
|
2010-11-20 08:21:36 +00:00
|
|
|
#include "fmt.h"
|
2011-01-16 07:55:41 +00:00
|
|
|
#include "SocketRecvBuffer.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;
|
2010-11-12 12:48:48 +00:00
|
|
|
if(proxyRequest) {
|
2012-09-26 14:00:05 +00:00
|
|
|
std::string options;
|
2010-05-21 12:22:04 +00:00
|
|
|
SharedHandle<SocketCore> pooledSocket;
|
2010-06-09 12:43:44 +00:00
|
|
|
std::string proxyMethod = resolveProxyMethod(getRequest()->getProtocol());
|
2010-05-21 12:22:04 +00:00
|
|
|
if(proxyMethod == V_GET) {
|
2010-06-09 12:43:44 +00:00
|
|
|
pooledSocket = getDownloadEngine()->popPooledSocket
|
|
|
|
(getRequest()->getHost(), getRequest()->getPort(),
|
2010-05-21 13:54:50 +00:00
|
|
|
proxyRequest->getHost(), proxyRequest->getPort());
|
2010-05-21 12:22:04 +00:00
|
|
|
} else {
|
2010-06-09 12:43:44 +00:00
|
|
|
pooledSocket = getDownloadEngine()->popPooledSocket
|
|
|
|
(options, getRequest()->getHost(), getRequest()->getPort(),
|
|
|
|
getDownloadEngine()->getAuthConfigFactory()->createAuthConfig
|
|
|
|
(getRequest(), getOption().get())->getUser(),
|
2010-05-21 13:54:50 +00:00
|
|
|
proxyRequest->getHost(), proxyRequest->getPort());
|
2010-05-21 12:22:04 +00:00
|
|
|
}
|
2010-11-12 12:48:48 +00:00
|
|
|
if(!pooledSocket) {
|
2010-11-20 08:21:36 +00:00
|
|
|
A2_LOG_INFO(fmt(MSG_CONNECTING_TO_SERVER,
|
2010-11-20 12:12:06 +00:00
|
|
|
getCuid(), addr.c_str(), port));
|
2010-06-09 12:43:44 +00:00
|
|
|
createSocket();
|
|
|
|
getSocket()->establishConnection(addr, port);
|
2012-10-01 14:52:22 +00:00
|
|
|
|
2010-09-09 12:00:42 +00:00
|
|
|
getRequest()->setConnectedAddrInfo(hostname, addr, port);
|
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.
|
2010-06-09 12:43:44 +00:00
|
|
|
getRequest()->setMethod(Request::METHOD_GET);
|
2011-01-16 07:55:41 +00:00
|
|
|
SharedHandle<SocketRecvBuffer> socketRecvBuffer
|
|
|
|
(new SocketRecvBuffer(getSocket()));
|
2010-01-05 16:01:46 +00:00
|
|
|
SharedHandle<HttpConnection> hc
|
2011-01-16 07:55:41 +00:00
|
|
|
(new HttpConnection(getCuid(), getSocket(), socketRecvBuffer));
|
2012-10-01 14:52:22 +00:00
|
|
|
|
2010-01-05 16:01:46 +00:00
|
|
|
HttpRequestCommand* c =
|
2010-06-09 12:43:44 +00:00
|
|
|
new HttpRequestCommand(getCuid(), getRequest(), getFileEntry(),
|
|
|
|
getRequestGroup(), hc, getDownloadEngine(),
|
|
|
|
getSocket());
|
2010-01-05 16:01:46 +00:00
|
|
|
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 =
|
2010-06-09 12:43:44 +00:00
|
|
|
new FtpTunnelRequestCommand(getCuid(), getRequest(), getFileEntry(),
|
|
|
|
getRequestGroup(), getDownloadEngine(),
|
|
|
|
proxyRequest, getSocket());
|
2010-01-05 16:01:46 +00:00
|
|
|
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 {
|
2010-09-09 12:00:42 +00:00
|
|
|
setConnectedAddrInfo(getRequest(), hostname, pooledSocket);
|
2009-02-07 13:46:08 +00:00
|
|
|
if(proxyMethod == V_TUNNEL) {
|
2012-09-26 14:00:05 +00:00
|
|
|
// options contains "baseWorkingDir"
|
2010-01-05 16:01:46 +00:00
|
|
|
command =
|
2010-06-09 12:43:44 +00:00
|
|
|
new FtpNegotiationCommand(getCuid(), getRequest(), getFileEntry(),
|
|
|
|
getRequestGroup(), getDownloadEngine(),
|
|
|
|
pooledSocket,
|
2010-07-09 11:48:59 +00:00
|
|
|
FtpNegotiationCommand::SEQ_SEND_CWD_PREP,
|
2012-09-26 14:00:05 +00:00
|
|
|
options);
|
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.
|
2010-06-09 12:43:44 +00:00
|
|
|
getRequest()->setMethod(Request::METHOD_GET);
|
2011-01-16 07:55:41 +00:00
|
|
|
SharedHandle<SocketRecvBuffer> socketRecvBuffer
|
|
|
|
(new SocketRecvBuffer(pooledSocket));
|
2010-01-05 16:01:46 +00:00
|
|
|
SharedHandle<HttpConnection> hc
|
2011-01-16 07:55:41 +00:00
|
|
|
(new HttpConnection(getCuid(), pooledSocket, socketRecvBuffer));
|
2012-10-01 14:52:22 +00:00
|
|
|
|
2010-01-05 16:01:46 +00:00
|
|
|
HttpRequestCommand* c =
|
2010-06-09 12:43:44 +00:00
|
|
|
new HttpRequestCommand(getCuid(), getRequest(), getFileEntry(),
|
|
|
|
getRequestGroup(), hc, getDownloadEngine(),
|
|
|
|
pooledSocket);
|
2010-01-05 16:01:46 +00:00
|
|
|
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 {
|
2012-09-26 14:00:05 +00:00
|
|
|
std::string options;
|
2008-05-09 14:44:28 +00:00
|
|
|
SharedHandle<SocketCore> pooledSocket =
|
2010-06-09 12:43:44 +00:00
|
|
|
getDownloadEngine()->popPooledSocket
|
|
|
|
(options, resolvedAddresses,
|
|
|
|
getRequest()->getPort(),
|
|
|
|
getDownloadEngine()->getAuthConfigFactory()->createAuthConfig
|
|
|
|
(getRequest(), getOption().get())->getUser());
|
2010-11-12 12:48:48 +00:00
|
|
|
if(!pooledSocket) {
|
2010-11-20 08:21:36 +00:00
|
|
|
A2_LOG_INFO(fmt(MSG_CONNECTING_TO_SERVER,
|
2010-11-20 12:12:06 +00:00
|
|
|
getCuid(), addr.c_str(), port));
|
2010-06-09 12:43:44 +00:00
|
|
|
createSocket();
|
|
|
|
getSocket()->establishConnection(addr, port);
|
2009-07-02 15:18:13 +00:00
|
|
|
FtpNegotiationCommand* c =
|
2010-06-09 12:43:44 +00:00
|
|
|
new FtpNegotiationCommand(getCuid(), getRequest(), getFileEntry(),
|
|
|
|
getRequestGroup(), getDownloadEngine(),
|
|
|
|
getSocket());
|
2010-09-09 12:00:42 +00:00
|
|
|
getRequest()->setConnectedAddrInfo(hostname, addr, port);
|
2009-07-02 15:18:13 +00:00
|
|
|
command = c;
|
2008-05-09 14:44:28 +00:00
|
|
|
} else {
|
2012-09-26 14:00:05 +00:00
|
|
|
// options contains "baseWorkingDir"
|
2008-09-24 17:01:57 +00:00
|
|
|
command =
|
2010-06-09 12:43:44 +00:00
|
|
|
new FtpNegotiationCommand(getCuid(), getRequest(), getFileEntry(),
|
|
|
|
getRequestGroup(), getDownloadEngine(),
|
|
|
|
pooledSocket,
|
2010-07-09 11:48:59 +00:00
|
|
|
FtpNegotiationCommand::SEQ_SEND_CWD_PREP,
|
2012-09-26 14:00:05 +00:00
|
|
|
options);
|
2010-09-09 12:00:42 +00:00
|
|
|
setConnectedAddrInfo(getRequest(), hostname, pooledSocket);
|
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
|