From e34d0e7ffa602d611ec6ec14cf9aa381da41c24e Mon Sep 17 00:00:00 2001 From: Tatsuhiro Tsujikawa Date: Sun, 30 Sep 2012 17:30:35 +0900 Subject: [PATCH] Dont' return SharedHandle from HttpHeader::getRange() --- src/HttpDownloadCommand.cc | 7 ++--- src/HttpHeader.cc | 15 +++++----- src/HttpHeader.h | 2 +- src/HttpRequest.cc | 17 ++++++------ src/HttpRequest.h | 4 +-- src/HttpResponse.cc | 12 ++++---- src/Range.cc | 26 +++++++++--------- src/Range.h | 26 ++++-------------- test/HttpHeaderTest.cc | 56 +++++++++++++++++++------------------- test/HttpRequestTest.cc | 14 +++++----- 10 files changed, 80 insertions(+), 99 deletions(-) diff --git a/src/HttpDownloadCommand.cc b/src/HttpDownloadCommand.cc index 47c17f66..74153d4d 100644 --- a/src/HttpDownloadCommand.cc +++ b/src/HttpDownloadCommand.cc @@ -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 { diff --git a/src/HttpHeader.cc b/src/HttpHeader.cc index a349a7a3..ae622b27 100644 --- a/src/HttpHeader.cc +++ b/src/HttpHeader.cc @@ -85,13 +85,13 @@ HttpHeader::equalRange(int hdKey) const return table_.equal_range(hdKey); } -SharedHandle 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(new Range()); + return Range(); } else { int64_t contentLength; if(!util::parseLLIntNoThrow(contentLength, clenStr) || @@ -101,10 +101,9 @@ SharedHandle HttpHeader::getRange() const throw DOWNLOAD_FAILURE_EXCEPTION (fmt(EX_TOO_LARGE_FILE, contentLength)); } else if(contentLength == 0) { - return SharedHandle(new Range()); + return Range(); } else { - return SharedHandle - (new Range(0, contentLength-1, contentLength)); + return Range(0, contentLength - 1, contentLength); } } } @@ -130,11 +129,11 @@ SharedHandle 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(new Range()); + return Range(); } std::string::const_iterator minus = std::find(byteRangeSpec, slash, '-'); if(minus == slash) { - return SharedHandle(new Range()); + return Range(); } int64_t startByte, endByte, entityLength; if(!util::parseLLIntNoThrow(startByte, std::string(byteRangeSpec, minus)) || @@ -153,7 +152,7 @@ SharedHandle HttpHeader::getRange() const if(entityLength > std::numeric_limits::max()) { throw DOWNLOAD_FAILURE_EXCEPTION(fmt(EX_TOO_LARGE_FILE, entityLength)); } - return SharedHandle(new Range(startByte, endByte, entityLength)); + return Range(startByte, endByte, entityLength); } void HttpHeader::setVersion(const std::string& version) diff --git a/src/HttpHeader.h b/src/HttpHeader.h index 4a1d3f9d..217e96b7 100644 --- a/src/HttpHeader.h +++ b/src/HttpHeader.h @@ -108,7 +108,7 @@ public: std::multimap::const_iterator> equalRange(int hdKey) const; - SharedHandle getRange() const; + Range getRange() const; int getStatusCode() const; diff --git a/src/HttpRequest.cc b/src/HttpRequest.cc index fcf8ef0c..6cb6c454 100644 --- a/src/HttpRequest.cc +++ b/src/HttpRequest.cc @@ -101,31 +101,30 @@ int64_t HttpRequest::getEndByte() const } } -SharedHandle HttpRequest::getRange() const +Range HttpRequest::getRange() const { // content-length is always 0 if(!segment_) { - return SharedHandle(new Range()); + return Range(); } else { - return SharedHandle(new Range(getStartByte(), getEndByte(), - fileEntry_->getLength())); + return Range(getStartByte(), getEndByte(), fileEntry_->getLength()); } } -bool HttpRequest::isRangeSatisfied(const SharedHandle& 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; - } + } } namespace { diff --git a/src/HttpRequest.h b/src/HttpRequest.h index c467d601..76c954c9 100644 --- a/src/HttpRequest.h +++ b/src/HttpRequest.h @@ -128,13 +128,13 @@ public: std::string getURIHost() const; - SharedHandle getRange() const; + Range getRange() const; /** * Inspects whether the specified response range is satisfiable * with request range. */ - bool isRangeSatisfied(const SharedHandle& range) const; + bool isRangeSatisfied(const Range& range) const; const SharedHandle& getRequest() const { diff --git a/src/HttpResponse.cc b/src/HttpResponse.cc index b721d64b..cd7e821f 100644 --- a/src/HttpResponse.cc +++ b/src/HttpResponse.cc @@ -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 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; } } diff --git a/src/Range.cc b/src/Range.cc index 779b13f6..88dc149b 100644 --- a/src/Range.cc +++ b/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; } diff --git a/src/Range.h b/src/Range.h index c4d1ad4d..e7aef928 100644 --- a/src/Range.h +++ b/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; }; diff --git a/test/HttpHeaderTest.cc b/test/HttpHeaderTest.cc index 056d4f10..9573da23 100644 --- a/test/HttpHeaderTest.cc +++ b/test/HttpHeaderTest.cc @@ -32,72 +32,72 @@ void HttpHeaderTest::testGetRange() httpHeader.put(HttpHeader::CONTENT_RANGE, "9223372036854775800-9223372036854775801/9223372036854775807"); - SharedHandle 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 = 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 = 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 = 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 = 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 = 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 = 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; diff --git a/test/HttpRequestTest.cc b/test/HttpRequestTest.cc index 7091fcf3..d81536a0 100644 --- a/test/HttpRequestTest.cc +++ b/test/HttpRequestTest.cc @@ -649,7 +649,7 @@ void HttpRequestTest::testIsRangeSatisfied() httpRequest.setSegment(segment); httpRequest.setFileEntry(fileEntry); - SharedHandle 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(), - segment->getPosition()+segment->getLength()-1, - entityLength)); + range = Range(segment->getPosition(), + segment->getPosition()+segment->getLength()-1, + 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)); }