From 5216ff3e26a7dcb9f0a05ce55239786f9e964414 Mon Sep 17 00:00:00 2001 From: deer Date: Wed, 28 Jun 2023 19:56:27 +0900 Subject: [PATCH] fix: autoindex --- playground/tkg/Design1/src/HTML.cpp | 2 +- playground/tkg/Design1/src/Observee/GET.cpp | 12 ++++++++---- 2 files changed, 9 insertions(+), 5 deletions(-) diff --git a/playground/tkg/Design1/src/HTML.cpp b/playground/tkg/Design1/src/HTML.cpp index cea5fd2..f3a8ce1 100644 --- a/playground/tkg/Design1/src/HTML.cpp +++ b/playground/tkg/Design1/src/HTML.cpp @@ -10,7 +10,7 @@ std::string HTML::header() { std::string HTML::footer() { return "\n"; } std::string HTML::aTag(const std::string &url, const std::string &text) { - return "

" + text + "

"; + return "

" + text + "

\n"; } std::string HTML::sanitize(const std::string &origin) { diff --git a/playground/tkg/Design1/src/Observee/GET.cpp b/playground/tkg/Design1/src/Observee/GET.cpp index db5c3d3..8e86c0f 100644 --- a/playground/tkg/Design1/src/Observee/GET.cpp +++ b/playground/tkg/Design1/src/Observee/GET.cpp @@ -48,17 +48,21 @@ std::string GET::listFilesAndDirectories(std::string &directory_path, const Http ret += "

Index of

"; while ((entry = readdir(dir)) != NULL) { std::string file_path; - if (directory_path.back() != '/') + std::string target_path; + if (directory_path.back() != '/') { file_path = directory_path + "/" + entry->d_name; - else + target_path = req.request_target_->getPath() + "/" + entry->d_name; + } else { file_path = directory_path + entry->d_name; + target_path = req.request_target_->getPath() + entry->d_name; + } if (stat(file_path.c_str(), &file_stat) == -1) { perror("listFilesAndDirectories: stat error"); continue; } - std::string name = entry->d_name; std::stringstream ss; - ss << "http://" << req.headers_.host.uri_host << ":" << req.headers_.host.port << file_path; + ss << "http://" << req.headers_.host.uri_host << ":" << req.headers_.host.port << target_path; + std::string name = entry->d_name; if (S_ISREG(file_stat.st_mode)) { ret += HTML::aTag(HTML::sanitize(ss.str()), HTML::sanitize(name)); } else if (S_ISDIR(file_stat.st_mode)) {