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 "FtpNegotiationCommand.h"
|
2008-11-03 10:06:25 +00:00
|
|
|
|
|
|
|
#include <stdint.h>
|
|
|
|
#include <cassert>
|
|
|
|
#include <utility>
|
|
|
|
#include <map>
|
|
|
|
|
2008-02-08 15:53:45 +00:00
|
|
|
#include "Request.h"
|
2007-10-11 16:58:24 +00:00
|
|
|
#include "DownloadEngine.h"
|
|
|
|
#include "FtpConnection.h"
|
|
|
|
#include "RequestGroup.h"
|
|
|
|
#include "PieceStorage.h"
|
2006-02-21 12:28:42 +00:00
|
|
|
#include "FtpDownloadCommand.h"
|
2008-02-08 15:53:45 +00:00
|
|
|
#include "FileEntry.h"
|
2006-02-21 12:28:42 +00:00
|
|
|
#include "DlAbortEx.h"
|
|
|
|
#include "message.h"
|
2006-02-21 14:00:58 +00:00
|
|
|
#include "prefs.h"
|
2009-10-22 15:35:33 +00:00
|
|
|
#include "util.h"
|
2008-02-08 15:53:45 +00:00
|
|
|
#include "Option.h"
|
|
|
|
#include "Logger.h"
|
2010-11-20 08:21:36 +00:00
|
|
|
#include "LogFactory.h"
|
2008-02-08 15:53:45 +00:00
|
|
|
#include "Segment.h"
|
2009-06-28 10:37:15 +00:00
|
|
|
#include "DownloadContext.h"
|
2007-11-05 15:50:49 +00:00
|
|
|
#include "DefaultBtProgressInfoFile.h"
|
2007-11-13 11:46:58 +00:00
|
|
|
#include "RequestGroupMan.h"
|
|
|
|
#include "DownloadFailureException.h"
|
2008-02-08 15:53:45 +00:00
|
|
|
#include "Socket.h"
|
2010-11-20 08:21:36 +00:00
|
|
|
#include "fmt.h"
|
2008-06-24 14:31:23 +00:00
|
|
|
#include "DiskAdaptor.h"
|
|
|
|
#include "SegmentMan.h"
|
2008-11-03 10:06:25 +00:00
|
|
|
#include "AuthConfigFactory.h"
|
|
|
|
#include "AuthConfig.h"
|
2009-06-06 12:33:07 +00:00
|
|
|
#include "a2functional.h"
|
2009-06-29 08:42:58 +00:00
|
|
|
#include "URISelector.h"
|
2010-04-06 16:09:24 +00:00
|
|
|
#include "HttpConnection.h"
|
|
|
|
#include "HttpHeader.h"
|
|
|
|
#include "HttpRequest.h"
|
|
|
|
#include "HttpResponse.h"
|
|
|
|
#include "DlRetryEx.h"
|
2010-06-08 14:11:36 +00:00
|
|
|
#include "CheckIntegrityEntry.h"
|
2010-12-01 12:26:58 +00:00
|
|
|
#include "error_code.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
|
|
|
|
2009-06-23 15:35:45 +00:00
|
|
|
FtpNegotiationCommand::FtpNegotiationCommand
|
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,
|
|
|
|
RequestGroup* requestGroup,
|
|
|
|
DownloadEngine* e,
|
2010-06-09 12:43:44 +00:00
|
|
|
const SocketHandle& socket,
|
2009-06-23 15:35:45 +00:00
|
|
|
Seq seq,
|
|
|
|
const std::string& baseWorkingDir):
|
2011-11-14 14:19:37 +00:00
|
|
|
AbstractCommand(cuid, req, fileEntry, requestGroup, e, socket),
|
|
|
|
sequence_(seq),
|
2010-06-21 13:51:56 +00:00
|
|
|
ftp_(new FtpConnection(cuid, socket, req,
|
2010-01-05 16:01:46 +00:00
|
|
|
e->getAuthConfigFactory()->createAuthConfig
|
|
|
|
(req, requestGroup->getOption().get()),
|
2011-11-14 14:19:37 +00:00
|
|
|
getOption().get())),
|
|
|
|
pasvPort_(0)
|
2006-02-21 12:28:42 +00:00
|
|
|
{
|
2010-06-21 13:51:56 +00:00
|
|
|
ftp_->setBaseWorkingDir(baseWorkingDir);
|
2008-09-09 12:13:08 +00:00
|
|
|
if(seq == SEQ_RECV_GREETING) {
|
2009-05-08 16:41:21 +00:00
|
|
|
setTimeout(getOption()->getAsInt(PREF_CONNECT_TIMEOUT));
|
2008-09-09 12:13:08 +00:00
|
|
|
}
|
2006-07-19 17:07:45 +00:00
|
|
|
disableReadCheckSocket();
|
2010-06-09 12:43:44 +00:00
|
|
|
setWriteCheckSocket(getSocket());
|
2006-02-21 12:28:42 +00:00
|
|
|
}
|
|
|
|
|
2008-05-09 14:44:28 +00:00
|
|
|
FtpNegotiationCommand::~FtpNegotiationCommand() {}
|
2006-02-21 12:28:42 +00:00
|
|
|
|
2007-03-15 Tatsuhiro Tsujikawa <tujikawa at rednoah dot com>
To handle Segment as SegmentHandle:
* src/AbstractCommand.cc (execute): Rewritten.
* src/SegmentMan.h: Segment -> SegmentHandle
Introducded HttpResponse class, HttpRequest class to improve
code
extensiveness and make it clear:
* src/HttpDownloadCommand.cc: transfer encoders are now managed
by
HttpResponse class.
* src/HttpRequest.h, src/HttpRequest.cc: New class.
* src/HttpResponse.h, src/HttpResponse.cc: New class.
* src/HttpConnection.cc: Contruction of http request were moved
to
HttpRequest class.
* src/HttpResponseCommand.h, src/HttpResponseCommand.cc:
Refactored.
* src/HttpRequestCommand.cc (executeInternal): Rewritten.
* src/HttpAuthConfig.h: New class.
* src/Range.h: New class.
To make FtpTunnel{Request, Response}Command and
HttpProxy{Request, Response}Command derived from
AbstractProxy{Request, Response}Command:
* src/FtpTunnelResponseCommand.h,
src/FtpTunnelResponseCommand.cc:
Derived from AbstractProxyRequestCommand class.
* src/FtpTunnelRequestCommand.h, src/FtpTunnelRequestCommand.cc:
Derived from AbstractProxyResponseCommand class.
* src/HttpProxyRequestCommand.h, src/HttpProxyRequestCommand.cc:
Derived from AbstractProxyRequestCommand class.
* src/HttpProxyResponseCommand.h,
src/HttpProxyResponseCommand.cc:
Derived from AbstractProxyResponseCommand class.
* src/AbstractProxyRequestCommand.h,
src/AbstractProxyRequestCommand.cc
: New class.
* src/AbstractProxyResponseCommand.h,
src/AbstractProxyResponseCommand.cc: New class.
To add netrc support:
* src/Netrc.h, src/Netrc.cc: New class.
* src/Util.h, src/Util.cc (split): New function.
* src/HttpHeader.cc (getRange): Fixed so that it inspects
"Content-Range" header instead of "Range" header.
* src/HttpHeader.h
(getStatus): Removed.
(setStatus): Removed.
* src/Segment.h
(getPositionToWrite): New function.
2007-03-15 15:07:18 +00:00
|
|
|
bool FtpNegotiationCommand::executeInternal() {
|
2010-06-09 12:43:44 +00:00
|
|
|
while(processSequence(getSegments().front()));
|
2010-06-21 13:51:56 +00:00
|
|
|
if(sequence_ == SEQ_RETRY) {
|
2006-02-21 12:28:42 +00:00
|
|
|
return prepareForRetry(0);
|
2010-06-21 13:51:56 +00:00
|
|
|
} else if(sequence_ == SEQ_NEGOTIATION_COMPLETED) {
|
2006-07-19 17:07:45 +00:00
|
|
|
FtpDownloadCommand* command =
|
2009-06-23 15:35:45 +00:00
|
|
|
new FtpDownloadCommand
|
2010-06-21 13:51:56 +00:00
|
|
|
(getCuid(), getRequest(), getFileEntry(), getRequestGroup(), ftp_,
|
|
|
|
getDownloadEngine(), dataSocket_, getSocket());
|
2009-05-08 16:41:21 +00:00
|
|
|
command->setStartupIdleTime(getOption()->getAsInt(PREF_STARTUP_IDLE_TIME));
|
2010-06-08 15:02:20 +00:00
|
|
|
command->setLowestDownloadSpeedLimit
|
|
|
|
(getOption()->getAsInt(PREF_LOWEST_SPEED_LIMIT));
|
2010-07-14 11:39:05 +00:00
|
|
|
if(getFileEntry()->isUniqueProtocol()) {
|
2010-06-09 12:43:44 +00:00
|
|
|
getFileEntry()->removeURIWhoseHostnameIs(getRequest()->getHost());
|
2007-12-12 13:53:33 +00:00
|
|
|
}
|
2010-06-09 12:43:44 +00:00
|
|
|
getRequestGroup()->getURISelector()->tuneDownloadCommand
|
|
|
|
(getFileEntry()->getRemainingUris(), command);
|
|
|
|
getDownloadEngine()->addCommand(command);
|
2006-02-21 12:28:42 +00:00
|
|
|
return true;
|
2010-06-21 13:51:56 +00:00
|
|
|
} else if(sequence_ == SEQ_HEAD_OK ||
|
|
|
|
sequence_ == SEQ_DOWNLOAD_ALREADY_COMPLETED) {
|
2007-01-29 16:46:48 +00:00
|
|
|
return true;
|
2010-06-21 13:51:56 +00:00
|
|
|
} else if(sequence_ == SEQ_FILE_PREPARATION) {
|
2009-05-08 16:41:21 +00:00
|
|
|
if(getOption()->getAsBool(PREF_FTP_PASV)) {
|
2010-07-30 14:45:35 +00:00
|
|
|
sequence_ = SEQ_PREPARE_PASV;
|
2007-12-01 04:56:10 +00:00
|
|
|
} else {
|
2010-07-30 14:45:35 +00:00
|
|
|
sequence_ = SEQ_PREPARE_PORT;
|
2007-12-01 04:56:10 +00:00
|
|
|
}
|
|
|
|
return false;
|
2010-06-21 13:51:56 +00:00
|
|
|
} else if(sequence_ == SEQ_EXIT) {
|
2009-07-02 15:18:13 +00:00
|
|
|
return true;
|
2006-02-21 12:28:42 +00:00
|
|
|
} else {
|
2010-06-09 12:43:44 +00:00
|
|
|
getDownloadEngine()->addCommand(this);
|
2006-02-21 12:28:42 +00:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
bool FtpNegotiationCommand::recvGreeting() {
|
2009-07-02 15:18:13 +00:00
|
|
|
if(!checkIfConnectionEstablished
|
2010-09-09 12:00:42 +00:00
|
|
|
(getSocket(), getRequest()->getConnectedHostname(),
|
|
|
|
getRequest()->getConnectedAddr(), getRequest()->getConnectedPort())) {
|
2010-06-21 13:51:56 +00:00
|
|
|
sequence_ = SEQ_EXIT;
|
2009-07-02 15:18:13 +00:00
|
|
|
return false;
|
|
|
|
}
|
2010-06-09 12:43:44 +00:00
|
|
|
setTimeout(getRequestGroup()->getTimeout());
|
2008-09-13 16:32:47 +00:00
|
|
|
//socket->setBlockingMode();
|
2006-08-07 16:05:00 +00:00
|
|
|
disableWriteCheckSocket();
|
2010-06-09 12:43:44 +00:00
|
|
|
setReadCheckSocket(getSocket());
|
2006-08-07 16:05:00 +00:00
|
|
|
|
2011-12-08 12:38:00 +00:00
|
|
|
int status = ftp_->receiveResponse();
|
2006-02-21 12:28:42 +00:00
|
|
|
if(status == 0) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
if(status != 220) {
|
2010-12-01 12:26:58 +00:00
|
|
|
throw DL_ABORT_EX2(EX_CONNECTION_FAILED, error_code::FTP_PROTOCOL_ERROR);
|
2006-02-21 12:28:42 +00:00
|
|
|
}
|
2010-06-21 13:51:56 +00:00
|
|
|
sequence_ = SEQ_SEND_USER;
|
2006-02-21 12:28:42 +00:00
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool FtpNegotiationCommand::sendUser() {
|
2010-06-21 13:51:56 +00:00
|
|
|
if(ftp_->sendUser()) {
|
2008-09-13 16:32:47 +00:00
|
|
|
disableWriteCheckSocket();
|
2010-06-21 13:51:56 +00:00
|
|
|
sequence_ = SEQ_RECV_USER;
|
2008-09-13 16:32:47 +00:00
|
|
|
} else {
|
2010-06-09 12:43:44 +00:00
|
|
|
setWriteCheckSocket(getSocket());
|
2008-09-13 16:32:47 +00:00
|
|
|
}
|
2006-02-21 12:28:42 +00:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool FtpNegotiationCommand::recvUser() {
|
2011-12-08 12:38:00 +00:00
|
|
|
int status = ftp_->receiveResponse();
|
2006-02-21 12:28:42 +00:00
|
|
|
switch(status) {
|
|
|
|
case 0:
|
|
|
|
return false;
|
|
|
|
case 230:
|
2010-06-21 13:51:56 +00:00
|
|
|
sequence_ = SEQ_SEND_TYPE;
|
2006-02-21 12:28:42 +00:00
|
|
|
break;
|
|
|
|
case 331:
|
2010-06-21 13:51:56 +00:00
|
|
|
sequence_ = SEQ_SEND_PASS;
|
2006-02-21 12:28:42 +00:00
|
|
|
break;
|
|
|
|
default:
|
2010-12-01 12:26:58 +00:00
|
|
|
throw DL_ABORT_EX2(fmt(EX_BAD_STATUS, status),
|
|
|
|
error_code::FTP_PROTOCOL_ERROR);
|
2006-02-21 12:28:42 +00:00
|
|
|
}
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool FtpNegotiationCommand::sendPass() {
|
2010-06-21 13:51:56 +00:00
|
|
|
if(ftp_->sendPass()) {
|
2008-09-13 16:32:47 +00:00
|
|
|
disableWriteCheckSocket();
|
2010-06-21 13:51:56 +00:00
|
|
|
sequence_ = SEQ_RECV_PASS;
|
2008-09-13 16:32:47 +00:00
|
|
|
} else {
|
2010-06-09 12:43:44 +00:00
|
|
|
setWriteCheckSocket(getSocket());
|
2008-09-13 16:32:47 +00:00
|
|
|
}
|
2006-02-21 12:28:42 +00:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool FtpNegotiationCommand::recvPass() {
|
2011-12-08 12:38:00 +00:00
|
|
|
int status = ftp_->receiveResponse();
|
2006-02-21 12:28:42 +00:00
|
|
|
if(status == 0) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
if(status != 230) {
|
2010-12-01 12:26:58 +00:00
|
|
|
throw DL_ABORT_EX2(fmt(EX_BAD_STATUS, status),
|
|
|
|
error_code::FTP_PROTOCOL_ERROR);
|
2006-02-21 12:28:42 +00:00
|
|
|
}
|
2010-06-21 13:51:56 +00:00
|
|
|
sequence_ = SEQ_SEND_TYPE;
|
2006-02-21 12:28:42 +00:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool FtpNegotiationCommand::sendType() {
|
2010-06-21 13:51:56 +00:00
|
|
|
if(ftp_->sendType()) {
|
2008-09-13 16:32:47 +00:00
|
|
|
disableWriteCheckSocket();
|
2010-06-21 13:51:56 +00:00
|
|
|
sequence_ = SEQ_RECV_TYPE;
|
2008-09-13 16:32:47 +00:00
|
|
|
} else {
|
2010-06-09 12:43:44 +00:00
|
|
|
setWriteCheckSocket(getSocket());
|
2008-09-13 16:32:47 +00:00
|
|
|
}
|
2006-02-21 12:28:42 +00:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool FtpNegotiationCommand::recvType() {
|
2011-12-08 12:38:00 +00:00
|
|
|
int status = ftp_->receiveResponse();
|
2006-02-21 12:28:42 +00:00
|
|
|
if(status == 0) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
if(status != 200) {
|
2010-12-01 12:26:58 +00:00
|
|
|
throw DL_ABORT_EX2(fmt(EX_BAD_STATUS, status),
|
|
|
|
error_code::FTP_PROTOCOL_ERROR);
|
2006-02-21 12:28:42 +00:00
|
|
|
}
|
2010-06-21 13:51:56 +00:00
|
|
|
sequence_ = SEQ_SEND_PWD;
|
2008-09-24 17:01:57 +00:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool FtpNegotiationCommand::sendPwd()
|
|
|
|
{
|
2010-06-21 13:51:56 +00:00
|
|
|
if(ftp_->sendPwd()) {
|
2008-09-24 17:01:57 +00:00
|
|
|
disableWriteCheckSocket();
|
2010-06-21 13:51:56 +00:00
|
|
|
sequence_ = SEQ_RECV_PWD;
|
2008-09-24 17:01:57 +00:00
|
|
|
} else {
|
2010-06-09 12:43:44 +00:00
|
|
|
setWriteCheckSocket(getSocket());
|
2008-09-24 17:01:57 +00:00
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool FtpNegotiationCommand::recvPwd()
|
|
|
|
{
|
|
|
|
std::string pwd;
|
2011-12-08 12:38:00 +00:00
|
|
|
int status = ftp_->receivePwdResponse(pwd);
|
2008-09-24 17:01:57 +00:00
|
|
|
if(status == 0) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
if(status != 257) {
|
2010-12-01 12:26:58 +00:00
|
|
|
throw DL_ABORT_EX2(fmt(EX_BAD_STATUS, status),
|
|
|
|
error_code::FTP_PROTOCOL_ERROR);
|
2008-09-24 17:01:57 +00:00
|
|
|
}
|
2010-06-21 13:51:56 +00:00
|
|
|
ftp_->setBaseWorkingDir(pwd);
|
2010-11-20 12:12:06 +00:00
|
|
|
A2_LOG_INFO(fmt("CUID#%lld - base working directory is '%s'",
|
|
|
|
getCuid(), pwd.c_str()));
|
2010-07-09 11:48:59 +00:00
|
|
|
sequence_ = SEQ_SEND_CWD_PREP;
|
2006-02-21 12:28:42 +00:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2010-07-09 11:48:59 +00:00
|
|
|
bool FtpNegotiationCommand::sendCwdPrep()
|
|
|
|
{
|
2008-09-13 16:32:47 +00:00
|
|
|
// Calling setReadCheckSocket() is needed when the socket is reused,
|
2010-06-09 12:43:44 +00:00
|
|
|
setReadCheckSocket(getSocket());
|
2010-07-09 11:48:59 +00:00
|
|
|
cwdDirs_.push_front(ftp_->getBaseWorkingDir());
|
2011-11-04 13:27:58 +00:00
|
|
|
util::split(getRequest()->getDir().begin(), getRequest()->getDir().end(),
|
|
|
|
std::back_inserter(cwdDirs_), '/');
|
2010-07-09 11:48:59 +00:00
|
|
|
sequence_ = SEQ_SEND_CWD;
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool FtpNegotiationCommand::sendCwd()
|
|
|
|
{
|
|
|
|
if(ftp_->sendCwd(cwdDirs_.front())) {
|
2008-09-13 16:32:47 +00:00
|
|
|
disableWriteCheckSocket();
|
2010-06-21 13:51:56 +00:00
|
|
|
sequence_ = SEQ_RECV_CWD;
|
2008-09-13 16:32:47 +00:00
|
|
|
} else {
|
2010-06-09 12:43:44 +00:00
|
|
|
setWriteCheckSocket(getSocket());
|
2008-09-13 16:32:47 +00:00
|
|
|
}
|
2006-02-21 12:28:42 +00:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2010-07-09 11:48:59 +00:00
|
|
|
bool FtpNegotiationCommand::recvCwd()
|
|
|
|
{
|
2011-12-08 12:38:00 +00:00
|
|
|
int status = ftp_->receiveResponse();
|
2006-02-21 12:28:42 +00:00
|
|
|
if(status == 0) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
if(status != 250) {
|
2008-05-10 05:14:35 +00:00
|
|
|
poolConnection();
|
2010-06-09 12:43:44 +00:00
|
|
|
getRequestGroup()->increaseAndValidateFileNotFoundCount();
|
2009-01-12 12:27:34 +00:00
|
|
|
if (status == 550)
|
2009-06-29 08:42:58 +00:00
|
|
|
throw DL_ABORT_EX2(MSG_RESOURCE_NOT_FOUND,
|
2010-11-28 07:52:02 +00:00
|
|
|
error_code::RESOURCE_NOT_FOUND);
|
2009-01-12 12:27:34 +00:00
|
|
|
else
|
2010-12-01 12:26:58 +00:00
|
|
|
throw DL_ABORT_EX2(fmt(EX_BAD_STATUS, status),
|
|
|
|
error_code::FTP_PROTOCOL_ERROR);
|
2006-02-21 12:28:42 +00:00
|
|
|
}
|
2010-07-09 11:48:59 +00:00
|
|
|
cwdDirs_.pop_front();
|
|
|
|
if(cwdDirs_.empty()) {
|
|
|
|
if(getOption()->getAsBool(PREF_REMOTE_TIME)) {
|
|
|
|
sequence_ = SEQ_SEND_MDTM;
|
|
|
|
} else {
|
|
|
|
sequence_ = SEQ_SEND_SIZE;
|
|
|
|
}
|
2008-09-08 13:06:44 +00:00
|
|
|
} else {
|
2010-07-09 11:48:59 +00:00
|
|
|
sequence_ = SEQ_SEND_CWD;
|
2008-09-08 13:06:44 +00:00
|
|
|
}
|
2006-02-21 12:28:42 +00:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2008-09-08 13:06:44 +00:00
|
|
|
bool FtpNegotiationCommand::sendMdtm()
|
|
|
|
{
|
2010-06-21 13:51:56 +00:00
|
|
|
if(ftp_->sendMdtm()) {
|
2008-09-13 16:32:47 +00:00
|
|
|
disableWriteCheckSocket();
|
2010-06-21 13:51:56 +00:00
|
|
|
sequence_ = SEQ_RECV_MDTM;
|
2008-09-13 16:32:47 +00:00
|
|
|
} else {
|
2010-06-09 12:43:44 +00:00
|
|
|
setWriteCheckSocket(getSocket());
|
2008-09-13 16:32:47 +00:00
|
|
|
}
|
2008-09-08 13:06:44 +00:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool FtpNegotiationCommand::recvMdtm()
|
|
|
|
{
|
2008-11-05 10:30:43 +00:00
|
|
|
Time lastModifiedTime = Time::null();
|
2011-12-08 12:38:00 +00:00
|
|
|
int status = ftp_->receiveMdtmResponse(lastModifiedTime);
|
2008-09-08 13:06:44 +00:00
|
|
|
if(status == 0) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
if(status == 213) {
|
|
|
|
if(lastModifiedTime.good()) {
|
2010-06-09 12:43:44 +00:00
|
|
|
getRequestGroup()->updateLastModifiedTime(lastModifiedTime);
|
2010-11-20 08:21:36 +00:00
|
|
|
A2_LOG_DEBUG(fmt("MDTM result was parsed as: %s",
|
|
|
|
lastModifiedTime.toHTTPDate().c_str()));
|
2008-09-08 13:06:44 +00:00
|
|
|
} else {
|
2010-11-20 08:21:36 +00:00
|
|
|
A2_LOG_DEBUG("MDTM response was returned, but it seems not to be"
|
|
|
|
" a time value as in specified in RFC3659.");
|
2008-09-08 13:06:44 +00:00
|
|
|
}
|
|
|
|
} else {
|
2010-11-20 12:12:06 +00:00
|
|
|
A2_LOG_INFO(fmt("CUID#%lld - MDTM command failed.",
|
|
|
|
getCuid()));
|
2008-09-08 13:06:44 +00:00
|
|
|
}
|
2010-06-21 13:51:56 +00:00
|
|
|
sequence_ = SEQ_SEND_SIZE;
|
2008-09-08 13:06:44 +00:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2006-02-21 12:28:42 +00:00
|
|
|
bool FtpNegotiationCommand::sendSize() {
|
2010-06-21 13:51:56 +00:00
|
|
|
if(ftp_->sendSize()) {
|
2008-09-13 16:32:47 +00:00
|
|
|
disableWriteCheckSocket();
|
2010-06-21 13:51:56 +00:00
|
|
|
sequence_ = SEQ_RECV_SIZE;
|
2008-09-13 16:32:47 +00:00
|
|
|
} else {
|
2010-06-09 12:43:44 +00:00
|
|
|
setWriteCheckSocket(getSocket());
|
2008-09-13 16:32:47 +00:00
|
|
|
}
|
2006-02-21 12:28:42 +00:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2011-12-07 13:57:34 +00:00
|
|
|
bool FtpNegotiationCommand::onFileSizeDetermined(off_t totalLength)
|
2008-06-24 14:31:23 +00:00
|
|
|
{
|
2010-06-09 12:43:44 +00:00
|
|
|
getFileEntry()->setLength(totalLength);
|
|
|
|
if(getFileEntry()->getPath().empty()) {
|
|
|
|
getFileEntry()->setPath
|
2010-10-02 07:54:43 +00:00
|
|
|
(util::createSafePath
|
2010-12-03 04:08:22 +00:00
|
|
|
(getOption()->get(PREF_DIR),
|
2011-11-03 10:27:29 +00:00
|
|
|
util::percentDecode(getRequest()->getFile().begin(),
|
|
|
|
getRequest()->getFile().end())));
|
2009-06-28 10:37:15 +00:00
|
|
|
}
|
2010-06-09 12:43:44 +00:00
|
|
|
getRequestGroup()->preDownloadProcessing();
|
|
|
|
if(getDownloadEngine()->getRequestGroupMan()->
|
|
|
|
isSameFileBeingDownloaded(getRequestGroup())) {
|
2010-11-28 08:29:32 +00:00
|
|
|
throw DOWNLOAD_FAILURE_EXCEPTION2
|
2010-11-20 09:36:14 +00:00
|
|
|
(fmt(EX_DUPLICATE_FILE_DOWNLOAD,
|
2011-08-08 12:46:10 +00:00
|
|
|
getRequestGroup()->getFirstFilePath().c_str()),
|
2010-11-28 08:29:32 +00:00
|
|
|
error_code::DUPLICATE_DOWNLOAD);
|
2006-02-21 12:28:42 +00:00
|
|
|
}
|
2008-06-24 14:31:23 +00:00
|
|
|
if(totalLength == 0) {
|
2007-11-13 11:46:58 +00:00
|
|
|
|
2009-05-08 16:41:21 +00:00
|
|
|
if(getOption()->getAsBool(PREF_FTP_PASV)) {
|
2010-07-30 14:45:35 +00:00
|
|
|
sequence_ = SEQ_PREPARE_PASV;
|
2008-11-26 15:22:54 +00:00
|
|
|
} else {
|
2010-07-30 14:45:35 +00:00
|
|
|
sequence_ = SEQ_PREPARE_PORT;
|
2008-11-26 15:22:54 +00:00
|
|
|
}
|
2009-05-07 03:49:27 +00:00
|
|
|
|
2009-05-08 16:41:21 +00:00
|
|
|
if(getOption()->getAsBool(PREF_DRY_RUN)) {
|
2010-06-09 12:43:44 +00:00
|
|
|
getRequestGroup()->initPieceStorage();
|
2009-05-07 03:49:27 +00:00
|
|
|
onDryRunFileFound();
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2010-09-13 11:47:04 +00:00
|
|
|
if(getDownloadContext()->knowsTotalLength() &&
|
|
|
|
getRequestGroup()->downloadFinishedByFileLength()) {
|
2010-09-13 11:53:38 +00:00
|
|
|
// TODO If metalink file does not contain size and it contains
|
|
|
|
// hash and file is not zero length, but remote server says the
|
|
|
|
// file size is 0, no hash check is performed in the current
|
|
|
|
// implementation. See also
|
|
|
|
// HttpResponseCommand::handleOtherEncoding()
|
2010-06-09 12:43:44 +00:00
|
|
|
getRequestGroup()->initPieceStorage();
|
|
|
|
getPieceStorage()->markAllPiecesDone();
|
2010-09-08 14:38:32 +00:00
|
|
|
getDownloadContext()->setChecksumVerified(true);
|
2010-06-21 13:51:56 +00:00
|
|
|
sequence_ = SEQ_DOWNLOAD_ALREADY_COMPLETED;
|
2011-08-04 12:43:02 +00:00
|
|
|
A2_LOG_NOTICE
|
|
|
|
(fmt(MSG_DOWNLOAD_ALREADY_COMPLETED,
|
2011-12-02 12:36:11 +00:00
|
|
|
getRequestGroup()->getGID(),
|
2011-08-08 12:46:10 +00:00
|
|
|
getRequestGroup()->getFirstFilePath().c_str()));
|
2008-11-26 15:22:54 +00:00
|
|
|
poolConnection();
|
|
|
|
|
|
|
|
return false;
|
|
|
|
}
|
2009-05-07 03:49:27 +00:00
|
|
|
|
2010-06-09 12:43:44 +00:00
|
|
|
getRequestGroup()->shouldCancelDownloadForSafety();
|
|
|
|
getRequestGroup()->initPieceStorage();
|
|
|
|
getPieceStorage()->getDiskAdaptor()->initAndOpenFile();
|
2008-06-24 14:31:23 +00:00
|
|
|
|
2009-06-28 10:37:15 +00:00
|
|
|
if(getDownloadContext()->knowsTotalLength()) {
|
2010-06-21 13:51:56 +00:00
|
|
|
sequence_ = SEQ_DOWNLOAD_ALREADY_COMPLETED;
|
2010-09-13 11:47:04 +00:00
|
|
|
getPieceStorage()->markAllPiecesDone();
|
2008-11-26 15:22:54 +00:00
|
|
|
poolConnection();
|
|
|
|
return false;
|
|
|
|
}
|
2010-03-28 07:23:33 +00:00
|
|
|
// We have to make sure that command that has Request object must
|
|
|
|
// have segment after PieceStorage is initialized. See
|
|
|
|
// AbstractCommand::execute()
|
2010-07-17 14:36:18 +00:00
|
|
|
getSegmentMan()->getSegmentWithIndex(getCuid(), 0);
|
2008-08-27 15:00:00 +00:00
|
|
|
return true;
|
2008-06-24 14:31:23 +00:00
|
|
|
} else {
|
2010-11-11 05:12:37 +00:00
|
|
|
SharedHandle<BtProgressInfoFile> progressInfoFile
|
|
|
|
(new DefaultBtProgressInfoFile
|
|
|
|
(getDownloadContext(), SharedHandle<PieceStorage>(), getOption().get()));
|
|
|
|
getRequestGroup()->adjustFilename(progressInfoFile);
|
2010-06-09 12:43:44 +00:00
|
|
|
getRequestGroup()->initPieceStorage();
|
2007-05-20 13:51:52 +00:00
|
|
|
|
2009-05-08 16:41:21 +00:00
|
|
|
if(getOption()->getAsBool(PREF_DRY_RUN)) {
|
2009-05-07 03:49:27 +00:00
|
|
|
onDryRunFileFound();
|
2009-03-08 10:20:42 +00:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2010-09-15 11:46:25 +00:00
|
|
|
SharedHandle<CheckIntegrityEntry> checkIntegrityEntry =
|
|
|
|
getRequestGroup()->createCheckIntegrityEntry();
|
2010-11-12 12:48:48 +00:00
|
|
|
if(!checkIntegrityEntry) {
|
2010-09-15 11:46:25 +00:00
|
|
|
sequence_ = SEQ_DOWNLOAD_ALREADY_COMPLETED;
|
|
|
|
poolConnection();
|
|
|
|
return false;
|
2007-05-20 13:51:52 +00:00
|
|
|
}
|
2010-09-15 11:46:25 +00:00
|
|
|
checkIntegrityEntry->pushNextCommand(this);
|
2010-03-28 07:23:33 +00:00
|
|
|
// We have to make sure that command that has Request object must
|
|
|
|
// have segment after PieceStorage is initialized. See
|
|
|
|
// AbstractCommand::execute()
|
2010-07-17 14:36:18 +00:00
|
|
|
getSegmentMan()->getSegmentWithIndex(getCuid(), 0);
|
2007-12-01 04:56:10 +00:00
|
|
|
|
2010-09-15 11:46:25 +00:00
|
|
|
prepareForNextAction(checkIntegrityEntry);
|
2008-06-28 08:54:54 +00:00
|
|
|
|
2007-12-01 04:56:10 +00:00
|
|
|
disableReadCheckSocket();
|
2008-06-24 14:31:23 +00:00
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
2008-05-09 14:44:28 +00:00
|
|
|
|
2008-06-24 14:31:23 +00:00
|
|
|
bool FtpNegotiationCommand::recvSize() {
|
2011-12-07 13:57:34 +00:00
|
|
|
off_t size = 0;
|
2011-12-08 12:38:00 +00:00
|
|
|
int status = ftp_->receiveSizeResponse(size);
|
2008-06-24 14:31:23 +00:00
|
|
|
if(status == 0) {
|
2007-05-20 13:51:52 +00:00
|
|
|
return false;
|
2008-06-24 14:31:23 +00:00
|
|
|
}
|
|
|
|
if(status == 213) {
|
|
|
|
|
|
|
|
if(size > INT64_MAX) {
|
2010-12-01 12:26:58 +00:00
|
|
|
throw DL_ABORT_EX2
|
2011-11-13 11:55:06 +00:00
|
|
|
(fmt(EX_TOO_LARGE_FILE, static_cast<long long int>(size)),
|
2010-12-01 12:26:58 +00:00
|
|
|
error_code::FTP_PROTOCOL_ERROR);
|
2008-06-24 14:31:23 +00:00
|
|
|
}
|
2010-11-12 12:48:48 +00:00
|
|
|
if(!getPieceStorage()) {
|
2008-06-24 14:31:23 +00:00
|
|
|
|
2010-06-21 13:51:56 +00:00
|
|
|
sequence_ = SEQ_FILE_PREPARATION;
|
2008-06-24 14:31:23 +00:00
|
|
|
return onFileSizeDetermined(size);
|
|
|
|
|
|
|
|
} else {
|
2010-06-09 12:43:44 +00:00
|
|
|
getRequestGroup()->validateTotalLength(getFileEntry()->getLength(), size);
|
2008-06-24 14:31:23 +00:00
|
|
|
}
|
|
|
|
|
2007-05-20 13:51:52 +00:00
|
|
|
} else {
|
2010-11-20 12:12:06 +00:00
|
|
|
A2_LOG_INFO(fmt("CUID#%lld - The remote FTP Server doesn't recognize SIZE"
|
|
|
|
" command. Continue.", getCuid()));
|
2008-06-28 08:54:54 +00:00
|
|
|
// Even if one of the other servers waiting in the queue supports SIZE
|
|
|
|
// command, resuming and segmented downloading are disabled when the first
|
|
|
|
// contacted FTP server doesn't support it.
|
2010-11-12 12:48:48 +00:00
|
|
|
if(!getPieceStorage()) {
|
2009-06-28 10:37:15 +00:00
|
|
|
getDownloadContext()->markTotalLengthIsUnknown();
|
2008-06-28 08:54:54 +00:00
|
|
|
return onFileSizeDetermined(0);
|
|
|
|
|
|
|
|
}
|
|
|
|
// TODO Skipping RequestGroup::validateTotalLength(0) here will allow
|
|
|
|
// wrong file to be downloaded if user-specified URL is wrong.
|
2006-02-21 12:28:42 +00:00
|
|
|
}
|
2009-05-08 16:41:21 +00:00
|
|
|
if(getOption()->getAsBool(PREF_FTP_PASV)) {
|
2010-07-30 14:45:35 +00:00
|
|
|
sequence_ = SEQ_PREPARE_PASV;
|
2006-02-21 12:28:42 +00:00
|
|
|
} else {
|
2010-07-30 14:45:35 +00:00
|
|
|
sequence_ = SEQ_PREPARE_PORT;
|
2006-02-21 12:28:42 +00:00
|
|
|
}
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2008-02-19 13:36:39 +00:00
|
|
|
void FtpNegotiationCommand::afterFileAllocation()
|
|
|
|
{
|
2010-06-09 12:43:44 +00:00
|
|
|
setReadCheckSocket(getSocket());
|
2008-02-19 13:36:39 +00:00
|
|
|
}
|
|
|
|
|
2010-07-30 14:45:35 +00:00
|
|
|
bool FtpNegotiationCommand::preparePort() {
|
|
|
|
afterFileAllocation();
|
|
|
|
if(getSocket()->getAddressFamily() == AF_INET6) {
|
|
|
|
sequence_ = SEQ_PREPARE_SERVER_SOCKET_EPRT;
|
|
|
|
} else {
|
|
|
|
sequence_ = SEQ_PREPARE_SERVER_SOCKET;
|
|
|
|
}
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool FtpNegotiationCommand::prepareServerSocketEprt() {
|
|
|
|
serverSocket_ = ftp_->createServerSocket();
|
|
|
|
sequence_ = SEQ_SEND_EPRT;
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2008-09-13 16:32:47 +00:00
|
|
|
bool FtpNegotiationCommand::prepareServerSocket()
|
|
|
|
{
|
2010-06-21 13:51:56 +00:00
|
|
|
serverSocket_ = ftp_->createServerSocket();
|
|
|
|
sequence_ = SEQ_SEND_PORT;
|
2008-09-13 16:32:47 +00:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2010-07-30 14:45:35 +00:00
|
|
|
bool FtpNegotiationCommand::sendEprt() {
|
|
|
|
if(ftp_->sendEprt(serverSocket_)) {
|
|
|
|
disableWriteCheckSocket();
|
|
|
|
sequence_ = SEQ_RECV_EPRT;
|
|
|
|
} else {
|
|
|
|
setWriteCheckSocket(getSocket());
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool FtpNegotiationCommand::recvEprt() {
|
2011-12-08 12:38:00 +00:00
|
|
|
int status = ftp_->receiveResponse();
|
2010-07-30 14:45:35 +00:00
|
|
|
if(status == 0) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
if(status == 200) {
|
|
|
|
sequence_ = SEQ_SEND_REST;
|
|
|
|
} else {
|
|
|
|
sequence_ = SEQ_PREPARE_SERVER_SOCKET;
|
|
|
|
}
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2006-02-21 12:28:42 +00:00
|
|
|
bool FtpNegotiationCommand::sendPort() {
|
2010-06-21 13:51:56 +00:00
|
|
|
if(ftp_->sendPort(serverSocket_)) {
|
2008-09-13 16:32:47 +00:00
|
|
|
disableWriteCheckSocket();
|
2010-06-21 13:51:56 +00:00
|
|
|
sequence_ = SEQ_RECV_PORT;
|
2008-09-13 16:32:47 +00:00
|
|
|
} else {
|
2010-06-09 12:43:44 +00:00
|
|
|
setWriteCheckSocket(getSocket());
|
2008-09-13 16:32:47 +00:00
|
|
|
}
|
2006-02-21 12:28:42 +00:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool FtpNegotiationCommand::recvPort() {
|
2011-12-08 12:38:00 +00:00
|
|
|
int status = ftp_->receiveResponse();
|
2006-02-21 12:28:42 +00:00
|
|
|
if(status == 0) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
if(status != 200) {
|
2010-12-01 12:26:58 +00:00
|
|
|
throw DL_ABORT_EX2(fmt(EX_BAD_STATUS, status),
|
|
|
|
error_code::FTP_PROTOCOL_ERROR);
|
2006-02-21 12:28:42 +00:00
|
|
|
}
|
2010-06-21 13:51:56 +00:00
|
|
|
sequence_ = SEQ_SEND_REST;
|
2006-02-21 12:28:42 +00:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2010-07-30 14:45:35 +00:00
|
|
|
bool FtpNegotiationCommand::preparePasv() {
|
2008-02-19 13:36:39 +00:00
|
|
|
afterFileAllocation();
|
2010-07-30 14:45:35 +00:00
|
|
|
if(getSocket()->getAddressFamily() == AF_INET6) {
|
|
|
|
sequence_ = SEQ_SEND_EPSV;
|
|
|
|
} else {
|
|
|
|
sequence_ = SEQ_SEND_PASV;
|
|
|
|
}
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool FtpNegotiationCommand::sendEpsv() {
|
|
|
|
if(ftp_->sendEpsv()) {
|
|
|
|
disableWriteCheckSocket();
|
|
|
|
sequence_ = SEQ_RECV_EPSV;
|
|
|
|
} else {
|
|
|
|
setWriteCheckSocket(getSocket());
|
|
|
|
}
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool FtpNegotiationCommand::recvEpsv() {
|
|
|
|
uint16_t port;
|
2011-12-08 12:38:00 +00:00
|
|
|
int status = ftp_->receiveEpsvResponse(port);
|
2010-07-30 14:45:35 +00:00
|
|
|
if(status == 0) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
if(status == 229) {
|
2011-11-14 14:19:37 +00:00
|
|
|
pasvPort_ = port;
|
2010-07-30 14:45:35 +00:00
|
|
|
return preparePasvConnect();
|
|
|
|
} else {
|
|
|
|
sequence_ = SEQ_SEND_PASV;
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
bool FtpNegotiationCommand::sendPasv() {
|
2010-06-21 13:51:56 +00:00
|
|
|
if(ftp_->sendPasv()) {
|
2008-09-13 16:32:47 +00:00
|
|
|
disableWriteCheckSocket();
|
2010-06-21 13:51:56 +00:00
|
|
|
sequence_ = SEQ_RECV_PASV;
|
2008-09-13 16:32:47 +00:00
|
|
|
} else {
|
2010-06-09 12:43:44 +00:00
|
|
|
setWriteCheckSocket(getSocket());
|
2008-09-13 16:32:47 +00:00
|
|
|
}
|
2006-02-21 12:28:42 +00:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool FtpNegotiationCommand::recvPasv() {
|
2008-03-09 12:24:01 +00:00
|
|
|
std::pair<std::string, uint16_t> dest;
|
2011-12-08 12:38:00 +00:00
|
|
|
int status = ftp_->receivePasvResponse(dest);
|
2006-02-21 12:28:42 +00:00
|
|
|
if(status == 0) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
if(status != 227) {
|
2010-12-01 12:26:58 +00:00
|
|
|
throw DL_ABORT_EX2(fmt(EX_BAD_STATUS, status),
|
|
|
|
error_code::FTP_PROTOCOL_ERROR);
|
2006-02-21 12:28:42 +00:00
|
|
|
}
|
2011-11-14 14:19:37 +00:00
|
|
|
pasvPort_ = dest.second;;
|
2010-07-30 14:45:35 +00:00
|
|
|
return preparePasvConnect();
|
|
|
|
}
|
|
|
|
|
|
|
|
bool FtpNegotiationCommand::preparePasvConnect() {
|
2010-04-06 16:09:24 +00:00
|
|
|
if(isProxyDefined()) {
|
2010-06-21 13:51:56 +00:00
|
|
|
sequence_ = SEQ_RESOLVE_PROXY;
|
2010-04-06 16:09:24 +00:00
|
|
|
return true;
|
|
|
|
} else {
|
2011-11-14 14:19:37 +00:00
|
|
|
std::pair<std::string, uint16_t> dataAddr;
|
|
|
|
getSocket()->getPeerInfo(dataAddr);
|
2010-04-06 16:09:24 +00:00
|
|
|
// make a data connection to the server.
|
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(),
|
2011-11-14 14:19:37 +00:00
|
|
|
dataAddr.first.c_str(),
|
|
|
|
pasvPort_));
|
2010-06-21 13:51:56 +00:00
|
|
|
dataSocket_.reset(new SocketCore());
|
2011-11-14 14:19:37 +00:00
|
|
|
dataSocket_->establishConnection(dataAddr.first, pasvPort_);
|
2010-04-06 16:09:24 +00:00
|
|
|
disableReadCheckSocket();
|
2010-06-21 13:51:56 +00:00
|
|
|
setWriteCheckSocket(dataSocket_);
|
|
|
|
sequence_ = SEQ_SEND_REST_PASV;
|
2010-04-06 16:09:24 +00:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
bool FtpNegotiationCommand::resolveProxy()
|
|
|
|
{
|
|
|
|
SharedHandle<Request> proxyReq = createProxyRequest();
|
|
|
|
std::vector<std::string> addrs;
|
2010-06-21 13:51:56 +00:00
|
|
|
proxyAddr_ = resolveHostname
|
2010-04-06 16:09:24 +00:00
|
|
|
(addrs, proxyReq->getHost(), proxyReq->getPort());
|
2010-06-21 13:51:56 +00:00
|
|
|
if(proxyAddr_.empty()) {
|
2010-04-06 16:09:24 +00:00
|
|
|
return false;
|
|
|
|
}
|
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(),
|
2010-11-20 08:21:36 +00:00
|
|
|
proxyAddr_.c_str(), proxyReq->getPort()));
|
2010-06-21 13:51:56 +00:00
|
|
|
dataSocket_.reset(new SocketCore());
|
|
|
|
dataSocket_->establishConnection(proxyAddr_, proxyReq->getPort());
|
2006-07-19 17:07:45 +00:00
|
|
|
disableReadCheckSocket();
|
2010-06-21 13:51:56 +00:00
|
|
|
setWriteCheckSocket(dataSocket_);
|
2011-01-16 07:55:41 +00:00
|
|
|
SharedHandle<SocketRecvBuffer> socketRecvBuffer
|
|
|
|
(new SocketRecvBuffer(dataSocket_));
|
|
|
|
http_.reset(new HttpConnection(getCuid(), dataSocket_, socketRecvBuffer));
|
2010-06-21 13:51:56 +00:00
|
|
|
sequence_ = SEQ_SEND_TUNNEL_REQUEST;
|
2010-04-06 16:09:24 +00:00
|
|
|
return false;
|
|
|
|
}
|
2006-02-21 12:28:42 +00:00
|
|
|
|
2010-04-06 16:09:24 +00:00
|
|
|
bool FtpNegotiationCommand::sendTunnelRequest()
|
|
|
|
{
|
2010-06-21 13:51:56 +00:00
|
|
|
if(http_->sendBufferIsEmpty()) {
|
|
|
|
if(dataSocket_->isReadable(0)) {
|
2010-06-09 12:43:44 +00:00
|
|
|
std::string error = getSocket()->getSocketError();
|
2010-04-06 16:09:24 +00:00
|
|
|
if(!error.empty()) {
|
|
|
|
SharedHandle<Request> proxyReq = createProxyRequest();
|
2010-06-09 12:43:44 +00:00
|
|
|
getDownloadEngine()->markBadIPAddress(proxyReq->getHost(),
|
2010-06-21 13:51:56 +00:00
|
|
|
proxyAddr_,proxyReq->getPort());
|
2010-06-09 12:43:44 +00:00
|
|
|
std::string nextProxyAddr = getDownloadEngine()->findCachedIPAddress
|
2010-04-06 16:09:24 +00:00
|
|
|
(proxyReq->getHost(), proxyReq->getPort());
|
|
|
|
if(nextProxyAddr.empty()) {
|
2010-06-09 12:43:44 +00:00
|
|
|
getDownloadEngine()->removeCachedIPAddress(proxyReq->getHost(),
|
|
|
|
proxyReq->getPort());
|
2010-04-06 16:09:24 +00:00
|
|
|
throw DL_RETRY_EX
|
2010-11-20 09:36:14 +00:00
|
|
|
(fmt(MSG_ESTABLISHING_CONNECTION_FAILED,
|
|
|
|
error.c_str()));
|
2010-04-06 16:09:24 +00:00
|
|
|
} else {
|
2010-11-20 08:21:36 +00:00
|
|
|
A2_LOG_INFO(fmt(MSG_CONNECT_FAILED_AND_RETRY,
|
2010-11-20 12:12:06 +00:00
|
|
|
getCuid(),
|
2010-11-20 08:21:36 +00:00
|
|
|
proxyAddr_.c_str(), proxyReq->getPort()));
|
2010-06-21 13:51:56 +00:00
|
|
|
proxyAddr_ = nextProxyAddr;
|
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(),
|
2010-11-20 08:21:36 +00:00
|
|
|
proxyAddr_.c_str(), proxyReq->getPort()));
|
2010-06-21 13:51:56 +00:00
|
|
|
dataSocket_->establishConnection(proxyAddr_, proxyReq->getPort());
|
2010-04-06 16:09:24 +00:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
SharedHandle<HttpRequest> httpRequest(new HttpRequest());
|
|
|
|
httpRequest->setUserAgent(getOption()->get(PREF_USER_AGENT));
|
|
|
|
SharedHandle<Request> req(new Request());
|
|
|
|
// Construct fake URI in order to use HttpRequest
|
2011-11-14 14:19:37 +00:00
|
|
|
std::pair<std::string, uint16_t> dataAddr;
|
|
|
|
int family = getSocket()->getPeerInfo(dataAddr);
|
|
|
|
uri::UriStruct us;
|
|
|
|
us.protocol = "ftp";
|
2011-12-09 12:37:54 +00:00
|
|
|
us.host = getRequest()->getHost();
|
2011-11-14 14:19:37 +00:00
|
|
|
us.port = pasvPort_;
|
|
|
|
us.ipv6LiteralAddress = (family == AF_INET6);
|
|
|
|
req->setUri(uri::construct(us));
|
2010-04-06 16:09:24 +00:00
|
|
|
httpRequest->setRequest(req);
|
|
|
|
httpRequest->setProxyRequest(createProxyRequest());
|
2010-06-21 13:51:56 +00:00
|
|
|
http_->sendProxyRequest(httpRequest);
|
2010-04-06 16:09:24 +00:00
|
|
|
} else {
|
2010-06-21 13:51:56 +00:00
|
|
|
http_->sendPendingData();
|
2010-04-06 16:09:24 +00:00
|
|
|
}
|
2010-06-21 13:51:56 +00:00
|
|
|
if(http_->sendBufferIsEmpty()) {
|
2010-04-06 16:09:24 +00:00
|
|
|
disableWriteCheckSocket();
|
2010-06-21 13:51:56 +00:00
|
|
|
setReadCheckSocket(dataSocket_);
|
|
|
|
sequence_ = SEQ_RECV_TUNNEL_RESPONSE;
|
2010-04-06 16:09:24 +00:00
|
|
|
return false;
|
|
|
|
} else {
|
2010-06-21 13:51:56 +00:00
|
|
|
setWriteCheckSocket(dataSocket_);
|
2010-04-06 16:09:24 +00:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
bool FtpNegotiationCommand::recvTunnelResponse()
|
|
|
|
{
|
2010-06-21 13:51:56 +00:00
|
|
|
SharedHandle<HttpResponse> httpResponse = http_->receiveResponse();
|
2010-11-12 12:48:48 +00:00
|
|
|
if(!httpResponse) {
|
2010-04-06 16:09:24 +00:00
|
|
|
return false;
|
|
|
|
}
|
2010-11-15 12:52:03 +00:00
|
|
|
if(httpResponse->getStatusCode() != 200) {
|
2010-04-06 16:09:24 +00:00
|
|
|
throw DL_RETRY_EX(EX_PROXY_CONNECTION_FAILED);
|
|
|
|
}
|
2010-06-21 13:51:56 +00:00
|
|
|
sequence_ = SEQ_SEND_REST_PASV;
|
2010-04-06 16:09:24 +00:00
|
|
|
return true;
|
2006-02-21 12:28:42 +00:00
|
|
|
}
|
|
|
|
|
2010-02-28 12:30:11 +00:00
|
|
|
bool FtpNegotiationCommand::sendRestPasv(const SharedHandle<Segment>& segment) {
|
2010-06-21 13:51:56 +00:00
|
|
|
//dataSocket_->setBlockingMode();
|
2010-07-30 14:45:35 +00:00
|
|
|
// Check connection is made properly
|
|
|
|
if(dataSocket_->isReadable(0)) {
|
|
|
|
std::string error = dataSocket_->getSocketError();
|
2010-12-01 12:26:58 +00:00
|
|
|
throw DL_ABORT_EX2
|
|
|
|
(fmt(MSG_ESTABLISHING_CONNECTION_FAILED, error.c_str()),
|
|
|
|
error_code::FTP_PROTOCOL_ERROR);
|
2010-07-30 14:45:35 +00:00
|
|
|
}
|
2010-06-09 12:43:44 +00:00
|
|
|
setReadCheckSocket(getSocket());
|
2006-07-19 17:07:45 +00:00
|
|
|
disableWriteCheckSocket();
|
2006-02-21 12:28:42 +00:00
|
|
|
return sendRest(segment);
|
|
|
|
}
|
|
|
|
|
2010-02-28 12:30:11 +00:00
|
|
|
bool FtpNegotiationCommand::sendRest(const SharedHandle<Segment>& segment) {
|
2010-06-21 13:51:56 +00:00
|
|
|
if(ftp_->sendRest(segment)) {
|
2008-09-13 16:32:47 +00:00
|
|
|
disableWriteCheckSocket();
|
2010-06-21 13:51:56 +00:00
|
|
|
sequence_ = SEQ_RECV_REST;
|
2008-09-13 16:32:47 +00:00
|
|
|
} else {
|
2010-06-09 12:43:44 +00:00
|
|
|
setWriteCheckSocket(getSocket());
|
2008-09-13 16:32:47 +00:00
|
|
|
}
|
2006-02-21 12:28:42 +00:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2008-06-24 14:43:27 +00:00
|
|
|
bool FtpNegotiationCommand::recvRest(const SharedHandle<Segment>& segment) {
|
2011-12-08 12:38:00 +00:00
|
|
|
int status = ftp_->receiveResponse();
|
2006-02-21 12:28:42 +00:00
|
|
|
if(status == 0) {
|
|
|
|
return false;
|
|
|
|
}
|
2008-06-24 14:43:27 +00:00
|
|
|
// If we recieve negative response and requested file position is not 0,
|
|
|
|
// then throw exception here.
|
2006-02-21 12:28:42 +00:00
|
|
|
if(status != 350) {
|
2010-11-12 12:48:48 +00:00
|
|
|
if(segment && segment->getPositionToWrite() != 0) {
|
2010-03-28 07:23:33 +00:00
|
|
|
throw DL_ABORT_EX2("FTP server doesn't support resuming.",
|
2010-11-28 07:52:02 +00:00
|
|
|
error_code::CANNOT_RESUME);
|
2008-06-24 14:43:27 +00:00
|
|
|
}
|
2006-02-21 12:28:42 +00:00
|
|
|
}
|
2010-06-21 13:51:56 +00:00
|
|
|
sequence_ = SEQ_SEND_RETR;
|
2006-02-21 12:28:42 +00:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool FtpNegotiationCommand::sendRetr() {
|
2010-06-21 13:51:56 +00:00
|
|
|
if(ftp_->sendRetr()) {
|
2008-09-13 16:32:47 +00:00
|
|
|
disableWriteCheckSocket();
|
2010-06-21 13:51:56 +00:00
|
|
|
sequence_ = SEQ_RECV_RETR;
|
2008-09-13 16:32:47 +00:00
|
|
|
} else {
|
2010-06-09 12:43:44 +00:00
|
|
|
setWriteCheckSocket(getSocket());
|
2008-09-13 16:32:47 +00:00
|
|
|
}
|
2006-02-21 12:28:42 +00:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool FtpNegotiationCommand::recvRetr() {
|
2011-12-08 12:38:00 +00:00
|
|
|
int status = ftp_->receiveResponse();
|
2006-02-21 12:28:42 +00:00
|
|
|
if(status == 0) {
|
|
|
|
return false;
|
|
|
|
}
|
2006-08-07 17:21:09 +00:00
|
|
|
if(status != 150 && status != 125) {
|
2010-06-09 12:43:44 +00:00
|
|
|
getRequestGroup()->increaseAndValidateFileNotFoundCount();
|
2009-01-12 12:27:34 +00:00
|
|
|
if (status == 550)
|
2009-06-29 08:42:58 +00:00
|
|
|
throw DL_ABORT_EX2(MSG_RESOURCE_NOT_FOUND,
|
2010-11-28 07:52:02 +00:00
|
|
|
error_code::RESOURCE_NOT_FOUND);
|
2009-01-12 12:27:34 +00:00
|
|
|
else
|
2010-12-01 12:26:58 +00:00
|
|
|
throw DL_ABORT_EX2(fmt(EX_BAD_STATUS, status),
|
|
|
|
error_code::FTP_PROTOCOL_ERROR);
|
2006-02-21 12:28:42 +00:00
|
|
|
}
|
2009-05-08 16:41:21 +00:00
|
|
|
if(getOption()->getAsBool(PREF_FTP_PASV)) {
|
2010-06-21 13:51:56 +00:00
|
|
|
sequence_ = SEQ_NEGOTIATION_COMPLETED;
|
2008-02-19 13:36:39 +00:00
|
|
|
return false;
|
|
|
|
} else {
|
|
|
|
disableReadCheckSocket();
|
2010-06-21 13:51:56 +00:00
|
|
|
setReadCheckSocket(serverSocket_);
|
|
|
|
sequence_ = SEQ_WAIT_CONNECTION;
|
2008-02-19 13:36:39 +00:00
|
|
|
return false;
|
2006-02-21 12:28:42 +00:00
|
|
|
}
|
2008-02-19 13:36:39 +00:00
|
|
|
}
|
2006-02-21 12:28:42 +00:00
|
|
|
|
2008-02-19 13:36:39 +00:00
|
|
|
bool FtpNegotiationCommand::waitConnection()
|
|
|
|
{
|
|
|
|
disableReadCheckSocket();
|
2010-06-09 12:43:44 +00:00
|
|
|
setReadCheckSocket(getSocket());
|
2010-06-21 13:51:56 +00:00
|
|
|
dataSocket_.reset(serverSocket_->acceptConnection());
|
|
|
|
dataSocket_->setNonBlockingMode();
|
|
|
|
sequence_ = SEQ_NEGOTIATION_COMPLETED;
|
2006-02-21 12:28:42 +00:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2010-02-28 12:30:11 +00:00
|
|
|
bool FtpNegotiationCommand::processSequence
|
|
|
|
(const SharedHandle<Segment>& segment) {
|
2006-02-21 12:28:42 +00:00
|
|
|
bool doNextSequence = true;
|
2010-06-21 13:51:56 +00:00
|
|
|
switch(sequence_) {
|
2006-02-21 12:28:42 +00:00
|
|
|
case SEQ_RECV_GREETING:
|
|
|
|
return recvGreeting();
|
|
|
|
case SEQ_SEND_USER:
|
|
|
|
return sendUser();
|
|
|
|
case SEQ_RECV_USER:
|
|
|
|
return recvUser();
|
|
|
|
case SEQ_SEND_PASS:
|
|
|
|
return sendPass();
|
|
|
|
case SEQ_RECV_PASS:
|
|
|
|
return recvPass();
|
|
|
|
case SEQ_SEND_TYPE:
|
|
|
|
return sendType();
|
|
|
|
case SEQ_RECV_TYPE:
|
|
|
|
return recvType();
|
2008-09-24 17:01:57 +00:00
|
|
|
case SEQ_SEND_PWD:
|
|
|
|
return sendPwd();
|
|
|
|
case SEQ_RECV_PWD:
|
|
|
|
return recvPwd();
|
2010-07-09 11:48:59 +00:00
|
|
|
case SEQ_SEND_CWD_PREP:
|
|
|
|
return sendCwdPrep();
|
2006-02-21 12:28:42 +00:00
|
|
|
case SEQ_SEND_CWD:
|
|
|
|
return sendCwd();
|
|
|
|
case SEQ_RECV_CWD:
|
|
|
|
return recvCwd();
|
2008-09-08 13:06:44 +00:00
|
|
|
case SEQ_SEND_MDTM:
|
|
|
|
return sendMdtm();
|
|
|
|
case SEQ_RECV_MDTM:
|
|
|
|
return recvMdtm();
|
2006-02-21 12:28:42 +00:00
|
|
|
case SEQ_SEND_SIZE:
|
|
|
|
return sendSize();
|
|
|
|
case SEQ_RECV_SIZE:
|
|
|
|
return recvSize();
|
2010-07-30 14:45:35 +00:00
|
|
|
case SEQ_PREPARE_PORT:
|
|
|
|
return preparePort();
|
|
|
|
case SEQ_PREPARE_SERVER_SOCKET_EPRT:
|
|
|
|
return prepareServerSocketEprt();
|
|
|
|
case SEQ_SEND_EPRT:
|
|
|
|
return sendEprt();
|
|
|
|
case SEQ_RECV_EPRT:
|
|
|
|
return recvEprt();
|
2008-09-13 16:32:47 +00:00
|
|
|
case SEQ_PREPARE_SERVER_SOCKET:
|
|
|
|
return prepareServerSocket();
|
2006-02-21 12:28:42 +00:00
|
|
|
case SEQ_SEND_PORT:
|
|
|
|
return sendPort();
|
|
|
|
case SEQ_RECV_PORT:
|
|
|
|
return recvPort();
|
2010-07-30 14:45:35 +00:00
|
|
|
case SEQ_PREPARE_PASV:
|
|
|
|
return preparePasv();
|
|
|
|
case SEQ_SEND_EPSV:
|
|
|
|
return sendEpsv();
|
|
|
|
case SEQ_RECV_EPSV:
|
|
|
|
return recvEpsv();
|
2006-02-21 12:28:42 +00:00
|
|
|
case SEQ_SEND_PASV:
|
|
|
|
return sendPasv();
|
|
|
|
case SEQ_RECV_PASV:
|
|
|
|
return recvPasv();
|
2010-04-06 16:09:24 +00:00
|
|
|
case SEQ_RESOLVE_PROXY:
|
|
|
|
return resolveProxy();
|
|
|
|
case SEQ_SEND_TUNNEL_REQUEST:
|
|
|
|
return sendTunnelRequest();
|
|
|
|
case SEQ_RECV_TUNNEL_RESPONSE:
|
|
|
|
return recvTunnelResponse();
|
2006-02-21 12:28:42 +00:00
|
|
|
case SEQ_SEND_REST_PASV:
|
|
|
|
return sendRestPasv(segment);
|
|
|
|
case SEQ_SEND_REST:
|
|
|
|
return sendRest(segment);
|
|
|
|
case SEQ_RECV_REST:
|
2008-06-24 14:43:27 +00:00
|
|
|
return recvRest(segment);
|
2006-02-21 12:28:42 +00:00
|
|
|
case SEQ_SEND_RETR:
|
|
|
|
return sendRetr();
|
|
|
|
case SEQ_RECV_RETR:
|
|
|
|
return recvRetr();
|
2008-02-19 13:36:39 +00:00
|
|
|
case SEQ_WAIT_CONNECTION:
|
|
|
|
return waitConnection();
|
2006-02-21 12:28:42 +00:00
|
|
|
default:
|
|
|
|
abort();
|
|
|
|
}
|
|
|
|
return doNextSequence;
|
|
|
|
}
|
2008-02-08 15:53:45 +00:00
|
|
|
|
2008-05-10 05:14:35 +00:00
|
|
|
void FtpNegotiationCommand::poolConnection() const
|
|
|
|
{
|
2009-05-08 16:41:21 +00:00
|
|
|
if(getOption()->getAsBool(PREF_FTP_REUSE_CONNECTION)) {
|
2008-09-24 17:01:57 +00:00
|
|
|
std::map<std::string, std::string> options;
|
2010-06-21 13:51:56 +00:00
|
|
|
options["baseWorkingDir"] = ftp_->getBaseWorkingDir();
|
|
|
|
getDownloadEngine()->poolSocket(getRequest(), ftp_->getUser(),
|
2010-06-09 12:43:44 +00:00
|
|
|
createProxyRequest(), getSocket(), options);
|
2008-05-10 05:14:35 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2009-05-07 03:49:27 +00:00
|
|
|
void FtpNegotiationCommand::onDryRunFileFound()
|
|
|
|
{
|
2010-06-09 12:43:44 +00:00
|
|
|
getPieceStorage()->markAllPiecesDone();
|
2010-09-15 11:46:25 +00:00
|
|
|
getDownloadContext()->setChecksumVerified(true);
|
2009-05-07 03:49:27 +00:00
|
|
|
poolConnection();
|
2010-06-21 13:51:56 +00:00
|
|
|
sequence_ = SEQ_HEAD_OK;
|
2009-05-07 03:49:27 +00:00
|
|
|
}
|
|
|
|
|
2008-02-08 15:53:45 +00:00
|
|
|
} // namespace aria2
|