From d2636a68c4250e09505254661f078d0bc753d8ae Mon Sep 17 00:00:00 2001 From: kelson42 Date: Tue, 22 Jun 2010 19:19:15 +0000 Subject: [PATCH] + kiwix-serve defalte feature --- src/server/kiwix-serve.cpp | 41 ++++++++++++++++++++++++++++++++++---- 1 file changed, 37 insertions(+), 4 deletions(-) diff --git a/src/server/kiwix-serve.cpp b/src/server/kiwix-serve.cpp index 7364483..571a50d 100644 --- a/src/server/kiwix-serve.cpp +++ b/src/server/kiwix-serve.cpp @@ -18,6 +18,7 @@ #include #include #include +#include #include #include @@ -111,6 +112,7 @@ static kiwix::Reader* reader; static kiwix::Searcher* searcher; static pthread_mutex_t readerLock = PTHREAD_MUTEX_INITIALIZER; static pthread_mutex_t searcherLock = PTHREAD_MUTEX_INITIALIZER; +static pthread_mutex_t compressorLock = PTHREAD_MUTEX_INITIALIZER; static bool hasSearchIndex = false; static void appendToFirstOccurence(string &content, const string regex, const string &replacement) { @@ -126,6 +128,11 @@ static void appendToFirstOccurence(string &content, const string regex, const st regfree(®exp); } +/* For compression */ +#define COMPRESSOR_BUFFER_SIZE 5000000 +static Bytef *compr = (Bytef *)malloc(COMPRESSOR_BUFFER_SIZE); +static uLongf comprLen; + static int accessHandlerCallback(void *cls, struct MHD_Connection * connection, const char * url, @@ -146,6 +153,11 @@ static int accessHandlerCallback(void *cls, return MHD_YES; } + /* Check if the response can be compressed */ + const string acceptEncodingHeaderValue = MHD_lookup_connection_value(connection, MHD_HEADER_KIND, MHD_HTTP_HEADER_ACCEPT_ENCODING) ? + MHD_lookup_connection_value(connection, MHD_HEADER_KIND, MHD_HTTP_HEADER_ACCEPT_ENCODING) : ""; + const bool acceptEncodingDeflate = (!acceptEncodingHeaderValue.empty() && acceptEncodingHeaderValue.find("deflate") != string::npos ? true : false ); + /* Prepare the variables */ struct MHD_Response *response; string content = ""; @@ -177,16 +189,15 @@ static int accessHandlerCallback(void *cls, content += "\n"; mimeType = "text/html; charset=utf-8"; - contentLength = content.size(); /* Mutex unlock */ pthread_mutex_unlock(&searcherLock); } else { - + /* urlstr */ std::string urlStr = string(url); - + /* Mutex Lock */ pthread_mutex_lock(&readerLock); @@ -213,7 +224,22 @@ static int accessHandlerCallback(void *cls, if (hasSearchIndex && mimeType.find("text/html") != string::npos) { appendToFirstOccurence(content, "", HTMLScripts); appendToFirstOccurence(content, "]*>", HTMLDiv); - contentLength = content.size(); + } + + /* Compute the lengh */ + contentLength = content.size(); + + /* Compress the content if necessary */ + if (acceptEncodingDeflate && mimeType.find("text/html") != string::npos) { + pthread_mutex_lock(&compressorLock); + comprLen = COMPRESSOR_BUFFER_SIZE; + + compress(compr, &comprLen, (const Bytef*)(content.data()), contentLength); + + content = string((char *)compr, comprLen); + contentLength = comprLen; + + pthread_mutex_unlock(&compressorLock); } /* clear context pointer */ @@ -225,6 +251,11 @@ static int accessHandlerCallback(void *cls, MHD_NO, MHD_YES); + /* Add if necessary the content-encoding */ + if (acceptEncodingDeflate && mimeType.find("text/html") != string::npos) { + MHD_add_response_header(response, "Content-encoding", "deflate"); + } + /* Specify the mime type */ MHD_add_response_header(response, "Content-Type", mimeType.c_str()); @@ -338,6 +369,7 @@ int main(int argc, char **argv) { /* Mutex init */ pthread_mutex_init(&readerLock, NULL); pthread_mutex_init(&searcherLock, NULL); + pthread_mutex_init(&compressorLock, NULL); /* Start the HTTP daemon */ daemon = MHD_start_daemon(MHD_USE_THREAD_PER_CONNECTION, @@ -362,6 +394,7 @@ int main(int argc, char **argv) { /* Mutex destroy */ pthread_mutex_destroy(&readerLock); pthread_mutex_destroy(&searcherLock); + pthread_mutex_destroy(&compressorLock); exit(0); }