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
|
2006-09-21 15:31:24 +00:00
|
|
|
* 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.
|
2006-02-21 12:28:42 +00:00
|
|
|
*/
|
|
|
|
/* copyright --> */
|
|
|
|
#include "FtpConnection.h"
|
|
|
|
#include "Util.h"
|
|
|
|
#include "message.h"
|
2006-02-21 14:00:58 +00:00
|
|
|
#include "prefs.h"
|
2006-04-17 16:17:20 +00:00
|
|
|
#include "LogFactory.h"
|
2006-02-21 12:28:42 +00:00
|
|
|
|
2007-07-21 08:56:16 +00:00
|
|
|
FtpConnection::FtpConnection(int32_t cuid, const SocketHandle& socket,
|
2006-10-18 14:57:00 +00:00
|
|
|
const RequestHandle req, const Option* op)
|
2006-07-19 17:07:45 +00:00
|
|
|
:cuid(cuid), socket(socket), req(req), option(op) {
|
2006-04-17 16:17:20 +00:00
|
|
|
logger = LogFactory::getInstance();
|
|
|
|
}
|
2006-02-21 12:28:42 +00:00
|
|
|
|
|
|
|
FtpConnection::~FtpConnection() {}
|
|
|
|
|
2007-10-11 16:58:24 +00:00
|
|
|
void FtpConnection::sendUser() const
|
|
|
|
throw(DlRetryEx*)
|
|
|
|
{
|
2007-03-21 10:19:23 +00:00
|
|
|
string request = "USER "+req->resolveFtpAuthConfig()->getUser()+"\r\n";
|
2006-02-21 15:01:05 +00:00
|
|
|
logger->info(MSG_SENDING_REQUEST, cuid, request.c_str());
|
2006-02-21 12:28:42 +00:00
|
|
|
socket->writeData(request);
|
|
|
|
}
|
|
|
|
|
2007-10-11 16:58:24 +00:00
|
|
|
void FtpConnection::sendPass() const
|
|
|
|
throw(DlRetryEx*)
|
|
|
|
{
|
2007-03-21 10:19:23 +00:00
|
|
|
string request = "PASS "+req->resolveFtpAuthConfig()->getPassword()+"\r\n";
|
2006-02-21 15:01:05 +00:00
|
|
|
logger->info(MSG_SENDING_REQUEST, cuid, "PASS ********");
|
2006-02-21 12:28:42 +00:00
|
|
|
socket->writeData(request);
|
|
|
|
}
|
|
|
|
|
2007-10-11 16:58:24 +00:00
|
|
|
void FtpConnection::sendType() const
|
|
|
|
throw(DlRetryEx*)
|
|
|
|
{
|
2006-02-21 14:00:58 +00:00
|
|
|
string type;
|
|
|
|
if(option->get(PREF_FTP_TYPE) == V_ASCII) {
|
|
|
|
type = "A";
|
|
|
|
} else {
|
|
|
|
type = "I";
|
|
|
|
}
|
|
|
|
string request = "TYPE "+type+"\r\n";
|
2006-02-21 15:01:05 +00:00
|
|
|
logger->info(MSG_SENDING_REQUEST, cuid, request.c_str());
|
2006-02-21 12:28:42 +00:00
|
|
|
socket->writeData(request);
|
|
|
|
}
|
|
|
|
|
2007-10-11 16:58:24 +00:00
|
|
|
void FtpConnection::sendCwd() const
|
|
|
|
throw(DlRetryEx*)
|
|
|
|
{
|
|
|
|
string request = "CWD "+Util::urldecode(req->getDir())+"\r\n";
|
2006-02-21 15:01:05 +00:00
|
|
|
logger->info(MSG_SENDING_REQUEST, cuid, request.c_str());
|
2006-02-21 12:28:42 +00:00
|
|
|
socket->writeData(request);
|
|
|
|
}
|
|
|
|
|
2007-10-11 16:58:24 +00:00
|
|
|
void FtpConnection::sendSize() const
|
|
|
|
throw(DlRetryEx*)
|
|
|
|
{
|
|
|
|
string request = "SIZE "+Util::urldecode(req->getFile())+"\r\n";
|
2006-02-21 15:01:05 +00:00
|
|
|
logger->info(MSG_SENDING_REQUEST, cuid, request.c_str());
|
2006-02-21 12:28:42 +00:00
|
|
|
socket->writeData(request);
|
|
|
|
}
|
|
|
|
|
2007-10-11 16:58:24 +00:00
|
|
|
void FtpConnection::sendPasv() const
|
|
|
|
throw(DlRetryEx*)
|
|
|
|
{
|
2006-02-21 12:28:42 +00:00
|
|
|
string request = "PASV\r\n";
|
2006-02-21 15:01:05 +00:00
|
|
|
logger->info(MSG_SENDING_REQUEST, cuid, request.c_str());
|
2006-02-21 12:28:42 +00:00
|
|
|
socket->writeData(request);
|
|
|
|
}
|
|
|
|
|
2007-10-11 16:58:24 +00:00
|
|
|
SocketHandle FtpConnection::sendPort() const
|
|
|
|
throw(DlAbortEx*, DlRetryEx*)
|
|
|
|
{
|
2006-07-19 17:07:45 +00:00
|
|
|
SocketHandle serverSocket;
|
|
|
|
serverSocket->beginListen();
|
|
|
|
|
2007-07-21 08:56:16 +00:00
|
|
|
pair<string, int32_t> addrinfo;
|
2006-07-19 17:07:45 +00:00
|
|
|
socket->getAddrInfo(addrinfo);
|
2007-07-21 08:56:16 +00:00
|
|
|
int32_t ipaddr[4];
|
2006-07-19 17:07:45 +00:00
|
|
|
sscanf(addrinfo.first.c_str(), "%d.%d.%d.%d",
|
|
|
|
&ipaddr[0], &ipaddr[1], &ipaddr[2], &ipaddr[3]);
|
|
|
|
serverSocket->getAddrInfo(addrinfo);
|
|
|
|
string request = "PORT "+
|
|
|
|
Util::itos(ipaddr[0])+","+Util::itos(ipaddr[1])+","+
|
|
|
|
Util::itos(ipaddr[2])+","+Util::itos(ipaddr[3])+","+
|
|
|
|
Util::itos(addrinfo.second/256)+","+Util::itos(addrinfo.second%256)+"\r\n";
|
|
|
|
logger->info(MSG_SENDING_REQUEST, cuid, request.c_str());
|
|
|
|
socket->writeData(request);
|
2006-02-21 12:28:42 +00:00
|
|
|
return serverSocket;
|
|
|
|
}
|
|
|
|
|
2007-10-11 16:58:24 +00:00
|
|
|
void FtpConnection::sendRest(const SegmentHandle& segment) const
|
|
|
|
throw(DlRetryEx*)
|
|
|
|
{
|
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
|
|
|
string request = "REST "+Util::llitos(segment->getPositionToWrite())+"\r\n";
|
2006-02-21 15:01:05 +00:00
|
|
|
logger->info(MSG_SENDING_REQUEST, cuid, request.c_str());
|
2006-02-21 12:28:42 +00:00
|
|
|
socket->writeData(request);
|
|
|
|
}
|
|
|
|
|
2007-10-11 16:58:24 +00:00
|
|
|
void FtpConnection::sendRetr() const
|
|
|
|
throw(DlRetryEx*)
|
|
|
|
{
|
|
|
|
string request = "RETR "+Util::urldecode(req->getFile())+"\r\n";
|
2006-02-21 15:01:05 +00:00
|
|
|
logger->info(MSG_SENDING_REQUEST, cuid, request.c_str());
|
2006-02-21 12:28:42 +00:00
|
|
|
socket->writeData(request);
|
|
|
|
}
|
|
|
|
|
2007-10-11 16:58:24 +00:00
|
|
|
int32_t FtpConnection::getStatus(const string& response) const
|
|
|
|
{
|
2007-07-21 08:56:16 +00:00
|
|
|
int32_t status;
|
2006-02-22 11:18:47 +00:00
|
|
|
// When the response is not like "%d %*s",
|
|
|
|
// we return 0.
|
|
|
|
if(response.find_first_not_of("0123456789") != 3
|
2006-02-22 14:30:47 +00:00
|
|
|
|| !(response.find(" ") == 3 || response.find("-") == 3)) {
|
2006-02-22 11:18:47 +00:00
|
|
|
return 0;
|
|
|
|
}
|
2006-02-21 12:28:42 +00:00
|
|
|
if(sscanf(response.c_str(), "%d %*s", &status) == 1) {
|
|
|
|
return status;
|
|
|
|
} else {
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2007-10-11 16:58:24 +00:00
|
|
|
bool FtpConnection::isEndOfResponse(int32_t status, const string& response) const
|
|
|
|
{
|
2006-02-21 12:28:42 +00:00
|
|
|
if(response.size() <= 4) {
|
|
|
|
return false;
|
|
|
|
}
|
2006-03-01 15:28:03 +00:00
|
|
|
// if 4th character of buf is '-', then multi line response is expected.
|
2006-02-21 12:28:42 +00:00
|
|
|
if(response.at(3) == '-') {
|
|
|
|
// multi line response
|
|
|
|
string::size_type p;
|
|
|
|
p = response.find("\r\n"+Util::itos(status)+" ");
|
|
|
|
if(p == string::npos) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if(Util::endsWith(response, "\r\n")) {
|
|
|
|
return true;
|
|
|
|
} else {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2007-10-11 16:58:24 +00:00
|
|
|
bool FtpConnection::bulkReceiveResponse(pair<int32_t, string>& response)
|
|
|
|
throw(DlRetryEx*)
|
|
|
|
{
|
2006-02-21 12:28:42 +00:00
|
|
|
char buf[1024];
|
|
|
|
while(socket->isReadable(0)) {
|
2007-07-21 08:56:16 +00:00
|
|
|
int32_t size = sizeof(buf)-1;
|
2006-02-21 12:28:42 +00:00
|
|
|
socket->readData(buf, size);
|
2006-11-08 16:25:38 +00:00
|
|
|
if(size == 0) {
|
|
|
|
throw new DlRetryEx(EX_GOT_EOF);
|
|
|
|
}
|
2006-02-21 12:28:42 +00:00
|
|
|
buf[size] = '\0';
|
|
|
|
strbuf += buf;
|
|
|
|
}
|
2007-07-21 08:56:16 +00:00
|
|
|
int32_t status;
|
2006-02-21 12:28:42 +00:00
|
|
|
if(strbuf.size() >= 4) {
|
|
|
|
status = getStatus(strbuf);
|
|
|
|
if(status == 0) {
|
2006-02-21 14:00:58 +00:00
|
|
|
throw new DlRetryEx(EX_INVALID_RESPONSE);
|
2006-02-21 12:28:42 +00:00
|
|
|
}
|
|
|
|
} else {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
if(isEndOfResponse(status, strbuf)) {
|
|
|
|
logger->info(MSG_RECEIVE_RESPONSE, cuid, strbuf.c_str());
|
|
|
|
response.first = status;
|
|
|
|
response.second = strbuf;
|
|
|
|
strbuf.erase();
|
|
|
|
return true;
|
|
|
|
} else {
|
|
|
|
// didn't receive response fully.
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2007-10-11 16:58:24 +00:00
|
|
|
int32_t FtpConnection::receiveResponse()
|
|
|
|
throw(DlRetryEx*)
|
|
|
|
{
|
2007-07-21 08:56:16 +00:00
|
|
|
pair<int32_t, string> response;
|
2006-02-21 12:28:42 +00:00
|
|
|
if(bulkReceiveResponse(response)) {
|
|
|
|
return response.first;
|
|
|
|
} else {
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2007-10-11 16:58:24 +00:00
|
|
|
int32_t FtpConnection::receiveSizeResponse(int64_t& size)
|
|
|
|
throw(DlRetryEx*)
|
|
|
|
{
|
2007-07-21 08:56:16 +00:00
|
|
|
pair<int32_t, string> response;
|
2006-02-21 12:28:42 +00:00
|
|
|
if(bulkReceiveResponse(response)) {
|
|
|
|
if(response.first == 213) {
|
2007-07-23 13:04:48 +00:00
|
|
|
sscanf(response.second.c_str(), "%*d " LONGLONG_SCANF, &size);
|
2006-02-21 12:28:42 +00:00
|
|
|
}
|
|
|
|
return response.first;
|
|
|
|
} else {
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2007-10-11 16:58:24 +00:00
|
|
|
int32_t FtpConnection::receivePasvResponse(pair<string, int32_t>& dest)
|
|
|
|
throw(DlRetryEx*)
|
|
|
|
{
|
2007-07-21 08:56:16 +00:00
|
|
|
pair<int32_t, string> response;
|
2006-02-21 12:28:42 +00:00
|
|
|
if(bulkReceiveResponse(response)) {
|
|
|
|
if(response.first == 227) {
|
|
|
|
// we assume the format of response is "227 Entering Passive Mode (h1,h2,h3,h4,p1,p2)."
|
2007-07-21 08:56:16 +00:00
|
|
|
int32_t h1, h2, h3, h4, p1, p2;
|
2006-02-21 12:28:42 +00:00
|
|
|
string::size_type p = response.second.find("(");
|
|
|
|
if(p >= 4) {
|
|
|
|
sscanf(response.second.substr(response.second.find("(")).c_str(),
|
|
|
|
"(%d,%d,%d,%d,%d,%d).",
|
|
|
|
&h1, &h2, &h3, &h4, &p1, &p2);
|
|
|
|
// ip address
|
|
|
|
dest.first = Util::itos(h1)+"."+Util::itos(h2)+"."+Util::itos(h3)+"."+Util::itos(h4);
|
|
|
|
// port number
|
|
|
|
dest.second = 256*p1+p2;
|
|
|
|
} else {
|
2006-02-21 14:00:58 +00:00
|
|
|
throw new DlRetryEx(EX_INVALID_RESPONSE);
|
2006-02-21 12:28:42 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
return response.first;
|
|
|
|
} else {
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
}
|