mirror of https://github.com/aria2/aria2
Dont' return SharedHandle from HttpHeader::getRange()
parent
492d6d1eeb
commit
e34d0e7ffa
|
@ -120,9 +120,8 @@ bool HttpDownloadCommand::prepareForNextSegment() {
|
|||
int64_t lastOffset =getFileEntry()->gtoloff
|
||||
(std::min(segment->getPosition()+segment->getLength(),
|
||||
getFileEntry()->getLastOffset()));
|
||||
|
||||
if(lastOffset ==
|
||||
httpResponse_->getHttpHeader()->getRange()->getEndByte()+1) {
|
||||
Range range = httpResponse_->getHttpHeader()->getRange();
|
||||
if(lastOffset == range.endByte + 1) {
|
||||
return prepareForRetry(0);
|
||||
}
|
||||
}
|
||||
|
@ -132,7 +131,7 @@ bool HttpDownloadCommand::prepareForNextSegment() {
|
|||
|
||||
int64_t HttpDownloadCommand::getRequestEndOffset() const
|
||||
{
|
||||
int64_t endByte = httpResponse_->getHttpHeader()->getRange()->getEndByte();
|
||||
int64_t endByte = httpResponse_->getHttpHeader()->getRange().endByte;
|
||||
if(endByte > 0) {
|
||||
return endByte+1;
|
||||
} else {
|
||||
|
|
|
@ -85,13 +85,13 @@ HttpHeader::equalRange(int hdKey) const
|
|||
return table_.equal_range(hdKey);
|
||||
}
|
||||
|
||||
SharedHandle<Range> HttpHeader::getRange() const
|
||||
Range HttpHeader::getRange() const
|
||||
{
|
||||
const std::string& rangeStr = find(CONTENT_RANGE);
|
||||
if(rangeStr.empty()) {
|
||||
const std::string& clenStr = find(CONTENT_LENGTH);
|
||||
if(clenStr.empty()) {
|
||||
return SharedHandle<Range>(new Range());
|
||||
return Range();
|
||||
} else {
|
||||
int64_t contentLength;
|
||||
if(!util::parseLLIntNoThrow(contentLength, clenStr) ||
|
||||
|
@ -101,10 +101,9 @@ SharedHandle<Range> HttpHeader::getRange() const
|
|||
throw DOWNLOAD_FAILURE_EXCEPTION
|
||||
(fmt(EX_TOO_LARGE_FILE, contentLength));
|
||||
} else if(contentLength == 0) {
|
||||
return SharedHandle<Range>(new Range());
|
||||
return Range();
|
||||
} else {
|
||||
return SharedHandle<Range>
|
||||
(new Range(0, contentLength-1, contentLength));
|
||||
return Range(0, contentLength - 1, contentLength);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@ -130,11 +129,11 @@ SharedHandle<Range> HttpHeader::getRange() const
|
|||
// If byte-range-resp-spec or instance-length is "*", we returns
|
||||
// empty Range. The former is usually sent with 416 (Request range
|
||||
// not satisfiable) status.
|
||||
return SharedHandle<Range>(new Range());
|
||||
return Range();
|
||||
}
|
||||
std::string::const_iterator minus = std::find(byteRangeSpec, slash, '-');
|
||||
if(minus == slash) {
|
||||
return SharedHandle<Range>(new Range());
|
||||
return Range();
|
||||
}
|
||||
int64_t startByte, endByte, entityLength;
|
||||
if(!util::parseLLIntNoThrow(startByte, std::string(byteRangeSpec, minus)) ||
|
||||
|
@ -153,7 +152,7 @@ SharedHandle<Range> HttpHeader::getRange() const
|
|||
if(entityLength > std::numeric_limits<off_t>::max()) {
|
||||
throw DOWNLOAD_FAILURE_EXCEPTION(fmt(EX_TOO_LARGE_FILE, entityLength));
|
||||
}
|
||||
return SharedHandle<Range>(new Range(startByte, endByte, entityLength));
|
||||
return Range(startByte, endByte, entityLength);
|
||||
}
|
||||
|
||||
void HttpHeader::setVersion(const std::string& version)
|
||||
|
|
|
@ -108,7 +108,7 @@ public:
|
|||
std::multimap<int, std::string>::const_iterator>
|
||||
equalRange(int hdKey) const;
|
||||
|
||||
SharedHandle<Range> getRange() const;
|
||||
Range getRange() const;
|
||||
|
||||
int getStatusCode() const;
|
||||
|
||||
|
|
|
@ -101,27 +101,26 @@ int64_t HttpRequest::getEndByte() const
|
|||
}
|
||||
}
|
||||
|
||||
SharedHandle<Range> HttpRequest::getRange() const
|
||||
Range HttpRequest::getRange() const
|
||||
{
|
||||
// content-length is always 0
|
||||
if(!segment_) {
|
||||
return SharedHandle<Range>(new Range());
|
||||
return Range();
|
||||
} else {
|
||||
return SharedHandle<Range>(new Range(getStartByte(), getEndByte(),
|
||||
fileEntry_->getLength()));
|
||||
return Range(getStartByte(), getEndByte(), fileEntry_->getLength());
|
||||
}
|
||||
}
|
||||
|
||||
bool HttpRequest::isRangeSatisfied(const SharedHandle<Range>& range) const
|
||||
bool HttpRequest::isRangeSatisfied(const Range& range) const
|
||||
{
|
||||
if(!segment_) {
|
||||
return true;
|
||||
}
|
||||
if((getStartByte() == range->getStartByte()) &&
|
||||
if((getStartByte() == range.startByte) &&
|
||||
((getEndByte() == 0) ||
|
||||
(getEndByte() == range->getEndByte())) &&
|
||||
(getEndByte() == range.endByte)) &&
|
||||
((fileEntry_->getLength() == 0) ||
|
||||
(fileEntry_->getLength() == range->getEntityLength()))) {
|
||||
(fileEntry_->getLength() == range.entityLength))) {
|
||||
return true;
|
||||
} else {
|
||||
return false;
|
||||
|
|
|
@ -128,13 +128,13 @@ public:
|
|||
|
||||
std::string getURIHost() const;
|
||||
|
||||
SharedHandle<Range> getRange() const;
|
||||
Range getRange() const;
|
||||
|
||||
/**
|
||||
* Inspects whether the specified response range is satisfiable
|
||||
* with request range.
|
||||
*/
|
||||
bool isRangeSatisfied(const SharedHandle<Range>& range) const;
|
||||
bool isRangeSatisfied(const Range& range) const;
|
||||
|
||||
const SharedHandle<Request>& getRequest() const
|
||||
{
|
||||
|
|
|
@ -96,16 +96,16 @@ void HttpResponse::validateResponse() const
|
|||
} else if(statusCode == 200 || statusCode == 206) {
|
||||
if(!httpHeader_->defined(HttpHeader::TRANSFER_ENCODING)) {
|
||||
// compare the received range against the requested range
|
||||
SharedHandle<Range> responseRange = httpHeader_->getRange();
|
||||
Range responseRange = httpHeader_->getRange();
|
||||
if(!httpRequest_->isRangeSatisfied(responseRange)) {
|
||||
throw DL_ABORT_EX2
|
||||
(fmt(EX_INVALID_RANGE_HEADER,
|
||||
httpRequest_->getStartByte(),
|
||||
httpRequest_->getEndByte(),
|
||||
httpRequest_->getEntityLength(),
|
||||
responseRange->getStartByte(),
|
||||
responseRange->getEndByte(),
|
||||
responseRange->getEntityLength()),
|
||||
responseRange.startByte,
|
||||
responseRange.endByte,
|
||||
responseRange.entityLength),
|
||||
error_code::CANNOT_RESUME);
|
||||
}
|
||||
}
|
||||
|
@ -231,7 +231,7 @@ int64_t HttpResponse::getContentLength() const
|
|||
if(!httpHeader_) {
|
||||
return 0;
|
||||
} else {
|
||||
return httpHeader_->getRange()->getContentLength();
|
||||
return httpHeader_->getRange().getContentLength();
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -240,7 +240,7 @@ int64_t HttpResponse::getEntityLength() const
|
|||
if(!httpHeader_) {
|
||||
return 0;
|
||||
} else {
|
||||
return httpHeader_->getRange()->getEntityLength();
|
||||
return httpHeader_->getRange().entityLength;
|
||||
}
|
||||
}
|
||||
|
||||
|
|
26
src/Range.cc
26
src/Range.cc
|
@ -36,16 +36,16 @@
|
|||
|
||||
namespace aria2 {
|
||||
|
||||
Range::Range():startByte_(0), endByte_(0), entityLength_(0) {}
|
||||
Range::Range():startByte(0), endByte(0), entityLength(0) {}
|
||||
|
||||
Range::Range(int64_t startByte, int64_t endByte, int64_t entityLength)
|
||||
: startByte_(startByte), endByte_(endByte), entityLength_(entityLength)
|
||||
: startByte(startByte), endByte(endByte), entityLength(entityLength)
|
||||
{}
|
||||
|
||||
Range::Range(const Range& c)
|
||||
: startByte_(c.startByte_),
|
||||
endByte_(c.endByte_),
|
||||
entityLength_(c.entityLength_)
|
||||
: startByte(c.startByte),
|
||||
endByte(c.endByte),
|
||||
entityLength(c.entityLength)
|
||||
{}
|
||||
|
||||
Range::~Range() {}
|
||||
|
@ -53,18 +53,18 @@ Range::~Range() {}
|
|||
Range& Range::operator=(const Range& c)
|
||||
{
|
||||
if(this != &c) {
|
||||
startByte_ = c.startByte_;
|
||||
endByte_ = c.endByte_;
|
||||
entityLength_ = c.entityLength_;
|
||||
startByte = c.startByte;
|
||||
endByte = c.endByte;
|
||||
entityLength = c.entityLength;
|
||||
}
|
||||
return *this;
|
||||
}
|
||||
|
||||
bool Range::operator==(const Range& range) const
|
||||
{
|
||||
return startByte_ == range.startByte_ &&
|
||||
endByte_ == range.endByte_ &&
|
||||
entityLength_ == range.entityLength_;
|
||||
return startByte == range.startByte &&
|
||||
endByte == range.endByte &&
|
||||
entityLength == range.entityLength;
|
||||
}
|
||||
|
||||
bool Range::operator!=(const Range& range) const
|
||||
|
@ -74,8 +74,8 @@ bool Range::operator!=(const Range& range) const
|
|||
|
||||
int64_t Range::getContentLength() const
|
||||
{
|
||||
if(endByte_ >= startByte_) {
|
||||
return endByte_-startByte_+1;
|
||||
if(endByte >= startByte) {
|
||||
return endByte-startByte+1;
|
||||
} else {
|
||||
return 0;
|
||||
}
|
||||
|
|
26
src/Range.h
26
src/Range.h
|
@ -41,12 +41,11 @@
|
|||
|
||||
namespace aria2 {
|
||||
|
||||
class Range {
|
||||
private:
|
||||
int64_t startByte_;
|
||||
int64_t endByte_;
|
||||
int64_t entityLength_;
|
||||
public:
|
||||
struct Range {
|
||||
int64_t startByte;
|
||||
int64_t endByte;
|
||||
int64_t entityLength;
|
||||
|
||||
Range();
|
||||
|
||||
Range(int64_t startByte, int64_t endByte, int64_t entityLength);
|
||||
|
@ -61,21 +60,6 @@ public:
|
|||
|
||||
bool operator!=(const Range& range) const;
|
||||
|
||||
int64_t getStartByte() const
|
||||
{
|
||||
return startByte_;
|
||||
}
|
||||
|
||||
int64_t getEndByte() const
|
||||
{
|
||||
return endByte_;
|
||||
}
|
||||
|
||||
int64_t getEntityLength() const
|
||||
{
|
||||
return entityLength_;
|
||||
}
|
||||
|
||||
int64_t getContentLength() const;
|
||||
};
|
||||
|
||||
|
|
|
@ -32,72 +32,72 @@ void HttpHeaderTest::testGetRange()
|
|||
httpHeader.put(HttpHeader::CONTENT_RANGE,
|
||||
"9223372036854775800-9223372036854775801/9223372036854775807");
|
||||
|
||||
SharedHandle<Range> range = httpHeader.getRange();
|
||||
Range range = httpHeader.getRange();
|
||||
|
||||
CPPUNIT_ASSERT_EQUAL((int64_t)9223372036854775800LL, range->getStartByte());
|
||||
CPPUNIT_ASSERT_EQUAL((int64_t)9223372036854775801LL, range->getEndByte());
|
||||
CPPUNIT_ASSERT_EQUAL((int64_t)9223372036854775807LL, range->getEntityLength());
|
||||
CPPUNIT_ASSERT_EQUAL((int64_t)9223372036854775800LL, range.startByte);
|
||||
CPPUNIT_ASSERT_EQUAL((int64_t)9223372036854775801LL, range.endByte);
|
||||
CPPUNIT_ASSERT_EQUAL((int64_t)9223372036854775807LL, range.entityLength);
|
||||
}
|
||||
{
|
||||
HttpHeader httpHeader;
|
||||
httpHeader.put(HttpHeader::CONTENT_RANGE,
|
||||
"9223372036854775800-9223372036854775801/9223372036854775807");
|
||||
|
||||
SharedHandle<Range> range = httpHeader.getRange();
|
||||
Range range = httpHeader.getRange();
|
||||
|
||||
CPPUNIT_ASSERT_EQUAL((int64_t)9223372036854775800LL, range->getStartByte());
|
||||
CPPUNIT_ASSERT_EQUAL((int64_t)9223372036854775801LL, range->getEndByte());
|
||||
CPPUNIT_ASSERT_EQUAL((int64_t)9223372036854775807LL, range->getEntityLength());
|
||||
CPPUNIT_ASSERT_EQUAL((int64_t)9223372036854775800LL, range.startByte);
|
||||
CPPUNIT_ASSERT_EQUAL((int64_t)9223372036854775801LL, range.endByte);
|
||||
CPPUNIT_ASSERT_EQUAL((int64_t)9223372036854775807LL, range.entityLength);
|
||||
}
|
||||
{
|
||||
HttpHeader httpHeader;
|
||||
httpHeader.put(HttpHeader::CONTENT_RANGE, "bytes */1024");
|
||||
|
||||
SharedHandle<Range> range = httpHeader.getRange();
|
||||
Range range = httpHeader.getRange();
|
||||
|
||||
CPPUNIT_ASSERT_EQUAL((int64_t)0, range->getStartByte());
|
||||
CPPUNIT_ASSERT_EQUAL((int64_t)0, range->getEndByte());
|
||||
CPPUNIT_ASSERT_EQUAL((int64_t)0, range->getEntityLength());
|
||||
CPPUNIT_ASSERT_EQUAL((int64_t)0, range.startByte);
|
||||
CPPUNIT_ASSERT_EQUAL((int64_t)0, range.endByte);
|
||||
CPPUNIT_ASSERT_EQUAL((int64_t)0, range.entityLength);
|
||||
}
|
||||
{
|
||||
HttpHeader httpHeader;
|
||||
httpHeader.put(HttpHeader::CONTENT_RANGE, "bytes 0-9/*");
|
||||
|
||||
SharedHandle<Range> range = httpHeader.getRange();
|
||||
Range range = httpHeader.getRange();
|
||||
|
||||
CPPUNIT_ASSERT_EQUAL((int64_t)0, range->getStartByte());
|
||||
CPPUNIT_ASSERT_EQUAL((int64_t)0, range->getEndByte());
|
||||
CPPUNIT_ASSERT_EQUAL((int64_t)0, range->getEntityLength());
|
||||
CPPUNIT_ASSERT_EQUAL((int64_t)0, range.startByte);
|
||||
CPPUNIT_ASSERT_EQUAL((int64_t)0, range.endByte);
|
||||
CPPUNIT_ASSERT_EQUAL((int64_t)0, range.entityLength);
|
||||
}
|
||||
{
|
||||
HttpHeader httpHeader;
|
||||
httpHeader.put(HttpHeader::CONTENT_RANGE, "bytes */*");
|
||||
|
||||
SharedHandle<Range> range = httpHeader.getRange();
|
||||
Range range = httpHeader.getRange();
|
||||
|
||||
CPPUNIT_ASSERT_EQUAL((int64_t)0, range->getStartByte());
|
||||
CPPUNIT_ASSERT_EQUAL((int64_t)0, range->getEndByte());
|
||||
CPPUNIT_ASSERT_EQUAL((int64_t)0, range->getEntityLength());
|
||||
CPPUNIT_ASSERT_EQUAL((int64_t)0, range.startByte);
|
||||
CPPUNIT_ASSERT_EQUAL((int64_t)0, range.endByte);
|
||||
CPPUNIT_ASSERT_EQUAL((int64_t)0, range.entityLength);
|
||||
}
|
||||
{
|
||||
HttpHeader httpHeader;
|
||||
httpHeader.put(HttpHeader::CONTENT_RANGE, "bytes 0");
|
||||
|
||||
SharedHandle<Range> range = httpHeader.getRange();
|
||||
Range range = httpHeader.getRange();
|
||||
|
||||
CPPUNIT_ASSERT_EQUAL((int64_t)0, range->getStartByte());
|
||||
CPPUNIT_ASSERT_EQUAL((int64_t)0, range->getEndByte());
|
||||
CPPUNIT_ASSERT_EQUAL((int64_t)0, range->getEntityLength());
|
||||
CPPUNIT_ASSERT_EQUAL((int64_t)0, range.startByte);
|
||||
CPPUNIT_ASSERT_EQUAL((int64_t)0, range.endByte);
|
||||
CPPUNIT_ASSERT_EQUAL((int64_t)0, range.entityLength);
|
||||
}
|
||||
{
|
||||
HttpHeader httpHeader;
|
||||
httpHeader.put(HttpHeader::CONTENT_RANGE, "bytes 0/");
|
||||
|
||||
SharedHandle<Range> range = httpHeader.getRange();
|
||||
Range range = httpHeader.getRange();
|
||||
|
||||
CPPUNIT_ASSERT_EQUAL((int64_t)0, range->getStartByte());
|
||||
CPPUNIT_ASSERT_EQUAL((int64_t)0, range->getEndByte());
|
||||
CPPUNIT_ASSERT_EQUAL((int64_t)0, range->getEntityLength());
|
||||
CPPUNIT_ASSERT_EQUAL((int64_t)0, range.startByte);
|
||||
CPPUNIT_ASSERT_EQUAL((int64_t)0, range.endByte);
|
||||
CPPUNIT_ASSERT_EQUAL((int64_t)0, range.entityLength);
|
||||
}
|
||||
{
|
||||
HttpHeader httpHeader;
|
||||
|
|
|
@ -649,7 +649,7 @@ void HttpRequestTest::testIsRangeSatisfied()
|
|||
httpRequest.setSegment(segment);
|
||||
httpRequest.setFileEntry(fileEntry);
|
||||
|
||||
SharedHandle<Range> range(new Range());
|
||||
Range range;
|
||||
|
||||
CPPUNIT_ASSERT(httpRequest.isRangeSatisfied(range));
|
||||
|
||||
|
@ -661,7 +661,7 @@ void HttpRequestTest::testIsRangeSatisfied()
|
|||
|
||||
uint64_t entityLength = segment->getSegmentLength()*10;
|
||||
|
||||
range.reset(new Range(segment->getPosition(), 0, entityLength));
|
||||
range = Range(segment->getPosition(), 0, entityLength);
|
||||
|
||||
CPPUNIT_ASSERT(httpRequest.isRangeSatisfied(range));
|
||||
|
||||
|
@ -677,14 +677,14 @@ void HttpRequestTest::testIsRangeSatisfied()
|
|||
|
||||
CPPUNIT_ASSERT(!httpRequest.isRangeSatisfied(range));
|
||||
|
||||
range.reset(new Range(segment->getPosition(),
|
||||
range = Range(segment->getPosition(),
|
||||
segment->getPosition()+segment->getLength()-1,
|
||||
entityLength));
|
||||
entityLength);
|
||||
|
||||
CPPUNIT_ASSERT(httpRequest.isRangeSatisfied(range));
|
||||
|
||||
range.reset(new Range(0, segment->getPosition()+segment->getLength()-1,
|
||||
entityLength));
|
||||
range = Range(0, segment->getPosition()+segment->getLength()-1,
|
||||
entityLength);
|
||||
|
||||
CPPUNIT_ASSERT(!httpRequest.isRangeSatisfied(range));
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue