Loading src/compress.h +21 −8 Changes for src/compress.h: 21 added lines, 8 removed lines. Original line number Diff line number Diff line Loading @@ -3,19 +3,32 @@ #pragma once template<typename T> void compress(const std::string in,std::string &out){ void compress(const std::string& in, std::string& out) { size_t compressed_size = BrotliEncoderMaxCompressedSize(in.length()); size_t len= BrotliEncoderMaxCompressedSize(in.length()); unsigned char *buf = new unsigned char[compressed_size]; unsigned char *buf = new unsigned char[len]; size_t actual_compressed_len = compressed_size; BrotliEncoderCompress(BROTLI_DEFAULT_QUALITY,BROTLI_DEFAULT_WINDOW,BROTLI_MODE_TEXT, in.length(),(const unsigned char*)in.c_str(),&len,buf); BROTLI_BOOL result = BrotliEncoderCompress( BROTLI_DEFAULT_QUALITY, BROTLI_DEFAULT_WINDOW, BROTLI_MODE_TEXT, in.length(), (const unsigned char*)in.c_str(), &actual_compressed_len, // buf ); out.resize(len); if (result != BROTLI_TRUE) { out.clear(); delete[] buf; return; } out.resize(actual_compressed_len); std::copy(buf,buf+len,std::inserter<std::string>(out,out.begin())); std::memcpy(out.data(), buf, actual_compressed_len); delete[] buf; } src/theme.cpp +121 −61 Changes for src/theme.cpp: 121 added lines, 61 removed lines. Original line number Diff line number Diff line Loading @@ -43,9 +43,9 @@ #include "conf.h" #include "compress.h" blogi::Theme::Theme(blogi::ThemeConfig& config,bool formated) : _Config(config){ auto trimlines = [&] (const std::string in){ // Lambda function to minify/compress whitespace in text content (CSS/JS/HTML) std::string out; bool space=false; for(auto i : in){ Loading Loading @@ -87,26 +87,24 @@ blogi::Theme::Theme(blogi::ThemeConfig& config,bool formated) : _Config(config){ std::filesystem::path public_path = std::filesystem::path(ppath); if (std::filesystem::exists(public_path) && std::filesystem::is_directory(public_path)) { // Use directory_iterator to loop through files in the directory // [DEBUG] Theme: Found public asset directory: ppath for (const auto& entry : std::filesystem::directory_iterator(public_path)) { // Check if the current entry is a regular file if (std::filesystem::is_regular_file(entry.path())) { std::string buf; char* tmp = new char[BLOCKSIZE]; std::ifstream fs_stream; try { // Open the file. Use binary mode for correctness, as some files are images. fs_stream.open(entry.path(), std::ios::in | std::ios::binary); } catch (const std::exception& e) { std::cerr << "Could not open file: " << e.what() << std::endl; std::cerr << "Could not open file: " << entry.path().filename().string() << " - " << e.what() << std::endl; // Enhanced error log delete[] tmp; continue; } // Read file content into buffer while (fs_stream.good()) { fs_stream.read(tmp, BLOCKSIZE); buf.append(tmp, fs_stream.gcount()); Loading @@ -116,31 +114,34 @@ blogi::Theme::Theme(blogi::ThemeConfig& config,bool formated) : _Config(config){ delete[] tmp; ThemeFiles tfile; // Construct the path string using the filename // Path is stored with leading slash for direct matching later tfile.Path = "/theme/public/" + entry.path().filename().string(); tfile.Content = trimlines(buf); std::string fname = entry.path().filename().string(); size_t dot_pos = fname.rfind('.'); size_t end_pos = fname.rfind('?') != std::string::npos ? fname.rfind('?') : fname.length(); if (++dot_pos != std::string::npos) { // Determine file extension if (dot_pos != std::string::npos && ++dot_pos < end_pos) { tfile.Ending = fname.substr(dot_pos,end_pos-dot_pos); } else { tfile.Ending = ""; } std::cout << tfile.Ending << std::endl; // Initialize content with the raw buffer tfile.Content = buf; if (tfile.Ending == "png" || tfile.Ending == "jpg" || tfile.Ending == "webp" || tfile.Ending == "ico" || tfile.Ending == "svg") { tfile.Type = ThemeFilesTypes::IMAGE; } else if (tfile.Ending == "css" || tfile.Ending == "html") { compress<std::string>(tfile.Content, tfile.Compressed); tfile.Content = trimlines(buf); // Apply minification compress(tfile.Content, tfile.Compressed); // Compress content tfile.Type = ThemeFilesTypes::TEXT; } else if (tfile.Ending == "js") { compress<std::string>(tfile.Content, tfile.Compressed); tfile.Content = trimlines(buf); // Apply minification compress(tfile.Content, tfile.Compressed); // Compress content tfile.Type = ThemeFilesTypes::JAVASCRIPT; } else { Loading @@ -148,13 +149,16 @@ blogi::Theme::Theme(blogi::ThemeConfig& config,bool formated) : _Config(config){ } _PublicFiles.push_back(tfile); // [DEBUG] Theme: Successfully loaded asset: tfile.Path (bytes) } } } else { std::cerr << "[WARNING] Theme: Public asset directory not found or is not a directory: " << ppath << std::endl; // Warning log } } blogi::Theme::~Theme(){ // Destructor implementation } void blogi::Theme::renderPage(const int tid,const char *name,libhtmlpp::HtmlPage& page, libhtmlpp::HtmlElement &index){ Loading @@ -171,23 +175,24 @@ void blogi::Theme::renderPage(const int tid,const char *name,libhtmlpp::HtmlPage if (fhfile.is_open()) { char ch; // The loop continues as long as a character can be successfully read. // Read the entire file character by character while (fhfile.get(ch)) { temp.push_back(ch); } fhfile.close(); } else { // Handle error, e.g., print to console std::cerr << "Error: Could not open file " << htmlfile << std::endl; return; } auto pos=temp.end(),epos=temp.begin(); // Map of variables to replace in the HTML template std::map<std::string, std::string> variable_map = { {"PREFIX", _Config.config.getprefix()}, // Assuming _Config provides the prefix {"SITEURL", _Config.config.getsiteurl()} // Example URL {"PREFIX", _Config.config.getprefix()}, {"SITEURL", _Config.config.getsiteurl()} }; // Template variable replacement loop (e.g., replacing ${VARIABLE} placeholders) do { pos = std::find(epos, temp.end(), '$'); Loading @@ -199,12 +204,12 @@ void blogi::Theme::renderPage(const int tid,const char *name,libhtmlpp::HtmlPage std::copy(epos, pos, std::back_inserter(data)); auto var_start = pos; var_start+=2; var_start+=2; // Move past "${" epos = std::find(var_start, temp.end(), '}'); if (epos == temp.end()) { std::copy(pos, temp.end(), std::back_inserter(data)); std::copy(pos, temp.end(), std::back_inserter(data)); // Copy literal "$..." if closing bracket is missing break; } Loading @@ -216,29 +221,63 @@ void blogi::Theme::renderPage(const int tid,const char *name,libhtmlpp::HtmlPage const std::string& replacement = it->second; std::copy(replacement.begin(), replacement.end(), std::back_inserter(data)); } else { std::copy(pos, epos+1, std::back_inserter(data)); // Copy from '$' up to '}' std::copy(pos, epos+1, std::back_inserter(data)); // Copy original placeholder if variable not found } ++epos; } while (pos != temp.end()); data.push_back('\0'); page.loadString(index,data.data()); data.push_back('\0'); // Null-terminate the string for C-style functions page.loadString(index,data.data()); // Load processed HTML content } bool blogi::Theme::Controller(const int tid,libhttppp::HttpRequest &req){ std::string publicf = req.getRequestURL(); if(publicf.length() >_Config.config.getprefix().length() && publicf.compare(_Config.config.getprefix().length(),13,"/theme/public/",13)==0){ // Helper lambda for converting a string to lowercase auto to_lower = [](const std::string& str) { std::string data = str; std::transform(data.begin(), data.end(), data.begin(), [](unsigned char c){ return std::tolower(c); }); return data; }; std::string full_request_url = req.getRequestURL(); // Strip the query string (e.g., ?v=17) from the requested URL size_t query_pos = full_request_url.find('?'); if (query_pos != std::string::npos) { full_request_url = full_request_url.substr(0, query_pos); } // Convert the cleaned URL and prefix to lowercase for case-insensitive matching std::string publicf_lower = to_lower(full_request_url); std::string prefix_lower = to_lower(_Config.config.getprefix()); // Standardize prefix by removing trailing slash if present (e.g., "/blog/" -> "/blog") if (prefix_lower.length() > 0 && prefix_lower.back() == '/') { prefix_lower.pop_back(); } // Loop through all loaded public files and attempt an exact match for(auto curfile=_PublicFiles.begin(); curfile!=_PublicFiles.end(); curfile++){ if(publicf.compare(_Config.config.getprefix().length(),curfile->Path.length(),curfile->Path,0,curfile->Path.length())==0){ std::string curfile_path_lower = to_lower(curfile->Path); // e.g., /theme/public/style.css // Construct the full expected request path: /prefix/theme/public/style.css std::string expected_full_path = prefix_lower + curfile_path_lower; // Check if the request URL matches the expected full path exactly (case-insensitive) if (publicf_lower.length() == expected_full_path.length() && publicf_lower.compare(0, publicf_lower.length(), expected_full_path) == 0) { libhttppp::HttpResponse resp; resp.setVersion(HTTPVERSION(1.1)); resp.setState(HTTP200); resp.setHeaderData("cache-control")->push_back("max-age=31536000"); // Set Content-Type based on file ending if(curfile->Type==ThemeFilesTypes::IMAGE){ if(curfile->Ending=="svg") resp.setContentType(std::string("image/svg+xml").c_str()); Loading @@ -253,44 +292,39 @@ bool blogi::Theme::Controller(const int tid,libhttppp::HttpRequest &req){ } bool compressed = false; libhttppp::HttpHeader::HeaderData *acph=req.getHeaderData("accept-encoding"); // Check if client supports Brotli ("br") if(acph){ for(libhttppp::HttpHeader::HeaderData::Values *cdat=acph->getfirstValue(); cdat; cdat=cdat->nextvalue()){ if(cdat->getvalue().find("br")!=std::string::npos){ if(to_lower(cdat->getvalue()).find("br")!=std::string::npos){ compressed=true; break; } } } if( curfile->Compressed.length() >0 && req.getHeaderData("accept-encoding") && compressed ){ // Final Header Setup: Set Content-Encoding and Content-Length if( curfile->Compressed.length() > 0 && compressed ){ resp.setHeaderData("content-encoding")->push_back("br"); resp.setContentLength(curfile->Compressed.length()); resp.send(req,"",-1); }else{ // Send uncompressed/minified content resp.setContentLength(curfile->Content.length()); resp.send(req,"",-1); } req.SendData.pos=0; req.SendData.pos=0; // Reset position for streaming in the Response function return true; } } libhttppp::HTTPException excep; excep[libhttppp::HTTPException::Error] << "template file nod found!"; throw excep; } // If no match was found after checking all assets return false; } #include <algorithm> #include <cctype> #include <string> bool blogi::Theme::Response(const int tid, libhttppp::HttpRequest &req){ auto to_lower = [](const std::string& str) { Loading @@ -300,26 +334,54 @@ bool blogi::Theme::Response(const int tid, libhttppp::HttpRequest &req){ return data; }; std::string publicf = to_lower(req.getRequestURL()); std::string full_request_url = req.getRequestURL(); // Strip the query string (?v=17) from the requested URL size_t query_pos = full_request_url.find('?'); if (query_pos != std::string::npos) { full_request_url = full_request_url.substr(0, query_pos); } // Convert the cleaned URL and prefix to lowercase std::string publicf = to_lower(full_request_url); std::string prefix_lower = to_lower(_Config.config.getprefix()); size_t prefix_len = prefix_lower.length(); const std::string public_theme_path = "/theme/public/"; size_t public_theme_path_len = public_theme_path.length(); // Standardize prefix by removing trailing slash if present if (prefix_lower.length() > 0 && prefix_lower.back() == '/') { prefix_lower.pop_back(); } if(publicf.length() > prefix_len && publicf.compare(prefix_len, public_theme_path_len, public_theme_path, public_theme_path_len) == 0) { // --- Compression Acceptance Check --- bool acpt_bz = false; libhttppp::HttpHeader::HeaderData *acph = req.getHeaderData("accept-encoding"); // Check if client supports Brotli ("br") if (acph) { for(libhttppp::HttpHeader::HeaderData::Values *cdat = acph->getfirstValue(); cdat; cdat = cdat->nextvalue()){ if(to_lower(cdat->getvalue()).find("br") != std::string::npos){ acpt_bz = true; break; } } } // ------------------------------------ // Loop through all loaded public files and attempt an exact match for(auto curfile=_PublicFiles.begin(); curfile!=_PublicFiles.end(); curfile++){ // 3. Convert the stored file path to lowercase for case-insensitive comparison std::string curfile_path_lower = to_lower(curfile->Path); size_t curfile_path_len = curfile_path_lower.length(); if(publicf.compare(prefix_len, curfile_path_len, curfile_path_lower, curfile_path_len) == 0){ if(req.SendData.pos < curfile->Content.length() ){ if( curfile->Compressed.length() > 0 && req.getHeaderData("accept-encoding") && to_lower(req.getHeaderData("accept-encoding")->at(0).getvalue()).find("br") != std::string::npos){ // Construct the full expected request path: /prefix/theme/public/style.css std::string expected_full_path = prefix_lower + curfile_path_lower; // Check if the request URL matches the expected full path exactly (case-insensitive) if (publicf.length() == expected_full_path.length() && publicf.compare(0, publicf.length(), expected_full_path) == 0) { // If there is more data to send (streaming response) if(req.SendData.pos < curfile->Content.length() ){ if( curfile->Compressed.length() > 0 && acpt_bz){ size_t len = BLOCKSIZE < curfile->Compressed.length()-req.SendData.pos ? BLOCKSIZE : curfile->Compressed.length()-req.SendData.pos; req.SendData.append(curfile->Compressed.substr(req.SendData.pos,len).c_str(),len); Loading @@ -334,7 +396,6 @@ bool blogi::Theme::Response(const int tid, libhttppp::HttpRequest &req){ } } } } return false; } Loading @@ -347,23 +408,26 @@ void blogi::Theme::printSite(const int tid,libhtmlpp::HtmlString &output,libhtml std::string hostpath; if(meta && head){ // Add meta description libhtmlpp::HtmlElement des("meta"); des.setAttribute("name","description"); des.setAttribute("content",meta); head->appendChild(&des); // Add Open Graph image meta tag libhtmlpp::HtmlElement thumbnail("meta"); thumbnail.setAttribute("property","og:image"); thumbnail.setAttribute("content",std::string(_Config.config.getsiteurl()).append(_Config.config.buildurl("theme/public/thumbnail.webp")).c_str()); head->appendChild(&thumbnail); // Add Open Graph URL meta tag libhtmlpp::HtmlElement baseurl("meta"); baseurl.setAttribute("property","og:url"); baseurl.setAttribute("content",std::string(_Config.config.getsiteurl()).append(crrurl).c_str()); head->appendChild(&baseurl); } // Add footer navigation links (Impressum, Login/Logout, Settings) footernav = index->getElementbyID("footernav"); libhtmlpp::HtmlString footerancor; footerancor << "<div><a class=\"footer\" href=\"" << _Config.config.buildurl("staticpage/impressum") << "\" >Impressum</a>"; Loading @@ -378,6 +442,7 @@ void blogi::Theme::printSite(const int tid,libhtmlpp::HtmlString &output,libhtml if(footernav) footernav->appendChild(footerancor.parse()); // Print the final HTML structure to the output stream libhtmlpp::print(*index,output,_Formated); }catch(libhtmlpp::HTMLException &e){ libhttppp::HTTPException excep; Loading @@ -386,8 +451,3 @@ void blogi::Theme::printSite(const int tid,libhtmlpp::HtmlString &output,libhtml } } Loading
src/compress.h +21 −8 Changes for src/compress.h: 21 added lines, 8 removed lines. Original line number Diff line number Diff line Loading @@ -3,19 +3,32 @@ #pragma once template<typename T> void compress(const std::string in,std::string &out){ void compress(const std::string& in, std::string& out) { size_t compressed_size = BrotliEncoderMaxCompressedSize(in.length()); size_t len= BrotliEncoderMaxCompressedSize(in.length()); unsigned char *buf = new unsigned char[compressed_size]; unsigned char *buf = new unsigned char[len]; size_t actual_compressed_len = compressed_size; BrotliEncoderCompress(BROTLI_DEFAULT_QUALITY,BROTLI_DEFAULT_WINDOW,BROTLI_MODE_TEXT, in.length(),(const unsigned char*)in.c_str(),&len,buf); BROTLI_BOOL result = BrotliEncoderCompress( BROTLI_DEFAULT_QUALITY, BROTLI_DEFAULT_WINDOW, BROTLI_MODE_TEXT, in.length(), (const unsigned char*)in.c_str(), &actual_compressed_len, // buf ); out.resize(len); if (result != BROTLI_TRUE) { out.clear(); delete[] buf; return; } out.resize(actual_compressed_len); std::copy(buf,buf+len,std::inserter<std::string>(out,out.begin())); std::memcpy(out.data(), buf, actual_compressed_len); delete[] buf; }
src/theme.cpp +121 −61 Changes for src/theme.cpp: 121 added lines, 61 removed lines. Original line number Diff line number Diff line Loading @@ -43,9 +43,9 @@ #include "conf.h" #include "compress.h" blogi::Theme::Theme(blogi::ThemeConfig& config,bool formated) : _Config(config){ auto trimlines = [&] (const std::string in){ // Lambda function to minify/compress whitespace in text content (CSS/JS/HTML) std::string out; bool space=false; for(auto i : in){ Loading Loading @@ -87,26 +87,24 @@ blogi::Theme::Theme(blogi::ThemeConfig& config,bool formated) : _Config(config){ std::filesystem::path public_path = std::filesystem::path(ppath); if (std::filesystem::exists(public_path) && std::filesystem::is_directory(public_path)) { // Use directory_iterator to loop through files in the directory // [DEBUG] Theme: Found public asset directory: ppath for (const auto& entry : std::filesystem::directory_iterator(public_path)) { // Check if the current entry is a regular file if (std::filesystem::is_regular_file(entry.path())) { std::string buf; char* tmp = new char[BLOCKSIZE]; std::ifstream fs_stream; try { // Open the file. Use binary mode for correctness, as some files are images. fs_stream.open(entry.path(), std::ios::in | std::ios::binary); } catch (const std::exception& e) { std::cerr << "Could not open file: " << e.what() << std::endl; std::cerr << "Could not open file: " << entry.path().filename().string() << " - " << e.what() << std::endl; // Enhanced error log delete[] tmp; continue; } // Read file content into buffer while (fs_stream.good()) { fs_stream.read(tmp, BLOCKSIZE); buf.append(tmp, fs_stream.gcount()); Loading @@ -116,31 +114,34 @@ blogi::Theme::Theme(blogi::ThemeConfig& config,bool formated) : _Config(config){ delete[] tmp; ThemeFiles tfile; // Construct the path string using the filename // Path is stored with leading slash for direct matching later tfile.Path = "/theme/public/" + entry.path().filename().string(); tfile.Content = trimlines(buf); std::string fname = entry.path().filename().string(); size_t dot_pos = fname.rfind('.'); size_t end_pos = fname.rfind('?') != std::string::npos ? fname.rfind('?') : fname.length(); if (++dot_pos != std::string::npos) { // Determine file extension if (dot_pos != std::string::npos && ++dot_pos < end_pos) { tfile.Ending = fname.substr(dot_pos,end_pos-dot_pos); } else { tfile.Ending = ""; } std::cout << tfile.Ending << std::endl; // Initialize content with the raw buffer tfile.Content = buf; if (tfile.Ending == "png" || tfile.Ending == "jpg" || tfile.Ending == "webp" || tfile.Ending == "ico" || tfile.Ending == "svg") { tfile.Type = ThemeFilesTypes::IMAGE; } else if (tfile.Ending == "css" || tfile.Ending == "html") { compress<std::string>(tfile.Content, tfile.Compressed); tfile.Content = trimlines(buf); // Apply minification compress(tfile.Content, tfile.Compressed); // Compress content tfile.Type = ThemeFilesTypes::TEXT; } else if (tfile.Ending == "js") { compress<std::string>(tfile.Content, tfile.Compressed); tfile.Content = trimlines(buf); // Apply minification compress(tfile.Content, tfile.Compressed); // Compress content tfile.Type = ThemeFilesTypes::JAVASCRIPT; } else { Loading @@ -148,13 +149,16 @@ blogi::Theme::Theme(blogi::ThemeConfig& config,bool formated) : _Config(config){ } _PublicFiles.push_back(tfile); // [DEBUG] Theme: Successfully loaded asset: tfile.Path (bytes) } } } else { std::cerr << "[WARNING] Theme: Public asset directory not found or is not a directory: " << ppath << std::endl; // Warning log } } blogi::Theme::~Theme(){ // Destructor implementation } void blogi::Theme::renderPage(const int tid,const char *name,libhtmlpp::HtmlPage& page, libhtmlpp::HtmlElement &index){ Loading @@ -171,23 +175,24 @@ void blogi::Theme::renderPage(const int tid,const char *name,libhtmlpp::HtmlPage if (fhfile.is_open()) { char ch; // The loop continues as long as a character can be successfully read. // Read the entire file character by character while (fhfile.get(ch)) { temp.push_back(ch); } fhfile.close(); } else { // Handle error, e.g., print to console std::cerr << "Error: Could not open file " << htmlfile << std::endl; return; } auto pos=temp.end(),epos=temp.begin(); // Map of variables to replace in the HTML template std::map<std::string, std::string> variable_map = { {"PREFIX", _Config.config.getprefix()}, // Assuming _Config provides the prefix {"SITEURL", _Config.config.getsiteurl()} // Example URL {"PREFIX", _Config.config.getprefix()}, {"SITEURL", _Config.config.getsiteurl()} }; // Template variable replacement loop (e.g., replacing ${VARIABLE} placeholders) do { pos = std::find(epos, temp.end(), '$'); Loading @@ -199,12 +204,12 @@ void blogi::Theme::renderPage(const int tid,const char *name,libhtmlpp::HtmlPage std::copy(epos, pos, std::back_inserter(data)); auto var_start = pos; var_start+=2; var_start+=2; // Move past "${" epos = std::find(var_start, temp.end(), '}'); if (epos == temp.end()) { std::copy(pos, temp.end(), std::back_inserter(data)); std::copy(pos, temp.end(), std::back_inserter(data)); // Copy literal "$..." if closing bracket is missing break; } Loading @@ -216,29 +221,63 @@ void blogi::Theme::renderPage(const int tid,const char *name,libhtmlpp::HtmlPage const std::string& replacement = it->second; std::copy(replacement.begin(), replacement.end(), std::back_inserter(data)); } else { std::copy(pos, epos+1, std::back_inserter(data)); // Copy from '$' up to '}' std::copy(pos, epos+1, std::back_inserter(data)); // Copy original placeholder if variable not found } ++epos; } while (pos != temp.end()); data.push_back('\0'); page.loadString(index,data.data()); data.push_back('\0'); // Null-terminate the string for C-style functions page.loadString(index,data.data()); // Load processed HTML content } bool blogi::Theme::Controller(const int tid,libhttppp::HttpRequest &req){ std::string publicf = req.getRequestURL(); if(publicf.length() >_Config.config.getprefix().length() && publicf.compare(_Config.config.getprefix().length(),13,"/theme/public/",13)==0){ // Helper lambda for converting a string to lowercase auto to_lower = [](const std::string& str) { std::string data = str; std::transform(data.begin(), data.end(), data.begin(), [](unsigned char c){ return std::tolower(c); }); return data; }; std::string full_request_url = req.getRequestURL(); // Strip the query string (e.g., ?v=17) from the requested URL size_t query_pos = full_request_url.find('?'); if (query_pos != std::string::npos) { full_request_url = full_request_url.substr(0, query_pos); } // Convert the cleaned URL and prefix to lowercase for case-insensitive matching std::string publicf_lower = to_lower(full_request_url); std::string prefix_lower = to_lower(_Config.config.getprefix()); // Standardize prefix by removing trailing slash if present (e.g., "/blog/" -> "/blog") if (prefix_lower.length() > 0 && prefix_lower.back() == '/') { prefix_lower.pop_back(); } // Loop through all loaded public files and attempt an exact match for(auto curfile=_PublicFiles.begin(); curfile!=_PublicFiles.end(); curfile++){ if(publicf.compare(_Config.config.getprefix().length(),curfile->Path.length(),curfile->Path,0,curfile->Path.length())==0){ std::string curfile_path_lower = to_lower(curfile->Path); // e.g., /theme/public/style.css // Construct the full expected request path: /prefix/theme/public/style.css std::string expected_full_path = prefix_lower + curfile_path_lower; // Check if the request URL matches the expected full path exactly (case-insensitive) if (publicf_lower.length() == expected_full_path.length() && publicf_lower.compare(0, publicf_lower.length(), expected_full_path) == 0) { libhttppp::HttpResponse resp; resp.setVersion(HTTPVERSION(1.1)); resp.setState(HTTP200); resp.setHeaderData("cache-control")->push_back("max-age=31536000"); // Set Content-Type based on file ending if(curfile->Type==ThemeFilesTypes::IMAGE){ if(curfile->Ending=="svg") resp.setContentType(std::string("image/svg+xml").c_str()); Loading @@ -253,44 +292,39 @@ bool blogi::Theme::Controller(const int tid,libhttppp::HttpRequest &req){ } bool compressed = false; libhttppp::HttpHeader::HeaderData *acph=req.getHeaderData("accept-encoding"); // Check if client supports Brotli ("br") if(acph){ for(libhttppp::HttpHeader::HeaderData::Values *cdat=acph->getfirstValue(); cdat; cdat=cdat->nextvalue()){ if(cdat->getvalue().find("br")!=std::string::npos){ if(to_lower(cdat->getvalue()).find("br")!=std::string::npos){ compressed=true; break; } } } if( curfile->Compressed.length() >0 && req.getHeaderData("accept-encoding") && compressed ){ // Final Header Setup: Set Content-Encoding and Content-Length if( curfile->Compressed.length() > 0 && compressed ){ resp.setHeaderData("content-encoding")->push_back("br"); resp.setContentLength(curfile->Compressed.length()); resp.send(req,"",-1); }else{ // Send uncompressed/minified content resp.setContentLength(curfile->Content.length()); resp.send(req,"",-1); } req.SendData.pos=0; req.SendData.pos=0; // Reset position for streaming in the Response function return true; } } libhttppp::HTTPException excep; excep[libhttppp::HTTPException::Error] << "template file nod found!"; throw excep; } // If no match was found after checking all assets return false; } #include <algorithm> #include <cctype> #include <string> bool blogi::Theme::Response(const int tid, libhttppp::HttpRequest &req){ auto to_lower = [](const std::string& str) { Loading @@ -300,26 +334,54 @@ bool blogi::Theme::Response(const int tid, libhttppp::HttpRequest &req){ return data; }; std::string publicf = to_lower(req.getRequestURL()); std::string full_request_url = req.getRequestURL(); // Strip the query string (?v=17) from the requested URL size_t query_pos = full_request_url.find('?'); if (query_pos != std::string::npos) { full_request_url = full_request_url.substr(0, query_pos); } // Convert the cleaned URL and prefix to lowercase std::string publicf = to_lower(full_request_url); std::string prefix_lower = to_lower(_Config.config.getprefix()); size_t prefix_len = prefix_lower.length(); const std::string public_theme_path = "/theme/public/"; size_t public_theme_path_len = public_theme_path.length(); // Standardize prefix by removing trailing slash if present if (prefix_lower.length() > 0 && prefix_lower.back() == '/') { prefix_lower.pop_back(); } if(publicf.length() > prefix_len && publicf.compare(prefix_len, public_theme_path_len, public_theme_path, public_theme_path_len) == 0) { // --- Compression Acceptance Check --- bool acpt_bz = false; libhttppp::HttpHeader::HeaderData *acph = req.getHeaderData("accept-encoding"); // Check if client supports Brotli ("br") if (acph) { for(libhttppp::HttpHeader::HeaderData::Values *cdat = acph->getfirstValue(); cdat; cdat = cdat->nextvalue()){ if(to_lower(cdat->getvalue()).find("br") != std::string::npos){ acpt_bz = true; break; } } } // ------------------------------------ // Loop through all loaded public files and attempt an exact match for(auto curfile=_PublicFiles.begin(); curfile!=_PublicFiles.end(); curfile++){ // 3. Convert the stored file path to lowercase for case-insensitive comparison std::string curfile_path_lower = to_lower(curfile->Path); size_t curfile_path_len = curfile_path_lower.length(); if(publicf.compare(prefix_len, curfile_path_len, curfile_path_lower, curfile_path_len) == 0){ if(req.SendData.pos < curfile->Content.length() ){ if( curfile->Compressed.length() > 0 && req.getHeaderData("accept-encoding") && to_lower(req.getHeaderData("accept-encoding")->at(0).getvalue()).find("br") != std::string::npos){ // Construct the full expected request path: /prefix/theme/public/style.css std::string expected_full_path = prefix_lower + curfile_path_lower; // Check if the request URL matches the expected full path exactly (case-insensitive) if (publicf.length() == expected_full_path.length() && publicf.compare(0, publicf.length(), expected_full_path) == 0) { // If there is more data to send (streaming response) if(req.SendData.pos < curfile->Content.length() ){ if( curfile->Compressed.length() > 0 && acpt_bz){ size_t len = BLOCKSIZE < curfile->Compressed.length()-req.SendData.pos ? BLOCKSIZE : curfile->Compressed.length()-req.SendData.pos; req.SendData.append(curfile->Compressed.substr(req.SendData.pos,len).c_str(),len); Loading @@ -334,7 +396,6 @@ bool blogi::Theme::Response(const int tid, libhttppp::HttpRequest &req){ } } } } return false; } Loading @@ -347,23 +408,26 @@ void blogi::Theme::printSite(const int tid,libhtmlpp::HtmlString &output,libhtml std::string hostpath; if(meta && head){ // Add meta description libhtmlpp::HtmlElement des("meta"); des.setAttribute("name","description"); des.setAttribute("content",meta); head->appendChild(&des); // Add Open Graph image meta tag libhtmlpp::HtmlElement thumbnail("meta"); thumbnail.setAttribute("property","og:image"); thumbnail.setAttribute("content",std::string(_Config.config.getsiteurl()).append(_Config.config.buildurl("theme/public/thumbnail.webp")).c_str()); head->appendChild(&thumbnail); // Add Open Graph URL meta tag libhtmlpp::HtmlElement baseurl("meta"); baseurl.setAttribute("property","og:url"); baseurl.setAttribute("content",std::string(_Config.config.getsiteurl()).append(crrurl).c_str()); head->appendChild(&baseurl); } // Add footer navigation links (Impressum, Login/Logout, Settings) footernav = index->getElementbyID("footernav"); libhtmlpp::HtmlString footerancor; footerancor << "<div><a class=\"footer\" href=\"" << _Config.config.buildurl("staticpage/impressum") << "\" >Impressum</a>"; Loading @@ -378,6 +442,7 @@ void blogi::Theme::printSite(const int tid,libhtmlpp::HtmlString &output,libhtml if(footernav) footernav->appendChild(footerancor.parse()); // Print the final HTML structure to the output stream libhtmlpp::print(*index,output,_Formated); }catch(libhtmlpp::HTMLException &e){ libhttppp::HTTPException excep; Loading @@ -386,8 +451,3 @@ void blogi::Theme::printSite(const int tid,libhtmlpp::HtmlString &output,libhtml } }