Converted RequestContext::ByteRange to a class

Also renamed the `range_pair` data member of `RequestContext` to `byteRange_`
This commit is contained in:
Veloman Yunkan 2020-05-23 19:59:47 +04:00
parent 54db6049b7
commit 3fba8c20a0
3 changed files with 22 additions and 11 deletions

View File

@ -104,7 +104,7 @@ RequestContext::RequestContext(struct MHD_Connection* connection,
version(version), version(version),
requestIndex(s_requestIndex++), requestIndex(s_requestIndex++),
acceptEncodingDeflate(false), acceptEncodingDeflate(false),
range_pair(0, -1) byteRange_()
{ {
MHD_get_connection_values(connection, MHD_HEADER_KIND, &RequestContext::fill_header, this); MHD_get_connection_values(connection, MHD_HEADER_KIND, &RequestContext::fill_header, this);
MHD_get_connection_values(connection, MHD_GET_ARGUMENT_KIND, &RequestContext::fill_argument, this); MHD_get_connection_values(connection, MHD_GET_ARGUMENT_KIND, &RequestContext::fill_argument, this);
@ -115,7 +115,7 @@ RequestContext::RequestContext(struct MHD_Connection* connection,
} catch (const std::out_of_range&) {} } catch (const std::out_of_range&) {}
try { try {
range_pair = parse_byte_range(get_header(MHD_HTTP_HEADER_RANGE)); byteRange_ = parse_byte_range(get_header(MHD_HTTP_HEADER_RANGE));
} catch (const std::out_of_range&) {} } catch (const std::out_of_range&) {}
} }
@ -199,11 +199,11 @@ bool RequestContext::is_valid_url() const {
} }
bool RequestContext::has_range() const { bool RequestContext::has_range() const {
return range_pair.first <= range_pair.second; return byteRange_.first() <= byteRange_.last();
} }
RequestContext::ByteRange RequestContext::get_range() const { RequestContext::ByteRange RequestContext::get_range() const {
return range_pair; return byteRange_;
} }
template<> template<>

View File

@ -52,7 +52,18 @@ class IndexError: public std::runtime_error {};
class RequestContext { class RequestContext {
public: // types public: // types
typedef std::pair<int, int> ByteRange; class ByteRange {
public: // functions
ByteRange() : ByteRange(0, -1) {}
ByteRange(int64_t first, int64_t last) : first_(first), last_(last) {}
int64_t first() const { return first_; }
int64_t last() const { return last_; }
private: // data
int64_t first_;
int64_t last_;
};
public: // functions public: // functions
RequestContext(struct MHD_Connection* connection, RequestContext(struct MHD_Connection* connection,
@ -95,7 +106,7 @@ class RequestContext {
bool acceptEncodingDeflate; bool acceptEncodingDeflate;
ByteRange range_pair; ByteRange byteRange_;
std::map<std::string, std::string> headers; std::map<std::string, std::string> headers;
std::map<std::string, std::string> arguments; std::map<std::string, std::string> arguments;

View File

@ -41,10 +41,10 @@ bool is_compressible_mime_type(const std::string& mimeType)
int get_range_len(const kiwix::Entry& entry, RequestContext::ByteRange range) int get_range_len(const kiwix::Entry& entry, RequestContext::ByteRange range)
{ {
const int entrySize = entry.getSize(); const int64_t entrySize = entry.getSize();
return range.second == -1 return range.last() == -1
? entrySize - range.first ? entrySize - range.first()
: std::min(range.second + 1, entrySize) - range.first; : std::min(range.last() + 1, entrySize) - range.first();
} }
} // unnamed namespace } // unnamed namespace
@ -330,7 +330,7 @@ void Response::set_entry(const Entry& entry, const RequestContext& request) {
set_compress(true); set_compress(true);
} else { } else {
const int range_len = get_range_len(entry, request.get_range()); const int range_len = get_range_len(entry, request.get_range());
set_range_first(request.get_range().first); set_range_first(request.get_range().first());
set_range_len(range_len); set_range_len(range_len);
} }
} }