From 189d97b220ca1e9601df9e0cafd6bfe81bcfcd55 Mon Sep 17 00:00:00 2001 From: kelson42 Date: Sun, 20 Dec 2009 17:16:07 +0000 Subject: [PATCH] + kiwix-serve stub --- src/server/kiwix-serve.cpp | 181 +++++++++++++++++++++++++++++++++++++ 1 file changed, 181 insertions(+) create mode 100644 src/server/kiwix-serve.cpp diff --git a/src/server/kiwix-serve.cpp b/src/server/kiwix-serve.cpp new file mode 100644 index 0000000..04e239a --- /dev/null +++ b/src/server/kiwix-serve.cpp @@ -0,0 +1,181 @@ +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include + +using namespace std; + +zim::File* zimFileHandler; + +static int accessHandlerCallback(void *cls, + struct MHD_Connection * connection, + const char * url, + const char * method, + const char * version, + const char * upload_data, + size_t * upload_data_size, + void ** ptr) { + + /* Unexpected method */ + if (0 != strcmp(method, "GET")) + return MHD_NO; + + /* The first time only the headers are valid, do not respond in the first round... */ + static int dummy; + if (&dummy != *ptr) { + *ptr = &dummy; + return MHD_YES; + } + + /* Prepare the variable */ + zim::Article article; + struct MHD_Response * response; + //const char *page = (char*)cls; + const char *content; + unsigned int contentLength = 0; + const char *mimeType; + string mimeTypeModified = ""; + + /* Prepare the url */ + unsigned int urlLength = strlen(url); + unsigned int offset = 0; + + /* Ignore the '/' */ + while((offset < urlLength) && (url[offset] == '/')) offset++; + + /* Get namespace */ + char ns[1024]; + unsigned int nsOffset = 0; + while((offset < urlLength) && (url[offset] != '/')) { + ns[nsOffset] = url[offset]; + offset++; + nsOffset++; + } + ns[nsOffset] = 0; + + /* Ignore the '/' */ + while((offset < urlLength) && (url[offset] == '/')) offset++; + + /* Get content title */ + char title[1024]; + unsigned int titleOffset = 0; + while((offset < urlLength) && (url[offset] != '/')) { + title[titleOffset] = url[offset]; + offset++; + titleOffset++; + } + title[titleOffset] = 0; + + /* Load the article from the ZIM file */ + cout << "Loading '" << title << "' in namespace '" << ns << "'" << endl; + try { + std::pair resultPair = zimFileHandler->findx(ns[0], zim::QUnicodeString(title)); + + /* Test if the article was found */ + if (resultPair.first == true) { + + /* Get the article */ + zim::Article article = zimFileHandler->getArticle(resultPair.second.getIndex()); + + /* If redirect */ + unsigned int loopCounter = 0; + while (article.isRedirect() && loopCounter++<42) { + article = article.getRedirectArticle(); + } + + /* Get the content */ + content = article.getData().data(); + contentLength = article.getArticleSize(); + + /* Get the content mime-type */ + mimeType = article.getMimeType().data(); + unsigned int mimeTypeLength = strlen(mimeType); + for (int i=0; i 4) { + cout << "Usage: ZIM_PATH PORT [INDEX_PATH]" << endl; + exit(1); + } + + string zimPath = (argv[1]); + int port = atoi(argv[2]); + void *page = strdup("42222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222"); + + /* Start the HTTP daemon */ + daemon = MHD_start_daemon(MHD_USE_THREAD_PER_CONNECTION, + port, + NULL, + NULL, + &accessHandlerCallback, + page, + MHD_OPTION_END); + + if (daemon == NULL) { + cout << "Unable to instanciate the HTTP daemon."<< endl; + exit(1); + } + + /* Instanciate the ZIM file handler */ + try { + zimFileHandler = new zim::File(zimPath); + } catch (...) { + cout << "Unable to open the ZIM file '" << zimPath << "'." << endl; + exit(1); + } + + /* Run endless */ + while (42) sleep(1); + + /* Stop the daemon */ + MHD_stop_daemon(daemon); + + exit(0); +}