/* The function assembles the response to be given to the browser. code: an integer response code, normally this is 200. header: An extra header to be sent with the response. May be empty. contents: the response body to be sent. The function inserts the correct headers, and creates the entire result to be sent back to the browser. */ void http_assemble_response (Webserver_Request * request) { ostringstream length; length << request->reply.size (); // Assemble the HTTP response code fragment. string http_response_code_fragment = filter_url_http_response_code_text (request->response_code); // Assemble the Content-Type. string extension = filter_url_get_extension (request->get); extension = unicode_string_casefold (extension); string content_type; if (extension == "js") content_type = "application/javascript"; else if (extension == "css") content_type = "text/css"; else if (extension == "ico") content_type = "image/vnd.microsoft.icon"; else if (extension == "gif") content_type = "image/gif"; else if (extension == "jpe") content_type = "image/jpeg"; else if (extension == "jpg") content_type = "image/jpeg"; else if (extension == "jpeg") content_type = "image/jpeg"; else if (extension == "png") content_type = "image/png"; else if (extension == "svg") content_type = "image/svg+xml"; else if (extension == "bmp") content_type = "image/bmp"; else if (extension == "txt") content_type = "text/plain"; else if (extension == "usfm") content_type = "text/plain"; else if (extension == "otf") content_type = "font/opentype"; else if (extension == "ttf") content_type = "application/font-sfnt"; else if (extension == "woff") content_type = "application/font-woff"; else if (extension == "sh") content_type = "application/octet-stream"; else if (extension == "sqlite") content_type = "application/octet-stream"; else if (extension == "htm") content_type = "text/html"; else if (extension == "html") content_type = "text/html"; else if (extension == "") content_type = "text/html"; else if (extension == "download") content_type = "application/octet-stream"; else content_type = "application/octet-stream"; // Assemble the complete response for the browser. vector <string> response; response.push_back ("HTTP/1.1 " + http_response_code_fragment); response.push_back ("Accept-Ranges: bytes"); response.push_back ("Content-Length: " + length.str()); response.push_back ("Content-Type: " + content_type); if (!request->etag.empty ()) { response.push_back ("Cache-Control: max-age=120"); response.push_back ("ETag: " + request->etag); } if (!request->header.empty ()) response.push_back (request->header); response.push_back (""); if (!request->reply.empty ()) response.push_back (request->reply); string assembly; for (unsigned int i = 0; i < response.size (); i++) { if (i > 0) assembly += "\n"; assembly += response [i]; } request->reply = assembly; }
// Imports the file at $path into $resource. void resource_logic_import_images (string resource, string path) { Database_ImageResources database_imageresources; Database_Logs::log ("Importing: " + filter_url_basename (path)); // To begin with, add the path to the main file to the list of paths to be processed. vector <string> paths = {path}; while (!paths.empty ()) { // Take and remove the first path from the container. path = paths[0]; paths.erase (paths.begin()); string basename = filter_url_basename (path); string extension = filter_url_get_extension (path); extension = unicode_string_casefold (extension); if (extension == "pdf") { Database_Logs::log ("Processing PDF: " + basename); // Retrieve PDF information. filter_shell_run ("", "pdfinfo", {path}, NULL, NULL); // Convert the PDF file to separate images. string folder = filter_url_tempfile (); filter_url_mkdir (folder); filter_shell_run (folder, "pdftocairo", {"-jpeg", path}, NULL, NULL); // Add the images to the ones to be processed. filter_url_recursive_scandir (folder, paths); } else if (filter_archive_is_archive (path)) { Database_Logs::log ("Unpacking archive: " + basename); string folder = filter_archive_uncompress (path); filter_url_recursive_scandir (folder, paths); } else { if (!extension.empty ()) { basename = database_imageresources.store (resource, path); Database_Logs::log ("Storing image " + basename ); } } } Database_Logs::log ("Ready importing images"); }
vector <string> Fonts_Logic::getFonts () { vector <string> files = filter_url_scandir (folder()); vector <string> fonts; for (auto & file : files) { string suffix = filter_url_get_extension (file); if (suffix == "txt") continue; if (suffix == "html") continue; if (suffix == "h") continue; if (suffix == "cpp") continue; if (suffix == "o") continue; fonts.push_back (file); } return fonts; }
// Prepares a sample Bible. // The output of this is supposed to be manually put into the source tree, folder "samples". // This will be used to quickly create a sample Bible, that is fast, even on mobile devices. void demo_prepare_sample_bible () { Database_Bibles database_bibles; // Remove the Bible to remove all stuff that might have been in it. database_bibles.deleteBible (demo_sample_bible_name ()); search_logic_delete_bible (demo_sample_bible_name ()); // Create a new one. database_bibles.createBible (demo_sample_bible_name ()); // Location of the USFM files for the sample Bible. string directory = filter_url_create_root_path ("demo"); vector <string> files = filter_url_scandir (directory); for (auto file : files) { // Only process the USFM files. if (filter_url_get_extension (file) == "usfm") { cout << file << endl; // Read the USFM. file = filter_url_create_path (directory, file); string usfm = filter_url_file_get_contents (file); usfm = filter_string_str_replace (" ", " ", usfm); // Import the USFM into the Bible. vector <BookChapterData> book_chapter_data = usfm_import (usfm, styles_logic_standard_sheet ()); for (auto data : book_chapter_data) { Bible_Logic::storeChapter (demo_sample_bible_name (), data.book, data.chapter, data.data); } } } // Clean the destination location for the Bible. string destination = sample_bible_bible_path (); filter_url_rmdir (destination); // Copy the Bible data to the destination. string source = database_bibles.bibleFolder (demo_sample_bible_name ()); filter_url_dir_cp (source, destination); // Clean the destination location for the Bible search index. destination = sample_bible_index_path (); filter_url_rmdir (destination); // Create destination location. filter_url_mkdir (destination); // Copy the index files over to the destination. source = search_logic_index_folder (); files = filter_url_scandir (source); for (auto file : files) { if (file.find (demo_sample_bible_name ()) != string::npos) { string source_file = filter_url_create_path (source, file); string destination_file = filter_url_create_path (destination, file); filter_url_file_cp (source_file, destination_file); } } }
// Get the names of the available default versification systems that come with Bibledit. vector <string> versification_logic_names () { vector <string> names; string directory = filter_url_create_root_path ("versification"); vector <string> files = filter_url_scandir (directory); for (auto file : files) { if (filter_url_get_extension (file) == "txt") { // Remove the dot and extension. file = file.substr (0, file.length () - 4); // Change underscores to spaces for the names. file = filter_string_str_replace ("_", " ", file); names.push_back (file); } } return names; }
// Returns 0 is not an archive that Bibledit supports. // Else returns 1, 2, 3... depending on the type of archive. int filter_archive_is_archive (string file) { // Tar (.tar) archives, including those compressed with gzip (.tar.gz, .tgz), bzip (.tar.bz, .tbz), bzip2 (.tar.bz2, .tbz2), compress (.tar.Z, .taz), lzop (.tar.lzo, .tzo) and lzma (.tar.lzma) // Zip archives (.zip) // Jar archives (.jar, .ear, .war) // 7z archives (.7z) // iso9660 CD images (.iso) // Lha archives (.lzh) // Single files compressed with gzip (.gz), bzip (.bz), bzip2 (.bz2), compress (.Z), lzop (.lzo) and lzma (.lzma) string suffix = filter_url_get_extension (file); if ((suffix == "tar.gz") || (suffix == "gz") || (suffix == "tgz")) { return 1; } if ((suffix == "zip")) { return 2; } if ((suffix == "tar.bz") || (suffix == "tbz") || (suffix == "tar.bz2") || (suffix == "tbz2")) { return 0; } return 0; }