Skip to content

Commit

Permalink
Implemented redirections for main server block
Browse files Browse the repository at this point in the history
  • Loading branch information
ratavare committed May 28, 2024
1 parent 1c6c9b1 commit 62916e5
Show file tree
Hide file tree
Showing 6 changed files with 19 additions and 10 deletions.
2 changes: 2 additions & 0 deletions config.yml
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ server:
server_name: example.com
max_cbsize: 2000m # to do: conversion and mapping
max_conn: 10
redirect: /ola
# * Root definition
root: /
dir_listing: on
Expand All @@ -27,6 +28,7 @@ server:
# * Server routing;
route /main: # to do: HTTP redirect.
root: /pages
redirect: adeus
index: favicon.ico # default file to open if the request is the dir
dir_listing: off
http_methods: GET POST
Expand Down
1 change: 1 addition & 0 deletions inc/Parser.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ typedef struct s_server {
int port;
std::string serverName;
std::string root;
std::string redir;
std::list<std::string> httpMethods;
std::list<std::string> index;
std::list<std::pair<int, std::string> > errorPages;
Expand Down
5 changes: 1 addition & 4 deletions srcs/conn/Res.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -76,8 +76,7 @@ int Res::is_redirection(void)
{
std::stringstream ss;
ss << "HTTP/1.1 302 Found\r\n";
ss << "Location: " << stream->server->routes[i].redirect << "\r\n";
// ss << "Content-Length: " << content.length() << "\r\n\r\n";
ss << "Location: " << stream->server->routes[i].redirect << "\r\n\r\n";
ss << content;

this->data = ss.str();
Expand All @@ -98,8 +97,6 @@ int Res::send(void)

if (!this->status_code.empty() && !this->error_msg.empty())
return (build_http_response());

stream->server->routes[0].redirect = "srcs/main.cpp";
try
{
if (this->is_redirection() == 1)
Expand Down
8 changes: 6 additions & 2 deletions srcs/parser/Parser.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -168,7 +168,8 @@ bool Parser::directivesCase3() {
return false;
std::cout << "Entered directivesCase3 with: " << it->content << " | type: " << it->type << std::endl;
if (it->type != NAME && it->type != ROOT && it->type != INDEX &&
it->type != MAX_CBSIZE && it->type != MAX_CONN && it->type != DIR_LISTING && it->type != METHOD)
it->type != MAX_CBSIZE && it->type != MAX_CONN && it->type != DIR_LISTING && it->type != METHOD
&& it->type != REDIRECT)
return false;
if (it->identLevel != 1)
return false;
Expand Down Expand Up @@ -232,7 +233,8 @@ bool Parser::directivesCase6() {
return false;
std::cout << "Entered directivesCase6 with: " << it->content << " | type: " << it->type << std::endl;
if (it->type != NAME && it->type != ROOT && it->type != INDEX &&
it->type != MAX_CBSIZE && it->type != MAX_CONN && it->type != DIR_LISTING && it->type != METHOD)
it->type != MAX_CBSIZE && it->type != MAX_CONN && it->type != DIR_LISTING && it->type != METHOD
&& it->type != REDIRECT)
return false;
if (it->identLevel != 1)
return false;
Expand Down Expand Up @@ -327,6 +329,8 @@ bool Parser::parameterLstCase1(T& container) {
container.back().host = getParam(*it);
else if (it->type == METHOD && container.back().httpMethods.empty())
pushBackMultipleParams(container.back().httpMethods, getParam(*it));
else if (it->type == REDIRECT && container.back().redir.empty())
container.back().redir = getParam(*it);
else if (it->type == PORT && container.back().port == -1)
container.back().port = atoi(getParam(*it).c_str());
else if (it->type == INDEX && container.back().index.empty())
Expand Down
10 changes: 8 additions & 2 deletions srcs/parser/ParserUtils.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ void Parser::printServerNodes(std::list<t_server>::iterator it) {
std::cout << " Port: " << it->port << std::endl;
std::cout << " Server name: " << it->serverName << std::endl;
std::cout << " Root: " << it->root << std::endl;
std::cout << " Redirection: " << it->redir << std::endl;
std::cout << " Max client body size: " << it->maxCBSize << std::endl;
std::cout << " Max connections: " << it->maxConn << std::endl;
std::cout << " Directory listing: " << (it->dirListing == 1 ? "true" : "false") << std::endl;
Expand Down Expand Up @@ -43,6 +44,7 @@ void Parser::printServerNodes(std::list<t_server>::iterator it) {

s_server::s_server() {
this->host = std::string();
this->redir = std::string();
this->port = -1;
this->maxCBSize = -1;
this->maxConn = -1;
Expand Down Expand Up @@ -100,8 +102,12 @@ void Parser::resetParam(int type, int identLevel) {
serverNodes.back().maxCBSize = -1;
if (type == MAX_CONN)
serverNodes.back().maxConn = -1;
if (type == REDIRECT)
serverNodes.back().route.back().redir = std::string();
if (type == REDIRECT) {
if (identLevel == 2)
serverNodes.back().route.back().redir = std::string();
else if (identLevel == 1)
serverNodes.back().redir = std::string();
}
if (type == CGI_PARAM)
serverNodes.back().cgi.pop_back();
}
Expand Down
3 changes: 1 addition & 2 deletions srcs/server_engine/ServerContext.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,6 @@ ServerContext::ServerContext(t_server serverNode)
else
new_location.root = it->rroot;
new_location.redirect = it->redir;
std::cout << "dir redirect: " << it->redir << std::endl;
new_location.dir_listing = it->dirListing;

// * Http Allowed methods
Expand Down Expand Up @@ -77,7 +76,7 @@ ServerContext::ServerContext(t_server serverNode)
server_location.index.push_back(*it);
for (std::list<std::string>::iterator it = serverNode.httpMethods.begin(); it != serverNode.httpMethods.end(); it++)
server_location.http_methods.push_back(*it);

server_location.redirect = serverNode.redir;
server_location.dir_listing = serverNode.dirListing; // ! NEEDS TO BE CHANGED LATER.
// std::cout << "dir listing root: " << server_location.dir_listing << std::endl;

Expand Down

0 comments on commit 62916e5

Please sign in to comment.