Loading src/theme.cpp +83 −65 Changes for src/theme.cpp: 83 added lines, 65 removed lines. Original line number Diff line number Diff line Loading @@ -32,6 +32,7 @@ #include <compare> #include <string> #include <filesystem> #include <functional> #include <map> #include <iterator> Loading Loading @@ -80,6 +81,13 @@ blogi::Theme::Theme(blogi::ThemeConfig& config,bool formated) : _Config(config){ _Formated=formated; // Pre-compute lowercase prefix for O(1) lookups _PrefixLower = _Config.config.getprefix(); std::transform(_PrefixLower.begin(), _PrefixLower.end(), _PrefixLower.begin(), [](unsigned char c){ return std::tolower(c); }); if (!_PrefixLower.empty() && _PrefixLower.back() == '/') _PrefixLower.pop_back(); std::string ppath(_Config.Theme); #ifdef Windows ppath+="\\public"; Loading Loading @@ -150,7 +158,17 @@ blogi::Theme::Theme(blogi::ThemeConfig& config,bool formated) : _Config(config){ tfile.Type = ThemeFilesTypes::GENERIC; } // Pre-compute the lowercase lookup key: /prefix/theme/public/file.ext tfile.LookupKey = _PrefixLower; { std::string pathLower = tfile.Path; std::transform(pathLower.begin(), pathLower.end(), pathLower.begin(), [](unsigned char c){ return std::tolower(c); }); tfile.LookupKey += pathLower; } _PublicFiles.push_back(tfile); _PublicFileIndex[_PublicFiles.back().LookupKey] = _PublicFiles.size() - 1; // [DEBUG] Theme: Successfully loaded asset: tfile.Path (bytes) } } Loading Loading @@ -303,45 +321,34 @@ bool blogi::Theme::Controller(const int tid,libhttppp::HttpRequest &req){ 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++){ // Convert to lowercase for case-insensitive matching std::string lookup = to_lower(full_request_url); std::string curfile_path_lower = to_lower(curfile->Path); // e.g., /theme/public/style.css // O(1) lookup via pre-computed index auto it = _PublicFileIndex.find(lookup); if (it == _PublicFileIndex.end()) return false; // Construct the full expected request path: /prefix/theme/public/style.css std::string expected_full_path = prefix_lower + curfile_path_lower; auto &curfile = _PublicFiles[it->second]; // 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.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") if(curfile.Type==ThemeFilesTypes::IMAGE){ if(curfile.Ending=="svg") resp.setContentType(std::string("image/svg+xml").c_str()); else if(curfile->Ending=="ico") else if(curfile.Ending=="ico") resp.setContentType(std::string("image/x-icon").c_str()); else resp.setContentType(std::string("image/").append(curfile->Ending).c_str()); }else if(curfile->Type==ThemeFilesTypes::TEXT){ resp.setContentType(std::string("text/").append(curfile->Ending).c_str()); }else if(curfile->Type==ThemeFilesTypes::JAVASCRIPT){ resp.setContentType(std::string("image/").append(curfile.Ending).c_str()); }else if(curfile.Type==ThemeFilesTypes::TEXT){ resp.setContentType(std::string("text/").append(curfile.Ending).c_str()); }else if(curfile.Type==ThemeFilesTypes::JAVASCRIPT){ resp.setContentType("application/javascript"); }else if(curfile->Type==ThemeFilesTypes::GENERIC){ }else if(curfile.Type==ThemeFilesTypes::GENERIC){ resp.setContentType("application/octet-stream"); } Loading @@ -360,13 +367,13 @@ bool blogi::Theme::Controller(const int tid,libhttppp::HttpRequest &req){ } // Final Header Setup: Set Content-Encoding and Content-Length if( curfile->Compressed.length() > 0 && compressed ){ if( curfile.Compressed.length() > 0 && compressed ){ resp.setHeaderData("content-encoding")->push_back("br"); resp.setContentLength(curfile->Compressed.length()); resp.setContentLength(curfile.Compressed.length()); resp.send(req,"",-1); }else{ // Send uncompressed/minified content resp.setContentLength(curfile->Content.length()); resp.setContentLength(curfile.Content.length()); resp.send(req,"",-1); } Loading @@ -375,10 +382,6 @@ bool blogi::Theme::Controller(const int tid,libhttppp::HttpRequest &req){ } } // If no match was found after checking all assets return false; } bool blogi::Theme::Response(const int tid, libhttppp::HttpRequest &req){ auto to_lower = [](const std::string& str) { Loading @@ -396,20 +399,20 @@ bool blogi::Theme::Response(const int tid, libhttppp::HttpRequest &req){ 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()); // Convert to lowercase for case-insensitive matching std::string lookup = to_lower(full_request_url); // Standardize prefix by removing trailing slash if present if (prefix_lower.length() > 0 && prefix_lower.back() == '/') { prefix_lower.pop_back(); } // O(1) lookup via pre-computed index auto it = _PublicFileIndex.find(lookup); if (it == _PublicFileIndex.end()) return false; auto &curfile = _PublicFiles[it->second]; // --- 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()){ Loading @@ -419,37 +422,26 @@ bool blogi::Theme::Response(const int tid, libhttppp::HttpRequest &req){ } } } // ------------------------------------ // Loop through all loaded public files and attempt an exact match for(auto curfile=_PublicFiles.begin(); curfile!=_PublicFiles.end(); curfile++){ std::string curfile_path_lower = to_lower(curfile->Path); // 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); // Stream data using direct pointer access instead of substr copies if( curfile.Compressed.length() > 0 && acpt_bz){ if(req.SendData.pos < curfile.Compressed.length()){ size_t remaining = curfile.Compressed.length() - req.SendData.pos; size_t len = BLOCKSIZE < remaining ? BLOCKSIZE : remaining; req.SendData.append(curfile.Compressed.c_str() + req.SendData.pos, len); req.SendData.pos += len; return true; } }else{ size_t len = BLOCKSIZE < curfile->Content.length()-req.SendData.pos ? BLOCKSIZE : curfile->Content.length()-req.SendData.pos; req.SendData.append(curfile->Content.substr(req.SendData.pos,len).c_str(),len); if(req.SendData.pos < curfile.Content.length()){ size_t remaining = curfile.Content.length() - req.SendData.pos; size_t len = BLOCKSIZE < remaining ? BLOCKSIZE : remaining; req.SendData.append(curfile.Content.c_str() + req.SendData.pos, len); req.SendData.pos += len; } return true; } } } return false; } Loading Loading @@ -511,6 +503,32 @@ void blogi::Theme::printSite(const int tid,libhtmlpp::HtmlString &output,libhtml // Inject theme color CSS variables into <head> injectThemeColors(tid, index); // Add loading="lazy" to all <img> tags except the banner logo (above the fold) { std::function<void(libhtmlpp::Element*)> addLazyImages = [&](libhtmlpp::Element *node) { if (!node) return; if (node->getType() == libhtmlpp::HtmlEl) { libhtmlpp::HtmlElement *hel = static_cast<libhtmlpp::HtmlElement*>(node); if (hel->getTagname() == "img") { // Check if this is the banner logo (skip — above the fold) bool isBanner = false; bool hasLoading = false; for (auto *attr = hel->firstAttribute(); attr; attr = attr->nextAttribute()) { if (attr->getKey() == "id" && attr->getValue() == "banner") isBanner = true; if (attr->getKey() == "loading") hasLoading = true; } if (!isBanner && !hasLoading) hel->setAttribute("loading", "lazy"); } for (auto *child = hel->firstChild(); child; child = child->nextElement()) addLazyImages(child); } }; addLazyImages(index); } // Print the final HTML structure to the output stream libhtmlpp::print(*index,output,_Formated); }catch(libhtmlpp::HTMLException &e){ Loading src/theme.h +4 −0 Changes for src/theme.h: 4 added lines, 0 removed lines. Original line number Diff line number Diff line Loading @@ -27,6 +27,7 @@ SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. #include <string> #include <vector> #include <unordered_map> #include <netplus/socket.h> #include <netplus/connection.h> Loading @@ -52,6 +53,7 @@ namespace blogi { struct ThemeFiles{ std::string Path; std::string LookupKey; // pre-computed lowercase full path (prefix + path) std::string Content; std::string Ending; std::string Compressed; Loading Loading @@ -97,6 +99,8 @@ namespace blogi { private: ThemeConfig _Config; std::vector<ThemeFiles> _PublicFiles; std::unordered_map<std::string, size_t> _PublicFileIndex; // lowercase path -> index std::string _PrefixLower; // pre-computed lowercase prefix bool _Formated; }; }; Loading
src/theme.cpp +83 −65 Changes for src/theme.cpp: 83 added lines, 65 removed lines. Original line number Diff line number Diff line Loading @@ -32,6 +32,7 @@ #include <compare> #include <string> #include <filesystem> #include <functional> #include <map> #include <iterator> Loading Loading @@ -80,6 +81,13 @@ blogi::Theme::Theme(blogi::ThemeConfig& config,bool formated) : _Config(config){ _Formated=formated; // Pre-compute lowercase prefix for O(1) lookups _PrefixLower = _Config.config.getprefix(); std::transform(_PrefixLower.begin(), _PrefixLower.end(), _PrefixLower.begin(), [](unsigned char c){ return std::tolower(c); }); if (!_PrefixLower.empty() && _PrefixLower.back() == '/') _PrefixLower.pop_back(); std::string ppath(_Config.Theme); #ifdef Windows ppath+="\\public"; Loading Loading @@ -150,7 +158,17 @@ blogi::Theme::Theme(blogi::ThemeConfig& config,bool formated) : _Config(config){ tfile.Type = ThemeFilesTypes::GENERIC; } // Pre-compute the lowercase lookup key: /prefix/theme/public/file.ext tfile.LookupKey = _PrefixLower; { std::string pathLower = tfile.Path; std::transform(pathLower.begin(), pathLower.end(), pathLower.begin(), [](unsigned char c){ return std::tolower(c); }); tfile.LookupKey += pathLower; } _PublicFiles.push_back(tfile); _PublicFileIndex[_PublicFiles.back().LookupKey] = _PublicFiles.size() - 1; // [DEBUG] Theme: Successfully loaded asset: tfile.Path (bytes) } } Loading Loading @@ -303,45 +321,34 @@ bool blogi::Theme::Controller(const int tid,libhttppp::HttpRequest &req){ 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++){ // Convert to lowercase for case-insensitive matching std::string lookup = to_lower(full_request_url); std::string curfile_path_lower = to_lower(curfile->Path); // e.g., /theme/public/style.css // O(1) lookup via pre-computed index auto it = _PublicFileIndex.find(lookup); if (it == _PublicFileIndex.end()) return false; // Construct the full expected request path: /prefix/theme/public/style.css std::string expected_full_path = prefix_lower + curfile_path_lower; auto &curfile = _PublicFiles[it->second]; // 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.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") if(curfile.Type==ThemeFilesTypes::IMAGE){ if(curfile.Ending=="svg") resp.setContentType(std::string("image/svg+xml").c_str()); else if(curfile->Ending=="ico") else if(curfile.Ending=="ico") resp.setContentType(std::string("image/x-icon").c_str()); else resp.setContentType(std::string("image/").append(curfile->Ending).c_str()); }else if(curfile->Type==ThemeFilesTypes::TEXT){ resp.setContentType(std::string("text/").append(curfile->Ending).c_str()); }else if(curfile->Type==ThemeFilesTypes::JAVASCRIPT){ resp.setContentType(std::string("image/").append(curfile.Ending).c_str()); }else if(curfile.Type==ThemeFilesTypes::TEXT){ resp.setContentType(std::string("text/").append(curfile.Ending).c_str()); }else if(curfile.Type==ThemeFilesTypes::JAVASCRIPT){ resp.setContentType("application/javascript"); }else if(curfile->Type==ThemeFilesTypes::GENERIC){ }else if(curfile.Type==ThemeFilesTypes::GENERIC){ resp.setContentType("application/octet-stream"); } Loading @@ -360,13 +367,13 @@ bool blogi::Theme::Controller(const int tid,libhttppp::HttpRequest &req){ } // Final Header Setup: Set Content-Encoding and Content-Length if( curfile->Compressed.length() > 0 && compressed ){ if( curfile.Compressed.length() > 0 && compressed ){ resp.setHeaderData("content-encoding")->push_back("br"); resp.setContentLength(curfile->Compressed.length()); resp.setContentLength(curfile.Compressed.length()); resp.send(req,"",-1); }else{ // Send uncompressed/minified content resp.setContentLength(curfile->Content.length()); resp.setContentLength(curfile.Content.length()); resp.send(req,"",-1); } Loading @@ -375,10 +382,6 @@ bool blogi::Theme::Controller(const int tid,libhttppp::HttpRequest &req){ } } // If no match was found after checking all assets return false; } bool blogi::Theme::Response(const int tid, libhttppp::HttpRequest &req){ auto to_lower = [](const std::string& str) { Loading @@ -396,20 +399,20 @@ bool blogi::Theme::Response(const int tid, libhttppp::HttpRequest &req){ 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()); // Convert to lowercase for case-insensitive matching std::string lookup = to_lower(full_request_url); // Standardize prefix by removing trailing slash if present if (prefix_lower.length() > 0 && prefix_lower.back() == '/') { prefix_lower.pop_back(); } // O(1) lookup via pre-computed index auto it = _PublicFileIndex.find(lookup); if (it == _PublicFileIndex.end()) return false; auto &curfile = _PublicFiles[it->second]; // --- 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()){ Loading @@ -419,37 +422,26 @@ bool blogi::Theme::Response(const int tid, libhttppp::HttpRequest &req){ } } } // ------------------------------------ // Loop through all loaded public files and attempt an exact match for(auto curfile=_PublicFiles.begin(); curfile!=_PublicFiles.end(); curfile++){ std::string curfile_path_lower = to_lower(curfile->Path); // 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); // Stream data using direct pointer access instead of substr copies if( curfile.Compressed.length() > 0 && acpt_bz){ if(req.SendData.pos < curfile.Compressed.length()){ size_t remaining = curfile.Compressed.length() - req.SendData.pos; size_t len = BLOCKSIZE < remaining ? BLOCKSIZE : remaining; req.SendData.append(curfile.Compressed.c_str() + req.SendData.pos, len); req.SendData.pos += len; return true; } }else{ size_t len = BLOCKSIZE < curfile->Content.length()-req.SendData.pos ? BLOCKSIZE : curfile->Content.length()-req.SendData.pos; req.SendData.append(curfile->Content.substr(req.SendData.pos,len).c_str(),len); if(req.SendData.pos < curfile.Content.length()){ size_t remaining = curfile.Content.length() - req.SendData.pos; size_t len = BLOCKSIZE < remaining ? BLOCKSIZE : remaining; req.SendData.append(curfile.Content.c_str() + req.SendData.pos, len); req.SendData.pos += len; } return true; } } } return false; } Loading Loading @@ -511,6 +503,32 @@ void blogi::Theme::printSite(const int tid,libhtmlpp::HtmlString &output,libhtml // Inject theme color CSS variables into <head> injectThemeColors(tid, index); // Add loading="lazy" to all <img> tags except the banner logo (above the fold) { std::function<void(libhtmlpp::Element*)> addLazyImages = [&](libhtmlpp::Element *node) { if (!node) return; if (node->getType() == libhtmlpp::HtmlEl) { libhtmlpp::HtmlElement *hel = static_cast<libhtmlpp::HtmlElement*>(node); if (hel->getTagname() == "img") { // Check if this is the banner logo (skip — above the fold) bool isBanner = false; bool hasLoading = false; for (auto *attr = hel->firstAttribute(); attr; attr = attr->nextAttribute()) { if (attr->getKey() == "id" && attr->getValue() == "banner") isBanner = true; if (attr->getKey() == "loading") hasLoading = true; } if (!isBanner && !hasLoading) hel->setAttribute("loading", "lazy"); } for (auto *child = hel->firstChild(); child; child = child->nextElement()) addLazyImages(child); } }; addLazyImages(index); } // Print the final HTML structure to the output stream libhtmlpp::print(*index,output,_Formated); }catch(libhtmlpp::HTMLException &e){ Loading
src/theme.h +4 −0 Changes for src/theme.h: 4 added lines, 0 removed lines. Original line number Diff line number Diff line Loading @@ -27,6 +27,7 @@ SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. #include <string> #include <vector> #include <unordered_map> #include <netplus/socket.h> #include <netplus/connection.h> Loading @@ -52,6 +53,7 @@ namespace blogi { struct ThemeFiles{ std::string Path; std::string LookupKey; // pre-computed lowercase full path (prefix + path) std::string Content; std::string Ending; std::string Compressed; Loading Loading @@ -97,6 +99,8 @@ namespace blogi { private: ThemeConfig _Config; std::vector<ThemeFiles> _PublicFiles; std::unordered_map<std::string, size_t> _PublicFileIndex; // lowercase path -> index std::string _PrefixLower; // pre-computed lowercase prefix bool _Formated; }; };