mirror of https://github.com/kiwix/libkiwix.git
Some refactoring of byte-range parsing
This commit is contained in:
parent
ff23b28e7c
commit
7301bf89bb
|
@ -24,6 +24,32 @@
|
||||||
|
|
||||||
namespace kiwix {
|
namespace kiwix {
|
||||||
|
|
||||||
|
namespace {
|
||||||
|
|
||||||
|
ByteRange parseByteRange(const std::string& rangeStr)
|
||||||
|
{
|
||||||
|
std::istringstream iss(rangeStr);
|
||||||
|
|
||||||
|
int64_t start, end = INT64_MAX;
|
||||||
|
if (iss >> start) {
|
||||||
|
if ( start < 0 ) {
|
||||||
|
if ( iss.eof() )
|
||||||
|
return ByteRange(ByteRange::PARSED, start, end);
|
||||||
|
} else {
|
||||||
|
char c;
|
||||||
|
if (iss >> c && c=='-') {
|
||||||
|
iss >> end; // if this fails, end is not modified, which is OK
|
||||||
|
if (iss.eof() && start <= end)
|
||||||
|
return ByteRange(ByteRange::PARSED, start, end);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return ByteRange(ByteRange::INVALID, 0, INT64_MAX);
|
||||||
|
}
|
||||||
|
|
||||||
|
} // unnamed namespace
|
||||||
|
|
||||||
ByteRange::ByteRange()
|
ByteRange::ByteRange()
|
||||||
: kind_(NONE)
|
: kind_(NONE)
|
||||||
, first_(0)
|
, first_(0)
|
||||||
|
@ -36,31 +62,13 @@ ByteRange::ByteRange(Kind kind, int64_t first, int64_t last)
|
||||||
, last_(last)
|
, last_(last)
|
||||||
{}
|
{}
|
||||||
|
|
||||||
ByteRange ByteRange::parse(std::string rangeStr)
|
ByteRange ByteRange::parse(const std::string& rangeStr)
|
||||||
{
|
{
|
||||||
ByteRange byteRange(INVALID, 0, INT64_MAX);
|
|
||||||
const std::string byteUnitSpec("bytes=");
|
const std::string byteUnitSpec("bytes=");
|
||||||
if ( kiwix::startsWith(rangeStr, byteUnitSpec) ) {
|
if ( ! kiwix::startsWith(rangeStr, byteUnitSpec) )
|
||||||
rangeStr.erase(0, byteUnitSpec.size());
|
return ByteRange(INVALID, 0, INT64_MAX);
|
||||||
std::istringstream iss(rangeStr);
|
|
||||||
|
|
||||||
int64_t start, end = INT64_MAX;
|
return parseByteRange(rangeStr.substr(byteUnitSpec.size()));
|
||||||
if (iss >> start) {
|
|
||||||
if ( start < 0 ) {
|
|
||||||
if ( iss.eof() )
|
|
||||||
byteRange = ByteRange(PARSED, start, end);
|
|
||||||
} else {
|
|
||||||
char c;
|
|
||||||
if (iss >> c && c=='-') {
|
|
||||||
iss >> end; // if this fails, end is not modified, which is OK
|
|
||||||
if (iss.eof() && start <= end)
|
|
||||||
byteRange = ByteRange(PARSED, start, end);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
return byteRange;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
ByteRange ByteRange::resolve(int64_t contentSize) const
|
ByteRange ByteRange::resolve(int64_t contentSize) const
|
||||||
|
|
|
@ -57,7 +57,7 @@ class ByteRange
|
||||||
int64_t last() const { return last_; }
|
int64_t last() const { return last_; }
|
||||||
int64_t length() const { return last_ + 1 - first_; }
|
int64_t length() const { return last_ + 1 - first_; }
|
||||||
|
|
||||||
static ByteRange parse(std::string rangeStr);
|
static ByteRange parse(const std::string& rangeStr);
|
||||||
ByteRange resolve(int64_t contentSize) const;
|
ByteRange resolve(int64_t contentSize) const;
|
||||||
|
|
||||||
private: // data
|
private: // data
|
||||||
|
|
Loading…
Reference in New Issue