Skip to content

Commit

Permalink
Refactor StaticFileHandler: update getNextLink method to use boolean …
Browse files Browse the repository at this point in the history
…for directory type and improve path handling
  • Loading branch information
shmorish committed Jan 10, 2025
1 parent df68497 commit 31f5c6c
Show file tree
Hide file tree
Showing 2 changed files with 7 additions and 8 deletions.
13 changes: 6 additions & 7 deletions src/lib/http/handler/static_file_handler.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -33,16 +33,15 @@ namespace http {
return result;
}

std::string
StaticFileHandler::getNextLink(const std::string &newPath, const std::string &dName, const unsigned char dType) {
const std::string isDir = (dType == DT_DIR) ? "/" : "";
std::string StaticFileHandler::getNextLink(const std::string &newPath, const std::string &dName, const bool dir) {
const std::string dirSlash = dir ? "/" : "";
if (dName == "." || dName == "..") {
if (dName == ".") {
return newPath + isDir;
return newPath + dirSlash;
}
return newPath.substr(0, newPath.find_last_of('/')) + isDir;
return newPath.substr(0, newPath.find_last_of('/')) + dirSlash;
}
return newPath + "/" + dName + isDir;
return newPath + "/" + dName + dirSlash;
}

Result<std::string, HttpStatusCode> StaticFileHandler::makeDirectoryListingHtml(const std::string &path) {
Expand All @@ -64,7 +63,7 @@ namespace http {
while ((dp = readdir(dir)) != NULL) {
const std::string dName = dp->d_name;
const unsigned char dType = dp->d_type;
const std::string dNameLink = getNextLink(normalizedPath, dName, dType);
const std::string dNameLink = getNextLink(normalizedPath, dName, dType == DT_DIR);
if (dName == ".") {
continue;
}
Expand Down
2 changes: 1 addition & 1 deletion src/lib/http/handler/static_file_handler.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ namespace http {
virtual Response serve(const Request &req);

private:
static std::string getNextLink(const std::string &path, const std::string &dName, unsigned char dType);
static std::string getNextLink(const std::string &newPath, const std::string &dName, bool isDir);
static Result<std::string, HttpStatusCode> makeDirectoryListingHtml(const std::string &path);
static Response directoryListing(const std::string &path);
config::LocationContext::DocumentRootConfig docRootConfig_;
Expand Down

0 comments on commit 31f5c6c

Please sign in to comment.