Skip to content

Commit

Permalink
Router(feat): implement git reset --soft HEAD~1
Browse files Browse the repository at this point in the history
  • Loading branch information
lmangall committed May 20, 2024
1 parent 07729dd commit 6b47b06
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 11 deletions.
45 changes: 34 additions & 11 deletions src/Router.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ Router::Router()
{
}

Router::Router(Directives& directive) : _directive(directive), _FDsRef(NULL), _pollFd(NULL)
Router::Router(Directives &directive) : _directive(directive), _FDsRef(NULL), _pollFd(NULL)
{
}

Expand Down Expand Up @@ -63,7 +63,7 @@ void Router::adaptPathForFirefox(HTTPRequest &request)
void Router::routeRequest(HTTPRequest &request, HTTPResponse &response)
{
Debug::log("Routing Request: host = " + request.getSingleHeader("host").second, Debug::NORMAL);

if (!_directive._return.empty())
{
response.setStatusCode(301, "Redirection");
Expand All @@ -88,10 +88,10 @@ void Router::routeRequest(HTTPRequest &request, HTTPResponse &response)
PathValidation pathResult = pathIsValid(response, request);
std::cout << BLUE << "path: " << request.getPath() << RESET << std::endl;
std::cout << BLUE << "PathValidation: " << pathResult << RESET << std::endl;

// check if method is allowed
if (!_directive._allowedMethods.empty())
{
{
for (size_t i = 0; i < _directive._allowedMethods.size(); i++)
{
std::cout << "allowed method: " << _directive._allowedMethods[i] << std::endl;
Expand Down Expand Up @@ -135,7 +135,7 @@ void Router::routeRequest(HTTPRequest &request, HTTPResponse &response)
case PathInvalid:
std::cout << "Path is not valid, handling as error" << std::endl;
handleServerBlockError(request, response, 404);
return ;
return;
}

if (request.getMethod() == "SALAD")
Expand All @@ -156,6 +156,27 @@ bool Router::isDynamicRequest(const HTTPRequest &request)
return false;
}

bool Router::isEndPointCGI(const HTTPRequest &request)
{
std::string path = request.getPath();
std::string fileExtension = getFileExtension(path);
std::vector<std::string> cgiExtensions = _directive._cgiExt;

if (!cgiExtensions.empty())
{
for (size_t i = 0; i < cgiExtensions.size(); i++)
{
if (cgiExtensions[i] == fileExtension)
{
Debug::log("isEndPointCGI: CGI request detected", Debug::NORMAL);
return true;
}
}
}
Debug::log("isEndPointCGI: Not a CGI request", Debug::NORMAL);
return false;
}

std::string Router::getFileExtension(const std::string &fileName)
{
size_t dotIndex = fileName.find_last_of(".");
Expand Down Expand Up @@ -321,12 +342,14 @@ void Router::generateDirectoryListing(HTTPResponse &Response,
Response.setHeader("Content-Type", "text/html");
}

bool isDirectory(std::string& path) {
struct stat buffer;
if (stat(path.c_str(), &buffer) == 0) {
return S_ISDIR(buffer.st_mode);
}
return false; // Failed to get file information
bool isDirectory(std::string &path)
{
struct stat buffer;
if (stat(path.c_str(), &buffer) == 0)
{
return S_ISDIR(buffer.st_mode);
}
return false; // Failed to get file information
}

enum PathValidation Router::pathIsValid(HTTPResponse &response, HTTPRequest &request)
Expand Down
1 change: 1 addition & 0 deletions src/Router.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ class Router

void splitTarget(const std::string &target);
bool isDynamicRequest(const HTTPRequest &request);
bool isEndPointCGI(const HTTPRequest &request);
enum PathValidation pathIsValid(HTTPResponse &response, HTTPRequest &request);
void setFDsRef(std::vector<struct pollfd> *FDsRef);
void setPollFd(struct pollfd *pollFd);
Expand Down

0 comments on commit 6b47b06

Please sign in to comment.