2006-02-17 13:35:04 +00:00
|
|
|
/* <!-- copyright */
|
|
|
|
/*
|
2006-09-21 15:31:24 +00:00
|
|
|
* aria2 - The high speed download utility
|
2006-02-17 13:35:04 +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-17 13:35:04 +00:00
|
|
|
*/
|
|
|
|
/* copyright --> */
|
|
|
|
#include "Request.h"
|
2008-10-08 15:35:52 +00:00
|
|
|
|
|
|
|
#include <utility>
|
|
|
|
|
2009-10-22 15:35:33 +00:00
|
|
|
#include "util.h"
|
2006-07-30 12:58:27 +00:00
|
|
|
#include "FeatureConfig.h"
|
2008-04-26 05:58:49 +00:00
|
|
|
#include "StringFormat.h"
|
2008-05-13 14:15:23 +00:00
|
|
|
#include "A2STR.h"
|
2009-06-06 12:33:07 +00:00
|
|
|
#include "a2functional.h"
|
2008-10-08 15:35:52 +00:00
|
|
|
|
2008-02-08 15:53:45 +00:00
|
|
|
namespace aria2 {
|
2007-01-23 Tatsuhiro Tsujikawa <tujikawa at rednoah dot com>
To add chunk checksum validation:
* src/MetalinkEntry.h
(MetalinkChunkChecksum.h): New include.
(chunkChecksum): New variable.
* src/Request.h
(method): New variable.
(setMethod): New function.
(getMethod): New function.
(METHOD_GET): New static constant.
(METHOD_HEAD): New static constant.
* src/Xml2MetalinkProcessor.h
(getPieceHash): New function.
* src/PieceStorage.h
(markAllPiecesDone): New function.
(checkIntegrity): New function.
* src/FileAllocator.h
(NullFileAllocationMonitor.h): New include.
(FileAllocator): Initialize fileAllocationMonitor with new
NullFileAllocationMonitor().
* src/MultiDiskAdaptor.h
(messageDigest.h): Remove include.
(ctx): Removed.
(hashUpdate): Added ctx.
(MultiDiskAdaptor): Removed ctx.
(sha1Sum): Renamed as messageDigest.
(messageDigest): New function.
* src/UrlRequestInfo.h
(HeadResult): New class.
(digestAlgo): New variable.
(chunkChecksumLength): New variable.
(chunkChecksums): New variable.
(getHeadResult): New function.
(UrlRequestInfo): Added digestAlgo, chunkChecksumLength.
(setDigestAlgo): New function.
(setChunkChecksumLength): New function.
(setChunkChecksums): New function.
* src/DefaultPieceStorage.cc
(DiskAdaptorWriter.h): New include.
(ChunkChecksumValidator.h): New include.
(markAllPiecesDone): New function.
(checkIntegrity): New function.
* src/DefaultBtContext.h
(getPieceHashes): New function.
* src/TorrentRequestInfo.cc
(execute): Try to validate chunk checksum if file already exists
and
.aria2 file doesn't there and user allows aria2 to overwrite it.
* src/messageDigest.h
(~MessageDigestContext): Added digestFree().
* src/MetalinkRequestInfo.cc
(execute): Set digestAlgo, chunkChecksum, chunkChecksums to
reqInfo.
* src/DiskAdaptor.h
(messageDigest.h): New include.
(sha1Sum): Renamed as messageDigest.
(messageDigest): New function.
* src/DownloadCommand.h
(PeerStat.h): New include.
(maxDownloadSpeedLimit): New variable.
(startupIdleTime): New variable.
(lowestDownloadSpeedLimit): New variable.
(peerStat): New variable.
(setMaxDownloadSpeedLimit): New function.
(setStartupIdleTime): New function.
(setLowestDownloadSPeedLimit): New function.
* src/BtContext.h
(getPieceHashes): New function.
* src/main.cc
(main): Set PREF_REALTIME_CHUNK_CHECKSUM and
PREF_CHECK_INTEGRITY
option to true for testing purpose.
* src/BtPieceMessage.cc
(checkPieceHash): Use messageDigest
* src/DownloadEngine.cc
(SetDescriptor): Removed.
(AccumulateActiveCommand): Removed.
(waitData): Rewritten.
(updateFdSet): Rewritten.
* src/MultiDiskAdaptor.cc
(hashUpdate): Added ctx.
(sha1Sum): Renamed as messageDigest.
(messageDigest): New function.
* src/BitfieldMan.h
(isBitRangeSet): New function.
(unsetBitRange): New function.
* src/ByteArrayDiskWriter.h
(sha1Sum): Renamed as messageDigest.
(messageDigest): New function.
* src/ConsoleDownloadEngine.cc
(calculateStatistics): If nspeed < 0 then set nspeed to 0.
* src/DiskWriter.h
(messageDigest.h): New include.
(sha1Sum): Renamed as messageDigest.
(messageDigest): New function.
* src/ChunkChecksumValidator.h: New class.
* src/DiskAdaptorWriter.h: New class.
* src/prefs.h
(PREF_REALTIME_CHUNK_CHECKSUM): New definition.
(PREF_CHECK_INTEGRITY): New definition.
* src/HttpResponseCommand.cc
(handleDefaultEncoding): Added method "HEAD" handling.
Removed the call to
e->segmentMan->shouldCancelDownloadForSafety().
(handleOtherEncoding):
Added the call to
e->segmentMan->shouldCancelDownloadForSafety().
(createHttpDownloadCommand): Set maxDownloadSpeedLimit,
startupIdleTime, lowestDownloadSpeedLimit to command.
* src/SegmentMan.h
(getSegmentEntryByIndex): New function.
(getSegmentEntryByCuid): New function.
(getSegmentEntryIteratorByCuid): New function.
(diskWriter): DiskWriter -> DiskWriterHandle
(pieceHashes): New variable.
(chunkHashLength): New variable.
(digestAlgo): New variable.
(FindPeerStat): Removed.
(getPeerStat): Rewritten.
(markAllPiecesDone): New function.
(checkIntegrity): New function.
(tryChunkChecksumValidation): New function.
(isChunkChecksumValidationReady): New function.
* src/BitfieldMan.cc
(BitfieldMan): Initialized bitfieldLength, blocks to 0.
(BitfieldMan): Initialized blockLength, totalLength,
bitfieldLength,
blocks to 0.
(isBitRangeSet): New function.
(unsetBitRange): New function.
* src/FtpNegotiateCommand.cc
(executeInternal): Set maxDownloadSpeedLimit,
startupIdleTime, lowestDownloadSpeedLimit to command.
(recvSize): Added method "HEAD" handling.
Removed the call to
e->segmentMan->shouldCancelDownloadForSafety().
* src/AbstractSingleDiskAdaptor.cc
(sha1Sum): Renamed as messageDigest.
(messageDigest): New function.
* src/AbstractSingleDiskAdaptor.h
(sha1Sum): Renamed as messageDigest.
(messageDigest): New function.
* src/Util.h
(indexRange): New function.
* src/MetalinkEntry.cc
(MetalinkEntry): Initialized chunkChecksum to 0.
* src/ShaVisitor.cc
(~ShaVisitor): Removed the call to ctx.digestFree().
* src/SegmentMan.cc
(ChunkChecksumValidator.h): New include.
(SegmentMan): Initialized chunkHashLength to 0. Initialized
digestAlgo
to DIGEST_ALGO_SHA1.
(~SegmentMan): Removed diskWriter.
(FindSegmentEntryByIndex): Removed.
(FindSegmentEntryByCuid): Removed.
(checkoutSegment): Rewritten.
(findSlowerSegmentEntry): Rewritten.
(getSegment): Rewritten.
(updateSegment): Rewritten.
(completeSegment): Rewritten.
(markAllPiecesDone): New function.
(checkIntegrity): New function.
(isChunkChecksumValidationReady): New function.
(tryChunkChecksumValidation): New function.
* src/Xml2MetalinkProcessor.cc
(getEntry): Get size and set it to entry.
Get chunk checksum and set it to entry.
(getPieceHash): New function.
* src/Util.cc
(sha1Sum): Removed ctx.digestFree()
(fileChecksum): Removed ctx.digestFree()
(indexRange): New function.
* src/Request.cc
(METHOD_GET): New variable.
(METHOD_HEAD): New variable.
(Request): Added method.
* src/UrlRequestInfo.cc
(FatalException.h): New include.
(message.h): New include.
(operator<<): Added operator<< for class HeadResult.
(getHeadResult): New function.
(execute): Get filename and size in separate download engine.
* src/ChunkChecksumValidator.cc: New class.
* src/DownloadCommand.cc:
(DownloadCommand): Added peerStat.
(executeInternal): Use maxDownloadSpeedLimit member instead of
getting
the value from Option.
The buffer size is now 16KB.
Use peerStat member instead of getting it from SegmentMan.
Use startupIdleTime member instead of gettingit from Option.
Added chunk checksum validation.
* src/AbstractDiskWriter.cc
(AbstractDiskWriter): Removed ctx.
(~AbstractDiskWriter): Removed ctx.digestFree()
(writeDataInternal): Returns the return value of write.
(readDataInternal): Returns the return value of read.
(sha1Sum): Renamed as messageDigest
(messageDigest): New function.
* src/AbstractDiwkWriter.h
(messageDigest.h): Removed include.
(ctx): Removed.
(sha1Sum): Renamed as messageDigest
(messageDigest): New function.
* src/DefaultPieceStorage.h
(markAllPiecesDone): New function.
(checkIntegrity): New function.
* src/NullFileAllocationMonitor.h: New class.
2007-01-24 15:55:34 +00:00
|
|
|
|
2008-04-25 16:24:34 +00:00
|
|
|
const std::string Request::METHOD_GET = "GET";
|
2008-02-08 15:53:45 +00:00
|
|
|
|
2008-04-25 16:24:34 +00:00
|
|
|
const std::string Request::METHOD_HEAD = "HEAD";
|
2007-01-23 Tatsuhiro Tsujikawa <tujikawa at rednoah dot com>
To add chunk checksum validation:
* src/MetalinkEntry.h
(MetalinkChunkChecksum.h): New include.
(chunkChecksum): New variable.
* src/Request.h
(method): New variable.
(setMethod): New function.
(getMethod): New function.
(METHOD_GET): New static constant.
(METHOD_HEAD): New static constant.
* src/Xml2MetalinkProcessor.h
(getPieceHash): New function.
* src/PieceStorage.h
(markAllPiecesDone): New function.
(checkIntegrity): New function.
* src/FileAllocator.h
(NullFileAllocationMonitor.h): New include.
(FileAllocator): Initialize fileAllocationMonitor with new
NullFileAllocationMonitor().
* src/MultiDiskAdaptor.h
(messageDigest.h): Remove include.
(ctx): Removed.
(hashUpdate): Added ctx.
(MultiDiskAdaptor): Removed ctx.
(sha1Sum): Renamed as messageDigest.
(messageDigest): New function.
* src/UrlRequestInfo.h
(HeadResult): New class.
(digestAlgo): New variable.
(chunkChecksumLength): New variable.
(chunkChecksums): New variable.
(getHeadResult): New function.
(UrlRequestInfo): Added digestAlgo, chunkChecksumLength.
(setDigestAlgo): New function.
(setChunkChecksumLength): New function.
(setChunkChecksums): New function.
* src/DefaultPieceStorage.cc
(DiskAdaptorWriter.h): New include.
(ChunkChecksumValidator.h): New include.
(markAllPiecesDone): New function.
(checkIntegrity): New function.
* src/DefaultBtContext.h
(getPieceHashes): New function.
* src/TorrentRequestInfo.cc
(execute): Try to validate chunk checksum if file already exists
and
.aria2 file doesn't there and user allows aria2 to overwrite it.
* src/messageDigest.h
(~MessageDigestContext): Added digestFree().
* src/MetalinkRequestInfo.cc
(execute): Set digestAlgo, chunkChecksum, chunkChecksums to
reqInfo.
* src/DiskAdaptor.h
(messageDigest.h): New include.
(sha1Sum): Renamed as messageDigest.
(messageDigest): New function.
* src/DownloadCommand.h
(PeerStat.h): New include.
(maxDownloadSpeedLimit): New variable.
(startupIdleTime): New variable.
(lowestDownloadSpeedLimit): New variable.
(peerStat): New variable.
(setMaxDownloadSpeedLimit): New function.
(setStartupIdleTime): New function.
(setLowestDownloadSPeedLimit): New function.
* src/BtContext.h
(getPieceHashes): New function.
* src/main.cc
(main): Set PREF_REALTIME_CHUNK_CHECKSUM and
PREF_CHECK_INTEGRITY
option to true for testing purpose.
* src/BtPieceMessage.cc
(checkPieceHash): Use messageDigest
* src/DownloadEngine.cc
(SetDescriptor): Removed.
(AccumulateActiveCommand): Removed.
(waitData): Rewritten.
(updateFdSet): Rewritten.
* src/MultiDiskAdaptor.cc
(hashUpdate): Added ctx.
(sha1Sum): Renamed as messageDigest.
(messageDigest): New function.
* src/BitfieldMan.h
(isBitRangeSet): New function.
(unsetBitRange): New function.
* src/ByteArrayDiskWriter.h
(sha1Sum): Renamed as messageDigest.
(messageDigest): New function.
* src/ConsoleDownloadEngine.cc
(calculateStatistics): If nspeed < 0 then set nspeed to 0.
* src/DiskWriter.h
(messageDigest.h): New include.
(sha1Sum): Renamed as messageDigest.
(messageDigest): New function.
* src/ChunkChecksumValidator.h: New class.
* src/DiskAdaptorWriter.h: New class.
* src/prefs.h
(PREF_REALTIME_CHUNK_CHECKSUM): New definition.
(PREF_CHECK_INTEGRITY): New definition.
* src/HttpResponseCommand.cc
(handleDefaultEncoding): Added method "HEAD" handling.
Removed the call to
e->segmentMan->shouldCancelDownloadForSafety().
(handleOtherEncoding):
Added the call to
e->segmentMan->shouldCancelDownloadForSafety().
(createHttpDownloadCommand): Set maxDownloadSpeedLimit,
startupIdleTime, lowestDownloadSpeedLimit to command.
* src/SegmentMan.h
(getSegmentEntryByIndex): New function.
(getSegmentEntryByCuid): New function.
(getSegmentEntryIteratorByCuid): New function.
(diskWriter): DiskWriter -> DiskWriterHandle
(pieceHashes): New variable.
(chunkHashLength): New variable.
(digestAlgo): New variable.
(FindPeerStat): Removed.
(getPeerStat): Rewritten.
(markAllPiecesDone): New function.
(checkIntegrity): New function.
(tryChunkChecksumValidation): New function.
(isChunkChecksumValidationReady): New function.
* src/BitfieldMan.cc
(BitfieldMan): Initialized bitfieldLength, blocks to 0.
(BitfieldMan): Initialized blockLength, totalLength,
bitfieldLength,
blocks to 0.
(isBitRangeSet): New function.
(unsetBitRange): New function.
* src/FtpNegotiateCommand.cc
(executeInternal): Set maxDownloadSpeedLimit,
startupIdleTime, lowestDownloadSpeedLimit to command.
(recvSize): Added method "HEAD" handling.
Removed the call to
e->segmentMan->shouldCancelDownloadForSafety().
* src/AbstractSingleDiskAdaptor.cc
(sha1Sum): Renamed as messageDigest.
(messageDigest): New function.
* src/AbstractSingleDiskAdaptor.h
(sha1Sum): Renamed as messageDigest.
(messageDigest): New function.
* src/Util.h
(indexRange): New function.
* src/MetalinkEntry.cc
(MetalinkEntry): Initialized chunkChecksum to 0.
* src/ShaVisitor.cc
(~ShaVisitor): Removed the call to ctx.digestFree().
* src/SegmentMan.cc
(ChunkChecksumValidator.h): New include.
(SegmentMan): Initialized chunkHashLength to 0. Initialized
digestAlgo
to DIGEST_ALGO_SHA1.
(~SegmentMan): Removed diskWriter.
(FindSegmentEntryByIndex): Removed.
(FindSegmentEntryByCuid): Removed.
(checkoutSegment): Rewritten.
(findSlowerSegmentEntry): Rewritten.
(getSegment): Rewritten.
(updateSegment): Rewritten.
(completeSegment): Rewritten.
(markAllPiecesDone): New function.
(checkIntegrity): New function.
(isChunkChecksumValidationReady): New function.
(tryChunkChecksumValidation): New function.
* src/Xml2MetalinkProcessor.cc
(getEntry): Get size and set it to entry.
Get chunk checksum and set it to entry.
(getPieceHash): New function.
* src/Util.cc
(sha1Sum): Removed ctx.digestFree()
(fileChecksum): Removed ctx.digestFree()
(indexRange): New function.
* src/Request.cc
(METHOD_GET): New variable.
(METHOD_HEAD): New variable.
(Request): Added method.
* src/UrlRequestInfo.cc
(FatalException.h): New include.
(message.h): New include.
(operator<<): Added operator<< for class HeadResult.
(getHeadResult): New function.
(execute): Get filename and size in separate download engine.
* src/ChunkChecksumValidator.cc: New class.
* src/DownloadCommand.cc:
(DownloadCommand): Added peerStat.
(executeInternal): Use maxDownloadSpeedLimit member instead of
getting
the value from Option.
The buffer size is now 16KB.
Use peerStat member instead of getting it from SegmentMan.
Use startupIdleTime member instead of gettingit from Option.
Added chunk checksum validation.
* src/AbstractDiskWriter.cc
(AbstractDiskWriter): Removed ctx.
(~AbstractDiskWriter): Removed ctx.digestFree()
(writeDataInternal): Returns the return value of write.
(readDataInternal): Returns the return value of read.
(sha1Sum): Renamed as messageDigest
(messageDigest): New function.
* src/AbstractDiwkWriter.h
(messageDigest.h): Removed include.
(ctx): Removed.
(sha1Sum): Renamed as messageDigest
(messageDigest): New function.
* src/DefaultPieceStorage.h
(markAllPiecesDone): New function.
(checkIntegrity): New function.
* src/NullFileAllocationMonitor.h: New class.
2007-01-24 15:55:34 +00:00
|
|
|
|
2008-05-13 14:15:23 +00:00
|
|
|
const std::string Request::PROTO_HTTP("http");
|
|
|
|
|
|
|
|
const std::string Request::PROTO_HTTPS("https");
|
|
|
|
|
|
|
|
const std::string Request::PROTO_FTP("ftp");
|
|
|
|
|
2008-04-22 07:15:35 +00:00
|
|
|
Request::Request():
|
2010-06-21 13:51:56 +00:00
|
|
|
port_(0), tryCount_(0),
|
|
|
|
redirectCount_(0),
|
|
|
|
supportsPersistentConnection_(true),
|
|
|
|
keepAliveHint_(false),
|
|
|
|
pipeliningHint_(false),
|
|
|
|
maxPipelinedRequest_(1),
|
|
|
|
method_(METHOD_GET),
|
|
|
|
hasPassword_(false),
|
|
|
|
ipv6LiteralAddress_(false),
|
|
|
|
removalRequested_(false)
|
2008-04-22 07:15:35 +00:00
|
|
|
{}
|
2006-02-17 13:35:04 +00:00
|
|
|
|
2010-03-07 10:36:42 +00:00
|
|
|
static std::string removeFragment(const std::string& uri)
|
2008-10-08 15:35:52 +00:00
|
|
|
{
|
2010-03-07 10:36:42 +00:00
|
|
|
std::string::size_type sharpIndex = uri.find("#");
|
2008-10-08 15:35:52 +00:00
|
|
|
if(sharpIndex == std::string::npos) {
|
2010-03-07 10:36:42 +00:00
|
|
|
return uri;
|
2008-10-08 15:35:52 +00:00
|
|
|
} else {
|
2010-03-07 10:36:42 +00:00
|
|
|
return uri.substr(0, sharpIndex);
|
2008-10-08 15:35:52 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
static bool isHexNumber(const char c)
|
|
|
|
{
|
|
|
|
return ('0' <= c && c <= '9') || ('A' <= c && c <= 'F') ||
|
|
|
|
('a' <= c && c <= 'f');
|
|
|
|
}
|
|
|
|
|
2010-03-07 10:36:42 +00:00
|
|
|
static std::string percentEncode(const std::string& src)
|
2008-10-08 15:35:52 +00:00
|
|
|
{
|
|
|
|
std::string result = src;
|
|
|
|
if(src.empty()) {
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
result += " ";
|
2008-10-08 16:06:27 +00:00
|
|
|
for(int index = src.size()-1; index >= 0; --index) {
|
2008-10-08 15:35:52 +00:00
|
|
|
const unsigned char c = result[index];
|
2010-03-07 10:36:42 +00:00
|
|
|
// '/' is not percent encoded because src is expected to be a path.
|
2009-10-22 15:09:00 +00:00
|
|
|
if(!util::inRFC3986ReservedChars(c) && !util::inRFC3986UnreservedChars(c)) {
|
2008-10-08 15:35:52 +00:00
|
|
|
if(c == '%') {
|
2010-01-05 16:01:46 +00:00
|
|
|
if(!isHexNumber(result[index+1]) || !isHexNumber(result[index+2])) {
|
|
|
|
result.replace(index, 1, "%25");
|
|
|
|
}
|
2008-10-08 15:35:52 +00:00
|
|
|
} else {
|
2010-01-05 16:01:46 +00:00
|
|
|
result.replace(index, 1, StringFormat("%%%02X", c).str());
|
2008-10-08 15:35:52 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
result.erase(result.size()-2);
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
2010-03-07 10:36:42 +00:00
|
|
|
bool Request::setUri(const std::string& uri) {
|
2010-06-21 13:51:56 +00:00
|
|
|
supportsPersistentConnection_ = true;
|
|
|
|
uri_ = uri;
|
|
|
|
return parseUri(uri_);
|
2006-02-17 13:35:04 +00:00
|
|
|
}
|
|
|
|
|
2010-03-07 10:36:42 +00:00
|
|
|
bool Request::resetUri() {
|
2010-06-21 13:51:56 +00:00
|
|
|
previousUri_ = referer_;
|
|
|
|
supportsPersistentConnection_ = true;
|
|
|
|
return parseUri(uri_);
|
2006-02-17 13:35:04 +00:00
|
|
|
}
|
|
|
|
|
2010-03-07 10:36:42 +00:00
|
|
|
void Request::setReferer(const std::string& uri)
|
2008-10-08 15:35:52 +00:00
|
|
|
{
|
2010-06-21 13:51:56 +00:00
|
|
|
referer_ = previousUri_ = percentEncode(removeFragment(uri));
|
2008-10-08 15:35:52 +00:00
|
|
|
}
|
|
|
|
|
2010-03-07 10:36:42 +00:00
|
|
|
bool Request::redirectUri(const std::string& uri) {
|
2010-06-21 13:51:56 +00:00
|
|
|
supportsPersistentConnection_ = true;
|
|
|
|
++redirectCount_;
|
2010-03-07 10:36:42 +00:00
|
|
|
std::string redirectedUri;
|
|
|
|
if(uri.find("://") == std::string::npos) {
|
2008-07-18 15:20:52 +00:00
|
|
|
// rfc2616 requires absolute URI should be provided by Location header
|
|
|
|
// field, but some servers don't obey this rule.
|
2010-03-07 10:36:42 +00:00
|
|
|
if(util::startsWith(uri, "/")) {
|
2008-07-18 15:20:52 +00:00
|
|
|
// abosulute path
|
2010-06-21 13:51:56 +00:00
|
|
|
redirectedUri = strconcat(protocol_, "://", host_, uri);
|
2008-07-18 15:20:52 +00:00
|
|
|
} else {
|
|
|
|
// relative path
|
2010-06-21 13:51:56 +00:00
|
|
|
redirectedUri = strconcat(protocol_, "://", host_, dir_, "/", uri);
|
2008-07-18 15:20:52 +00:00
|
|
|
}
|
|
|
|
} else {
|
2010-03-07 10:36:42 +00:00
|
|
|
redirectedUri = uri;
|
2008-07-18 15:20:52 +00:00
|
|
|
}
|
2010-03-07 10:36:42 +00:00
|
|
|
return parseUri(redirectedUri);
|
2006-02-17 13:35:04 +00:00
|
|
|
}
|
|
|
|
|
2010-03-07 10:36:42 +00:00
|
|
|
bool Request::parseUri(const std::string& srcUri) {
|
|
|
|
const std::string uri = percentEncode(removeFragment(srcUri));
|
2010-06-21 13:51:56 +00:00
|
|
|
currentUri_ = uri;
|
|
|
|
host_ = A2STR::NIL;
|
|
|
|
port_ = 0;
|
|
|
|
dir_ = A2STR::NIL;
|
|
|
|
file_ = A2STR::NIL;
|
|
|
|
query_ = A2STR::NIL;
|
|
|
|
username_ = A2STR::NIL;
|
|
|
|
password_ = A2STR::NIL;
|
|
|
|
hasPassword_ = false;
|
|
|
|
ipv6LiteralAddress_ = false;
|
2009-11-12 14:50:23 +00:00
|
|
|
|
|
|
|
// http://user:password@aria2.sourceforge.net:80/dir/file?query
|
|
|
|
// | || || | | |
|
|
|
|
// | || hostLast| | | |
|
|
|
|
// | || portFirst| | |
|
|
|
|
// authorityFirst || authorityLast | |
|
|
|
|
// || | | |
|
|
|
|
// userInfoLast | | |
|
|
|
|
// | | | |
|
|
|
|
// hostPortFirst | | |
|
|
|
|
// | | |
|
|
|
|
// dirFirst dirLast|
|
|
|
|
// |
|
|
|
|
// queryFirst
|
|
|
|
|
2007-10-30 12:48:01 +00:00
|
|
|
// find query part
|
2010-03-07 10:36:42 +00:00
|
|
|
std::string::const_iterator queryFirst = uri.begin();
|
|
|
|
for(; queryFirst != uri.end(); ++queryFirst) {
|
2009-11-12 14:50:23 +00:00
|
|
|
if(*queryFirst == '?') break;
|
2006-06-18 09:23:25 +00:00
|
|
|
}
|
2010-06-21 13:51:56 +00:00
|
|
|
query_ = std::string(queryFirst, uri.end());
|
2007-10-30 12:48:01 +00:00
|
|
|
// find protocol
|
2010-03-07 10:36:42 +00:00
|
|
|
std::string::size_type protocolOffset = uri.find("://");
|
2009-11-12 14:50:23 +00:00
|
|
|
if(protocolOffset == std::string::npos) return false;
|
2010-06-21 13:51:56 +00:00
|
|
|
protocol_ = std::string(uri.begin(), uri.begin()+protocolOffset);
|
2008-03-09 12:24:01 +00:00
|
|
|
uint16_t defPort;
|
2010-06-21 13:51:56 +00:00
|
|
|
if((defPort = FeatureConfig::getInstance()->getDefaultPort(protocol_)) == 0) {
|
2006-02-17 13:35:04 +00:00
|
|
|
return false;
|
|
|
|
}
|
2009-11-12 14:50:23 +00:00
|
|
|
// find authority
|
2010-03-07 10:36:42 +00:00
|
|
|
std::string::const_iterator authorityFirst = uri.begin()+protocolOffset+3;
|
2009-11-12 14:50:23 +00:00
|
|
|
std::string::const_iterator authorityLast = authorityFirst;
|
|
|
|
for(; authorityLast != queryFirst; ++authorityLast) {
|
|
|
|
if(*authorityLast == '/') break;
|
2006-02-17 13:35:04 +00:00
|
|
|
}
|
2009-11-12 14:50:23 +00:00
|
|
|
if(authorityFirst == authorityLast) {
|
|
|
|
// No authority found
|
|
|
|
return false;
|
2007-10-30 12:48:01 +00:00
|
|
|
}
|
2009-11-12 14:50:23 +00:00
|
|
|
// find userinfo(username and password) in authority if they exist
|
2010-09-08 14:35:30 +00:00
|
|
|
std::string::const_iterator userInfoLast = authorityLast;
|
2009-11-12 14:50:23 +00:00
|
|
|
std::string::const_iterator hostPortFirst = authorityFirst;
|
2010-09-08 14:35:30 +00:00
|
|
|
for(; userInfoLast != authorityFirst-1; --userInfoLast) {
|
2009-11-12 14:50:23 +00:00
|
|
|
if(*userInfoLast == '@') {
|
|
|
|
hostPortFirst = userInfoLast;
|
|
|
|
++hostPortFirst;
|
|
|
|
std::string::const_iterator userLast = authorityFirst;
|
|
|
|
for(; userLast != userInfoLast; ++userLast) {
|
2010-01-05 16:01:46 +00:00
|
|
|
if(*userLast == ':') {
|
2010-06-21 13:51:56 +00:00
|
|
|
password_ = util::percentDecode(std::string(userLast+1,userInfoLast));
|
|
|
|
hasPassword_ = true;
|
2010-01-05 16:01:46 +00:00
|
|
|
break;
|
|
|
|
}
|
2008-11-04 14:08:26 +00:00
|
|
|
}
|
2010-06-21 13:51:56 +00:00
|
|
|
username_ = util::percentDecode(std::string(authorityFirst, userLast));
|
2009-11-12 14:50:23 +00:00
|
|
|
break;
|
2009-08-30 15:05:30 +00:00
|
|
|
}
|
2009-11-12 14:50:23 +00:00
|
|
|
}
|
|
|
|
std::string::const_iterator hostLast = hostPortFirst;
|
|
|
|
std::string::const_iterator portFirst = authorityLast;
|
|
|
|
if(*hostPortFirst == '[') {
|
|
|
|
// Detected IPv6 literal address in square brackets
|
|
|
|
for(; hostLast != authorityLast; ++hostLast) {
|
|
|
|
if(*hostLast == ']') {
|
2010-01-05 16:01:46 +00:00
|
|
|
++hostLast;
|
|
|
|
if(hostLast == authorityLast) {
|
2010-06-21 13:51:56 +00:00
|
|
|
ipv6LiteralAddress_ = true;
|
2010-01-05 16:01:46 +00:00
|
|
|
} else {
|
|
|
|
if(*hostLast == ':') {
|
|
|
|
portFirst = hostLast;
|
|
|
|
++portFirst;
|
2010-06-21 13:51:56 +00:00
|
|
|
ipv6LiteralAddress_ = true;
|
2010-01-05 16:01:46 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
break;
|
2009-08-30 15:05:30 +00:00
|
|
|
}
|
|
|
|
}
|
2010-06-21 13:51:56 +00:00
|
|
|
if(!ipv6LiteralAddress_) {
|
2009-11-12 14:50:23 +00:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
for(; hostLast != authorityLast; ++hostLast) {
|
|
|
|
if(*hostLast == ':') {
|
2010-01-05 16:01:46 +00:00
|
|
|
portFirst = hostLast;
|
|
|
|
++portFirst;
|
|
|
|
break;
|
2009-11-12 14:50:23 +00:00
|
|
|
}
|
2006-02-17 13:35:04 +00:00
|
|
|
}
|
|
|
|
}
|
2009-11-12 14:50:23 +00:00
|
|
|
if(hostPortFirst == hostLast) {
|
|
|
|
// No host
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
if(portFirst == authorityLast) {
|
|
|
|
// If port is not specified, then we set it to default port of
|
|
|
|
// its protocol..
|
2010-06-21 13:51:56 +00:00
|
|
|
port_ = defPort;
|
2006-02-17 13:35:04 +00:00
|
|
|
} else {
|
2009-11-12 15:34:55 +00:00
|
|
|
uint32_t tempPort;
|
|
|
|
if(util::parseUIntNoThrow(tempPort, std::string(portFirst, authorityLast))){
|
2009-11-12 14:50:23 +00:00
|
|
|
if(65535 < tempPort) {
|
2010-01-05 16:01:46 +00:00
|
|
|
return false;
|
2009-11-12 14:50:23 +00:00
|
|
|
}
|
2010-06-21 13:51:56 +00:00
|
|
|
port_ = tempPort;
|
2009-11-12 15:34:55 +00:00
|
|
|
} else {
|
2009-11-12 14:50:23 +00:00
|
|
|
return false;
|
2007-10-14 16:29:05 +00:00
|
|
|
}
|
2009-11-12 14:50:23 +00:00
|
|
|
}
|
2010-06-21 13:51:56 +00:00
|
|
|
if(ipv6LiteralAddress_) {
|
|
|
|
host_ = std::string(hostPortFirst+1, hostLast-1);
|
2009-11-12 14:50:23 +00:00
|
|
|
} else {
|
2010-06-21 13:51:56 +00:00
|
|
|
host_ = std::string(hostPortFirst, hostLast);
|
2009-11-12 14:50:23 +00:00
|
|
|
}
|
|
|
|
// find directory and file part
|
|
|
|
std::string::const_iterator dirLast = authorityLast;
|
|
|
|
for(std::string::const_iterator i = authorityLast;
|
|
|
|
i != queryFirst; ++i) {
|
|
|
|
if(*i == '/') {
|
|
|
|
dirLast = i;
|
2007-10-14 16:29:05 +00:00
|
|
|
}
|
2006-02-17 13:35:04 +00:00
|
|
|
}
|
2009-11-12 14:50:23 +00:00
|
|
|
if(dirLast != queryFirst) {
|
2010-06-21 13:51:56 +00:00
|
|
|
file_ = std::string(dirLast+1, queryFirst);
|
2009-11-12 14:50:23 +00:00
|
|
|
}
|
|
|
|
// Erase duplicated slashes.
|
|
|
|
std::string::const_iterator dirFirst = authorityLast;
|
|
|
|
for(; dirFirst != dirLast; ++dirFirst) {
|
|
|
|
if(*dirFirst != '/') {
|
|
|
|
--dirFirst;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
for(; dirLast != dirFirst; --dirLast) {
|
|
|
|
if(*dirLast != '/') {
|
|
|
|
++dirLast;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if(dirFirst == dirLast) {
|
2010-06-21 13:51:56 +00:00
|
|
|
dir_ = A2STR::SLASH_C;
|
2009-11-12 14:50:23 +00:00
|
|
|
} else {
|
2010-06-21 13:51:56 +00:00
|
|
|
dir_ = std::string(dirFirst, dirLast);
|
2006-02-17 13:35:04 +00:00
|
|
|
}
|
|
|
|
return true;
|
|
|
|
}
|
2007-11-11 04:25:56 +00:00
|
|
|
|
2008-06-17 09:09:31 +00:00
|
|
|
void Request::resetRedirectCount()
|
|
|
|
{
|
2010-06-21 13:51:56 +00:00
|
|
|
redirectCount_ = 0;
|
2008-06-17 09:09:31 +00:00
|
|
|
}
|
|
|
|
|
2008-12-15 15:38:07 +00:00
|
|
|
void Request::setMaxPipelinedRequest(unsigned int num)
|
|
|
|
{
|
2010-06-21 13:51:56 +00:00
|
|
|
maxPipelinedRequest_ = num;
|
2008-12-15 15:38:07 +00:00
|
|
|
}
|
|
|
|
|
2009-06-29 15:18:21 +00:00
|
|
|
const SharedHandle<PeerStat>& Request::initPeerStat()
|
|
|
|
{
|
2009-11-26 13:09:15 +00:00
|
|
|
// Use host and protocol in original URI, because URI selector
|
|
|
|
// selects URI based on original URI, not redirected one.
|
|
|
|
Request origReq;
|
2010-06-21 13:51:56 +00:00
|
|
|
origReq.setUri(uri_);
|
|
|
|
peerStat_.reset(new PeerStat(0, origReq.getHost(), origReq.getProtocol()));
|
|
|
|
return peerStat_;
|
2009-06-29 15:18:21 +00:00
|
|
|
}
|
|
|
|
|
2008-02-08 15:53:45 +00:00
|
|
|
} // namespace aria2
|