mirror of https://github.com/kiwix/libkiwix.git
Refactoring: extracted get_range_len()
This commit is contained in:
parent
21c6de2f80
commit
a004d96cd7
|
@ -860,6 +860,13 @@ bool is_compressible_mime_type(const std::string& mimeType)
|
||||||
|| mimeType.find("application/json") != string::npos;
|
|| 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
|
} // unnamed namespace
|
||||||
|
|
||||||
std::shared_ptr<Reader>
|
std::shared_ptr<Reader>
|
||||||
|
@ -926,11 +933,9 @@ Response InternalServer::handle_content(const RequestContext& request)
|
||||||
printf("mimeType: %s\n", mimeType.c_str());
|
printf("mimeType: %s\n", mimeType.c_str());
|
||||||
}
|
}
|
||||||
|
|
||||||
std::string content;
|
|
||||||
|
|
||||||
if ( is_compressible_mime_type(mimeType) ) {
|
if ( is_compressible_mime_type(mimeType) ) {
|
||||||
zim::Blob raw_content = entry.getBlob();
|
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();
|
auto response = get_default_response();
|
||||||
|
|
||||||
if (mimeType.find("text/html") != string::npos)
|
if (mimeType.find("text/html") != string::npos)
|
||||||
|
@ -942,12 +947,7 @@ Response InternalServer::handle_content(const RequestContext& request)
|
||||||
response.set_cache(true);
|
response.set_cache(true);
|
||||||
return response;
|
return response;
|
||||||
} else {
|
} else {
|
||||||
int range_len;
|
const int range_len = get_range_len(entry, request.get_range());
|
||||||
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;
|
|
||||||
}
|
|
||||||
auto response = get_default_response();
|
auto response = get_default_response();
|
||||||
response.set_entry(entry);
|
response.set_entry(entry);
|
||||||
response.set_mimeType(mimeType);
|
response.set_mimeType(mimeType);
|
||||||
|
|
|
@ -51,7 +51,10 @@ class IndexError: public std::runtime_error {};
|
||||||
|
|
||||||
|
|
||||||
class RequestContext {
|
class RequestContext {
|
||||||
public:
|
public: // types
|
||||||
|
typedef std::pair<int, int> ByteRange;
|
||||||
|
|
||||||
|
public: // functions
|
||||||
RequestContext(struct MHD_Connection* connection,
|
RequestContext(struct MHD_Connection* connection,
|
||||||
std::string rootLocation,
|
std::string rootLocation,
|
||||||
const std::string& url,
|
const std::string& url,
|
||||||
|
@ -79,11 +82,11 @@ class RequestContext {
|
||||||
std::string get_full_url() const;
|
std::string get_full_url() const;
|
||||||
|
|
||||||
bool has_range() const;
|
bool has_range() const;
|
||||||
std::pair<int, int> get_range() const;
|
ByteRange get_range() const;
|
||||||
|
|
||||||
bool can_compress() const { return acceptEncodingDeflate; }
|
bool can_compress() const { return acceptEncodingDeflate; }
|
||||||
|
|
||||||
private:
|
private: // data
|
||||||
std::string full_url;
|
std::string full_url;
|
||||||
std::string url;
|
std::string url;
|
||||||
RequestMethod method;
|
RequestMethod method;
|
||||||
|
@ -93,10 +96,11 @@ class RequestContext {
|
||||||
bool acceptEncodingDeflate;
|
bool acceptEncodingDeflate;
|
||||||
|
|
||||||
bool accept_range;
|
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> headers;
|
||||||
std::map<std::string, std::string> arguments;
|
std::map<std::string, std::string> arguments;
|
||||||
|
|
||||||
|
private: // functions
|
||||||
static int fill_header(void *, enum MHD_ValueKind, const char*, const char*);
|
static int fill_header(void *, enum MHD_ValueKind, const char*, const char*);
|
||||||
static int fill_argument(void *, enum MHD_ValueKind, const char*, const char*);
|
static int fill_argument(void *, enum MHD_ValueKind, const char*, const char*);
|
||||||
};
|
};
|
||||||
|
|
Loading…
Reference in New Issue