Skip to content

Commit

Permalink
redirections in send function
Browse files Browse the repository at this point in the history
  • Loading branch information
terezamr committed May 28, 2024
1 parent 26f8845 commit 1c6c9b1
Show file tree
Hide file tree
Showing 4 changed files with 30 additions and 12 deletions.
4 changes: 2 additions & 2 deletions config.yml
Original file line number Diff line number Diff line change
Expand Up @@ -23,13 +23,13 @@ server:
# * CGI extension exec. path map;
cgi:
.py: /usr/bin/python3
.php: /usr/bin/php
# .php: /usr/bin/php
# * Server routing;
route /main: # to do: HTTP redirect.
root: /pages
index: favicon.ico # default file to open if the request is the dir
dir_listing: off
http_methods: POST
http_methods: GET POST
route /ola:
root: /srcs/conn
dir_listing: on
Expand Down
9 changes: 5 additions & 4 deletions inc/Res.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -30,14 +30,15 @@ class Res
std::map<std::string, std::string> status;
std::map<std::string, std::string> content_type;

int is_redirection(void);
int check_method(void);
std::string check_index(void);
int check_dir_listing(void);

int exec_CGI(void);
int exec_get(void);
int exec_post(void);
void exec_delete(void);
int exec_CGI(void);
int exec_get(void);
int exec_post(void);
void exec_delete(void);

int build_http_response(void);

Expand Down
26 changes: 22 additions & 4 deletions srcs/conn/Res.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,24 @@ int Res::check_method(void)
return -1;
}

int Res::is_redirection(void)
{
int i = this->stream->req->route_id;
if (stream->server->routes[i].redirect != "")
{
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 << content;

this->data = ss.str();
write(stream->fd, this->data.c_str(), this->data.length());
return 1;
}
return 0;
}

/*
* Must check for the permissions before executing;
* Execute the action of each individual method;
Expand All @@ -81,8 +99,11 @@ 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)
return 1;
if (this->check_method() == -1)
throw HttpError("403" , "Forbidden");
if (stream->server->cgi_path.find(req->file_ext) != stream->server->cgi_path.end())
Expand All @@ -106,9 +127,6 @@ int Res::send(void)
return (build_http_response());
}

/*
TODO: Connect CGI with parser;
*/
int Res::exec_CGI(void)
{
Req *req = stream->req;
Expand Down Expand Up @@ -238,7 +256,7 @@ int Res::exec_get(void)

if (req->path_type == _FILE) {
content = FileManager::read_file(req->file_path);
c_type_response = content_type[FileManager::set_file_ext(req->file_path)];std::cout << "response type: " << this->c_type_response << std::endl;
c_type_response = content_type[FileManager::set_file_ext(req->file_path)];
status_code = "200";
}
if (req->path_type == _DIRECTORY) {
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 @@ -39,15 +39,14 @@ ServerContext::ServerContext(t_server serverNode)
{
t_location new_location;
new_location.name = it->path;
std::cout << "route root: " << it->rroot << std::endl;
if (Req::get_path_type("." + it->rroot) != _DIRECTORY)
throw ServerError("Invalid route root.");
if (it->rroot.size() == 1 && it->rroot[0] == '/')
new_location.root = "/.";
else
new_location.root = it->rroot;
new_location.redirect = it->redir;
// std::cout << "dir listing 0: " << it->dirListing << std::endl;
std::cout << "dir redirect: " << it->redir << std::endl;
new_location.dir_listing = it->dirListing;

// * Http Allowed methods
Expand Down

0 comments on commit 1c6c9b1

Please sign in to comment.