Explorar o código

2008-03-15 Tatsuhiro Tsujikawa <tujikawa at rednoah dot com>

	Suppress content-range error when 'Content-Range' response header
	doesn't contains bytes-unit specifier 'bytes'
	* src/HttpHeader.cc (getRange)
	* test/HttpHeaderTest.cc
Tatsuhiro Tsujikawa %!s(int64=18) %!d(string=hai) anos
pai
achega
83b746d94c
Modificáronse 3 ficheiros con 50 adicións e 18 borrados
  1. 7 0
      ChangeLog
  2. 22 10
      src/HttpHeader.cc
  3. 21 8
      test/HttpHeaderTest.cc

+ 7 - 0
ChangeLog

@@ -1,3 +1,10 @@
+2008-03-15  Tatsuhiro Tsujikawa  <tujikawa at rednoah dot com>
+
+	Suppress content-range error when 'Content-Range' response header
+	doesn't contains bytes-unit specifier 'bytes'
+	* src/HttpHeader.cc (getRange)
+	* test/HttpHeaderTest.cc
+
 2008-03-15  Tatsuhiro Tsujikawa  <tujikawa at rednoah dot com>
 
 	Fixed compilation error with g++-4.3

+ 22 - 10
src/HttpHeader.cc

@@ -90,18 +90,30 @@ RangeHandle HttpHeader::getRange() const
       return new Range(0, contentLength-1, contentLength);
     }
   }
-  std::string::size_type rangeSpecIndex = rangeStr.find("bytes ");
-  if(rangeSpecIndex == std::string::npos) {
-    return new Range(0, 0, 0);
+  
+  std::string byteRangeSpec;
+  {
+    // we expect that rangeStr looks like 'bytes 100-199/100'
+    // but some server returns '100-199/100', omitting bytes-unit sepcifier
+    // 'bytes'.
+    std::pair<std::string, std::string> splist;
+    Util::split(splist, rangeStr, ' ');
+    if(splist.second.empty()) {
+      // we assume bytes-unit specifier omitted.
+      byteRangeSpec = splist.first;
+    } else {
+      byteRangeSpec = splist.second;
+    }
   }
-  std::pair<std::string, std::string> rangePair;
-  Util::split(rangePair, rangeStr.substr(rangeSpecIndex+6), '/');
-  std::pair<std::string, std::string> startEndBytePair;
-  Util::split(startEndBytePair, rangePair.first, '-');
+  std::pair<std::string, std::string> byteRangeSpecPair;
+  Util::split(byteRangeSpecPair, byteRangeSpec, '/');
+
+  std::pair<std::string, std::string> byteRangeRespSpecPair;
+  Util::split(byteRangeRespSpecPair, byteRangeSpecPair.first, '-');
 
-  int64_t startByte = STRTOLL(startEndBytePair.first.c_str());
-  int64_t endByte = STRTOLL(startEndBytePair.second.c_str());
-  int64_t entityLength = STRTOLL(rangePair.second.c_str());
+  int64_t startByte = STRTOLL(byteRangeRespSpecPair.first.c_str());
+  int64_t endByte = STRTOLL(byteRangeRespSpecPair.second.c_str());
+  int64_t entityLength = STRTOLL(byteRangeSpecPair.second.c_str());
 
   return new Range(startByte, endByte, entityLength);
 }

+ 21 - 8
test/HttpHeaderTest.cc

@@ -20,14 +20,27 @@ CPPUNIT_TEST_SUITE_REGISTRATION( HttpHeaderTest );
 
 void HttpHeaderTest::testGetRange()
 {
-  HttpHeader httpHeader;
-  httpHeader.put("Content-Range", "bytes 1-499/1234");
-
-  SharedHandle<Range> range = httpHeader.getRange();
-
-  CPPUNIT_ASSERT_EQUAL((int64_t)1, range->getStartByte());
-  CPPUNIT_ASSERT_EQUAL((int64_t)499, range->getEndByte());
-  CPPUNIT_ASSERT_EQUAL((int64_t)1234, range->getEntityLength());
+  {
+    HttpHeader httpHeader;
+    httpHeader.put("Content-Range", "bytes 1-499/1234");
+    
+    SharedHandle<Range> range = httpHeader.getRange();
+    
+    CPPUNIT_ASSERT_EQUAL((int64_t)1, range->getStartByte());
+    CPPUNIT_ASSERT_EQUAL((int64_t)499, range->getEndByte());
+    CPPUNIT_ASSERT_EQUAL((int64_t)1234, range->getEntityLength());
+  }
+  {
+    HttpHeader httpHeader;
+    httpHeader.put("Content-Range",
+		   "9223372036854775800-9223372036854775801/9223372036854775807");
+    
+    SharedHandle<Range> range = httpHeader.getRange();
+    
+    CPPUNIT_ASSERT_EQUAL(9223372036854775800LL, range->getStartByte());
+    CPPUNIT_ASSERT_EQUAL(9223372036854775801LL, range->getEndByte());
+    CPPUNIT_ASSERT_EQUAL(9223372036854775807LL, range->getEntityLength());
+  }
 }
 
 } // namespace aria2