From 4e3ff03059aa1741d024f0b21010262e2fa243bc Mon Sep 17 00:00:00 2001 From: Matthieu Gautier Date: Wed, 5 Jul 2017 15:52:32 +0200 Subject: [PATCH] Format all the code using clang-format. Add a script `format_code.sh` to easily format the code. --- .clang-format | 12 + format_code.sh | 15 + src/installer/kiwix-install.cpp | 112 +++-- src/manager/kiwix-manage.cpp | 176 +++---- src/reader/kiwix-read.cpp | 68 +-- src/searcher/kiwix-search.cpp | 80 ++-- src/server/kiwix-serve.cpp | 796 ++++++++++++++++++-------------- 7 files changed, 697 insertions(+), 562 deletions(-) create mode 100644 .clang-format create mode 100755 format_code.sh diff --git a/.clang-format b/.clang-format new file mode 100644 index 0000000..d5bd238 --- /dev/null +++ b/.clang-format @@ -0,0 +1,12 @@ +BasedOnStyle: Google +BinPackArguments: false +BinPackParameters: false +BreakBeforeBinaryOperators: All +BreakBeforeBraces: Linux +DerivePointerAlignment: false +SpacesInContainerLiterals: false +Standard: Cpp11 + +AllowShortFunctionsOnASingleLine: Inline +AllowShortIfStatementsOnASingleLine: false +AllowShortLoopsOnASingleLine: false diff --git a/format_code.sh b/format_code.sh new file mode 100755 index 0000000..f952404 --- /dev/null +++ b/format_code.sh @@ -0,0 +1,15 @@ +#!/usr/bin/bash + +files=( +"src/installer/kiwix-install.cpp" +"src/searcher/kiwix-search.cpp" +"src/reader/kiwix-read.cpp" +"src/manager/kiwix-manage.cpp" +"src/server/kiwix-serve.cpp" +) + +for i in "${files[@]}" +do + echo $i + clang-format -i -style=file $i +done diff --git a/src/installer/kiwix-install.cpp b/src/installer/kiwix-install.cpp index e271c66..b0b98ea 100644 --- a/src/installer/kiwix-install.cpp +++ b/src/installer/kiwix-install.cpp @@ -19,21 +19,23 @@ #include #include -#include #include +#include enum supportedAction { NONE, ADDCONTENT }; -void usage() { - cout << "Usage: kiwix-install [--verbose] addcontent ZIM_PATH KIWIX_PATH" << endl; - exit(1); +void usage() +{ + cout << "Usage: kiwix-install [--verbose] addcontent ZIM_PATH KIWIX_PATH" + << endl; + exit(1); } -int main(int argc, char **argv) { - +int main(int argc, char** argv) +{ /* Init the variables */ - const char *contentPath = NULL; - const char *kiwixPath = NULL; + const char* contentPath = NULL; + const char* kiwixPath = NULL; supportedAction action = NONE; bool verboseFlag = false; int option_index = 0; @@ -41,36 +43,33 @@ int main(int argc, char **argv) { /* Argument parsing */ while (42) { - - static struct option long_options[] = { - {"verbose", no_argument, 0, 'v'}, - {0, 0, 0, 0} - }; + static struct option long_options[] + = {{"verbose", no_argument, 0, 'v'}, {0, 0, 0, 0}}; if (c != -1) { c = getopt_long(argc, argv, "vi", long_options, &option_index); switch (c) { - case 'v': - verboseFlag = true; - break; + case 'v': + verboseFlag = true; + break; } } else { if (optind < argc) { - if (action == NONE) { - string actionString = argv[optind++]; - if (actionString == "addcontent" || actionString == "ADDCONTENT") { - action = ADDCONTENT; - } - } else if (contentPath == NULL) { - contentPath = argv[optind++]; - } else if (kiwixPath == NULL) { - kiwixPath = argv[optind++]; - } else { - usage(); - } + if (action == NONE) { + string actionString = argv[optind++]; + if (actionString == "addcontent" || actionString == "ADDCONTENT") { + action = ADDCONTENT; + } + } else if (contentPath == NULL) { + contentPath = argv[optind++]; + } else if (kiwixPath == NULL) { + kiwixPath = argv[optind++]; + } else { + usage(); + } } else { - break; + break; } } } @@ -82,38 +81,48 @@ int main(int argc, char **argv) { /* Make the action */ if (action == ADDCONTENT) { - /* Check if the content path exists and is readable */ - if (verboseFlag) { std::cout << "Check if the ZIM file exists..." << std::endl; } + if (verboseFlag) { + std::cout << "Check if the ZIM file exists..." << std::endl; + } if (!fileExists(contentPath)) { - cerr << "The content path '" << contentPath << "' does not exist or is not readable." << endl; + cerr << "The content path '" << contentPath + << "' does not exist or is not readable." << endl; exit(1); } /* Check if this is a ZIM file */ try { - if (verboseFlag) { std::cout << "Check if the ZIM file is valid..." << std::endl; } - kiwix::Reader *reader = new kiwix::Reader(contentPath); + if (verboseFlag) { + std::cout << "Check if the ZIM file is valid..." << std::endl; + } + kiwix::Reader* reader = new kiwix::Reader(contentPath); delete reader; - } catch (exception &e) { - cerr << "The content available at '" << contentPath << "' is not a ZIM file." << endl; + } catch (exception& e) { + cerr << "The content available at '" << contentPath + << "' is not a ZIM file." << endl; exit(1); } string contentFilename = getLastPathElement(contentPath); /* Check if kiwixPath/kiwix/kiwix.exe exists */ - if (verboseFlag) { std::cout << "Check if the Kiwix path is valid..." << std::endl; } + if (verboseFlag) { + std::cout << "Check if the Kiwix path is valid..." << std::endl; + } string kiwixBinaryPath = computeAbsolutePath(kiwixPath, "kiwix/kiwix.exe"); if (!fileExists(kiwixBinaryPath)) { kiwixBinaryPath = computeAbsolutePath(kiwixPath, "kiwix/kiwix"); if (!fileExists(kiwixBinaryPath)) { - cerr << "Unable to find the Kiwix binary at '" << kiwixBinaryPath << "[.exe]'." << endl; - exit(1); + cerr << "Unable to find the Kiwix binary at '" << kiwixBinaryPath + << "[.exe]'." << endl; + exit(1); } } /* Check if the directory "data" structure exists */ - if (verboseFlag) { std::cout << "Check the target data directory structure..." << std::endl; } + if (verboseFlag) { + std::cout << "Check the target data directory structure..." << std::endl; + } string dataPath = computeAbsolutePath(kiwixPath, "data/"); if (!fileExists(dataPath)) { makeDirectory(dataPath); @@ -132,22 +141,31 @@ int main(int argc, char **argv) { } /* Copy the file to the data/content directory */ - if (verboseFlag) { std::cout << "Copy ZIM file to the target directory..." << std::endl; } - string newContentPath = computeAbsolutePath(dataContentPath, contentFilename); - if (!fileExists(newContentPath) || getFileSize(contentPath) != getFileSize(newContentPath)) { + if (verboseFlag) { + std::cout << "Copy ZIM file to the target directory..." << std::endl; + } + string newContentPath + = computeAbsolutePath(dataContentPath, contentFilename); + if (!fileExists(newContentPath) + || getFileSize(contentPath) != getFileSize(newContentPath)) { copyFile(contentPath, newContentPath); } /* Add the file to the library.xml */ - if (verboseFlag) { std::cout << "Create the library XML file..." << std::endl; } + if (verboseFlag) { + std::cout << "Create the library XML file..." << std::endl; + } kiwix::Manager libraryManager; - string libraryPath = computeAbsolutePath(dataLibraryPath, contentFilename + ".xml"); - string bookId = libraryManager.addBookFromPathAndGetId(newContentPath, "../content/" + contentFilename, "", false); + string libraryPath + = computeAbsolutePath(dataLibraryPath, contentFilename + ".xml"); + string bookId = libraryManager.addBookFromPathAndGetId( + newContentPath, "../content/" + contentFilename, "", false); if (!bookId.empty()) { libraryManager.setCurrentBookId(bookId); libraryManager.writeFile(libraryPath); } else { - cerr << "Unable to build or save library file '" << libraryPath << "'" << endl; + cerr << "Unable to build or save library file '" << libraryPath << "'" + << endl; } } diff --git a/src/manager/kiwix-manage.cpp b/src/manager/kiwix-manage.cpp index 41d7e37..6fb2b14 100644 --- a/src/manager/kiwix-manage.cpp +++ b/src/manager/kiwix-manage.cpp @@ -21,47 +21,54 @@ #include #endif #include -#include -#include #include #include +#include +#include using namespace std; enum supportedAction { NONE, ADD, SHOW, REMOVE }; - -void show(kiwix::Library library) { - std::vector::iterator itr; - unsigned int inc = 1; - for ( itr = library.books.begin(); itr != library.books.end(); ++itr ) { - std::cout << "#" << inc++ - << std::endl << "id:\t\t" << itr->id - << std::endl << "path:\t\t" << itr->path - << std::endl << "indexpath:\t" << itr->indexPath - << std::endl << "url:\t\t" << itr->url - << std::endl << "title:\t\t" << itr->title - << std::endl << "name:\t\t" << itr->name - << std::endl << "tags:\t\t" << itr->tags - << std::endl << "description:\t" << itr->description - << std::endl << "creator:\t" << itr->creator - << std::endl << "date:\t\t" << itr->date - << std::endl << "articleCount:\t" << itr->articleCount - << std::endl << "mediaCount:\t" << itr->mediaCount - << std::endl << "size:\t\t" << itr->size << " KB" - << std::endl << std::endl; - } +void show(kiwix::Library library) +{ + std::vector::iterator itr; + unsigned int inc = 1; + for (itr = library.books.begin(); itr != library.books.end(); ++itr) { + std::cout << "#" << inc++ << std::endl + << "id:\t\t" << itr->id << std::endl + << "path:\t\t" << itr->path << std::endl + << "indexpath:\t" << itr->indexPath << std::endl + << "url:\t\t" << itr->url << std::endl + << "title:\t\t" << itr->title << std::endl + << "name:\t\t" << itr->name << std::endl + << "tags:\t\t" << itr->tags << std::endl + << "description:\t" << itr->description << std::endl + << "creator:\t" << itr->creator << std::endl + << "date:\t\t" << itr->date << std::endl + << "articleCount:\t" << itr->articleCount << std::endl + << "mediaCount:\t" << itr->mediaCount << std::endl + << "size:\t\t" << itr->size << " KB" << std::endl + << std::endl; + } } -void usage() { - cerr << "Usage:" << endl; - cerr << "\tkiwix-manage LIBRARY_PATH add ZIM_PATH [--zimPathToSave=../content/foobar.zim] [--current] [--indexBackend=xapian] [--indexPath=FULLTEXT_IDX_PATH] [--url=http://...metalink]" << endl; - cerr << "\tkiwix-manage LIBRARY_PATH show [CONTENTID1] [CONTENTID2] ... (show everything if no param.)" << endl; - cerr << "\tkiwix-manage LIBRARY_PATH remove CONTENTID1 [CONTENTID2]" << endl; +void usage() +{ + cerr << "Usage:" << endl; + cerr << "\tkiwix-manage LIBRARY_PATH add ZIM_PATH " + "[--zimPathToSave=../content/foobar.zim] [--current] " + "[--indexBackend=xapian] [--indexPath=FULLTEXT_IDX_PATH] " + "[--url=http://...metalink]" + << endl; + cerr << "\tkiwix-manage LIBRARY_PATH show [CONTENTID1] [CONTENTID2] ... " + "(show everything if no param.)" + << endl; + cerr << "\tkiwix-manage LIBRARY_PATH remove CONTENTID1 [CONTENTID2]" << endl; } -int main(int argc, char **argv) { - +int main(int argc, char** argv) +{ string libraryPath = ""; supportedAction action = NONE; string zimPath = ""; @@ -89,9 +96,9 @@ int main(int argc, char **argv) { } /* Try to read the file */ - libraryPath = isRelativePath(libraryPath) ? - computeAbsolutePath(getCurrentDirectory(), libraryPath) : - libraryPath; + libraryPath = isRelativePath(libraryPath) + ? computeAbsolutePath(getCurrentDirectory(), libraryPath) + : libraryPath; libraryManager.readFile(libraryPath, false); /* SHOW */ @@ -103,81 +110,78 @@ int main(int argc, char **argv) { string indexPath; kiwix::supportedIndexType indexBackend = kiwix::XAPIAN; string url; - string origID=""; + string origID = ""; bool setCurrent = false; - if (argc>3) { + if (argc > 3) { zimPath = argv[3]; } /* Options parsing */ optind = 2; while (42) { + static struct option long_options[] + = {{"url", required_argument, 0, 'u'}, + {"origId", required_argument, 0, 'o'}, + {"indexPath", required_argument, 0, 'i'}, + {"indexBackend", required_argument, 0, 'b'}, + {"zimPathToSave", required_argument, 0, 'z'}, + {"current", no_argument, 0, 'c'}, + {0, 0, 0, 0}}; - static struct option long_options[] = { - {"url", required_argument, 0, 'u'}, - {"origId", required_argument, 0, 'o'}, - {"indexPath", required_argument, 0, 'i'}, - {"indexBackend", required_argument, 0, 'b'}, - {"zimPathToSave", required_argument, 0, 'z'}, - {"current", no_argument, 0, 'c'}, - {0, 0, 0, 0} - }; - c = getopt_long(argc, argv, "cz:u:i:b:", long_options, &option_index); - + if (c != -1) { + switch (c) { + case 'u': + url = optarg; + break; - switch (c) { - case 'u': - url = optarg; - break; + case 'o': + origID = optarg; + break; - case 'o': - origID = optarg; - break; + case 'c': + setCurrent = true; + break; - case 'c': - setCurrent = true; - break; + case 'i': + indexPath = optarg; + break; - case 'i': - indexPath = optarg; - break; + case 'b': + if (!strcmp(optarg, "xapian")) { + indexBackend = kiwix::XAPIAN; + } else { + usage(); + } + break; - case 'b': - if (!strcmp(optarg, "xapian")) { - indexBackend = kiwix::XAPIAN; - } else { - usage(); - } - break; - - case 'z': - zimPathToSave = optarg; - break; - - } + case 'z': + zimPathToSave = optarg; + break; + } } else { - break; + break; } } if (!zimPath.empty()) { zimPathToSave = zimPathToSave == "." ? zimPath : zimPathToSave; - string bookId = libraryManager.addBookFromPathAndGetId(zimPath, zimPathToSave, url, false); + string bookId = libraryManager.addBookFromPathAndGetId( + zimPath, zimPathToSave, url, false); if (!bookId.empty()) { + if (setCurrent) + libraryManager.setCurrentBookId(bookId); - if (setCurrent) - libraryManager.setCurrentBookId(bookId); - - /* Save the index infos if necessary */ - if (!indexPath.empty()) - libraryManager.setBookIndex(bookId, indexPath, indexBackend); + /* Save the index infos if necessary */ + if (!indexPath.empty()) + libraryManager.setBookIndex(bookId, indexPath, indexBackend); } else { - cerr << "Unable to build or save library file '" << libraryPath << "'" << endl; + cerr << "Unable to build or save library file '" << libraryPath << "'" + << endl; } } else { std::cerr << "Invalid zim file path" << std::endl; @@ -187,7 +191,7 @@ int main(int argc, char **argv) { unsigned int bookIndex = 0; const unsigned int totalBookCount = libraryManager.getBookCount(true, true); - if (argc>3) { + if (argc > 3) { bookIndex = atoi(argv[3]); } @@ -195,9 +199,13 @@ int main(int argc, char **argv) { libraryManager.removeBookByIndex(bookIndex - 1); } else { if (totalBookCount > 0) { - std::cerr << "Invalid book index number. Please give a number between 1 and " << totalBookCount << std::endl; + std::cerr + << "Invalid book index number. Please give a number between 1 and " + << totalBookCount << std::endl; } else { - std::cerr << "Invalid book index number. Library is empty, no book to delete." << std::endl; + std::cerr + << "Invalid book index number. Library is empty, no book to delete." + << std::endl; } } } diff --git a/src/reader/kiwix-read.cpp b/src/reader/kiwix-read.cpp index eccd23a..5e81480 100644 --- a/src/reader/kiwix-read.cpp +++ b/src/reader/kiwix-read.cpp @@ -18,59 +18,60 @@ */ #include -#include -#include -#include #include #include +#include +#include +#include -void usage() { - cout << "Usage: kiwix-read --suggest= ZIM_FILE_PATH" << endl; - exit(1); +void usage() +{ + cout << "Usage: kiwix-read --suggest= ZIM_FILE_PATH" << endl; + exit(1); } -void updateSuggestionTree(tree< pair > &suggestionTree, string suggestion) { +void updateSuggestionTree(tree>& suggestionTree, + string suggestion) +{ return; } -int main(int argc, char **argv) { - +int main(int argc, char** argv) +{ /* Init the variables */ - const char *filePath = NULL; - const char *pattern = NULL; + const char* filePath = NULL; + const char* pattern = NULL; int option_index = 0; int c = 0; - kiwix::Reader *reader = NULL; + kiwix::Reader* reader = NULL; /* Argument parsing */ while (42) { + static struct option long_options[] + = {{"verbose", no_argument, 0, 'v'}, + {"suggest", required_argument, 0, 's'}, + {0, 0, 0, 0}}; - static struct option long_options[] = { - {"verbose", no_argument, 0, 'v'}, - {"suggest", required_argument, 0, 's'}, - {0, 0, 0, 0} - }; - if (c != -1) { c = getopt_long(argc, argv, "vs:", long_options, &option_index); switch (c) { - case 'v': - break; - case 's': - pattern = optarg; - break; + case 'v': + break; + case 's': + pattern = optarg; + break; } } else { if (optind < argc) { - if (filePath == NULL) { - filePath = argv[optind++]; - } else { - usage(); - } + if (filePath == NULL) { + filePath = argv[optind++]; + } else { + usage(); + } } else { - break; + break; } } } @@ -90,24 +91,25 @@ int main(int argc, char **argv) { string contentType; unsigned int contentLength = 0; string suggestion; - + if (pattern != NULL) { std::cout << "Searching suggestions for: " << pattern << std::endl; reader->searchSuggestionsSmart(pattern, 10); while (reader->getNextSuggestion(suggestion)) { - std::cout << suggestion << std::endl; + std::cout << suggestion << std::endl; } } /* - if (reader->getContentByUrl(mainPageUrl, content, contentLength, contentType)) { + if (reader->getContentByUrl(mainPageUrl, content, contentLength, + contentType)) { cout << content << endl; } */ // tree< pair > tree; - //updateSuggestionTree(tree, string(suggestion)); + // updateSuggestionTree(tree, string(suggestion)); delete reader; } else { diff --git a/src/searcher/kiwix-search.cpp b/src/searcher/kiwix-search.cpp index c3f14c7..6da9d42 100644 --- a/src/searcher/kiwix-search.cpp +++ b/src/searcher/kiwix-search.cpp @@ -18,57 +18,56 @@ */ #include -#include #include #include +#include -void usage() { - cout << "Usage: kiwix-search [--verbose|-v] ZIM_PATH SEARCH" << endl; - exit(1); +void usage() +{ + cout << "Usage: kiwix-search [--verbose|-v] ZIM_PATH SEARCH" << endl; + exit(1); } -int main(int argc, char **argv) { - +int main(int argc, char** argv) +{ /* Init the variables */ - //const char *indexPath = "/home/itamar/.www.kiwix.org/kiwix/43k0i1j4.default/6d2e587b-d586-dc6a-dc6a-e4ef035a1495d15c.index"; - //const char *indexPath = "/home/itamar/testindex"; - const char *zimPath = NULL; - const char *search = NULL; + // const char *indexPath = + // "/home/itamar/.www.kiwix.org/kiwix/43k0i1j4.default/6d2e587b-d586-dc6a-dc6a-e4ef035a1495d15c.index"; + // const char *indexPath = "/home/itamar/testindex"; + const char* zimPath = NULL; + const char* search = NULL; bool verboseFlag = false; int option_index = 0; int c = 0; - kiwix::Searcher *searcher = NULL; - kiwix::Reader *reader = NULL; + kiwix::Searcher* searcher = NULL; + kiwix::Reader* reader = NULL; /* Argument parsing */ while (42) { - - static struct option long_options[] = { - {"verbose", no_argument, 0, 'v'}, - {0, 0, 0, 0} - }; + static struct option long_options[] + = {{"verbose", no_argument, 0, 'v'}, {0, 0, 0, 0}}; if (c != -1) { c = getopt_long(argc, argv, "vb:", long_options, &option_index); switch (c) { - case 'v': - verboseFlag = true; - break; + case 'v': + verboseFlag = true; + break; } } else { if (optind < argc) { - if (zimPath == NULL) { - zimPath = argv[optind++]; - } else if (search == NULL) { - search = argv[optind++]; - } else { - cout << zimPath << endl; - usage(); - } + if (zimPath == NULL) { + zimPath = argv[optind++]; + } else if (search == NULL) { + search = argv[optind++]; + } else { + cout << zimPath << endl; + usage(); + } } else { - break; + break; } } } @@ -80,20 +79,21 @@ int main(int argc, char **argv) { /* Try to prepare the indexing */ try { - reader = new kiwix::Reader(zimPath); + reader = new kiwix::Reader(zimPath); } catch (...) { - /* Cannot open the zimPath, maybe it is a plain old xapian database directory */ + /* Cannot open the zimPath, maybe it is a plain old xapian database + * directory */ } - if ( reader ) { - searcher = new kiwix::Searcher("", reader); + if (reader) { + searcher = new kiwix::Searcher("", reader); } else { - try { - searcher = new kiwix::Searcher(zimPath, NULL); - } catch (...) { - cerr << "Unable to search through zim '" << zimPath << "'." << endl; - exit(1); - } + try { + searcher = new kiwix::Searcher(zimPath, NULL); + } catch (...) { + cerr << "Unable to search through zim '" << zimPath << "'." << endl; + exit(1); + } } /* Start the indexing */ @@ -101,7 +101,7 @@ int main(int argc, char **argv) { string searchString(search); searcher->search(searchString, 0, 10); kiwix::Result* p_result; - while ( (p_result = searcher->getNextResult()) ) { + while ((p_result = searcher->getNextResult())) { cout << p_result->get_title() << endl; delete p_result; } diff --git a/src/server/kiwix-serve.cpp b/src/server/kiwix-serve.cpp index 836ca88..4f0a97e 100644 --- a/src/server/kiwix-serve.cpp +++ b/src/server/kiwix-serve.cpp @@ -20,8 +20,8 @@ #define KIWIX_MIN_CONTENT_SIZE_TO_DEFLATE 100 #ifdef __APPLE__ -#import #import +#import #define MIBSIZE 4 #endif @@ -31,10 +31,10 @@ #include "stdint4win.h" #endif #include -#include // otherwise socklen_t is not a recognized type +#include // otherwise socklen_t is not a recognized type //#include // otherwise int is not a recognized type -//typedef int off_t; -//typedef SSIZE_T ssize_t; +// typedef int off_t; +// typedef SSIZE_T ssize_t; typedef UINT64 uint64_t; typedef UINT16 uint16_t; extern "C" { @@ -43,40 +43,40 @@ extern "C" { #endif -#include -#include -#include #include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include +#include #include #include #include -#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include #include "server-resources.h" #ifndef _WIN32 -#include -#include -#include #include -#include -#include #include +#include +#include +#include +#include +#include #endif #ifdef interface @@ -101,17 +101,19 @@ static pthread_mutex_t verboseFlagLock = PTHREAD_MUTEX_INITIALIZER; static pthread_mutex_t mimeTypeLock = PTHREAD_MUTEX_INITIALIZER; /* Try to get the mimeType from the file extension */ -static std::string getMimeTypeForFile(const std::string& filename) { +static std::string getMimeTypeForFile(const std::string& filename) +{ std::string mimeType = "text/plain"; unsigned int pos = filename.find_last_of("."); if (pos != std::string::npos) { - std::string extension = filename.substr(pos+1); + std::string extension = filename.substr(pos + 1); pthread_mutex_lock(&mimeTypeLock); if (extMimeTypes.find(extension) != extMimeTypes.end()) { mimeType = extMimeTypes[extension]; - } else if (extMimeTypes.find(kiwix::lcAll(extension)) != extMimeTypes.end()) { + } else if (extMimeTypes.find(kiwix::lcAll(extension)) + != extMimeTypes.end()) { mimeType = extMimeTypes[kiwix::lcAll(extension)]; } pthread_mutex_unlock(&mimeTypeLock); @@ -120,25 +122,33 @@ static std::string getMimeTypeForFile(const std::string& filename) { return mimeType; } -void introduceTaskbar(string &content, const string &humanReadableBookId) { +void introduceTaskbar(string& content, const string& humanReadableBookId) +{ if (!noSearchBarFlag) { - content = appendToFirstOccurence(content, "", - replaceRegex(RESOURCE::include_html_part, - humanReadableBookId, "__CONTENT__")); - content = appendToFirstOccurence(content, - "", - ""); - content = appendToFirstOccurence(content, "]*>", - replaceRegex(RESOURCE::taskbar_html_part, - humanReadableBookId, "__CONTENT__")); + content = appendToFirstOccurence( + content, + "", + replaceRegex( + RESOURCE::include_html_part, humanReadableBookId, "__CONTENT__")); + content = appendToFirstOccurence( + content, + "", + ""); + content = appendToFirstOccurence( + content, + "]*>", + replaceRegex( + RESOURCE::taskbar_html_part, humanReadableBookId, "__CONTENT__")); } } /* Should display debug information? */ -bool isVerbose() { +bool isVerbose() +{ bool value; pthread_mutex_lock(&verboseFlagLock); value = verboseFlag; @@ -146,26 +156,24 @@ bool isVerbose() { return value; } -static -bool compress_content(string &content, - const string &mimeType) +static bool compress_content(string& content, const string& mimeType) { static std::vector compr_buffer; /* Should be deflate */ - bool deflated = mimeType.find("text/") != string::npos || - mimeType.find("application/javascript") != string::npos || - mimeType.find("application/json") != string::npos; + bool deflated = mimeType.find("text/") != string::npos + || mimeType.find("application/javascript") != string::npos + || mimeType.find("application/json") != string::npos; - if ( ! deflated ) - return false; + if (!deflated) + return false; /* Compute the lengh */ unsigned int contentLength = content.size(); /* If the content is too short, no need to compress it */ - if ( contentLength <= KIWIX_MIN_CONTENT_SIZE_TO_DEFLATE) - return false; + if (contentLength <= KIWIX_MIN_CONTENT_SIZE_TO_DEFLATE) + return false; uLong bufferBound = compressBound(contentLength); @@ -173,15 +181,18 @@ bool compress_content(string &content, pthread_mutex_lock(&compressorLock); compr_buffer.reserve(bufferBound); uLongf comprLen = compr_buffer.capacity(); - int err = compress(&compr_buffer[0], &comprLen, (const Bytef*)(content.data()), contentLength); + int err = compress(&compr_buffer[0], + &comprLen, + (const Bytef*)(content.data()), + contentLength); - if (err == Z_OK && comprLen > 2 && comprLen < (contentLength+2)) { + 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 */ - content = string((char *)&compr_buffer[2], comprLen-2); + content = string((char*)&compr_buffer[2], comprLen - 2); } else { deflated = false; } @@ -190,75 +201,78 @@ bool compress_content(string &content, return deflated; } - -static -struct MHD_Response* build_response(const void* data, - unsigned int length, - const std::string& httpRedirection, - const std::string& mimeType, - bool deflated, - bool cacheEnabled) +static struct MHD_Response* build_response(const void* data, + unsigned int length, + const std::string& httpRedirection, + const std::string& mimeType, + bool deflated, + bool cacheEnabled) { /* Create the response */ - struct MHD_Response * response = MHD_create_response_from_data(length, - const_cast(data), - MHD_NO, - MHD_YES); + struct MHD_Response* response = MHD_create_response_from_data( + length, const_cast(data), MHD_NO, MHD_YES); /* Make a redirection if necessary otherwise send the content */ if (!httpRedirection.empty()) { - MHD_add_response_header(response, MHD_HTTP_HEADER_LOCATION, httpRedirection.c_str()); + MHD_add_response_header( + response, MHD_HTTP_HEADER_LOCATION, httpRedirection.c_str()); } else { - /* Add if necessary the content-encoding */ if (deflated) { - MHD_add_response_header(response, MHD_HTTP_HEADER_VARY, "Accept-Encoding"); - MHD_add_response_header(response, MHD_HTTP_HEADER_CONTENT_ENCODING, "deflate"); + MHD_add_response_header( + response, MHD_HTTP_HEADER_VARY, "Accept-Encoding"); + MHD_add_response_header( + response, MHD_HTTP_HEADER_CONTENT_ENCODING, "deflate"); } /* Tell the client that byte ranges are accepted */ MHD_add_response_header(response, MHD_HTTP_HEADER_ACCEPT_RANGES, "bytes"); /* Specify the mime type */ - MHD_add_response_header(response, MHD_HTTP_HEADER_CONTENT_TYPE, mimeType.c_str()); + MHD_add_response_header( + response, MHD_HTTP_HEADER_CONTENT_TYPE, mimeType.c_str()); } - /* Force to close the connection - cf. 100% CPU usage with v. 4.4 (in Lucid) */ - //MHD_add_response_header(response, MHD_HTTP_HEADER_CONNECTION, "close"); + /* Force to close the connection - cf. 100% CPU usage with v. 4.4 (in Lucid) + */ + // MHD_add_response_header(response, MHD_HTTP_HEADER_CONNECTION, "close"); /* Allow cross-domain requests */ - //MHD_add_response_header(response, MHD_HTTP_HEADER_ACCESS_CONTROL_ALLOW_ORIGIN, "*"); + // MHD_add_response_header(response, + // MHD_HTTP_HEADER_ACCESS_CONTROL_ALLOW_ORIGIN, "*"); MHD_add_response_header(response, "Access-Control-Allow-Origin", "*"); if (cacheEnabled) { /* Force cache */ - MHD_add_response_header(response, MHD_HTTP_HEADER_CACHE_CONTROL, "max-age=2723040, public"); + MHD_add_response_header( + response, MHD_HTTP_HEADER_CACHE_CONTROL, "max-age=2723040, public"); } else { /* Prevent cache (for random page) */ - MHD_add_response_header(response, MHD_HTTP_HEADER_CACHE_CONTROL, "no-cache, no-store, must-revalidate"); + MHD_add_response_header(response, + MHD_HTTP_HEADER_CACHE_CONTROL, + "no-cache, no-store, must-revalidate"); } return response; } -ssize_t callback_reader_from_blob(void *cls, +ssize_t callback_reader_from_blob(void* cls, uint64_t pos, - char *buf, + char* buf, size_t max) { zim::Blob* blob = static_cast(cls); pthread_mutex_lock(&readerLock); - size_t max_size_to_set = min(max, blob->size()-pos); + size_t max_size_to_set = min(max, blob->size() - pos); - if (max_size_to_set <= 0) - { + if (max_size_to_set <= 0) { pthread_mutex_unlock(&readerLock); return MHD_CONTENT_READER_END_WITH_ERROR; } - memcpy(buf, blob->data()+pos, max_size_to_set); + memcpy(buf, blob->data() + pos, max_size_to_set); pthread_mutex_unlock(&readerLock); return max_size_to_set; } -void callback_free_blob(void *cls) +void callback_free_blob(void* cls) { zim::Blob* blob = static_cast(cls); pthread_mutex_lock(&readerLock); @@ -266,41 +280,44 @@ void callback_free_blob(void *cls) pthread_mutex_unlock(&readerLock); } -static -struct MHD_Response* build_callback_response_from_blob(zim::Blob& blob, - const std::string& mimeType) +static struct MHD_Response* build_callback_response_from_blob( + zim::Blob& blob, const std::string& mimeType) { pthread_mutex_lock(&readerLock); zim::Blob* p_blob = new zim::Blob(blob); - struct MHD_Response * response = MHD_create_response_from_callback(blob.size(), - 16384, - callback_reader_from_blob, - p_blob, - callback_free_blob); + struct MHD_Response* response + = MHD_create_response_from_callback(blob.size(), + 16384, + callback_reader_from_blob, + p_blob, + callback_free_blob); pthread_mutex_unlock(&readerLock); /* Tell the client that byte ranges are accepted */ MHD_add_response_header(response, MHD_HTTP_HEADER_ACCEPT_RANGES, "bytes"); /* Specify the mime type */ - MHD_add_response_header(response, MHD_HTTP_HEADER_CONTENT_TYPE, mimeType.c_str()); + MHD_add_response_header( + response, MHD_HTTP_HEADER_CONTENT_TYPE, mimeType.c_str()); /* Allow cross-domain requests */ - //MHD_add_response_header(response, MHD_HTTP_HEADER_ACCESS_CONTROL_ALLOW_ORIGIN, "*"); + // MHD_add_response_header(response, + // MHD_HTTP_HEADER_ACCESS_CONTROL_ALLOW_ORIGIN, "*"); MHD_add_response_header(response, "Access-Control-Allow-Origin", "*"); - MHD_add_response_header(response, MHD_HTTP_HEADER_CACHE_CONTROL, "max-age=2723040, public"); + MHD_add_response_header( + response, MHD_HTTP_HEADER_CACHE_CONTROL, "max-age=2723040, public"); return response; } -static -struct MHD_Response* handle_suggest(struct MHD_Connection * connection, - int& httpResponseCode, - kiwix::Reader *reader, - kiwix::Searcher *searcher, - const std::string& urlStr, - const std::string& humanReadableBookId, - bool acceptEncodingDeflate) +static struct MHD_Response* handle_suggest( + struct MHD_Connection* connection, + int& httpResponseCode, + kiwix::Reader* reader, + kiwix::Searcher* searcher, + const std::string& urlStr, + const std::string& humanReadableBookId, + bool acceptEncodingDeflate) { std::string content; std::string mimeType; @@ -309,7 +326,8 @@ struct MHD_Response* handle_suggest(struct MHD_Connection * connection, std::string suggestion; /* Get the suggestion pattern from the HTTP request */ - const char* cTerm = MHD_lookup_connection_value(connection, MHD_GET_ARGUMENT_KIND, "term"); + const char* cTerm + = MHD_lookup_connection_value(connection, MHD_GET_ARGUMENT_KIND, "term"); std::string term = cTerm == NULL ? "" : cTerm; if (isVerbose()) { std::cout << "Searching suggestions for: \"" << term << "\"" << endl; @@ -321,53 +339,59 @@ struct MHD_Response* handle_suggest(struct MHD_Connection * connection, while (reader->getNextSuggestion(suggestion)) { kiwix::stringReplacement(suggestion, "\"", "\\\""); content += (content == "[" ? "" : ","); - content += "{\"value\":\"" + suggestion + "\",\"label\":\"" + suggestion + "\"}"; + content += "{\"value\":\"" + suggestion + "\",\"label\":\"" + suggestion + + "\"}"; suggestionCount++; } /* Propose the fulltext search if possible */ if (searcher != NULL) { content += (suggestionCount == 0 ? "" : ","); - content += "{\"value\":\"" + std::string(term) + " \", \"label\":\"containing '" + std::string(term) + "'...\"}"; + content += "{\"value\":\"" + std::string(term) + + " \", \"label\":\"containing '" + std::string(term) + + "'...\"}"; } content += "]"; mimeType = "application/json; charset=utf-8"; bool deflated = acceptEncodingDeflate && compress_content(content, mimeType); - return build_response(content.data(), content.size(), "", mimeType, deflated, true); + return build_response( + content.data(), content.size(), "", mimeType, deflated, true); } -static -struct MHD_Response* handle_skin(struct MHD_Connection * connection, - int& httpResponseCode, - kiwix::Reader *reader, - kiwix::Searcher *searcher, - const std::string& urlStr, - const std::string& humanReadableBookId, - bool acceptEncodingDeflate) +static struct MHD_Response* handle_skin(struct MHD_Connection* connection, + int& httpResponseCode, + kiwix::Reader* reader, + kiwix::Searcher* searcher, + const std::string& urlStr, + const std::string& humanReadableBookId, + bool acceptEncodingDeflate) { std::string content = getResource(urlStr.substr(6)); std::string mimeType = getMimeTypeForFile(urlStr); bool deflated = acceptEncodingDeflate && compress_content(content, mimeType); - return build_response(content.data(), content.size(), "", mimeType, deflated, true); + return build_response( + content.data(), content.size(), "", mimeType, deflated, true); } -static -struct MHD_Response* handle_search(struct MHD_Connection * connection, - int& httpResponseCode, - kiwix::Reader *reader, - kiwix::Searcher *searcher, - const std::string& urlStr, - const std::string& humanReadableBookId, - bool acceptEncodingDeflate) +static struct MHD_Response* handle_search( + struct MHD_Connection* connection, + int& httpResponseCode, + kiwix::Reader* reader, + kiwix::Searcher* searcher, + const std::string& urlStr, + const std::string& humanReadableBookId, + bool acceptEncodingDeflate) { std::string content; std::string mimeType; std::string httpRedirection; /* Retrieve the pattern to search */ - const char* pattern = MHD_lookup_connection_value(connection, MHD_GET_ARGUMENT_KIND, "pattern"); - std::string patternString = kiwix::urlDecode(pattern == NULL ? "" : string(pattern)); + const char* pattern = MHD_lookup_connection_value( + connection, MHD_GET_ARGUMENT_KIND, "pattern"); + std::string patternString + = kiwix::urlDecode(pattern == NULL ? "" : string(pattern)); std::string patternCorrespondingUrl; /* Try first to load directly the article */ @@ -384,7 +408,8 @@ struct MHD_Response* handle_search(struct MHD_Connection * connection, /* If article found then redirect directly to it */ if (!patternCorrespondingUrl.empty()) { - httpRedirection = "/" + humanReadableBookId + "/" + patternCorrespondingUrl; + httpRedirection + = "/" + humanReadableBookId + "/" + patternCorrespondingUrl; httpResponseCode = MHD_HTTP_FOUND; return build_response("", 0, httpRedirection, "", false, true); } @@ -392,8 +417,10 @@ struct MHD_Response* handle_search(struct MHD_Connection * connection, /* Make the search */ if (patternCorrespondingUrl.empty() && searcher != NULL) { - const char* start = MHD_lookup_connection_value(connection, MHD_GET_ARGUMENT_KIND, "start"); - const char* end = MHD_lookup_connection_value(connection, MHD_GET_ARGUMENT_KIND, "end"); + const char* start = MHD_lookup_connection_value( + connection, MHD_GET_ARGUMENT_KIND, "start"); + const char* end + = MHD_lookup_connection_value(connection, MHD_GET_ARGUMENT_KIND, "end"); unsigned int startNumber = start != NULL ? atoi(start) : 0; unsigned int endNumber = end != NULL ? atoi(end) : 25; @@ -418,17 +445,22 @@ struct MHD_Response* handle_search(struct MHD_Connection * connection, introduceTaskbar(content, humanReadableBookId); bool deflated = acceptEncodingDeflate && compress_content(content, mimeType); - return build_response(content.data(), content.size(), httpRedirection, mimeType, deflated, true); + return build_response(content.data(), + content.size(), + httpRedirection, + mimeType, + deflated, + true); } -static -struct MHD_Response* handle_random(struct MHD_Connection * connection, - int& httpResponseCode, - kiwix::Reader *reader, - kiwix::Searcher *searcher, - const std::string& urlStr, - const std::string& humanReadableBookId, - bool acceptEncodingDeflate) +static struct MHD_Response* handle_random( + struct MHD_Connection* connection, + int& httpResponseCode, + kiwix::Reader* reader, + kiwix::Searcher* searcher, + const std::string& urlStr, + const std::string& humanReadableBookId, + bool acceptEncodingDeflate) { std::string httpRedirection; httpResponseCode = MHD_HTTP_FOUND; @@ -436,19 +468,20 @@ struct MHD_Response* handle_random(struct MHD_Connection * connection, pthread_mutex_lock(&readerLock); std::string randomUrl = reader->getRandomPageUrl(); pthread_mutex_unlock(&readerLock); - httpRedirection = "/" + humanReadableBookId + "/" + kiwix::urlEncode(randomUrl); + httpRedirection + = "/" + humanReadableBookId + "/" + kiwix::urlEncode(randomUrl); } return build_response("", 0, httpRedirection, "", false, false); } -static -struct MHD_Response* handle_content(struct MHD_Connection * connection, - int& httpResponseCode, - kiwix::Reader *reader, - kiwix::Searcher *searcher, - const std::string& urlStr, - const std::string& humanReadableBookId, - bool acceptEncodingDeflate) +static struct MHD_Response* handle_content( + struct MHD_Connection* connection, + int& httpResponseCode, + kiwix::Reader* reader, + kiwix::Searcher* searcher, + const std::string& urlStr, + const std::string& humanReadableBookId, + bool acceptEncodingDeflate) { std::string baseUrl; std::string content; @@ -463,7 +496,7 @@ struct MHD_Response* handle_content(struct MHD_Connection * connection, if (found) { /* If redirect */ unsigned int loopCounter = 0; - while (article.isRedirect() && loopCounter++<42) { + while (article.isRedirect() && loopCounter++ < 42) { article = article.getRedirectArticle(); } @@ -481,19 +514,26 @@ struct MHD_Response* handle_content(struct MHD_Connection * connection, if (isVerbose()) cout << "Failed to find " << urlStr << endl; - content = "\nContent not found

Not Found

The requested URL \"" + urlStr + "\" was not found on this server.

"; + content + = "\nContent not found

Not " + "Found

The requested URL \"" + + urlStr + "\" was not found on this server.

"; mimeType = "text/html"; httpResponseCode = MHD_HTTP_NOT_FOUND; introduceTaskbar(content, humanReadableBookId); - bool deflated = acceptEncodingDeflate && compress_content(content, mimeType); - return build_response(content.data(), content.size(), "", mimeType, deflated, false); + bool deflated + = acceptEncodingDeflate && compress_content(content, mimeType); + return build_response( + content.data(), content.size(), "", mimeType, deflated, false); } try { pthread_mutex_lock(&readerLock); mimeType = article.getMimeType(); pthread_mutex_unlock(&readerLock); - } catch (exception &e) { + } catch (exception& e) { mimeType = "application/octet-stream"; } @@ -506,47 +546,52 @@ struct MHD_Response* handle_content(struct MHD_Connection * connection, zim::Blob raw_content = article.getData(); pthread_mutex_unlock(&readerLock); - if (mimeType.find("text/") != string::npos || - mimeType.find("application/javascript") != string::npos || - mimeType.find("application/json") != string::npos) - { + if (mimeType.find("text/") != string::npos + || mimeType.find("application/javascript") != string::npos + || mimeType.find("application/json") != string::npos) { pthread_mutex_lock(&readerLock); content = string(raw_content.data(), raw_content.size()); pthread_mutex_unlock(&readerLock); - /* Special rewrite URL in case of ZIM file use intern *asbolute* url like /A/Kiwix */ + /* Special rewrite URL in case of ZIM file use intern *asbolute* url like + * /A/Kiwix */ if (mimeType.find("text/html") != string::npos) { - baseUrl = "/" + std::string(1, article.getNamespace()) + "/" + article.getUrl(); - content = replaceRegex(content, "$1$2" + humanReadableBookId + "/$3/", - "(href|src)(=[\"|\']{0,1}/)([A-Z|\\-])/"); - content = replaceRegex(content, "$1$2" + humanReadableBookId + "/$3/", - "(@import[ ]+)([\"|\']{0,1}/)([A-Z|\\-])/"); + baseUrl = "/" + std::string(1, article.getNamespace()) + "/" + + article.getUrl(); content = replaceRegex(content, - "", - ""); + "$1$2" + humanReadableBookId + "/$3/", + "(href|src)(=[\"|\']{0,1}/)([A-Z|\\-])/"); + content = replaceRegex(content, + "$1$2" + humanReadableBookId + "/$3/", + "(@import[ ]+)([\"|\']{0,1}/)([A-Z|\\-])/"); + content = replaceRegex( + content, + "", + ""); introduceTaskbar(content, humanReadableBookId); } else if (mimeType.find("text/css") != string::npos) { - content = replaceRegex(content, "$1$2" + humanReadableBookId + "/$3/", - "(url|URL)(\\([\"|\']{0,1}/)([A-Z|\\-])/"); + content = replaceRegex(content, + "$1$2" + humanReadableBookId + "/$3/", + "(url|URL)(\\([\"|\']{0,1}/)([A-Z|\\-])/"); } - bool deflated = acceptEncodingDeflate && compress_content(content, mimeType); - return build_response(content.data(), content.size(), "", mimeType, deflated, true); - } - else - { + bool deflated + = acceptEncodingDeflate && compress_content(content, mimeType); + return build_response( + content.data(), content.size(), "", mimeType, deflated, true); + } else { return build_callback_response_from_blob(raw_content, mimeType); } } -static -struct MHD_Response* handle_default(struct MHD_Connection * connection, - int& httpResponseCode, - kiwix::Reader *reader, - kiwix::Searcher *searcher, - const std::string& urlStr, - const std::string& humanReadableBookId, - bool acceptEncodingDeflate) +static struct MHD_Response* handle_default( + struct MHD_Connection* connection, + int& httpResponseCode, + kiwix::Reader* reader, + kiwix::Searcher* searcher, + const std::string& urlStr, + const std::string& humanReadableBookId, + bool acceptEncodingDeflate) { pthread_mutex_lock(&welcomeLock); std::string content = welcomeHTML; @@ -555,23 +600,25 @@ struct MHD_Response* handle_default(struct MHD_Connection * connection, std::string mimeType = "text/html; charset=utf-8"; bool deflated = acceptEncodingDeflate && compress_content(content, mimeType); - return build_response(content.data(), content.size(), "", mimeType, deflated, true); + return build_response( + content.data(), content.size(), "", mimeType, deflated, true); } -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) +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") && 0 != strcmp(method, "POST")) return MHD_NO; - /* The first time only the headers are valid, do not respond in the first round... */ + /* The first time only the headers are valid, do not respond in the first + * round... */ static int dummy; if (&dummy != *ptr) { *ptr = &dummy; @@ -587,41 +634,53 @@ static int accessHandlerCallback(void *cls, } /* Check if the response can be compressed */ - 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; + 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. [TODO] Handle this somehow */ - const char* acceptRangeHeaderValue = MHD_lookup_connection_value(connection, MHD_HEADER_KIND, MHD_HTTP_HEADER_RANGE); + const char* acceptRangeHeaderValue = MHD_lookup_connection_value( + connection, MHD_HEADER_KIND, MHD_HTTP_HEADER_RANGE); const bool acceptRange = acceptRangeHeaderValue != NULL; /* Prepare the variables */ - struct MHD_Response *response; + struct MHD_Response* response; int httpResponseCode = MHD_HTTP_OK; std::string urlStr = string(url); /* Get searcher and reader */ std::string humanReadableBookId = ""; if (!(urlStr.size() > 5 && urlStr.substr(0, 6) == "/skin/")) { - if (!strcmp(url, "/search") || !strcmp(url, "/suggest") || !strcmp(url, "/random")) { - const char* tmpGetValue = MHD_lookup_connection_value(connection, MHD_GET_ARGUMENT_KIND, "content"); + if (!strcmp(url, "/search") || !strcmp(url, "/suggest") + || !strcmp(url, "/random")) { + const char* tmpGetValue = MHD_lookup_connection_value( + connection, MHD_GET_ARGUMENT_KIND, "content"); humanReadableBookId = (tmpGetValue != NULL ? string(tmpGetValue) : ""); } else { - humanReadableBookId = urlStr.substr(1, urlStr.find("/", 1) != string::npos ? - urlStr.find("/", 1) - 1 : urlStr.size() - 2); + humanReadableBookId = urlStr.substr(1, + urlStr.find("/", 1) != string::npos + ? urlStr.find("/", 1) - 1 + : urlStr.size() - 2); if (!humanReadableBookId.empty()) { - urlStr = urlStr.substr(urlStr.find("/", 1) != string::npos ? - urlStr.find("/", 1) : humanReadableBookId.size()); + urlStr = urlStr.substr(urlStr.find("/", 1) != string::npos + ? urlStr.find("/", 1) + : humanReadableBookId.size()); } } } pthread_mutex_lock(&mapLock); - kiwix::Searcher *searcher = searchers.find(humanReadableBookId) != searchers.end() ? - searchers.find(humanReadableBookId)->second : NULL; - kiwix::Reader *reader = readers.find(humanReadableBookId) != readers.end() ? - readers.find(humanReadableBookId)->second : NULL; + kiwix::Searcher* searcher + = searchers.find(humanReadableBookId) != searchers.end() + ? searchers.find(humanReadableBookId)->second + : NULL; + kiwix::Reader* reader = readers.find(humanReadableBookId) != readers.end() + ? readers.find(humanReadableBookId)->second + : NULL; if (reader == NULL) { - humanReadableBookId=""; + humanReadableBookId = ""; } pthread_mutex_unlock(&mapLock); @@ -692,16 +751,15 @@ static int accessHandlerCallback(void *cls, } /* Queue the response */ - int ret = MHD_queue_response(connection, - httpResponseCode, - response); + int ret = MHD_queue_response(connection, httpResponseCode, response); MHD_destroy_response(response); return ret; } -int main(int argc, char **argv) { - struct MHD_Daemon *daemon; +int main(int argc, char** argv) +{ + struct MHD_Daemon* daemon; vector zimPathes; string libraryPath; string indexPath; @@ -714,79 +772,85 @@ int main(int argc, char **argv) { unsigned int PPID = 0; kiwix::Manager libraryManager; - static struct option long_options[] = { - {"daemon", no_argument, 0, 'd'}, - {"verbose", no_argument, 0, 'v'}, - {"library", no_argument, 0, 'l'}, - {"nolibrarybutton", no_argument, 0, 'm'}, - {"nosearchbar", no_argument, 0, 'n'}, - {"index", required_argument, 0, 'i'}, - {"attachToProcess", required_argument, 0, 'a'}, - {"port", required_argument, 0, 'p'}, - {"interface", required_argument, 0, 'f'}, - {0, 0, 0, 0} - }; + static struct option long_options[] + = {{"daemon", no_argument, 0, 'd'}, + {"verbose", no_argument, 0, 'v'}, + {"library", no_argument, 0, 'l'}, + {"nolibrarybutton", no_argument, 0, 'm'}, + {"nosearchbar", no_argument, 0, 'n'}, + {"index", required_argument, 0, 'i'}, + {"attachToProcess", required_argument, 0, 'a'}, + {"port", required_argument, 0, 'p'}, + {"interface", required_argument, 0, 'f'}, + {0, 0, 0, 0}}; /* Argument parsing */ while (true) { int option_index = 0; - int c = getopt_long(argc, argv, "mndvli:a:p:f:", long_options, &option_index); + int c + = getopt_long(argc, argv, "mndvli:a:p:f:", long_options, &option_index); if (c != -1) { - switch (c) { case 'd': - daemonFlag = true; - break; - case 'v': - verboseFlag = true; - break; - case 'l': - libraryFlag = true; - break; - case 'n': - noSearchBarFlag = true; - break; - case 'm': - noLibraryButtonFlag = true; - break; - case 'i': - indexPath = optarg; - break; - case 'p': - serverPort = atoi(optarg); - break; + daemonFlag = true; + break; + case 'v': + verboseFlag = true; + break; + case 'l': + libraryFlag = true; + break; + case 'n': + noSearchBarFlag = true; + break; + case 'm': + noLibraryButtonFlag = true; + break; + case 'i': + indexPath = optarg; + break; + case 'p': + serverPort = atoi(optarg); + break; case 'a': - PPIDString = string(optarg); - PPID = atoi(optarg); - break; - case 'f': + PPIDString = string(optarg); + PPID = atoi(optarg); + break; + case 'f': interface = string(optarg); break; } } else { if (optind <= argc) { - if (libraryFlag) - { - libraryPath = argv[optind++]; - } else { - while ( optind < argc ) - zimPathes.push_back(std::string(argv[optind++])); - } - break; - } + if (libraryFlag) { + libraryPath = argv[optind++]; + } else { + while (optind < argc) + zimPathes.push_back(std::string(argv[optind++])); + } + break; + } } } /* Print usage)) if necessary */ if (zimPathes.empty() && libraryPath.empty()) { - cerr << "Usage: kiwix-serve [--index=INDEX_PATH] [--port=PORT] [--verbose] [--nosearchbar] [--nolibrarybutton] [--daemon] [--attachToProcess=PID] [--interface=IF_NAME] ZIM_PATH+" << endl; - cerr << " kiwix-serve --library [--port=PORT] [--verbose] [--daemon] [--nosearchbar] [--nolibrarybutton] [--attachToProcess=PID] [--interface=IF_NAME] LIBRARY_PATH" << endl; - cerr << "\n If you set more than one ZIM_PATH, you cannot set a INDEX_PATH." << endl; + cerr << "Usage: kiwix-serve [--index=INDEX_PATH] [--port=PORT] [--verbose] " + "[--nosearchbar] [--nolibrarybutton] [--daemon] " + "[--attachToProcess=PID] [--interface=IF_NAME] ZIM_PATH+" + << endl; + cerr << " kiwix-serve --library [--port=PORT] [--verbose] [--daemon] " + "[--nosearchbar] [--nolibrarybutton] [--attachToProcess=PID] " + "[--interface=IF_NAME] LIBRARY_PATH" + << endl; + cerr << "\n If you set more than one ZIM_PATH, you cannot set a " + "INDEX_PATH." + << endl; exit(1); } - if ( (zimPathes.size() > 1) && !indexPath.empty() ) { + if ((zimPathes.size() > 1) && !indexPath.empty()) { cerr << "You cannot set a indexPath if you also set several zimPathes"; exit(1); } @@ -796,33 +860,41 @@ int main(int argc, char **argv) { vector libraryPaths = kiwix::split(libraryPath, ";"); vector::iterator itr; - for ( itr = libraryPaths.begin(); itr != libraryPaths.end(); ++itr ) { + for (itr = libraryPaths.begin(); itr != libraryPaths.end(); ++itr) { if (!itr->empty()) { - bool retVal = false; + bool retVal = false; - try { - string libraryPath = isRelativePath(*itr) ? computeAbsolutePath(removeLastPathElement(getExecutablePath(), true, false), *itr) : *itr; - retVal = libraryManager.readFile(libraryPath, true); - } catch (...) { - retVal = false; - } + try { + string libraryPath + = isRelativePath(*itr) + ? computeAbsolutePath(removeLastPathElement( + getExecutablePath(), true, false), + *itr) + : *itr; + retVal = libraryManager.readFile(libraryPath, true); + } catch (...) { + retVal = false; + } - if (!retVal) { - cerr << "Unable to open the XML library file '" << *itr << "'." << endl; - exit(1); - } + if (!retVal) { + cerr << "Unable to open the XML library file '" << *itr << "'." + << endl; + exit(1); + } } } /* Check if the library is not empty (or only remote books)*/ - if (libraryManager.getBookCount(true, false)==0) { - cerr << "The XML library file '" << libraryPath << "' is empty (or has only remote books)." << endl; + if (libraryManager.getBookCount(true, false) == 0) { + cerr << "The XML library file '" << libraryPath + << "' is empty (or has only remote books)." << endl; } } else { std::vector::iterator it; for (it = zimPathes.begin(); it != zimPathes.end(); it++) { if (!libraryManager.addBookFromPath(*it, *it, "", false)) { - cerr << "Unable to add the ZIM file '" << *it << "' to the internal library." << endl; + cerr << "Unable to add the ZIM file '" << *it + << "' to the internal library." << endl; exit(1); } } @@ -844,48 +916,52 @@ int main(int argc, char **argv) { indexPath = currentBook.indexPathAbsolute; /* Instanciate the ZIM file handler */ - kiwix::Reader *reader = NULL; + kiwix::Reader* reader = NULL; try { - reader = new kiwix::Reader(zimPath); - zimFileOk = true; + reader = new kiwix::Reader(zimPath); + zimFileOk = true; } catch (...) { - cerr << "Unable to open the ZIM file '" << zimPath << "'." << endl; + cerr << "Unable to open the ZIM file '" << zimPath << "'." << endl; } if (zimFileOk) { - string humanReadableId = currentBook.getHumanReadableIdFromPath(); - readers[humanReadableId] = reader; + string humanReadableId = currentBook.getHumanReadableIdFromPath(); + readers[humanReadableId] = reader; - /* Try to instanciate the zim index. - * If there is no specified indexPath, try to open the - * embedded index in the zim. */ - if (indexPath.empty() && reader->hasFulltextIndex()) { - indexPath = zimPath; - } + /* Try to instanciate the zim index. + * If there is no specified indexPath, try to open the + * embedded index in the zim. */ + if (indexPath.empty() && reader->hasFulltextIndex()) { + indexPath = zimPath; + } - if (!indexPath.empty()) { - try { - kiwix::Searcher *searcher = new kiwix::Searcher(indexPath, reader); - searcher->setProtocolPrefix("/"); - searcher->setSearchProtocolPrefix("/search?"); - searcher->setContentHumanReadableId(humanReadableId); - searchers[humanReadableId] = searcher; - } catch (...) { - cerr << "Unable to open the search index '" << indexPath << "'." << endl; - } - } + if (!indexPath.empty()) { + try { + kiwix::Searcher* searcher = new kiwix::Searcher(indexPath, reader); + searcher->setProtocolPrefix("/"); + searcher->setSearchProtocolPrefix("/search?"); + searcher->setContentHumanReadableId(humanReadableId); + searchers[humanReadableId] = searcher; + } catch (...) { + cerr << "Unable to open the search index '" << indexPath << "'." + << endl; + } + } } } } /* Compute the Welcome HTML */ - string welcomeBooksHtml = "" -"
"; + string welcomeBooksHtml + = "" + "
"; for (itr = booksIds.begin(); itr != booksIds.end(); ++itr) { libraryManager.getBookById(*itr, currentBook); - if (!currentBook.path.empty() && readers.find(currentBook.getHumanReadableIdFromPath()) != readers.end()) { - welcomeBooksHtml += "" + if (!currentBook.path.empty() + && readers.find(currentBook.getHumanReadableIdFromPath()) + != readers.end()) { + welcomeBooksHtml += "" "" "
" "
" @@ -902,7 +978,8 @@ int main(int argc, char **argv) { welcomeBooksHtml += "" "
"; - welcomeHTML = replaceRegex(RESOURCE::home_html_tmpl, welcomeBooksHtml, "__BOOKS__"); + welcomeHTML + = replaceRegex(RESOURCE::home_html_tmpl, welcomeBooksHtml, "__BOOKS__"); #ifndef _WIN32 /* Fork if necessary */ @@ -934,29 +1011,28 @@ int main(int argc, char **argv) { /* Hard coded mimetypes */ extMimeTypes["html"] = "text/html"; - extMimeTypes["htm"] = "text/html"; - extMimeTypes["png"] = "image/png"; + extMimeTypes["htm"] = "text/html"; + extMimeTypes["png"] = "image/png"; extMimeTypes["tiff"] = "image/tiff"; - extMimeTypes["tif"] = "image/tiff"; + extMimeTypes["tif"] = "image/tiff"; extMimeTypes["jpeg"] = "image/jpeg"; - extMimeTypes["jpg"] = "image/jpeg"; - extMimeTypes["gif"] = "image/gif"; - extMimeTypes["svg"] = "image/svg+xml"; - extMimeTypes["txt"] = "text/plain"; - extMimeTypes["xml"] = "text/xml"; - extMimeTypes["pdf"] = "application/pdf"; - extMimeTypes["ogg"] = "application/ogg"; - extMimeTypes["js"] = "application/javascript"; - extMimeTypes["css"] = "text/css"; - extMimeTypes["otf"] = "application/vnd.ms-opentype"; - extMimeTypes["ttf"] = "application/font-ttf"; + extMimeTypes["jpg"] = "image/jpeg"; + extMimeTypes["gif"] = "image/gif"; + extMimeTypes["svg"] = "image/svg+xml"; + extMimeTypes["txt"] = "text/plain"; + extMimeTypes["xml"] = "text/xml"; + extMimeTypes["pdf"] = "application/pdf"; + extMimeTypes["ogg"] = "application/ogg"; + extMimeTypes["js"] = "application/javascript"; + extMimeTypes["css"] = "text/css"; + extMimeTypes["otf"] = "application/vnd.ms-opentype"; + extMimeTypes["ttf"] = "application/font-ttf"; extMimeTypes["woff"] = "application/font-woff"; - extMimeTypes["vtt"] = "text/vtt"; + extMimeTypes["vtt"] = "text/vtt"; /* Start the HTTP daemon */ - void *page = NULL; + void* page = NULL; if (interface.length() > 0) { - #ifndef _WIN32 /* TBD IPv6 support */ @@ -966,29 +1042,30 @@ int main(int argc, char **argv) { /* Search all available interfaces */ if (getifaddrs(&ifaddr) == -1) { - cerr << "Getifaddrs() failed while searching for '" << interface << "'" << endl; + cerr << "Getifaddrs() failed while searching for '" << interface << "'" + << endl; exit(1); } /* Init 'sockAddr' with zeros */ - memset(&sockAddr,0, sizeof(sockAddr)); + memset(&sockAddr, 0, sizeof(sockAddr)); /* Try to find interfaces in the list of available interfaces */ for (ifa = ifaddr, n = 0; ifa != NULL; ifa = ifa->ifa_next, n++) { - /* Ignore if no IP attributed to the interface */ if (ifa->ifa_addr == NULL) - continue; + continue; /* Check if the interface is the right one */ family = ifa->ifa_addr->sa_family; if (family == AF_INET) { - if (strcasecmp(ifa->ifa_name, interface.c_str()) == 0) { - sockAddr.sin_family = family; - sockAddr.sin_port = htons(serverPort); - sockAddr.sin_addr.s_addr = ((struct sockaddr_in*) ifa->ifa_addr)->sin_addr.s_addr; - break; - } + if (strcasecmp(ifa->ifa_name, interface.c_str()) == 0) { + sockAddr.sin_family = family; + sockAddr.sin_port = htons(serverPort); + sockAddr.sin_addr.s_addr + = ((struct sockaddr_in*)ifa->ifa_addr)->sin_addr.s_addr; + break; + } } } @@ -997,19 +1074,19 @@ int main(int argc, char **argv) { /* Dies if interface was not found in the list */ if (sockAddr.sin_family == 0) { - cerr << "Unable to find interface '" << interface << "'" << endl; - exit(1); + cerr << "Unable to find interface '" << interface << "'" << endl; + exit(1); } daemon = MHD_start_daemon(MHD_USE_SELECT_INTERNALLY, - serverPort, - NULL, - NULL, - &accessHandlerCallback, - page, + serverPort, + NULL, + NULL, + &accessHandlerCallback, + page, MHD_OPTION_SOCK_ADDR, &sockAddr, - MHD_OPTION_END); + MHD_OPTION_END); #else cerr << "Setting 'interface' not yet implemented for Windows" << endl; exit(1); @@ -1017,16 +1094,19 @@ int main(int argc, char **argv) { } else { daemon = MHD_start_daemon(MHD_USE_SELECT_INTERNALLY, - serverPort, - NULL, - NULL, - &accessHandlerCallback, - page, - MHD_OPTION_END); + serverPort, + NULL, + NULL, + &accessHandlerCallback, + page, + MHD_OPTION_END); } if (daemon == NULL) { - cerr << "Unable to instanciate the HTTP daemon. The port " << serverPort << " is maybe already occupied or need more permissions to be open. Please try as root or with a port number higher or equal to 1024." << endl; + cerr << "Unable to instanciate the HTTP daemon. The port " << serverPort + << " is maybe already occupied or need more permissions to be open. " + "Please try as root or with a port number higher or equal to 1024." + << endl; exit(1); } @@ -1040,23 +1120,23 @@ int main(int argc, char **argv) { CloseHandle(process); if (ret == WAIT_TIMEOUT) { #elif __APPLE__ - int mib[MIBSIZE]; - struct kinfo_proc kp; - size_t len = sizeof(kp); + int mib[MIBSIZE]; + struct kinfo_proc kp; + size_t len = sizeof(kp); - mib[0]=CTL_KERN; - mib[1]=KERN_PROC; - mib[2]=KERN_PROC_PID; - mib[3]=PPID; + mib[0] = CTL_KERN; + mib[1] = KERN_PROC; + mib[2] = KERN_PROC_PID; + mib[3] = PPID; - int ret = sysctl(mib, MIBSIZE, &kp, &len, NULL, 0); + int ret = sysctl(mib, MIBSIZE, &kp, &len, NULL, 0); if (ret != -1 && len > 0) { #else /* Linux & co */ string procPath = "/proc/" + string(PPIDString); if (access(procPath.c_str(), F_OK) != -1) { #endif } else { - waiting = false; + waiting = false; } }