Range header starts with a unit spec

After this commit valid ranges of the form "bytes=firstbyte-lastbyte" should
be handled correctly.
This commit is contained in:
Veloman Yunkan 2020-05-22 17:12:54 +04:00
parent 2a35a86de6
commit de37489c53
1 changed files with 20 additions and 15 deletions

View File

@ -20,6 +20,7 @@
#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>
@ -87,22 +88,26 @@ RequestContext::RequestContext(struct MHD_Connection* connection,
/*Check if range is requested. */ /*Check if range is requested. */
try { try {
auto range = get_header(MHD_HTTP_HEADER_RANGE); std::string range = get_header(MHD_HTTP_HEADER_RANGE);
int start = 0; const std::string byteUnitSpec("bytes=");
int end = -1; if ( kiwix::startsWith(range, byteUnitSpec) ) {
std::istringstream iss(range); range.erase(0, byteUnitSpec.size());
char c; int start = 0;
int end = -1;
std::istringstream iss(range);
char c;
iss >> start >> c; iss >> start >> c;
if (iss.good() && c=='-') { if (iss.good() && c=='-') {
iss >> end; iss >> end;
if (iss.fail()) { if (iss.fail()) {
// Something went wrong will extracting. // Something went wrong will extracting.
end = -1; end = -1;
} }
if (iss.eof()) { if (iss.eof()) {
accept_range = true; accept_range = true;
range_pair = std::pair<int, int>(start, end); range_pair = std::pair<int, int>(start, end);
}
} }
} }
} catch (const std::out_of_range&) {} } catch (const std::out_of_range&) {}