diff options
Diffstat (limited to 'weblog.cpp')
-rw-r--r-- | weblog.cpp | 45 |
1 files changed, 31 insertions, 14 deletions
@@ -192,6 +192,24 @@ namespace { return article.substr(0, pos); } + std::string plugin_path(FCGX_Request& request) + { + std::string target{FCGX_GetParam("DOCUMENT_URI", request.envp)}; + while (target.size() > 1 && target.back() == '/') { + target = target.substr(0, target.size() - 1); + } + + std::string rel_target{FCGX_GetParam("PATH_INFO", request.envp)}; + while (rel_target.size() > 1 && rel_target.back() == '/') { + rel_target = rel_target.substr(0, rel_target.size() - 1); + } + + if (target.ends_with(rel_target)) { + return target.substr(0, target.size() - rel_target.size()); + } + return "/"; + } + class HtmlPage { Config& m_config; @@ -203,18 +221,18 @@ namespace { HtmlPage(FCGX_Request& request, Config& config, std::string s = ""s): m_config{config}, - mContents(s), + mContents{s}, mHeader("<!DOCTYPE html><html><head>" "<meta charset=\"utf-8\"/>" "<meta name=\"viewport\" content=\"width=device-width, initial-scale=1\">" "<title>" + m_config.getName() + "</title>" "<meta name=\"keywords\" content=\"" + m_config.getKeywords() + "\"/>" - "<link rel=\"shortcut icon\" href=\"" + FCGX_GetParam("PATH_INFO", request.envp) + "/favicon.ico\" type=\"image/x-icon\"/>" - "<link rel=\"stylesheet\" type=\"text/css\" href=\"" + FCGX_GetParam("PATH_INFO", request.envp) + "/blog.css\"/>" + "<link rel=\"shortcut icon\" href=\"" + (fs::path{plugin_path(request)} / "favicon.ico").generic_string() + "\" type=\"image/x-icon\"/>" + "<link rel=\"stylesheet\" type=\"text/css\" href=\"" + (fs::path{plugin_path(request)} / "blog.css").generic_string() + "\"/>" "</head><body><div class=\"page\">"), mFooter("<br/><br/><br/><div class=\"impressum\"><a href=\""s + - FCGX_GetParam("PATH_INFO", request.envp) + - "/impressum.html\">Impressum, Datenschutzerklärung</a></div></div></body></html>") + (fs::path{plugin_path(request)} / "impressum.html").generic_string() + + "\">Impressum, Datenschutzerklärung</a></div></div></body></html>") { } @@ -241,7 +259,7 @@ namespace { HtmlPage htmlPage{request, config, "<h1>"s + config.getName() + "</h1>"s}; - fs::path link{ FCGX_GetParam("PATH_INFO", request.envp)}; + fs::path link{ plugin_path(request) }; auto list{getArticleList(path, page)}; if (list.empty()) @@ -266,6 +284,7 @@ namespace { htmlPage += " <a href=\"?page="s + std::to_string(page + 1) + "\">older>></a>"s; htmlPage += "<br/>"; } + FCGX_PutS("Content-Type: text/html\r\n", request.out); FCGX_FPrintF(request.out, "Cache-Control: no-store\r\n\r\n"); std::string data{htmlPage}; FCGX_PutStr(data.c_str(), data.size(), request.out); @@ -295,8 +314,9 @@ namespace { "<div class=\"date\">" + metaData.at("Date") + "</div>" "<br/><br/>"s + data + "<br/>□"}; - data = htmlPage; - FCGX_PutStr(data.c_str(), data.size(), request.out); + std::string result {htmlPage}; + FCGX_PutS("Content-Type: text/html\r\n\r\n", request.out); + FCGX_PutStr(result.c_str(), result.size(), request.out); } catch (const std::exception& ex) { return HttpStatus("500", "Reading Article: "s + ex.what(), request); } @@ -379,13 +399,13 @@ void generate_page(FCGX_Request& request, Config& config) std::unordered_map<std::string, std::string> query { SplitQueryString(rel_target) }; // Build the path to the requested file - std::string path_translated{FCGX_GetParam("PATH_TRANSLATED", request.envp)}; + std::string path_translated{config.getDataPath()}; if (rel_target.size() >= 4 && std::all_of(rel_target.begin(), rel_target.begin() + 4, isdigit)) { rel_target = rel_target.substr(0, 4) + "/" + rel_target; } - fs::path path {path_translated}; + fs::path path {fs::path{path_translated} / rel_target}; if (target.size() && target.back() != '/' && fs::is_directory(path)) { - std::string location{FCGX_GetParam("REQUEST_URI", request.envp) + "/"s}; + std::string location{FCGX_GetParam("SCRIPT_NAME", request.envp) + "/"s}; FCGX_FPrintF(request.out, "Location: %s\r\n", location.c_str()); return HttpStatus("301", "Correcting directory path", request); } @@ -401,17 +421,14 @@ void generate_page(FCGX_Request& request, Config& config) } if (is_index_page(rel_target)) { - FCGX_PutS("Content-Type: text/html\r\n", request.out); return generateIndexPage(path, request, config, page); } if (is_article_page(rel_target, path)) { - FCGX_PutS("Content-Type: text/html\r\n", request.out); return generateArticlePage(path, request, config); } if (is_index_file(rel_target, path) || is_article_file(rel_target, path)) { - FCGX_PutS("Content-Type: text/html\r\n", request.out); return generateStaticFile(path, request); } |