From 67a347c0c47c253a80792c19c837ae7e1dc2f070 Mon Sep 17 00:00:00 2001 From: Veloman Yunkan Date: Mon, 25 May 2020 17:21:10 +0400 Subject: [PATCH] Moved byte-range parsing to byte_range.cpp --- src/server/byte_range.cpp | 28 ++++++++++++++++++++++++++++ src/server/byte_range.h | 3 +++ src/server/request_context.cpp | 29 +---------------------------- 3 files changed, 32 insertions(+), 28 deletions(-) diff --git a/src/server/byte_range.cpp b/src/server/byte_range.cpp index 5482914ab..14c2c33ea 100644 --- a/src/server/byte_range.cpp +++ b/src/server/byte_range.cpp @@ -20,6 +20,8 @@ #include "byte_range.h" +#include "tools/stringTools.h" + namespace kiwix { ByteRange::ByteRange() @@ -34,4 +36,30 @@ ByteRange::ByteRange(Kind kind, int64_t first, int64_t 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 diff --git a/src/server/byte_range.h b/src/server/byte_range.h index 4df6bd93c..7bdbbc090 100644 --- a/src/server/byte_range.h +++ b/src/server/byte_range.h @@ -22,6 +22,7 @@ #define KIWIXLIB_SERVER_BYTE_RANGE_H #include +#include namespace kiwix { @@ -51,6 +52,8 @@ class ByteRange int64_t last() const { return last_; } int64_t length() const { return last_ + 1 - first_; } + static ByteRange parse(std::string rangeStr); + private: // data Kind kind_; int64_t first_; diff --git a/src/server/request_context.cpp b/src/server/request_context.cpp index 931069b10..55836f4f4 100644 --- a/src/server/request_context.cpp +++ b/src/server/request_context.cpp @@ -20,7 +20,6 @@ #include "request_context.h" -#include "tools/stringTools.h" #include #include #include @@ -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 @@ -114,7 +87,7 @@ RequestContext::RequestContext(struct MHD_Connection* connection, } catch (const std::out_of_range&) {} 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&) {} }