Dont' return SharedHandle from HttpHeader::getRange()

pull/28/head
Tatsuhiro Tsujikawa 2012-09-30 17:30:35 +09:00
parent 492d6d1eeb
commit e34d0e7ffa
10 changed files with 80 additions and 99 deletions

View File

@ -120,9 +120,8 @@ bool HttpDownloadCommand::prepareForNextSegment() {
int64_t lastOffset =getFileEntry()->gtoloff int64_t lastOffset =getFileEntry()->gtoloff
(std::min(segment->getPosition()+segment->getLength(), (std::min(segment->getPosition()+segment->getLength(),
getFileEntry()->getLastOffset())); getFileEntry()->getLastOffset()));
Range range = httpResponse_->getHttpHeader()->getRange();
if(lastOffset == if(lastOffset == range.endByte + 1) {
httpResponse_->getHttpHeader()->getRange()->getEndByte()+1) {
return prepareForRetry(0); return prepareForRetry(0);
} }
} }
@ -132,7 +131,7 @@ bool HttpDownloadCommand::prepareForNextSegment() {
int64_t HttpDownloadCommand::getRequestEndOffset() const int64_t HttpDownloadCommand::getRequestEndOffset() const
{ {
int64_t endByte = httpResponse_->getHttpHeader()->getRange()->getEndByte(); int64_t endByte = httpResponse_->getHttpHeader()->getRange().endByte;
if(endByte > 0) { if(endByte > 0) {
return endByte+1; return endByte+1;
} else { } else {

View File

@ -85,13 +85,13 @@ HttpHeader::equalRange(int hdKey) const
return table_.equal_range(hdKey); return table_.equal_range(hdKey);
} }
SharedHandle<Range> HttpHeader::getRange() const Range HttpHeader::getRange() const
{ {
const std::string& rangeStr = find(CONTENT_RANGE); const std::string& rangeStr = find(CONTENT_RANGE);
if(rangeStr.empty()) { if(rangeStr.empty()) {
const std::string& clenStr = find(CONTENT_LENGTH); const std::string& clenStr = find(CONTENT_LENGTH);
if(clenStr.empty()) { if(clenStr.empty()) {
return SharedHandle<Range>(new Range()); return Range();
} else { } else {
int64_t contentLength; int64_t contentLength;
if(!util::parseLLIntNoThrow(contentLength, clenStr) || if(!util::parseLLIntNoThrow(contentLength, clenStr) ||
@ -101,10 +101,9 @@ SharedHandle<Range> HttpHeader::getRange() const
throw DOWNLOAD_FAILURE_EXCEPTION throw DOWNLOAD_FAILURE_EXCEPTION
(fmt(EX_TOO_LARGE_FILE, contentLength)); (fmt(EX_TOO_LARGE_FILE, contentLength));
} else if(contentLength == 0) { } else if(contentLength == 0) {
return SharedHandle<Range>(new Range()); return Range();
} else { } else {
return SharedHandle<Range> return Range(0, contentLength - 1, contentLength);
(new 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 // If byte-range-resp-spec or instance-length is "*", we returns
// empty Range. The former is usually sent with 416 (Request range // empty Range. The former is usually sent with 416 (Request range
// not satisfiable) status. // not satisfiable) status.
return SharedHandle<Range>(new Range()); return Range();
} }
std::string::const_iterator minus = std::find(byteRangeSpec, slash, '-'); std::string::const_iterator minus = std::find(byteRangeSpec, slash, '-');
if(minus == slash) { if(minus == slash) {
return SharedHandle<Range>(new Range()); return Range();
} }
int64_t startByte, endByte, entityLength; int64_t startByte, endByte, entityLength;
if(!util::parseLLIntNoThrow(startByte, std::string(byteRangeSpec, minus)) || 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()) { if(entityLength > std::numeric_limits<off_t>::max()) {
throw DOWNLOAD_FAILURE_EXCEPTION(fmt(EX_TOO_LARGE_FILE, entityLength)); 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) void HttpHeader::setVersion(const std::string& version)

View File

@ -108,7 +108,7 @@ public:
std::multimap<int, std::string>::const_iterator> std::multimap<int, std::string>::const_iterator>
equalRange(int hdKey) const; equalRange(int hdKey) const;
SharedHandle<Range> getRange() const; Range getRange() const;
int getStatusCode() const; int getStatusCode() const;

View File

@ -101,27 +101,26 @@ int64_t HttpRequest::getEndByte() const
} }
} }
SharedHandle<Range> HttpRequest::getRange() const Range HttpRequest::getRange() const
{ {
// content-length is always 0 // content-length is always 0
if(!segment_) { if(!segment_) {
return SharedHandle<Range>(new Range()); return Range();
} else { } else {
return SharedHandle<Range>(new Range(getStartByte(), getEndByte(), return Range(getStartByte(), getEndByte(), fileEntry_->getLength());
fileEntry_->getLength()));
} }
} }
bool HttpRequest::isRangeSatisfied(const SharedHandle<Range>& range) const bool HttpRequest::isRangeSatisfied(const Range& range) const
{ {
if(!segment_) { if(!segment_) {
return true; return true;
} }
if((getStartByte() == range->getStartByte()) && if((getStartByte() == range.startByte) &&
((getEndByte() == 0) || ((getEndByte() == 0) ||
(getEndByte() == range->getEndByte())) && (getEndByte() == range.endByte)) &&
((fileEntry_->getLength() == 0) || ((fileEntry_->getLength() == 0) ||
(fileEntry_->getLength() == range->getEntityLength()))) { (fileEntry_->getLength() == range.entityLength))) {
return true; return true;
} else { } else {
return false; return false;

View File

@ -128,13 +128,13 @@ public:
std::string getURIHost() const; std::string getURIHost() const;
SharedHandle<Range> getRange() const; Range getRange() const;
/** /**
* Inspects whether the specified response range is satisfiable * Inspects whether the specified response range is satisfiable
* with request range. * with request range.
*/ */
bool isRangeSatisfied(const SharedHandle<Range>& range) const; bool isRangeSatisfied(const Range& range) const;
const SharedHandle<Request>& getRequest() const const SharedHandle<Request>& getRequest() const
{ {

View File

@ -96,16 +96,16 @@ void HttpResponse::validateResponse() const
} else if(statusCode == 200 || statusCode == 206) { } else if(statusCode == 200 || statusCode == 206) {
if(!httpHeader_->defined(HttpHeader::TRANSFER_ENCODING)) { if(!httpHeader_->defined(HttpHeader::TRANSFER_ENCODING)) {
// compare the received range against the requested range // compare the received range against the requested range
SharedHandle<Range> responseRange = httpHeader_->getRange(); Range responseRange = httpHeader_->getRange();
if(!httpRequest_->isRangeSatisfied(responseRange)) { if(!httpRequest_->isRangeSatisfied(responseRange)) {
throw DL_ABORT_EX2 throw DL_ABORT_EX2
(fmt(EX_INVALID_RANGE_HEADER, (fmt(EX_INVALID_RANGE_HEADER,
httpRequest_->getStartByte(), httpRequest_->getStartByte(),
httpRequest_->getEndByte(), httpRequest_->getEndByte(),
httpRequest_->getEntityLength(), httpRequest_->getEntityLength(),
responseRange->getStartByte(), responseRange.startByte,
responseRange->getEndByte(), responseRange.endByte,
responseRange->getEntityLength()), responseRange.entityLength),
error_code::CANNOT_RESUME); error_code::CANNOT_RESUME);
} }
} }
@ -231,7 +231,7 @@ int64_t HttpResponse::getContentLength() const
if(!httpHeader_) { if(!httpHeader_) {
return 0; return 0;
} else { } else {
return httpHeader_->getRange()->getContentLength(); return httpHeader_->getRange().getContentLength();
} }
} }
@ -240,7 +240,7 @@ int64_t HttpResponse::getEntityLength() const
if(!httpHeader_) { if(!httpHeader_) {
return 0; return 0;
} else { } else {
return httpHeader_->getRange()->getEntityLength(); return httpHeader_->getRange().entityLength;
} }
} }

View File

@ -36,16 +36,16 @@
namespace aria2 { 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) 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) Range::Range(const Range& c)
: startByte_(c.startByte_), : startByte(c.startByte),
endByte_(c.endByte_), endByte(c.endByte),
entityLength_(c.entityLength_) entityLength(c.entityLength)
{} {}
Range::~Range() {} Range::~Range() {}
@ -53,18 +53,18 @@ Range::~Range() {}
Range& Range::operator=(const Range& c) Range& Range::operator=(const Range& c)
{ {
if(this != &c) { if(this != &c) {
startByte_ = c.startByte_; startByte = c.startByte;
endByte_ = c.endByte_; endByte = c.endByte;
entityLength_ = c.entityLength_; entityLength = c.entityLength;
} }
return *this; return *this;
} }
bool Range::operator==(const Range& range) const bool Range::operator==(const Range& range) const
{ {
return startByte_ == range.startByte_ && return startByte == range.startByte &&
endByte_ == range.endByte_ && endByte == range.endByte &&
entityLength_ == range.entityLength_; entityLength == range.entityLength;
} }
bool Range::operator!=(const Range& range) const bool Range::operator!=(const Range& range) const
@ -74,8 +74,8 @@ bool Range::operator!=(const Range& range) const
int64_t Range::getContentLength() const int64_t Range::getContentLength() const
{ {
if(endByte_ >= startByte_) { if(endByte >= startByte) {
return endByte_-startByte_+1; return endByte-startByte+1;
} else { } else {
return 0; return 0;
} }

View File

@ -41,12 +41,11 @@
namespace aria2 { namespace aria2 {
class Range { struct Range {
private: int64_t startByte;
int64_t startByte_; int64_t endByte;
int64_t endByte_; int64_t entityLength;
int64_t entityLength_;
public:
Range(); Range();
Range(int64_t startByte, int64_t endByte, int64_t entityLength); Range(int64_t startByte, int64_t endByte, int64_t entityLength);
@ -61,21 +60,6 @@ public:
bool operator!=(const Range& range) const; 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; int64_t getContentLength() const;
}; };

View File

@ -32,72 +32,72 @@ void HttpHeaderTest::testGetRange()
httpHeader.put(HttpHeader::CONTENT_RANGE, httpHeader.put(HttpHeader::CONTENT_RANGE,
"9223372036854775800-9223372036854775801/9223372036854775807"); "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)9223372036854775800LL, range.startByte);
CPPUNIT_ASSERT_EQUAL((int64_t)9223372036854775801LL, range->getEndByte()); CPPUNIT_ASSERT_EQUAL((int64_t)9223372036854775801LL, range.endByte);
CPPUNIT_ASSERT_EQUAL((int64_t)9223372036854775807LL, range->getEntityLength()); CPPUNIT_ASSERT_EQUAL((int64_t)9223372036854775807LL, range.entityLength);
} }
{ {
HttpHeader httpHeader; HttpHeader httpHeader;
httpHeader.put(HttpHeader::CONTENT_RANGE, httpHeader.put(HttpHeader::CONTENT_RANGE,
"9223372036854775800-9223372036854775801/9223372036854775807"); "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)9223372036854775800LL, range.startByte);
CPPUNIT_ASSERT_EQUAL((int64_t)9223372036854775801LL, range->getEndByte()); CPPUNIT_ASSERT_EQUAL((int64_t)9223372036854775801LL, range.endByte);
CPPUNIT_ASSERT_EQUAL((int64_t)9223372036854775807LL, range->getEntityLength()); CPPUNIT_ASSERT_EQUAL((int64_t)9223372036854775807LL, range.entityLength);
} }
{ {
HttpHeader httpHeader; HttpHeader httpHeader;
httpHeader.put(HttpHeader::CONTENT_RANGE, "bytes */1024"); 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.startByte);
CPPUNIT_ASSERT_EQUAL((int64_t)0, range->getEndByte()); CPPUNIT_ASSERT_EQUAL((int64_t)0, range.endByte);
CPPUNIT_ASSERT_EQUAL((int64_t)0, range->getEntityLength()); CPPUNIT_ASSERT_EQUAL((int64_t)0, range.entityLength);
} }
{ {
HttpHeader httpHeader; HttpHeader httpHeader;
httpHeader.put(HttpHeader::CONTENT_RANGE, "bytes 0-9/*"); 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.startByte);
CPPUNIT_ASSERT_EQUAL((int64_t)0, range->getEndByte()); CPPUNIT_ASSERT_EQUAL((int64_t)0, range.endByte);
CPPUNIT_ASSERT_EQUAL((int64_t)0, range->getEntityLength()); CPPUNIT_ASSERT_EQUAL((int64_t)0, range.entityLength);
} }
{ {
HttpHeader httpHeader; HttpHeader httpHeader;
httpHeader.put(HttpHeader::CONTENT_RANGE, "bytes */*"); 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.startByte);
CPPUNIT_ASSERT_EQUAL((int64_t)0, range->getEndByte()); CPPUNIT_ASSERT_EQUAL((int64_t)0, range.endByte);
CPPUNIT_ASSERT_EQUAL((int64_t)0, range->getEntityLength()); CPPUNIT_ASSERT_EQUAL((int64_t)0, range.entityLength);
} }
{ {
HttpHeader httpHeader; HttpHeader httpHeader;
httpHeader.put(HttpHeader::CONTENT_RANGE, "bytes 0"); 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.startByte);
CPPUNIT_ASSERT_EQUAL((int64_t)0, range->getEndByte()); CPPUNIT_ASSERT_EQUAL((int64_t)0, range.endByte);
CPPUNIT_ASSERT_EQUAL((int64_t)0, range->getEntityLength()); CPPUNIT_ASSERT_EQUAL((int64_t)0, range.entityLength);
} }
{ {
HttpHeader httpHeader; HttpHeader httpHeader;
httpHeader.put(HttpHeader::CONTENT_RANGE, "bytes 0/"); 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.startByte);
CPPUNIT_ASSERT_EQUAL((int64_t)0, range->getEndByte()); CPPUNIT_ASSERT_EQUAL((int64_t)0, range.endByte);
CPPUNIT_ASSERT_EQUAL((int64_t)0, range->getEntityLength()); CPPUNIT_ASSERT_EQUAL((int64_t)0, range.entityLength);
} }
{ {
HttpHeader httpHeader; HttpHeader httpHeader;

View File

@ -649,7 +649,7 @@ void HttpRequestTest::testIsRangeSatisfied()
httpRequest.setSegment(segment); httpRequest.setSegment(segment);
httpRequest.setFileEntry(fileEntry); httpRequest.setFileEntry(fileEntry);
SharedHandle<Range> range(new Range()); Range range;
CPPUNIT_ASSERT(httpRequest.isRangeSatisfied(range)); CPPUNIT_ASSERT(httpRequest.isRangeSatisfied(range));
@ -661,7 +661,7 @@ void HttpRequestTest::testIsRangeSatisfied()
uint64_t entityLength = segment->getSegmentLength()*10; 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)); CPPUNIT_ASSERT(httpRequest.isRangeSatisfied(range));
@ -677,14 +677,14 @@ void HttpRequestTest::testIsRangeSatisfied()
CPPUNIT_ASSERT(!httpRequest.isRangeSatisfied(range)); CPPUNIT_ASSERT(!httpRequest.isRangeSatisfied(range));
range.reset(new Range(segment->getPosition(), range = Range(segment->getPosition(),
segment->getPosition()+segment->getLength()-1, segment->getPosition()+segment->getLength()-1,
entityLength)); entityLength);
CPPUNIT_ASSERT(httpRequest.isRangeSatisfied(range)); CPPUNIT_ASSERT(httpRequest.isRangeSatisfied(range));
range.reset(new Range(0, segment->getPosition()+segment->getLength()-1, range = Range(0, segment->getPosition()+segment->getLength()-1,
entityLength)); entityLength);
CPPUNIT_ASSERT(!httpRequest.isRangeSatisfied(range)); CPPUNIT_ASSERT(!httpRequest.isRangeSatisfied(range));
} }