mirror of
https://github.com/kiwix/kiwix-tools.git
synced 2025-09-24 04:20:56 -04:00
Merge pull request #16 from kiwix/kiwix-serve.bugfix
Kiwix serve.bugfix
This commit is contained in:
commit
25091c320b
@ -49,6 +49,7 @@ extern "C" {
|
||||
#include <getopt.h>
|
||||
#include <iostream>
|
||||
#include <string>
|
||||
#include <vector>
|
||||
#include <map>
|
||||
#include <fstream>
|
||||
#include <iostream>
|
||||
@ -140,50 +141,47 @@ bool isVerbose() {
|
||||
return value;
|
||||
}
|
||||
|
||||
/* For compression */
|
||||
#define COMPRESSOR_BUFFER_SIZE 10000000
|
||||
static Bytef *compr = (Bytef *)malloc(COMPRESSOR_BUFFER_SIZE);
|
||||
static uLongf comprLen;
|
||||
|
||||
|
||||
static
|
||||
bool compress_content(string &content,
|
||||
const string &mimeType)
|
||||
{
|
||||
static std::vector<Bytef> compr_buffer;
|
||||
|
||||
/* Should be deflate */
|
||||
bool deflated = mimeType.find("text/") != string::npos ||
|
||||
mimeType.find("application/javascript") != string::npos ||
|
||||
mimeType.find("application/json") != string::npos;
|
||||
|
||||
if ( ! deflated )
|
||||
return false;
|
||||
|
||||
/* Compute the lengh */
|
||||
unsigned int contentLength = content.size();
|
||||
|
||||
/* Should be deflate */
|
||||
bool deflated =
|
||||
contentLength > KIWIX_MIN_CONTENT_SIZE_TO_DEFLATE &&
|
||||
contentLength < COMPRESSOR_BUFFER_SIZE &&
|
||||
(mimeType.find("text/") != string::npos ||
|
||||
mimeType.find("application/javascript") != string::npos ||
|
||||
mimeType.find("application/json") != string::npos);
|
||||
/* If the content is too short, no need to compress it */
|
||||
if ( contentLength <= KIWIX_MIN_CONTENT_SIZE_TO_DEFLATE)
|
||||
return false;
|
||||
|
||||
uLong bufferBound = compressBound(contentLength);
|
||||
|
||||
/* Compress the content if necessary */
|
||||
if (deflated) {
|
||||
pthread_mutex_lock(&compressorLock);
|
||||
comprLen = COMPRESSOR_BUFFER_SIZE;
|
||||
compress(compr, &comprLen, (const Bytef*)(content.data()), contentLength);
|
||||
|
||||
if (comprLen > 2 && comprLen < contentLength) {
|
||||
compr_buffer.reserve(bufferBound);
|
||||
uLongf comprLen = compr_buffer.capacity();
|
||||
int err = compress(&compr_buffer[0], &comprLen, (const Bytef*)(content.data()), contentLength);
|
||||
|
||||
if (err == Z_OK && comprLen > 2 && comprLen < (contentLength+2)) {
|
||||
/* /!\ Internet Explorer has a bug with deflate compression.
|
||||
It can not handle the first two bytes (compression headers)
|
||||
We need to chunk them off (move the content 2bytes)
|
||||
It has no incidence on other browsers
|
||||
See http://www.subbu.org/blog/2008/03/ie7-deflate-or-not and comments */
|
||||
compr += 2;
|
||||
|
||||
content = string((char *)compr, comprLen);
|
||||
contentLength = comprLen;
|
||||
content = string((char *)&compr_buffer[2], comprLen-2);
|
||||
} else {
|
||||
deflated = false;
|
||||
}
|
||||
|
||||
pthread_mutex_unlock(&compressorLock);
|
||||
}
|
||||
return deflated;
|
||||
}
|
||||
|
||||
@ -426,7 +424,6 @@ struct MHD_Response* handle_random(struct MHD_Connection * connection,
|
||||
bool acceptEncodingDeflate)
|
||||
{
|
||||
std::string httpRedirection;
|
||||
bool cacheEnabled = false;
|
||||
httpResponseCode = MHD_HTTP_FOUND;
|
||||
if (reader != NULL) {
|
||||
pthread_mutex_lock(&readerLock);
|
||||
@ -449,7 +446,6 @@ struct MHD_Response* handle_content(struct MHD_Connection * connection,
|
||||
std::string baseUrl;
|
||||
std::string content;
|
||||
std::string mimeType;
|
||||
unsigned int contentLength;
|
||||
|
||||
bool found = false;
|
||||
zim::Article article;
|
||||
@ -591,7 +587,7 @@ static int accessHandlerCallback(void *cls,
|
||||
const char* acceptEncodingHeaderValue = MHD_lookup_connection_value(connection, MHD_HEADER_KIND, MHD_HTTP_HEADER_ACCEPT_ENCODING);
|
||||
const bool acceptEncodingDeflate = acceptEncodingHeaderValue && string(acceptEncodingHeaderValue).find("deflate") != string::npos;
|
||||
|
||||
/* Check if range is requested */
|
||||
/* Check if range is requested. [TODO] Handle this somehow */
|
||||
const char* acceptRangeHeaderValue = MHD_lookup_connection_value(connection, MHD_HEADER_KIND, MHD_HTTP_HEADER_RANGE);
|
||||
const bool acceptRange = acceptRangeHeaderValue != NULL;
|
||||
|
||||
@ -956,7 +952,6 @@ int main(int argc, char **argv) {
|
||||
struct sockaddr_in sockAddr;
|
||||
struct ifaddrs *ifaddr, *ifa;
|
||||
int family, n;
|
||||
char host[NI_MAXHOST];
|
||||
|
||||
/* Search all available interfaces */
|
||||
if (getifaddrs(&ifaddr) == -1) {
|
||||
|
Loading…
x
Reference in New Issue
Block a user