Rewrite backup connection handling

Now dedicated ConnectCommand handles connection establishment.  It
checks whether connection is established or not.  It also handles
backup connection. The next Command creation is abstracted using
ControlChain struct template.
pull/60/head
Tatsuhiro Tsujikawa 2013-04-01 23:51:33 +09:00
parent cf23e3e11d
commit 26972b8535
24 changed files with 664 additions and 307 deletions

View File

@ -555,6 +555,13 @@ void AbstractCommand::setWriteCheckSocketIf
}
}
void AbstractCommand::swapSocket(SharedHandle<SocketCore>& socket)
{
disableReadCheckSocket();
disableWriteCheckSocket();
socket_.swap(socket);
}
namespace {
// Constructs proxy URI, merging username and password if they are
// defined.

View File

@ -89,7 +89,7 @@ private:
int32_t calculateMinSplitSize() const;
void useFasterRequest(const SharedHandle<Request>& fasterRequest);
protected:
public:
RequestGroup* getRequestGroup() const
{
return requestGroup_;
@ -151,9 +151,6 @@ protected:
(std::vector<std::string>& addrs, const std::string& hostname, uint16_t port);
void tryReserved();
virtual bool prepareForRetry(time_t wait);
virtual void onAbort();
virtual bool executeInternal() = 0;
void setReadCheckSocket(const SharedHandle<SocketCore>& socket);
void setWriteCheckSocket(const SharedHandle<SocketCore>& socket);
@ -171,6 +168,10 @@ protected:
*/
void setWriteCheckSocketIf(const SharedHandle<SocketCore>& socket, bool pred);
// Swaps socket_ with socket. This disables current read and write
// check.
void swapSocket(SharedHandle<SocketCore>& socket);
time_t getTimeout() const
{
return timeout_;
@ -221,12 +222,17 @@ protected:
void checkSocketRecvBuffer();
protected:
virtual bool prepareForRetry(time_t wait);
virtual void onAbort();
virtual bool executeInternal() = 0;
// Returns true if the derived class wants to execute
// executeInternal() unconditionally
virtual bool noCheck()
{
return false;
}
public:
AbstractCommand
(cuid_t cuid, const SharedHandle<Request>& req,

View File

@ -44,7 +44,6 @@
#include "SocketCore.h"
#include "DownloadContext.h"
#include "SocketRecvBuffer.h"
#include "BackupIPv4ConnectCommand.h"
#include "fmt.h"
#include "LogFactory.h"
@ -70,48 +69,11 @@ AbstractProxyRequestCommand::AbstractProxyRequestCommand
setWriteCheckSocket(getSocket());
}
AbstractProxyRequestCommand::~AbstractProxyRequestCommand()
{
if(backupConnectionInfo_) {
backupConnectionInfo_->cancel = true;
}
}
bool AbstractProxyRequestCommand::noCheck() {
return backupConnectionInfo_ && !backupConnectionInfo_->ipaddr.empty();
}
AbstractProxyRequestCommand::~AbstractProxyRequestCommand() {}
bool AbstractProxyRequestCommand::executeInternal() {
//socket->setBlockingMode();
if(httpConnection_->sendBufferIsEmpty()) {
if(backupConnectionInfo_ && !backupConnectionInfo_->ipaddr.empty()) {
A2_LOG_INFO(fmt("CUID#%"PRId64" - Use backup connection address %s",
getCuid(), backupConnectionInfo_->ipaddr.c_str()));
getDownloadEngine()->markBadIPAddress
(getRequest()->getConnectedHostname(),
getRequest()->getConnectedAddr(),
getRequest()->getConnectedPort());
getRequest()->setConnectedAddrInfo(getRequest()->getConnectedHostname(),
backupConnectionInfo_->ipaddr,
getRequest()->getConnectedPort());
Command* c = createSelf(backupConnectionInfo_->socket);
c->setStatus(STATUS_ONESHOT_REALTIME);
getDownloadEngine()->setNoWait(true);
getDownloadEngine()->addCommand(c);
backupConnectionInfo_.reset();
return true;
}
if(!checkIfConnectionEstablished
(getSocket(), getRequest()->getConnectedHostname(),
getRequest()->getConnectedAddr(), getRequest()->getConnectedPort())) {
return true;
}
if(backupConnectionInfo_) {
backupConnectionInfo_->cancel = true;
backupConnectionInfo_.reset();
}
SharedHandle<HttpRequest> httpRequest(new HttpRequest());
httpRequest->setUserAgent(getOption()->get(PREF_USER_AGENT));
httpRequest->setRequest(getRequest());
@ -131,10 +93,4 @@ bool AbstractProxyRequestCommand::executeInternal() {
}
}
void AbstractProxyRequestCommand::setBackupConnectInfo
(const SharedHandle<BackupConnectInfo>& info)
{
backupConnectionInfo_ = info;
}
} // namespace aria2

View File

@ -41,18 +41,14 @@ namespace aria2 {
class HttpConnection;
class SocketCore;
class BackupConnectInfo;
class AbstractProxyRequestCommand : public AbstractCommand {
private:
SharedHandle<Request> proxyRequest_;
SharedHandle<HttpConnection> httpConnection_;
SharedHandle<BackupConnectInfo> backupConnectionInfo_;
protected:
virtual bool executeInternal();
virtual bool noCheck();
const SharedHandle<HttpConnection>& getHttpConnection() const
{
@ -75,8 +71,6 @@ public:
virtual ~AbstractProxyRequestCommand();
virtual Command* getNextCommand() = 0;
virtual Command* createSelf(const SharedHandle<SocketCore>& socket) = 0;
void setBackupConnectInfo(const SharedHandle<BackupConnectInfo>& info);
};
} // namespace aria2

123
src/ConnectCommand.cc Normal file
View File

@ -0,0 +1,123 @@
/* <!-- copyright */
/*
* aria2 - The high speed download utility
*
* Copyright (C) 2013 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
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
*
* 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.
*/
/* copyright --> */
#include "ConnectCommand.h"
#include "BackupIPv4ConnectCommand.h"
#include "ControlChain.h"
#include "Option.h"
#include "message.h"
#include "SocketCore.h"
#include "fmt.h"
#include "LogFactory.h"
#include "DownloadEngine.h"
#include "Request.h"
#include "prefs.h"
#include "SocketRecvBuffer.h"
namespace aria2 {
ConnectCommand::ConnectCommand(cuid_t cuid,
const SharedHandle<Request>& req,
const SharedHandle<Request>& proxyRequest,
const SharedHandle<FileEntry>& fileEntry,
RequestGroup* requestGroup,
DownloadEngine* e,
const SharedHandle<SocketCore>& s)
: AbstractCommand(cuid, req, fileEntry, requestGroup, e, s),
proxyRequest_(proxyRequest)
{
setTimeout(getOption()->getAsInt(PREF_CONNECT_TIMEOUT));
disableReadCheckSocket();
setWriteCheckSocket(getSocket());
}
ConnectCommand::~ConnectCommand()
{
if(backupConnectionInfo_) {
backupConnectionInfo_->cancel = true;
}
}
void ConnectCommand::setControlChain
(const SharedHandle<ControlChain<ConnectCommand*> >& chain)
{
chain_ = chain;
}
void ConnectCommand::setBackupConnectInfo
(const SharedHandle<BackupConnectInfo>& info)
{
backupConnectionInfo_ = info;
}
const SharedHandle<Request>& ConnectCommand::getProxyRequest() const
{
return proxyRequest_;
}
bool ConnectCommand::executeInternal()
{
if(backupConnectionInfo_ && !backupConnectionInfo_->ipaddr.empty()) {
A2_LOG_INFO(fmt("CUID#%"PRId64" - Use backup connection address %s",
getCuid(), backupConnectionInfo_->ipaddr.c_str()));
getDownloadEngine()->markBadIPAddress
(getRequest()->getConnectedHostname(),
getRequest()->getConnectedAddr(),
getRequest()->getConnectedPort());
getRequest()->setConnectedAddrInfo(getRequest()->getConnectedHostname(),
backupConnectionInfo_->ipaddr,
getRequest()->getConnectedPort());
swapSocket(backupConnectionInfo_->socket);
backupConnectionInfo_.reset();
}
if(!checkIfConnectionEstablished
(getSocket(), getRequest()->getConnectedHostname(),
getRequest()->getConnectedAddr(), getRequest()->getConnectedPort())) {
return true;
}
if(backupConnectionInfo_) {
backupConnectionInfo_->cancel = true;
backupConnectionInfo_.reset();
}
chain_->run(this, getDownloadEngine());
return true;
}
bool ConnectCommand::noCheck()
{
return backupConnectionInfo_ && !backupConnectionInfo_->ipaddr.empty();
}
} // namespace aria2

70
src/ConnectCommand.h Normal file
View File

@ -0,0 +1,70 @@
/* <!-- copyright */
/*
* aria2 - The high speed download utility
*
* Copyright (C) 2013 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
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
*
* 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.
*/
/* copyright --> */
#ifndef CONNECT_COMMAND_H
#define CONNECT_COMMAND_H
#include "AbstractCommand.h"
#include "ControlChain.h"
namespace aria2 {
class BackupConnectInfo;
class ConnectCommand : public AbstractCommand {
public:
ConnectCommand(cuid_t cuid,
const SharedHandle<Request>& req,
const SharedHandle<Request>& proxyRequest,
const SharedHandle<FileEntry>& fileEntry,
RequestGroup* requestGroup,
DownloadEngine* e,
const SharedHandle<SocketCore>& s);
virtual ~ConnectCommand();
void setControlChain
(const SharedHandle<ControlChain<ConnectCommand*> >& chain);
void setBackupConnectInfo(const SharedHandle<BackupConnectInfo>& info);
const SharedHandle<Request>& getProxyRequest() const;
protected:
virtual bool executeInternal();
virtual bool noCheck();
private:
SharedHandle<Request> proxyRequest_;
SharedHandle<BackupConnectInfo> backupConnectionInfo_;
SharedHandle<ControlChain<ConnectCommand*> > chain_;
};
} // namespace aria2
#endif // CONNECT_COMMAND_H

52
src/ControlChain.h Normal file
View File

@ -0,0 +1,52 @@
/* <!-- copyright */
/*
* aria2 - The high speed download utility
*
* Copyright (C) 2013 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
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
*
* 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.
*/
/* copyright --> */
#ifndef CONTROL_CHAIN_H
#define CONTROL_CHAIN_H
namespace aria2 {
// This class template is used to transit to the next state using
// T. The main application is embed to the Command classes and get
// next Command from the previous Command.
template<typename T>
struct ControlChain {
virtual ~ControlChain() {}
// Currently, the implementation must returns 0.
virtual int run(T t, DownloadEngine* e) = 0;
};
} // namespace aria2
#endif // CONTROL_CHAIN_H

View File

@ -57,6 +57,9 @@
#include "fmt.h"
#include "SocketRecvBuffer.h"
#include "BackupIPv4ConnectCommand.h"
#include "FtpNegotiationConnectChain.h"
#include "FtpTunnelRequestConnectChain.h"
#include "HttpRequestConnectChain.h"
namespace aria2 {
@ -98,50 +101,43 @@ Command* FtpInitiateConnectionCommand::createNextCommand
getSocket()->establishConnection(addr, port);
getRequest()->setConnectedAddrInfo(hostname, addr, port);
ConnectCommand* c = new ConnectCommand(getCuid(),
getRequest(),
proxyRequest,
getFileEntry(),
getRequestGroup(),
getDownloadEngine(),
getSocket());
if(proxyMethod == V_GET) {
// Use GET for FTP via HTTP proxy.
getRequest()->setMethod(Request::METHOD_GET);
SharedHandle<SocketRecvBuffer> socketRecvBuffer
(new SocketRecvBuffer(getSocket()));
SharedHandle<HttpConnection> hc
(new HttpConnection(getCuid(), getSocket(), socketRecvBuffer));
HttpRequestCommand* c =
new HttpRequestCommand(getCuid(), getRequest(), getFileEntry(),
getRequestGroup(), hc, getDownloadEngine(),
getSocket());
c->setProxyRequest(proxyRequest);
SharedHandle<BackupConnectInfo> backupConnectInfo
= createBackupIPv4ConnectCommand(hostname, addr, port, c);
if(backupConnectInfo) {
c->setBackupConnectInfo(backupConnectInfo);
}
command = c;
SharedHandle<HttpRequestConnectChain> chain
(new HttpRequestConnectChain());
c->setControlChain(chain);
} else if(proxyMethod == V_TUNNEL) {
FtpTunnelRequestCommand* c =
new FtpTunnelRequestCommand(getCuid(), getRequest(), getFileEntry(),
getRequestGroup(), getDownloadEngine(),
proxyRequest, getSocket());
SharedHandle<BackupConnectInfo> backupConnectInfo
= createBackupIPv4ConnectCommand(hostname, addr, port, c);
if(backupConnectInfo) {
c->setBackupConnectInfo(backupConnectInfo);
}
command = c;
SharedHandle<FtpTunnelRequestConnectChain> chain
(new FtpTunnelRequestConnectChain());
c->setControlChain(chain);
} else {
// TODO
throw DL_ABORT_EX("ERROR");
// Unreachable
assert(0);
}
setupBackupConnection(hostname, addr, port, c);
command = c;
} else {
setConnectedAddrInfo(getRequest(), hostname, pooledSocket);
if(proxyMethod == V_TUNNEL) {
// options contains "baseWorkingDir"
command =
new FtpNegotiationCommand(getCuid(), getRequest(), getFileEntry(),
getRequestGroup(), getDownloadEngine(),
pooledSocket,
FtpNegotiationCommand::SEQ_SEND_CWD_PREP,
options);
command = new FtpNegotiationCommand
(getCuid(),
getRequest(),
getFileEntry(),
getRequestGroup(),
getDownloadEngine(),
pooledSocket,
FtpNegotiationCommand::SEQ_SEND_CWD_PREP,
options);
} else if(proxyMethod == V_GET) {
// Use GET for FTP via HTTP proxy.
getRequest()->setMethod(Request::METHOD_GET);
@ -150,15 +146,18 @@ Command* FtpInitiateConnectionCommand::createNextCommand
SharedHandle<HttpConnection> hc
(new HttpConnection(getCuid(), pooledSocket, socketRecvBuffer));
HttpRequestCommand* c =
new HttpRequestCommand(getCuid(), getRequest(), getFileEntry(),
getRequestGroup(), hc, getDownloadEngine(),
pooledSocket);
HttpRequestCommand* c = new HttpRequestCommand(getCuid(),
getRequest(),
getFileEntry(),
getRequestGroup(),
hc,
getDownloadEngine(),
pooledSocket);
c->setProxyRequest(proxyRequest);
command = c;
} else {
// TODO
throw DL_ABORT_EX("ERROR");
// Unreachable
assert(0);
}
}
} else {
@ -175,24 +174,29 @@ Command* FtpInitiateConnectionCommand::createNextCommand
createSocket();
getSocket()->establishConnection(addr, port);
getRequest()->setConnectedAddrInfo(hostname, addr, port);
FtpNegotiationCommand* c =
new FtpNegotiationCommand(getCuid(), getRequest(), getFileEntry(),
getRequestGroup(), getDownloadEngine(),
getSocket());
SharedHandle<BackupConnectInfo> backupConnectInfo
= createBackupIPv4ConnectCommand(hostname, addr, port, c);
if(backupConnectInfo) {
c->setBackupConnectInfo(backupConnectInfo);
}
ConnectCommand* c = new ConnectCommand(getCuid(),
getRequest(),
proxyRequest, // must be null
getFileEntry(),
getRequestGroup(),
getDownloadEngine(),
getSocket());
SharedHandle<FtpNegotiationConnectChain> chain
(new FtpNegotiationConnectChain());
c->setControlChain(chain);
setupBackupConnection(hostname, addr, port, c);
command = c;
} else {
// options contains "baseWorkingDir"
command =
new FtpNegotiationCommand(getCuid(), getRequest(), getFileEntry(),
getRequestGroup(), getDownloadEngine(),
pooledSocket,
FtpNegotiationCommand::SEQ_SEND_CWD_PREP,
options);
command = new FtpNegotiationCommand
(getCuid(),
getRequest(),
getFileEntry(),
getRequestGroup(),
getDownloadEngine(),
pooledSocket,
FtpNegotiationCommand::SEQ_SEND_CWD_PREP,
options);
setConnectedAddrInfo(getRequest(), hostname, pooledSocket);
}
}

View File

@ -74,7 +74,6 @@
#include "CheckIntegrityEntry.h"
#include "error_code.h"
#include "SocketRecvBuffer.h"
#include "BackupIPv4ConnectCommand.h"
#ifdef ENABLE_MESSAGE_DIGEST
# include "ChecksumCheckIntegrityEntry.h"
#endif // ENABLE_MESSAGE_DIGEST
@ -102,20 +101,9 @@ FtpNegotiationCommand::FtpNegotiationCommand
if(seq == SEQ_RECV_GREETING) {
setTimeout(getOption()->getAsInt(PREF_CONNECT_TIMEOUT));
}
disableReadCheckSocket();
setWriteCheckSocket(getSocket());
}
FtpNegotiationCommand::~FtpNegotiationCommand()
{
if(backupConnectionInfo_) {
backupConnectionInfo_->cancel = true;
}
}
bool FtpNegotiationCommand::noCheck() {
return backupConnectionInfo_ && !backupConnectionInfo_->ipaddr.empty();
}
FtpNegotiationCommand::~FtpNegotiationCommand() {}
bool FtpNegotiationCommand::executeInternal() {
while(processSequence(getSegments().front()));
@ -155,38 +143,6 @@ bool FtpNegotiationCommand::executeInternal() {
}
bool FtpNegotiationCommand::recvGreeting() {
if(backupConnectionInfo_ && !backupConnectionInfo_->ipaddr.empty()) {
A2_LOG_INFO(fmt("CUID#%"PRId64" - Use backup connection address %s",
getCuid(), backupConnectionInfo_->ipaddr.c_str()));
getDownloadEngine()->markBadIPAddress
(getRequest()->getConnectedHostname(),
getRequest()->getConnectedAddr(),
getRequest()->getConnectedPort());
getRequest()->setConnectedAddrInfo(getRequest()->getConnectedHostname(),
backupConnectionInfo_->ipaddr,
getRequest()->getConnectedPort());
FtpNegotiationCommand* c =
new FtpNegotiationCommand(getCuid(), getRequest(), getFileEntry(),
getRequestGroup(), getDownloadEngine(),
backupConnectionInfo_->socket);
c->setStatus(STATUS_ONESHOT_REALTIME);
getDownloadEngine()->setNoWait(true);
getDownloadEngine()->addCommand(c);
backupConnectionInfo_.reset();
return true;
}
if(!checkIfConnectionEstablished
(getSocket(), getRequest()->getConnectedHostname(),
getRequest()->getConnectedAddr(), getRequest()->getConnectedPort())) {
sequence_ = SEQ_EXIT;
return false;
}
if(backupConnectionInfo_) {
backupConnectionInfo_->cancel = true;
backupConnectionInfo_.reset();
}
setTimeout(getRequestGroup()->getTimeout());
//socket->setBlockingMode();
disableWriteCheckSocket();
@ -1009,10 +965,4 @@ void FtpNegotiationCommand::onDryRunFileFound()
sequence_ = SEQ_HEAD_OK;
}
void FtpNegotiationCommand::setBackupConnectInfo
(const SharedHandle<BackupConnectInfo>& info)
{
backupConnectionInfo_ = info;
}
} // namespace aria2

View File

@ -44,7 +44,6 @@ namespace aria2 {
class FtpConnection;
class SocketCore;
class HttpConnection;
class BackupConnectInfo;
class FtpNegotiationCommand : public AbstractCommand {
public:
@ -154,11 +153,8 @@ private:
std::string proxyAddr_;
std::deque<std::string> cwdDirs_;
SharedHandle<BackupConnectInfo> backupConnectionInfo_;
protected:
virtual bool executeInternal();
virtual bool noCheck();
public:
FtpNegotiationCommand(cuid_t cuid,
const SharedHandle<Request>& req,
@ -169,7 +165,6 @@ public:
Seq seq = SEQ_RECV_GREETING,
const std::string& baseWorkingDir = "/");
virtual ~FtpNegotiationCommand();
void setBackupConnectInfo(const SharedHandle<BackupConnectInfo>& info);
};
} // namespace aria2

View File

@ -0,0 +1,66 @@
/* <!-- copyright */
/*
* aria2 - The high speed download utility
*
* Copyright (C) 2013 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
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
*
* 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.
*/
/* copyright --> */
#ifndef FTP_NEGOTIATION_CONNECT_CHAIN_H
#define FTP_NEGOTIATION_CONNECT_CHAIN_H
#include "ControlChain.h"
#include "ConnectCommand.h"
#include "DownloadEngine.h"
#include "FtpNegotiationCommand.h"
namespace aria2 {
struct FtpNegotiationConnectChain : public ControlChain<ConnectCommand*> {
FtpNegotiationConnectChain() {}
virtual ~FtpNegotiationConnectChain() {}
virtual int run(ConnectCommand* t, DownloadEngine* e)
{
FtpNegotiationCommand* c = new FtpNegotiationCommand
(t->getCuid(),
t->getRequest(),
t->getFileEntry(),
t->getRequestGroup(),
t->getDownloadEngine(),
t->getSocket());
c->setStatus(Command::STATUS_ONESHOT_REALTIME);
e->setNoWait(true);
e->addCommand(c);
return 0;
}
};
} // namespace aria2
#endif // FTP_NEGOTIATION_CONNECT_CHAIN_H

View File

@ -63,12 +63,4 @@ Command* FtpTunnelRequestCommand::getNextCommand()
getHttpConnection(), getDownloadEngine(), getSocket());
}
Command* FtpTunnelRequestCommand::createSelf
(const SharedHandle<SocketCore>& socket)
{
return new FtpTunnelRequestCommand(getCuid(), getRequest(), getFileEntry(),
getRequestGroup(), getDownloadEngine(),
getProxyRequest(), socket);
}
} // namespace aria2

View File

@ -53,8 +53,6 @@ public:
virtual ~FtpTunnelRequestCommand();
virtual Command* getNextCommand();
virtual Command* createSelf(const SharedHandle<SocketCore>& socket);
};
} // namespace aria2

View File

@ -0,0 +1,67 @@
/* <!-- copyright */
/*
* aria2 - The high speed download utility
*
* Copyright (C) 2013 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
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
*
* 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.
*/
/* copyright --> */
#ifndef FTP_TUNNEL_REQUEST_CONNECT_CHAIN_H
#define FTP_TUNNEL_REQUEST_CONNECT_CHAIN_H
#include "ControlChain.h"
#include "ConnectCommand.h"
#include "DownloadEngine.h"
#include "FtpTunnelRequestCommand.h"
namespace aria2 {
struct FtpTunnelRequestConnectChain : public ControlChain<ConnectCommand*> {
FtpTunnelRequestConnectChain() {}
virtual ~FtpTunnelRequestConnectChain() {}
virtual int run(ConnectCommand* t, DownloadEngine* e)
{
FtpTunnelRequestCommand* c = new FtpTunnelRequestCommand
(t->getCuid(),
t->getRequest(),
t->getFileEntry(),
t->getRequestGroup(),
t->getDownloadEngine(),
t->getProxyRequest(),
t->getSocket());
c->setStatus(Command::STATUS_ONESHOT_REALTIME);
e->setNoWait(true);
e->addCommand(c);
return 0;
}
};
} // namespace aria2
#endif // FTP_TUNNEL_REQUEST_CONNECT_CHAIN_H

View File

@ -52,6 +52,9 @@
#include "fmt.h"
#include "SocketRecvBuffer.h"
#include "BackupIPv4ConnectCommand.h"
#include "ConnectCommand.h"
#include "HttpRequestConnectChain.h"
#include "HttpProxyRequestConnectChain.h"
namespace aria2 {
@ -85,44 +88,27 @@ Command* HttpInitiateConnectionCommand::createNextCommand
getSocket()->establishConnection(addr, port);
getRequest()->setConnectedAddrInfo(hostname, addr, port);
ConnectCommand* c = new ConnectCommand(getCuid(),
getRequest(),
proxyRequest,
getFileEntry(),
getRequestGroup(),
getDownloadEngine(),
getSocket());
if(proxyMethod == V_TUNNEL) {
HttpProxyRequestCommand* c =
new HttpProxyRequestCommand(getCuid(),
getRequest(),
getFileEntry(),
getRequestGroup(),
getDownloadEngine(),
proxyRequest,
getSocket());
SharedHandle<BackupConnectInfo> backupConnectionInfo
= createBackupIPv4ConnectCommand(hostname, addr, port, c);
if(backupConnectionInfo) {
c->setBackupConnectInfo(backupConnectionInfo);
}
command = c;
SharedHandle<HttpProxyRequestConnectChain> chain
(new HttpProxyRequestConnectChain());
c->setControlChain(chain);
} else if(proxyMethod == V_GET) {
SharedHandle<SocketRecvBuffer> socketRecvBuffer
(new SocketRecvBuffer(getSocket()));
SharedHandle<HttpConnection> httpConnection
(new HttpConnection(getCuid(), getSocket(), socketRecvBuffer));
HttpRequestCommand* c = new HttpRequestCommand(getCuid(),
getRequest(),
getFileEntry(),
getRequestGroup(),
httpConnection,
getDownloadEngine(),
getSocket());
c->setProxyRequest(proxyRequest);
SharedHandle<BackupConnectInfo> backupConnectionInfo
= createBackupIPv4ConnectCommand(hostname, addr, port, c);
if(backupConnectionInfo) {
c->setBackupConnectInfo(backupConnectionInfo);
}
command = c;
SharedHandle<HttpRequestConnectChain> chain
(new HttpRequestConnectChain());
c->setControlChain(chain);
} else {
// TODO
throw DL_ABORT_EX("ERROR");
// Unreachable
assert(0);
}
setupBackupConnection(hostname, addr, port, c);
command = c;
} else {
setConnectedAddrInfo(getRequest(), hostname, pooledSocket);
SharedHandle<SocketRecvBuffer> socketRecvBuffer
@ -142,7 +128,6 @@ Command* HttpInitiateConnectionCommand::createNextCommand
command = c;
}
} else {
bool connectRequired = false;
SharedHandle<SocketCore> pooledSocket =
getDownloadEngine()->popPooledSocket
(resolvedAddresses, getRequest()->getPort());
@ -153,30 +138,34 @@ Command* HttpInitiateConnectionCommand::createNextCommand
getSocket()->establishConnection(addr, port);
getRequest()->setConnectedAddrInfo(hostname, addr, port);
connectRequired = true;
ConnectCommand* c = new ConnectCommand(getCuid(),
getRequest(),
proxyRequest, // must be null
getFileEntry(),
getRequestGroup(),
getDownloadEngine(),
getSocket());
SharedHandle<HttpRequestConnectChain> chain
(new HttpRequestConnectChain());
c->setControlChain(chain);
setupBackupConnection(hostname, addr, port, c);
command = c;
} else {
setSocket(pooledSocket);
setConnectedAddrInfo(getRequest(), hostname, pooledSocket);
}
SharedHandle<SocketRecvBuffer> socketRecvBuffer
(new SocketRecvBuffer(getSocket()));
SharedHandle<HttpConnection> httpConnection
(new HttpConnection(getCuid(), getSocket(), socketRecvBuffer));
HttpRequestCommand* c =
new HttpRequestCommand(getCuid(), getRequest(), getFileEntry(),
getRequestGroup(),
httpConnection,
getDownloadEngine(),
getSocket());
if(connectRequired) {
SharedHandle<BackupConnectInfo> backupConnectInfo
= createBackupIPv4ConnectCommand(hostname, addr, port, c);
if(backupConnectInfo) {
c->setBackupConnectInfo(backupConnectInfo);
}
}
command = c;
SharedHandle<SocketRecvBuffer> socketRecvBuffer
(new SocketRecvBuffer(getSocket()));
SharedHandle<HttpConnection> httpConnection
(new HttpConnection(getCuid(), getSocket(), socketRecvBuffer));
command = new HttpRequestCommand(getCuid(),
getRequest(),
getFileEntry(),
getRequestGroup(),
httpConnection,
getDownloadEngine(),
getSocket());
}
}
return command;
}

View File

@ -62,16 +62,4 @@ Command* HttpProxyRequestCommand::getNextCommand()
getHttpConnection(), getDownloadEngine(), getSocket());
}
Command* HttpProxyRequestCommand::createSelf
(const SharedHandle<SocketCore>& socket)
{
return new HttpProxyRequestCommand(getCuid(),
getRequest(),
getFileEntry(),
getRequestGroup(),
getDownloadEngine(),
getProxyRequest(),
socket);
}
} // namespace aria2

View File

@ -53,8 +53,6 @@ public:
virtual ~HttpProxyRequestCommand();
virtual Command* getNextCommand();
virtual Command* createSelf(const SharedHandle<SocketCore>& socket);
};
} // namespace aria2

View File

@ -0,0 +1,67 @@
/* <!-- copyright */
/*
* aria2 - The high speed download utility
*
* Copyright (C) 2013 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
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
*
* 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.
*/
/* copyright --> */
#ifndef HTTP_PROXY_REQUEST_CONNECT_CHAIN_H
#define HTTP_PROXY_REQUEST_CONNECT_CHAIN_H
#include "ControlChain.h"
#include "ConnectCommand.h"
#include "DownloadEngine.h"
#include "HttpProxyRequestCommand.h"
namespace aria2 {
struct HttpProxyRequestConnectChain : public ControlChain<ConnectCommand*> {
HttpProxyRequestConnectChain() {}
virtual ~HttpProxyRequestConnectChain() {}
virtual int run(ConnectCommand* t, DownloadEngine* e)
{
HttpProxyRequestCommand* c = new HttpProxyRequestCommand
(t->getCuid(),
t->getRequest(),
t->getFileEntry(),
t->getRequestGroup(),
e,
t->getProxyRequest(),
t->getSocket());
c->setStatus(Command::STATUS_ONESHOT_REALTIME);
e->setNoWait(true);
e->addCommand(c);
return 0;
}
};
} // namespace aria2
#endif // HTTP_PROXY_REQUEST_CONNECT_CHAIN_H

View File

@ -59,7 +59,6 @@
#include "LogFactory.h"
#include "fmt.h"
#include "SocketRecvBuffer.h"
#include "BackupIPv4ConnectCommand.h"
namespace aria2 {
@ -80,12 +79,7 @@ HttpRequestCommand::HttpRequestCommand
setWriteCheckSocket(getSocket());
}
HttpRequestCommand::~HttpRequestCommand()
{
if(backupConnectionInfo_) {
backupConnectionInfo_->cancel = true;
}
}
HttpRequestCommand::~HttpRequestCommand() {}
namespace {
SharedHandle<HttpRequest>
@ -128,52 +122,9 @@ createHttpRequest(const SharedHandle<Request>& req,
}
} // namespace
bool HttpRequestCommand::noCheck() {
return backupConnectionInfo_ && !backupConnectionInfo_->ipaddr.empty();
}
bool HttpRequestCommand::executeInternal() {
//socket->setBlockingMode();
if(httpConnection_->sendBufferIsEmpty()) {
if(backupConnectionInfo_ && !backupConnectionInfo_->ipaddr.empty()) {
A2_LOG_INFO(fmt("CUID#%"PRId64" - Use backup connection address %s",
getCuid(), backupConnectionInfo_->ipaddr.c_str()));
getDownloadEngine()->markBadIPAddress
(getRequest()->getConnectedHostname(),
getRequest()->getConnectedAddr(),
getRequest()->getConnectedPort());
getRequest()->setConnectedAddrInfo(getRequest()->getConnectedHostname(),
backupConnectionInfo_->ipaddr,
getRequest()->getConnectedPort());
SharedHandle<SocketRecvBuffer> socketRecvBuffer
(new SocketRecvBuffer(backupConnectionInfo_->socket));
SharedHandle<HttpConnection> httpConnection
(new HttpConnection(getCuid(), backupConnectionInfo_->socket,
socketRecvBuffer));
HttpRequestCommand* c =
new HttpRequestCommand(getCuid(), getRequest(), getFileEntry(),
getRequestGroup(),
httpConnection,
getDownloadEngine(),
backupConnectionInfo_->socket);
c->setProxyRequest(proxyRequest_);
c->setStatus(STATUS_ONESHOT_REALTIME);
getDownloadEngine()->setNoWait(true);
getDownloadEngine()->addCommand(c);
backupConnectionInfo_.reset();
return true;
}
if(!checkIfConnectionEstablished
(getSocket(), getRequest()->getConnectedHostname(),
getRequest()->getConnectedAddr(), getRequest()->getConnectedPort())) {
return true;
}
if(backupConnectionInfo_) {
backupConnectionInfo_->cancel = true;
backupConnectionInfo_.reset();
}
#ifdef ENABLE_SSL
if(getRequest()->getProtocol() == "https") {
if(!getSocket()->tlsConnect(getRequest()->getHost())) {
@ -279,10 +230,4 @@ void HttpRequestCommand::setProxyRequest
proxyRequest_ = proxyRequest;
}
void HttpRequestCommand::setBackupConnectInfo
(const SharedHandle<BackupConnectInfo>& info)
{
backupConnectionInfo_ = info;
}
} // namespace aria2

View File

@ -41,7 +41,6 @@ namespace aria2 {
class HttpConnection;
class SocketCore;
class BackupConnectInfo;
// HttpRequestCommand sends HTTP request header to remote server.
// Because network I/O is non-blocking, execute() returns false if all
@ -54,11 +53,8 @@ private:
SharedHandle<Request> proxyRequest_;
SharedHandle<HttpConnection> httpConnection_;
SharedHandle<BackupConnectInfo> backupConnectionInfo_;
protected:
virtual bool executeInternal();
virtual bool noCheck();
public:
HttpRequestCommand(cuid_t cuid,
const SharedHandle<Request>& req,
@ -70,7 +66,6 @@ public:
virtual ~HttpRequestCommand();
void setProxyRequest(const SharedHandle<Request>& proxyRequest);
void setBackupConnectInfo(const SharedHandle<BackupConnectInfo>& info);
};
} // namespace aria2

View File

@ -0,0 +1,73 @@
/* <!-- copyright */
/*
* aria2 - The high speed download utility
*
* Copyright (C) 2013 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
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
*
* 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.
*/
/* copyright --> */
#ifndef HTTP_REQUEST_CONNECT_CHAIN_H
#define HTTP_REQUEST_CONNECT_CHAIN_H
#include "ControlChain.h"
#include "ConnectCommand.h"
#include "DownloadEngine.h"
#include "SocketRecvBuffer.h"
#include "HttpConnection.h"
#include "HttpRequestCommand.h"
namespace aria2 {
struct HttpRequestConnectChain : public ControlChain<ConnectCommand*> {
HttpRequestConnectChain() {}
virtual ~HttpRequestConnectChain() {}
virtual int run(ConnectCommand* t, DownloadEngine* e)
{
SharedHandle<SocketRecvBuffer> socketRecvBuffer
(new SocketRecvBuffer(t->getSocket()));
SharedHandle<HttpConnection> httpConnection
(new HttpConnection(t->getCuid(), t->getSocket(), socketRecvBuffer));
HttpRequestCommand* c = new HttpRequestCommand(t->getCuid(),
t->getRequest(),
t->getFileEntry(),
t->getRequestGroup(),
httpConnection,
e,
t->getSocket());
c->setProxyRequest(t->getProxyRequest());
c->setStatus(Command::STATUS_ONESHOT_REALTIME);
e->setNoWait(true);
e->addCommand(c);
return 0;
}
};
} // namespace aria2
#endif // HTTP_REQUEST_CONNECT_CHAIN_H

View File

@ -52,6 +52,7 @@
#include "fmt.h"
#include "SocketRecvBuffer.h"
#include "BackupIPv4ConnectCommand.h"
#include "ConnectCommand.h"
namespace aria2 {
@ -160,4 +161,15 @@ InitiateConnectionCommand::createBackupIPv4ConnectCommand
return info;
}
void InitiateConnectionCommand::setupBackupConnection
(const std::string& hostname, const std::string& addr, uint16_t port,
ConnectCommand* c)
{
SharedHandle<BackupConnectInfo> backupConnectInfo
= createBackupIPv4ConnectCommand(hostname, addr, port, c);
if(backupConnectInfo) {
c->setBackupConnectInfo(backupConnectInfo);
}
}
} // namespace aria2

View File

@ -40,6 +40,7 @@
namespace aria2 {
class BackupConnectInfo;
class ConnectCommand;
class InitiateConnectionCommand : public AbstractCommand {
protected:
@ -70,6 +71,10 @@ protected:
SharedHandle<BackupConnectInfo> createBackupIPv4ConnectCommand
(const std::string& hostname, const std::string& ipaddr, uint16_t port,
Command* mainCommand);
void setupBackupConnection
(const std::string& hostname, const std::string& addr, uint16_t port,
ConnectCommand* c);
public:
InitiateConnectionCommand(cuid_t cuid, const SharedHandle<Request>& req,
const SharedHandle<FileEntry>& fileEntry,

View File

@ -246,7 +246,12 @@ SRCS = SocketCore.cc SocketCore.h\
GroupId.cc GroupId.h\
IndexedList.h\
SaveSessionCommand.h SaveSessionCommand.cc\
BackupIPv4ConnectCommand.h BackupIPv4ConnectCommand.cc
BackupIPv4ConnectCommand.h BackupIPv4ConnectCommand.cc\
ConnectCommand.cc ConnectCommand.h\
HttpRequestConnectChain.h\
HttpProxyRequestConnectChain.h\
FtpNegotiationConnectChain.h\
FtpTunnelRequestConnectChain.h
if MINGW_BUILD
SRCS += WinConsoleFile.cc WinConsoleFile.h