From 8512fe992e2e86dc284d2d7c82decd22df2b0e2b Mon Sep 17 00:00:00 2001 From: Tatsuhiro Tsujikawa Date: Sat, 19 Dec 2015 19:26:39 +0900 Subject: [PATCH] Support content-range from non-compliant server See GH-509 --- src/HttpHeader.cc | 17 ++++++++++++----- test/HttpHeaderTest.cc | 11 +++++++++++ 2 files changed, 23 insertions(+), 5 deletions(-) diff --git a/src/HttpHeader.cc b/src/HttpHeader.cc index a9fe23a6..3708e723 100644 --- a/src/HttpHeader.cc +++ b/src/HttpHeader.cc @@ -113,13 +113,20 @@ Range HttpHeader::getRange() const } } } - // we expect that rangeStr looks like 'bytes 100-199/100' - // but some server returns '100-199/100', omitting bytes-unit specifier - // 'bytes'. + // we expect that rangeStr looks like 'bytes 100-199/200' but some + // server returns '100-199/200', omitting bytes-unit specifier + // 'bytes'. Moreover, some server may return like + // 'bytes=100-199/200'. auto byteRangeSpec = std::find(rangeStr.begin(), rangeStr.end(), ' '); if(byteRangeSpec == rangeStr.end()) { - // we assume bytes-unit specifier omitted. - byteRangeSpec = rangeStr.begin(); + // check for 'bytes=100-199/200' case + byteRangeSpec = std::find(rangeStr.begin(), rangeStr.end(), '='); + if (byteRangeSpec == rangeStr.end()) { + // we assume bytes-unit specifier omitted. + byteRangeSpec = rangeStr.begin(); + } else { + ++byteRangeSpec; + } } else { while(byteRangeSpec != rangeStr.end() && (*byteRangeSpec == ' ' || *byteRangeSpec == '\t')) { diff --git a/test/HttpHeaderTest.cc b/test/HttpHeaderTest.cc index 80bb7aa6..0500ccd1 100644 --- a/test/HttpHeaderTest.cc +++ b/test/HttpHeaderTest.cc @@ -101,6 +101,17 @@ void HttpHeaderTest::testGetRange() CPPUNIT_ASSERT_EQUAL((int64_t)0, range.endByte); CPPUNIT_ASSERT_EQUAL((int64_t)0, range.entityLength); } + { + // Support for non-compliant server + HttpHeader httpHeader; + httpHeader.put(HttpHeader::CONTENT_RANGE, "bytes=0-1023/1024"); + + Range range = httpHeader.getRange(); + + CPPUNIT_ASSERT_EQUAL((int64_t)0, range.startByte); + CPPUNIT_ASSERT_EQUAL((int64_t)1023, range.endByte); + CPPUNIT_ASSERT_EQUAL((int64_t)1024, range.entityLength); + } { HttpHeader httpHeader; httpHeader.put(HttpHeader::CONTENT_RANGE, "bytes 0-/3");