parse_byte_range() without side-effects

This commit is contained in:
Veloman Yunkan 2020-05-23 18:53:16 +04:00
parent e6a86c02ae
commit 81c38d6b2b
2 changed files with 7 additions and 6 deletions

View File

@ -86,16 +86,16 @@ RequestContext::RequestContext(struct MHD_Connection* connection,
} catch (const std::out_of_range&) {} } catch (const std::out_of_range&) {}
try { try {
std::string range = get_header(MHD_HTTP_HEADER_RANGE); range_pair = parse_byte_range(get_header(MHD_HTTP_HEADER_RANGE));
parse_byte_range(range);
} catch (const std::out_of_range&) {} } catch (const std::out_of_range&) {}
} }
RequestContext::~RequestContext() RequestContext::~RequestContext()
{} {}
void RequestContext::parse_byte_range(std::string range) RequestContext::ByteRange RequestContext::parse_byte_range(std::string range)
{ {
ByteRange byteRange{0, -1};
const std::string byteUnitSpec("bytes="); const std::string byteUnitSpec("bytes=");
if ( kiwix::startsWith(range, byteUnitSpec) ) { if ( kiwix::startsWith(range, byteUnitSpec) ) {
range.erase(0, byteUnitSpec.size()); range.erase(0, byteUnitSpec.size());
@ -112,10 +112,11 @@ void RequestContext::parse_byte_range(std::string range)
end = -1; end = -1;
} }
if (iss.eof()) { if (iss.eof()) {
range_pair = std::pair<int, int>(start, end); byteRange = ByteRange(start, end);
} }
} }
} }
return byteRange;
} }
@ -199,7 +200,7 @@ bool RequestContext::has_range() const {
return range_pair.first <= range_pair.second; return range_pair.first <= range_pair.second;
} }
std::pair<int, int> RequestContext::get_range() const { RequestContext::ByteRange RequestContext::get_range() const {
return range_pair; return range_pair;
} }

View File

@ -87,7 +87,7 @@ class RequestContext {
bool can_compress() const { return acceptEncodingDeflate; } bool can_compress() const { return acceptEncodingDeflate; }
private: // functions private: // functions
void parse_byte_range(std::string range); static ByteRange parse_byte_range(std::string range);
private: // data private: // data
std::string full_url; std::string full_url;