Refactoring: extracted get_range_len()

This commit is contained in:
Veloman Yunkan 2020-04-16 18:05:05 +04:00
parent 21c6de2f80
commit a004d96cd7
2 changed files with 17 additions and 13 deletions

View File

@ -860,6 +860,13 @@ bool is_compressible_mime_type(const std::string& mimeType)
|| mimeType.find("application/json") != string::npos;
}
int get_range_len(const kiwix::Entry& entry, RequestContext::ByteRange range)
{
return range.second == -1
? entry.getSize() - range.first
: range.second - range.first;
}
} // unnamed namespace
std::shared_ptr<Reader>
@ -926,11 +933,9 @@ Response InternalServer::handle_content(const RequestContext& request)
printf("mimeType: %s\n", mimeType.c_str());
}
std::string content;
if ( is_compressible_mime_type(mimeType) ) {
zim::Blob raw_content = entry.getBlob();
content = string(raw_content.data(), raw_content.size());
const std::string content = string(raw_content.data(), raw_content.size());
auto response = get_default_response();
if (mimeType.find("text/html") != string::npos)
@ -942,12 +947,7 @@ Response InternalServer::handle_content(const RequestContext& request)
response.set_cache(true);
return response;
} else {
int range_len;
if (request.get_range().second == -1) {
range_len = entry.getSize() - request.get_range().first;
} else {
range_len = request.get_range().second - request.get_range().first;
}
const int range_len = get_range_len(entry, request.get_range());
auto response = get_default_response();
response.set_entry(entry);
response.set_mimeType(mimeType);

View File

@ -51,7 +51,10 @@ class IndexError: public std::runtime_error {};
class RequestContext {
public:
public: // types
typedef std::pair<int, int> ByteRange;
public: // functions
RequestContext(struct MHD_Connection* connection,
std::string rootLocation,
const std::string& url,
@ -79,11 +82,11 @@ class RequestContext {
std::string get_full_url() const;
bool has_range() const;
std::pair<int, int> get_range() const;
ByteRange get_range() const;
bool can_compress() const { return acceptEncodingDeflate; }
private:
private: // data
std::string full_url;
std::string url;
RequestMethod method;
@ -93,10 +96,11 @@ class RequestContext {
bool acceptEncodingDeflate;
bool accept_range;
std::pair<int, int> range_pair;
ByteRange range_pair;
std::map<std::string, std::string> headers;
std::map<std::string, std::string> arguments;
private: // functions
static int fill_header(void *, enum MHD_ValueKind, const char*, const char*);
static int fill_argument(void *, enum MHD_ValueKind, const char*, const char*);
};