string index_listing (void * webserver_request, string url) { string page; page = Assets_Page::header ("Bibledit", webserver_request); // No breadcrumbs because the user can arrive here from more than one place. Assets_View view; url = filter_url_urldecode (url); url = filter_url_create_path ("", url); url = filter_string_str_replace ("\\", "/", url); view.set_variable ("url", url); string parent = filter_url_dirname_web (url); if (parent.length () > 1) { view.enable_zone ("parent"); view.set_variable ("parent", parent); } string directory = filter_url_create_root_path (url); if (!file_or_dir_exists (directory) || filter_url_is_dir (directory)) { vector <string> files = filter_url_scandir (directory); for (auto & file : files) { string path = filter_url_create_path (directory, file); string line; line.append ("<tr>"); line.append ("<td>"); line.append ("<a href=\"" + filter_url_create_path (url, file) + "\">"); line.append (file); line.append ("</a>"); line.append ("</td>"); line.append ("<td>"); if (!filter_url_is_dir (path)) { line.append (convert_to_string (filter_url_filesize (path))); } line.append ("</td>"); line.append ("</tr>"); file = line; } string listing = filter_string_implode (files, "\n"); if (listing.empty ()) listing = translate ("No files in this folder"); else { listing.insert (0, "<table>"); listing.append ("</table>"); } view.set_variable ("listing", listing); } else { string filename = filter_url_create_root_path (url); return filter_url_file_get_contents (filename); } page += view.render ("index", "listing"); page += Assets_Page::footer (); return page; }
// Does an integrity check on the database. // Returns true if healthy, false otherwise. bool database_sqlite_healthy (string database) { string file = database_sqlite_file (database); bool ok = false; // Do an integrity check on the database. // An empty file appears healthy too, so deal with that. if (filter_url_filesize (file) > 0) { sqlite3 * db = database_sqlite_connect (database); string query = "PRAGMA integrity_check;"; map <string, vector <string> > result = database_sqlite_query (db, query); vector <string> health = result ["integrity_check"]; if (health.size () == 1) { if (health [0] == "ok") ok = true; } database_sqlite_disconnect (db); } return ok; }
// This function serves a file and enables caching by the browser. void http_serve_cache_file (Webserver_Request * request) { // Full path to the file. string filename = filter_url_create_root_path (filter_url_urldecode (request->get)); // File size for browser caching. int size = filter_url_filesize (filename); request->etag = "\"" + convert_to_string (size) + "\""; // Deal with situation that the file in the browser's cache is up to date. // https://developers.google.com/web/fundamentals/performance/optimizing-content-efficiency/http-caching if (request->etag == request->if_none_match) { request->response_code = 304; return; } // Get file's contents. request->reply = filter_url_file_get_contents (filename); }