aria2/src/HttpResponseCommand.cc

163 lines
5.4 KiB
C++
Raw Normal View History

2006-02-17 13:35:04 +00:00
/* <!-- copyright */
/*
* aria2 - a simple utility for downloading files faster
*
* 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
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
*/
/* copyright --> */
#include "HttpResponseCommand.h"
#include "DlAbortEx.h"
2006-02-21 14:00:58 +00:00
#include "DlRetryEx.h"
2006-02-17 13:35:04 +00:00
#include "HttpDownloadCommand.h"
#include "HttpInitiateConnectionCommand.h"
#include "message.h"
#include "Util.h"
HttpResponseCommand::HttpResponseCommand(int cuid, Request* req, DownloadEngine* e, const Socket* s):
2006-02-17 13:35:04 +00:00
AbstractCommand(cuid, req, e, s) {
To add LogFactory which creates singleton logger: * src/LogFactory.h: New class. * src/LogFactory.cc: New class. * src/Command.h (logger): New variable. (Constructor): Use LogFactory. * src/AbstractCommand.cc: Use Command::logger * src/PeerConnection.cc (Constructor): Deleted the argument logger. Use LogFactory. * src/SegmentSplitter.h : Made logger protected. * src/SegmentSplitter.cc (Constructor): Use LogFactory. * src/SegmentMan.cc (Constructor): Use LogFactory. * src/DownloadEngine.h : Made logger protected. * src/DownloadEngine.cc (Constructor): Use LogFactory. * src/PeerInteractionCommand.cc : Use Command::logger. * src/HttpResponseCommand.cc : Use Command::logger. * src/SegmentMan.h : Made logger private. * src/TorrentMan.h : Made logger private. * src/TorrentMan.cc : Use LogFactory. * src/FtpNegotiateCommand.cc : Use Command::logger. * src/HttpConnection.h (Constructor): Deleted the argument logger. * src/HttpConnection.cc (Constructor): Deleted the argument logger. Use LogFactory. * src/FtpConnection.h (Constructor): Deleted the argument logger. * src/FtpConnection.cc (Constructor): Deleted the argument logger. Use LogFactory. * src/DownloadCommand.cc : Use Command::logger. * src/PeerAbstractCommand.cc : Use Command::logger. * src/PeerListenCommand.cc : Use Command::logger. * src/PeerInitiateConnectionCommand.cc : Use Command::logger. * src/HttpInitiateConnectionCommand.cc : Use Command::logger. * src/FtpInitiateConnectionCommand.cc : Use Command::logger. * src/TrackerWatcherCommand.cc : Use Command::logger. * src/TrackerUpdateCommand.cc : Use Command::logger. * src/TrackerDownloadCommand.cc : Use Command::logger. * src/RequestSlotMan.cc (Constructor): Deleted the argument logger. Use LogFactory. * src/SendMessageQueue.h (Constructor): Deleted the argument logger. * src/SendMessageQueue.cc (Constructor): Deleted the argument logger. Use LogFactory. * src/main.cc (main): Use LogFactory. * src/DiskAdaptor.h (logger): New variable. * src/DiskAdaptor.cc (Constructor): Use LogFactory. * src/CopyDiskAdaptor.cc (fixFilename): Added a log message.
2006-04-17 16:17:20 +00:00
http = new HttpConnection(cuid, socket, req, e->option);
2006-02-17 13:35:04 +00:00
}
2006-02-21 12:27:17 +00:00
HttpResponseCommand::~HttpResponseCommand() {
delete http;
}
2006-02-17 13:35:04 +00:00
bool HttpResponseCommand::executeInternal(Segment seg) {
2006-03-05 06:32:01 +00:00
if(req->seg.sp != seg.sp) {
To add LogFactory which creates singleton logger: * src/LogFactory.h: New class. * src/LogFactory.cc: New class. * src/Command.h (logger): New variable. (Constructor): Use LogFactory. * src/AbstractCommand.cc: Use Command::logger * src/PeerConnection.cc (Constructor): Deleted the argument logger. Use LogFactory. * src/SegmentSplitter.h : Made logger protected. * src/SegmentSplitter.cc (Constructor): Use LogFactory. * src/SegmentMan.cc (Constructor): Use LogFactory. * src/DownloadEngine.h : Made logger protected. * src/DownloadEngine.cc (Constructor): Use LogFactory. * src/PeerInteractionCommand.cc : Use Command::logger. * src/HttpResponseCommand.cc : Use Command::logger. * src/SegmentMan.h : Made logger private. * src/TorrentMan.h : Made logger private. * src/TorrentMan.cc : Use LogFactory. * src/FtpNegotiateCommand.cc : Use Command::logger. * src/HttpConnection.h (Constructor): Deleted the argument logger. * src/HttpConnection.cc (Constructor): Deleted the argument logger. Use LogFactory. * src/FtpConnection.h (Constructor): Deleted the argument logger. * src/FtpConnection.cc (Constructor): Deleted the argument logger. Use LogFactory. * src/DownloadCommand.cc : Use Command::logger. * src/PeerAbstractCommand.cc : Use Command::logger. * src/PeerListenCommand.cc : Use Command::logger. * src/PeerInitiateConnectionCommand.cc : Use Command::logger. * src/HttpInitiateConnectionCommand.cc : Use Command::logger. * src/FtpInitiateConnectionCommand.cc : Use Command::logger. * src/TrackerWatcherCommand.cc : Use Command::logger. * src/TrackerUpdateCommand.cc : Use Command::logger. * src/TrackerDownloadCommand.cc : Use Command::logger. * src/RequestSlotMan.cc (Constructor): Deleted the argument logger. Use LogFactory. * src/SendMessageQueue.h (Constructor): Deleted the argument logger. * src/SendMessageQueue.cc (Constructor): Deleted the argument logger. Use LogFactory. * src/main.cc (main): Use LogFactory. * src/DiskAdaptor.h (logger): New variable. * src/DiskAdaptor.cc (Constructor): Use LogFactory. * src/CopyDiskAdaptor.cc (fixFilename): Added a log message.
2006-04-17 16:17:20 +00:00
logger->info(MSG_SEGMENT_CHANGED, cuid);
return prepareForRetry(0);
2006-02-17 13:35:04 +00:00
}
HttpHeader headers;
2006-02-21 12:27:17 +00:00
int status = http->receiveResponse(headers);
if(status == 0) {
// didn't receive header fully
e->commands.push(this);
return false;
}
2006-02-17 13:35:04 +00:00
// check HTTP status number
checkResponse(status, seg);
retrieveCookie(headers);
// check whether Location header exists. If it does, update request object
// with redirected URL.
// then establish a connection to the new host and port
2006-02-21 12:27:17 +00:00
if(headers.defined("Location")) {
return handleRedirect(headers.getFirst("Location"), headers);
2006-02-17 13:35:04 +00:00
}
if(!e->segmentMan->downloadStarted) {
string transferEncoding;
2006-02-21 12:27:17 +00:00
if(headers.defined("Transfer-Encoding")) {
return handleOtherEncoding(headers.getFirst("Transfer-Encoding"), headers);
2006-02-17 13:35:04 +00:00
} else {
return handleDefaultEncoding(headers);
}
} else {
if(req->getFile() != e->segmentMan->filename) {
throw new DlAbortEx(EX_FILENAME_MISMATCH, req->getFile().c_str(), e->segmentMan->filename.c_str());
}
createHttpDownloadCommand();
return true;
}
}
void HttpResponseCommand::checkResponse(int status, const Segment& segment) {
if(!(status < 400 && status >= 300 ||
(segment.sp+segment.ds == 0 && status == 200)
|| (segment.sp+segment.ds > 0 && status == 206))) {
2006-02-21 14:00:58 +00:00
throw new DlRetryEx(EX_BAD_STATUS, status);
2006-02-17 13:35:04 +00:00
}
}
bool HttpResponseCommand::handleRedirect(string url, const HttpHeader& headers) {
req->redirectUrl(url);
To add LogFactory which creates singleton logger: * src/LogFactory.h: New class. * src/LogFactory.cc: New class. * src/Command.h (logger): New variable. (Constructor): Use LogFactory. * src/AbstractCommand.cc: Use Command::logger * src/PeerConnection.cc (Constructor): Deleted the argument logger. Use LogFactory. * src/SegmentSplitter.h : Made logger protected. * src/SegmentSplitter.cc (Constructor): Use LogFactory. * src/SegmentMan.cc (Constructor): Use LogFactory. * src/DownloadEngine.h : Made logger protected. * src/DownloadEngine.cc (Constructor): Use LogFactory. * src/PeerInteractionCommand.cc : Use Command::logger. * src/HttpResponseCommand.cc : Use Command::logger. * src/SegmentMan.h : Made logger private. * src/TorrentMan.h : Made logger private. * src/TorrentMan.cc : Use LogFactory. * src/FtpNegotiateCommand.cc : Use Command::logger. * src/HttpConnection.h (Constructor): Deleted the argument logger. * src/HttpConnection.cc (Constructor): Deleted the argument logger. Use LogFactory. * src/FtpConnection.h (Constructor): Deleted the argument logger. * src/FtpConnection.cc (Constructor): Deleted the argument logger. Use LogFactory. * src/DownloadCommand.cc : Use Command::logger. * src/PeerAbstractCommand.cc : Use Command::logger. * src/PeerListenCommand.cc : Use Command::logger. * src/PeerInitiateConnectionCommand.cc : Use Command::logger. * src/HttpInitiateConnectionCommand.cc : Use Command::logger. * src/FtpInitiateConnectionCommand.cc : Use Command::logger. * src/TrackerWatcherCommand.cc : Use Command::logger. * src/TrackerUpdateCommand.cc : Use Command::logger. * src/TrackerDownloadCommand.cc : Use Command::logger. * src/RequestSlotMan.cc (Constructor): Deleted the argument logger. Use LogFactory. * src/SendMessageQueue.h (Constructor): Deleted the argument logger. * src/SendMessageQueue.cc (Constructor): Deleted the argument logger. Use LogFactory. * src/main.cc (main): Use LogFactory. * src/DiskAdaptor.h (logger): New variable. * src/DiskAdaptor.cc (Constructor): Use LogFactory. * src/CopyDiskAdaptor.cc (fixFilename): Added a log message.
2006-04-17 16:17:20 +00:00
logger->info(MSG_REDIRECT, cuid, url.c_str());
2006-02-17 13:35:04 +00:00
e->noWait = true;
return prepareForRetry(0);
2006-02-17 13:35:04 +00:00
}
bool HttpResponseCommand::handleDefaultEncoding(const HttpHeader& headers) {
2006-03-21 14:12:51 +00:00
// TODO quick and dirty way
if(req->isTorrent) {
long long int size = headers.getFirstAsLLInt("Content-Length");
e->segmentMan->totalSize = size;
e->segmentMan->isSplittable = false;
2006-04-19 Tatsuhiro Tsujikawa <tujikawa at rednoah dot com> To add a readout of estimated remaining time to normal HTTP/FTP downloads: * src/ConsoleDownloadEngine.h (startup): New variable. (startupLength): New variable. (isStartupLengthSet): New variable. (avgSpeed): New variable. (eta): New variable. * src/ConsoleDownloadEngine.cc (sendStatistics): Added a readout of estimated remaining time. (initStatistics): Initialized newly added variables. (calculateStatistics): Calculate average speed and estimated remaining time. To decouple TorrentDownloadEngine from HttpResponseCommand: * src/TrackerDownloadCommand.h: Removed. * src/TrackerDownloadCommand.cc: Removed. * src/TrackerInitCommand.h: Removed. * src/TrackerInitCommand.cc: Removed. * src/TrackerUpdateCommand.h: Removed. * src/TrackerUpdateCommand.cc: Removed. * src/TrackerWatcherCommand.cc (execute): The construction of request url written in TrackerInitCommand was moved here. Do not create tracker request command if torretnMan->trackers != 0. * src/CompactTrackerResponseProcessor.h: New class. * src/CompactTrackerResponseProcessor.cc: New class. * src/message.h (MSG_TRACKER_WARNING_MESSAGE): Updated. * src/HttpResponseCommand.cc (createHttpDownloadCommand): Decoupled TorrentDownloadEngine from this. * src/SegmentMan.h (init): New function. * src/SegmentMan.cc (init): New function. * src/TorrentMan.h (responseProcessor): New variable. (trackers): New variable. (setTrackerResponseProcessor): New function. (getTrackerResponseProcessor): New function. (processTrackerResponse): New function. * src/TorrentMan.cc (Constructor): Initialized new variable trackers. (processTrackerResponse): New function. * src/main.cc (main): Use ByteArrayDiskWriter and CompactTrackerResponseProcessor. * src/TorrentDownloadEngine.cc (afterEachIteration): Call torrentMan-> processTrackerResponse(). * src/TorrentConsoleDownloadEngine.cc (printStatistics): Updated a readout. * src/TorrentDownloadEngine.cc (afterEachIteration): Added log message which indicates download has completed. * src/AbstractDiskWriter.cc (Destructor): fd >= 0, not fd >0 (closeFile): fd >= 0, not fd > 0 * src/main.cc (main): Added short cut for show-files. Added short cut for torrent-file. Added new command-line option listen-port. Updated i18n messages.
2006-04-18 17:06:17 +00:00
e->segmentMan->downloadStarted = true;
2006-03-21 14:12:51 +00:00
createHttpDownloadCommand();
return true;
}
2006-02-21 12:27:17 +00:00
long long int size = headers.getFirstAsLLInt("Content-Length");
2006-02-21 14:00:58 +00:00
if(size == LONG_LONG_MAX || size < 0) {
2006-02-21 12:27:17 +00:00
throw new DlAbortEx(EX_TOO_LARGE_FILE, size);
2006-02-17 13:35:04 +00:00
}
e->segmentMan->isSplittable = !(size == 0);
e->segmentMan->filename = req->getFile();
bool segFileExists = e->segmentMan->segmentFileExists();
e->segmentMan->downloadStarted = true;
if(segFileExists) {
2006-02-22 12:16:10 +00:00
e->segmentMan->load();
2006-02-17 13:35:04 +00:00
e->diskWriter->openExistingFile(e->segmentMan->getFilePath());
// send request again to the server with Range header
return prepareForRetry(0);
2006-02-17 13:35:04 +00:00
} else {
2006-02-21 12:27:17 +00:00
e->segmentMan->totalSize = size;
2006-03-05 06:32:01 +00:00
Segment seg;
e->segmentMan->getSegment(seg, cuid);
2006-02-17 13:35:04 +00:00
e->diskWriter->initAndOpenFile(e->segmentMan->getFilePath());
createHttpDownloadCommand();
return true;
}
}
bool HttpResponseCommand::handleOtherEncoding(string transferEncoding, const HttpHeader& headers) {
// we ignore content-length when transfer-encoding is set
e->segmentMan->downloadStarted = true;
e->segmentMan->isSplittable = false;
e->segmentMan->filename = req->getFile();
e->segmentMan->totalSize = 0;
Segment seg;
e->segmentMan->getSegment(seg, cuid);
e->diskWriter->initAndOpenFile(e->segmentMan->getFilePath());
createHttpDownloadCommand(transferEncoding);
return true;
}
void HttpResponseCommand::createHttpDownloadCommand(string transferEncoding) {
2006-04-19 Tatsuhiro Tsujikawa <tujikawa at rednoah dot com> To add a readout of estimated remaining time to normal HTTP/FTP downloads: * src/ConsoleDownloadEngine.h (startup): New variable. (startupLength): New variable. (isStartupLengthSet): New variable. (avgSpeed): New variable. (eta): New variable. * src/ConsoleDownloadEngine.cc (sendStatistics): Added a readout of estimated remaining time. (initStatistics): Initialized newly added variables. (calculateStatistics): Calculate average speed and estimated remaining time. To decouple TorrentDownloadEngine from HttpResponseCommand: * src/TrackerDownloadCommand.h: Removed. * src/TrackerDownloadCommand.cc: Removed. * src/TrackerInitCommand.h: Removed. * src/TrackerInitCommand.cc: Removed. * src/TrackerUpdateCommand.h: Removed. * src/TrackerUpdateCommand.cc: Removed. * src/TrackerWatcherCommand.cc (execute): The construction of request url written in TrackerInitCommand was moved here. Do not create tracker request command if torretnMan->trackers != 0. * src/CompactTrackerResponseProcessor.h: New class. * src/CompactTrackerResponseProcessor.cc: New class. * src/message.h (MSG_TRACKER_WARNING_MESSAGE): Updated. * src/HttpResponseCommand.cc (createHttpDownloadCommand): Decoupled TorrentDownloadEngine from this. * src/SegmentMan.h (init): New function. * src/SegmentMan.cc (init): New function. * src/TorrentMan.h (responseProcessor): New variable. (trackers): New variable. (setTrackerResponseProcessor): New function. (getTrackerResponseProcessor): New function. (processTrackerResponse): New function. * src/TorrentMan.cc (Constructor): Initialized new variable trackers. (processTrackerResponse): New function. * src/main.cc (main): Use ByteArrayDiskWriter and CompactTrackerResponseProcessor. * src/TorrentDownloadEngine.cc (afterEachIteration): Call torrentMan-> processTrackerResponse(). * src/TorrentConsoleDownloadEngine.cc (printStatistics): Updated a readout. * src/TorrentDownloadEngine.cc (afterEachIteration): Added log message which indicates download has completed. * src/AbstractDiskWriter.cc (Destructor): fd >= 0, not fd >0 (closeFile): fd >= 0, not fd > 0 * src/main.cc (main): Added short cut for show-files. Added short cut for torrent-file. Added new command-line option listen-port. Updated i18n messages.
2006-04-18 17:06:17 +00:00
HttpDownloadCommand* command = new HttpDownloadCommand(cuid, req, e, socket);
TransferEncoding* enc = NULL;
if(transferEncoding.size() && (enc = command->getTransferEncoding(transferEncoding)) == NULL) {
delete(command);
throw new DlAbortEx(EX_TRANSFER_ENCODING_NOT_SUPPORTED, transferEncoding.c_str());
2006-03-21 14:12:51 +00:00
} else {
2006-04-19 Tatsuhiro Tsujikawa <tujikawa at rednoah dot com> To add a readout of estimated remaining time to normal HTTP/FTP downloads: * src/ConsoleDownloadEngine.h (startup): New variable. (startupLength): New variable. (isStartupLengthSet): New variable. (avgSpeed): New variable. (eta): New variable. * src/ConsoleDownloadEngine.cc (sendStatistics): Added a readout of estimated remaining time. (initStatistics): Initialized newly added variables. (calculateStatistics): Calculate average speed and estimated remaining time. To decouple TorrentDownloadEngine from HttpResponseCommand: * src/TrackerDownloadCommand.h: Removed. * src/TrackerDownloadCommand.cc: Removed. * src/TrackerInitCommand.h: Removed. * src/TrackerInitCommand.cc: Removed. * src/TrackerUpdateCommand.h: Removed. * src/TrackerUpdateCommand.cc: Removed. * src/TrackerWatcherCommand.cc (execute): The construction of request url written in TrackerInitCommand was moved here. Do not create tracker request command if torretnMan->trackers != 0. * src/CompactTrackerResponseProcessor.h: New class. * src/CompactTrackerResponseProcessor.cc: New class. * src/message.h (MSG_TRACKER_WARNING_MESSAGE): Updated. * src/HttpResponseCommand.cc (createHttpDownloadCommand): Decoupled TorrentDownloadEngine from this. * src/SegmentMan.h (init): New function. * src/SegmentMan.cc (init): New function. * src/TorrentMan.h (responseProcessor): New variable. (trackers): New variable. (setTrackerResponseProcessor): New function. (getTrackerResponseProcessor): New function. (processTrackerResponse): New function. * src/TorrentMan.cc (Constructor): Initialized new variable trackers. (processTrackerResponse): New function. * src/main.cc (main): Use ByteArrayDiskWriter and CompactTrackerResponseProcessor. * src/TorrentDownloadEngine.cc (afterEachIteration): Call torrentMan-> processTrackerResponse(). * src/TorrentConsoleDownloadEngine.cc (printStatistics): Updated a readout. * src/TorrentDownloadEngine.cc (afterEachIteration): Added log message which indicates download has completed. * src/AbstractDiskWriter.cc (Destructor): fd >= 0, not fd >0 (closeFile): fd >= 0, not fd > 0 * src/main.cc (main): Added short cut for show-files. Added short cut for torrent-file. Added new command-line option listen-port. Updated i18n messages.
2006-04-18 17:06:17 +00:00
if(enc != NULL) {
command->transferEncoding = transferEncoding;
enc->init();
}
2006-02-17 13:35:04 +00:00
e->commands.push(command);
}
}
void HttpResponseCommand::retrieveCookie(const HttpHeader& headers) {
Strings v = headers.get("Set-Cookie");
for(Strings::const_iterator itr = v.begin(); itr != v.end(); itr++) {
2006-02-17 13:35:04 +00:00
Cookie c;
2006-02-21 12:27:17 +00:00
req->cookieBox->parse(c, *itr);
2006-02-17 13:35:04 +00:00
req->cookieBox->add(c);
}
}