Skip to content

Commit

Permalink
Static and delete file handlers now accept DocumentRootConfig for imp…
Browse files Browse the repository at this point in the history
…roved path handling
  • Loading branch information
shmorish committed Jan 7, 2025
1 parent ec26d66 commit 22fea97
Show file tree
Hide file tree
Showing 5 changed files with 19 additions and 14 deletions.
5 changes: 3 additions & 2 deletions src/lib/core/virtual_server.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -26,16 +26,17 @@ void VirtualServer::registerHandlers(const config::LocationContext &location) {
std::vector<http::HttpMethod> allowedMethods = location.getAllowedMethods();
for (std::vector<http::HttpMethod>::const_iterator iter = allowedMethods.begin(); iter != allowedMethods.end();
++iter) {
config::LocationContext::DocumentRootConfig documentRootConfig = location.getDocumentRootConfig().unwrap();
switch (*iter) {
case http::kMethodGet: {
LOG_DEBUGF("register GET handler: %s", location.getPath().c_str());
http::IHandler *handler = new http::StaticFileHandler();
http::IHandler *handler = new http::StaticFileHandler(documentRootConfig);
router_.onGet(location.getPath(), handler);
break;
}
case http::kMethodDelete: {
LOG_DEBUGF("register DELETE handler: %s", location.getPath().c_str());
http::IHandler *handler = new http::DeleteFileHandler();
http::IHandler *handler = new http::DeleteFileHandler(documentRootConfig);
router_.onDelete(location.getPath(), handler);
break;
}
Expand Down
9 changes: 3 additions & 6 deletions src/lib/http/handler/delete_file_handler.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -4,14 +4,11 @@
#include <sys/stat.h>

namespace http {
DeleteFileHandler::DeleteFileHandler(const config::LocationContext::DocumentRootConfig &docRootConfig)
: docRootConfig_(docRootConfig) {}
Response DeleteFileHandler::serve(const Request &req) {
const std::string path = docRootConfig_.getRoot() + '/' + req.getRequestTarget();
LOG_DEBUGF("request target: %s", req.getRequestTarget().c_str());
const std::string path = req.getRequestTarget().substr(1);

if (path.empty()) {
LOG_DEBUGF("invalid request target: %s", req.getRequestTarget().c_str());
return ResponseBuilder().status(kStatusNotFound).build();
}

struct stat buf = {};
if (stat(path.c_str(), &buf) == -1) {
Expand Down
5 changes: 5 additions & 0 deletions src/lib/http/handler/delete_file_handler.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,16 @@
#define SRC_LIB_HTTP_DELETE_FILE_HANDLER_HPP
#include "handler.hpp"
#include "delete_file_handler.hpp"
#include "config/config.hpp"

namespace http {
class DeleteFileHandler : public IHandler {
public:
explicit DeleteFileHandler(const config::LocationContext::DocumentRootConfig &docRootConfig);
Response serve(const Request &req);

private:
config::LocationContext::DocumentRootConfig docRootConfig_;
};
} // http

Expand Down
10 changes: 4 additions & 6 deletions src/lib/http/handler/static_file_handler.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -6,14 +6,12 @@
#include "static_file_handler.hpp"

namespace http {
StaticFileHandler::StaticFileHandler(const config::LocationContext::DocumentRootConfig &docRootConfig)
: docRootConfig_(docRootConfig) {}

Response StaticFileHandler::serve(const Request &req) {
const std::string path = docRootConfig_.getRoot() + '/' + req.getRequestTarget();
LOG_DEBUGF("request target: %s", req.getRequestTarget().c_str());
const std::string path = req.getRequestTarget().substr(1);

if (path.empty()) {
LOG_DEBUGF("invalid request target: %s", req.getRequestTarget().c_str());
return ResponseBuilder().status(kStatusNotFound).build();
}

struct stat buf = {};
if (stat(path.c_str(), &buf) == -1) {
Expand Down
4 changes: 4 additions & 0 deletions src/lib/http/handler/static_file_handler.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,15 @@
#define SRC_LIB_HTTP_STATIC_FILE_HANDLER_HPP

#include "handler.hpp"
#include "config/config.hpp"

namespace http {
class StaticFileHandler : public IHandler {
public:
explicit StaticFileHandler(const config::LocationContext::DocumentRootConfig &docRootConfig);
virtual Response serve(const Request &req);
private:
config::LocationContext::DocumentRootConfig docRootConfig_;
};
} // http

Expand Down

0 comments on commit 22fea97

Please sign in to comment.