From de37489c5357da79ae2b70a432b031e2e5d163bf Mon Sep 17 00:00:00 2001 From: Veloman Yunkan Date: Fri, 22 May 2020 17:12:54 +0400 Subject: [PATCH] Range header starts with a unit spec After this commit valid ranges of the form "bytes=firstbyte-lastbyte" should be handled correctly. --- src/server/request_context.cpp | 35 +++++++++++++++++++--------------- 1 file changed, 20 insertions(+), 15 deletions(-) diff --git a/src/server/request_context.cpp b/src/server/request_context.cpp index 37d490bf9..1ea184ebf 100644 --- a/src/server/request_context.cpp +++ b/src/server/request_context.cpp @@ -20,6 +20,7 @@ #include "request_context.h" +#include "tools/stringTools.h" #include #include #include @@ -87,22 +88,26 @@ RequestContext::RequestContext(struct MHD_Connection* connection, /*Check if range is requested. */ try { - auto range = get_header(MHD_HTTP_HEADER_RANGE); - int start = 0; - int end = -1; - std::istringstream iss(range); - char c; + std::string range = get_header(MHD_HTTP_HEADER_RANGE); + const std::string byteUnitSpec("bytes="); + if ( kiwix::startsWith(range, byteUnitSpec) ) { + range.erase(0, byteUnitSpec.size()); + int start = 0; + int end = -1; + std::istringstream iss(range); + char c; - iss >> start >> c; - if (iss.good() && c=='-') { - iss >> end; - if (iss.fail()) { - // Something went wrong will extracting. - end = -1; - } - if (iss.eof()) { - accept_range = true; - range_pair = std::pair(start, end); + iss >> start >> c; + if (iss.good() && c=='-') { + iss >> end; + if (iss.fail()) { + // Something went wrong will extracting. + end = -1; + } + if (iss.eof()) { + accept_range = true; + range_pair = std::pair(start, end); + } } } } catch (const std::out_of_range&) {}