mirror of https://github.com/kiwix/libkiwix.git
Moved byte-range parsing to byte_range.cpp
This commit is contained in:
parent
693905eb68
commit
67a347c0c4
|
@ -20,6 +20,8 @@
|
||||||
|
|
||||||
#include "byte_range.h"
|
#include "byte_range.h"
|
||||||
|
|
||||||
|
#include "tools/stringTools.h"
|
||||||
|
|
||||||
namespace kiwix {
|
namespace kiwix {
|
||||||
|
|
||||||
ByteRange::ByteRange()
|
ByteRange::ByteRange()
|
||||||
|
@ -34,4 +36,30 @@ ByteRange::ByteRange(Kind kind, int64_t first, int64_t last)
|
||||||
, last_(last)
|
, last_(last)
|
||||||
{}
|
{}
|
||||||
|
|
||||||
|
ByteRange ByteRange::parse(std::string rangeStr)
|
||||||
|
{
|
||||||
|
ByteRange byteRange;
|
||||||
|
const std::string byteUnitSpec("bytes=");
|
||||||
|
if ( kiwix::startsWith(rangeStr, byteUnitSpec) ) {
|
||||||
|
rangeStr.erase(0, byteUnitSpec.size());
|
||||||
|
std::istringstream iss(rangeStr);
|
||||||
|
|
||||||
|
int64_t start, end = INT64_MAX;
|
||||||
|
if (iss >> start) {
|
||||||
|
if ( start < 0 ) {
|
||||||
|
if ( iss.eof() )
|
||||||
|
byteRange = 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())
|
||||||
|
byteRange = ByteRange(ByteRange::PARSED, start, end);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return byteRange;
|
||||||
|
}
|
||||||
|
|
||||||
} // namespace kiwix
|
} // namespace kiwix
|
||||||
|
|
|
@ -22,6 +22,7 @@
|
||||||
#define KIWIXLIB_SERVER_BYTE_RANGE_H
|
#define KIWIXLIB_SERVER_BYTE_RANGE_H
|
||||||
|
|
||||||
#include <cstdint>
|
#include <cstdint>
|
||||||
|
#include <string>
|
||||||
|
|
||||||
namespace kiwix {
|
namespace kiwix {
|
||||||
|
|
||||||
|
@ -51,6 +52,8 @@ 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);
|
||||||
|
|
||||||
private: // data
|
private: // data
|
||||||
Kind kind_;
|
Kind kind_;
|
||||||
int64_t first_;
|
int64_t first_;
|
||||||
|
|
|
@ -20,7 +20,6 @@
|
||||||
|
|
||||||
|
|
||||||
#include "request_context.h"
|
#include "request_context.h"
|
||||||
#include "tools/stringTools.h"
|
|
||||||
#include <string.h>
|
#include <string.h>
|
||||||
#include <stdexcept>
|
#include <stdexcept>
|
||||||
#include <sstream>
|
#include <sstream>
|
||||||
|
@ -63,32 +62,6 @@ fullURL2LocalURL(const std::string& full_url, const std::string& rootLocation)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
ByteRange parse_byte_range(std::string range)
|
|
||||||
{
|
|
||||||
ByteRange byteRange;
|
|
||||||
const std::string byteUnitSpec("bytes=");
|
|
||||||
if ( kiwix::startsWith(range, byteUnitSpec) ) {
|
|
||||||
range.erase(0, byteUnitSpec.size());
|
|
||||||
std::istringstream iss(range);
|
|
||||||
|
|
||||||
int64_t start, end = INT64_MAX;
|
|
||||||
if (iss >> start) {
|
|
||||||
if ( start < 0 ) {
|
|
||||||
if ( iss.eof() )
|
|
||||||
byteRange = 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())
|
|
||||||
byteRange = ByteRange(ByteRange::PARSED, start, end);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
return byteRange;
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
} // unnamed namespace
|
} // unnamed namespace
|
||||||
|
|
||||||
|
@ -114,7 +87,7 @@ RequestContext::RequestContext(struct MHD_Connection* connection,
|
||||||
} catch (const std::out_of_range&) {}
|
} catch (const std::out_of_range&) {}
|
||||||
|
|
||||||
try {
|
try {
|
||||||
byteRange_ = parse_byte_range(get_header(MHD_HTTP_HEADER_RANGE));
|
byteRange_ = ByteRange::parse(get_header(MHD_HTTP_HEADER_RANGE));
|
||||||
} catch (const std::out_of_range&) {}
|
} catch (const std::out_of_range&) {}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
Loading…
Reference in New Issue