Skip to content

Commit

Permalink
refactor
Browse files Browse the repository at this point in the history
  • Loading branch information
shmorish committed Jan 15, 2025
1 parent 8e964ca commit a2c92a7
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 46 deletions.
53 changes: 10 additions & 43 deletions src/lib/http/handler/static_file_handler.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -12,46 +12,14 @@ namespace http {
StaticFileHandler::StaticFileHandler(const config::LocationContext::DocumentRootConfig &docRootConfig)
: docRootConfig_(docRootConfig) {}

static std::string removeUnneededPaths(const std::string &path) {
std::string result;
bool lastWasSlash = false;

for (std::size_t i = 0; i < path.size(); i++) {
const char c = path[i];
if (c != '/') {
result += c;
lastWasSlash = false;
} else if (!lastWasSlash) {
result += c;
lastWasSlash = true;
}
}
if (!result.empty() && result[result.size() - 1] == '/') {
result.pop_back();
}
if (result.front() == '.') {
result = result.substr(1);
}
return result;
}

std::string
StaticFileHandler::getNextLink(const std::string &currentDirPath, const std::string &entryName, const bool isDir) {
const std::string dirSlash = isDir ? "/" : "";
if (entryName == "..") {
return currentDirPath.substr(0, currentDirPath.find_last_of('/')) + dirSlash;
}
return currentDirPath + "/" + entryName + dirSlash;
}

Result<std::string, HttpStatusCode> StaticFileHandler::makeDirectoryListingHtml(const std::string &path) {
DIR *dir = opendir(path.c_str());
Result<std::string, HttpStatusCode>
StaticFileHandler::makeDirectoryListingHtml(const std::string &root, const std::string &target) {
DIR *dir = opendir((root + target).c_str());
if (dir == NULL) {
LOG_DEBUGF("failed to open directory: %s", path.c_str());
LOG_DEBUGF("failed to open directory: %s", (root + target).c_str());
return Err(kStatusInternalServerError);
}
const std::string normalizedPath = removeUnneededPaths(path);
const std::string title = "Index of " + normalizedPath + "/";
const std::string title = "Index of " + target;
std::string res = "<!DOCTYPE html>";
res += "<html>";
res += "<head><title>" + title + "</title></head>";
Expand All @@ -65,8 +33,7 @@ namespace http {
if (dName == ".") {
continue;
}
const unsigned char dType = dp->d_type;
const std::string dNameLink = getNextLink(normalizedPath, dName, dType == DT_DIR);
const std::string dNameLink = dName + (dp->d_type == DT_DIR ? "/" : "");
res += "<li>";
res += "<a href=\"" + dNameLink + "\">";
res += dName;
Expand All @@ -84,9 +51,8 @@ namespace http {
return Ok(res);
}


Response StaticFileHandler::directoryListing(const std::string &requestTarget) {
const Result<std::string, HttpStatusCode> result = makeDirectoryListingHtml(requestTarget);
Response StaticFileHandler::directoryListing(const std::string &root, const std::string &target) {
const Result<std::string, HttpStatusCode> result = makeDirectoryListingHtml(root, target);
if (result.isErr()) {
return ResponseBuilder().status(result.unwrapErr()).build();
}
Expand All @@ -95,6 +61,7 @@ namespace http {
}

Response StaticFileHandler::serve(const Request &req) {
// root が '/' で終わると、log で '//' が発生する。
const std::string path = docRootConfig_.getRoot() + req.getRequestTarget();
LOG_DEBUGF("request target: %s", req.getRequestTarget().c_str());

Expand All @@ -110,7 +77,7 @@ namespace http {

if (S_ISDIR(buf.st_mode)) {
LOG_DEBUGF("is a directory: %s", path.c_str());
return directoryListing(path);
return directoryListing(docRootConfig_.getRoot(), req.getRequestTarget());
}

if (!S_ISREG(buf.st_mode)) {
Expand Down
5 changes: 2 additions & 3 deletions src/lib/http/handler/static_file_handler.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -11,9 +11,8 @@ namespace http {
virtual Response serve(const Request &req);

private:
static std::string getNextLink(const std::string &currentDirPath, const std::string &entryName, bool isDir);
static Result<std::string, HttpStatusCode> makeDirectoryListingHtml(const std::string &path);
static Response directoryListing(const std::string &path);
static Result<std::string, HttpStatusCode> makeDirectoryListingHtml(const std::string &root, const std::string &target);
static Response directoryListing(const std::string &root, const std::string &target);
config::LocationContext::DocumentRootConfig docRootConfig_;
};
} // http
Expand Down

0 comments on commit a2c92a7

Please sign in to comment.