Skip to content

Commit

Permalink
cgi + cont type + leaks fix
Browse files Browse the repository at this point in the history
  • Loading branch information
terezamr committed May 23, 2024
1 parent 1a39e43 commit d523c47
Show file tree
Hide file tree
Showing 11 changed files with 124 additions and 33 deletions.
2 changes: 1 addition & 1 deletion Makefile
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
NAME = webserv
CXX = c++
CXXFLAGS = -Wall -Werror -Wextra -std=c++98 -g # -fsanitize=address
CXXFLAGS = -Wall -Werror -Wextra #-std=c++98 -g # -fsanitize=address
INCLUDES = -I ./inc


Expand Down
2 changes: 1 addition & 1 deletion cgi-bin/py/index.py
Original file line number Diff line number Diff line change
Expand Up @@ -121,12 +121,12 @@ def GET():

path = os.environ.get('path')
method = os.environ.get('method')
cont_type = os.environ.get('content-type')

response = ""
header = ""

if method == "GET":
print("check get\n")
response = GET()
header = 'HTTP/1.1 200 OK\nContent-Type: text/html' + f'\nContent-Length: {len(response)}' + '\r\n\r\n'
elif method == "POST":
Expand Down
4 changes: 3 additions & 1 deletion config.yml
Original file line number Diff line number Diff line change
Expand Up @@ -18,14 +18,16 @@ server:
error_pages:
403: error/403.html
400: error/400.html
# 404: error/404.asf # works with every type of file
404: error/404.html
# * CGI extension exec. path map;
cgi:
.py: /usr/bin/python3
# .php: /usr/bin/php
# * Server routing;
route /main: # to do: HTTP redirect.
root: /pages
index: index.html # default file to open if the request is the dir
index: favicon.ico # default file to open if the request is the dir
dir_listing: off
http_methods: GET POST
route /ola:
Expand Down
11 changes: 11 additions & 0 deletions error/404.asf
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Error 404</title>
</head>
<body>
<h1 style="background-color: blue; color: white; ">Error 404</h1>
</body>
</html>
1 change: 0 additions & 1 deletion inc/Res.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,6 @@ class Res

std::string content; // * What is going to get rendered
std::string data; // * Final Result in case it is not the CGI
std::string add_ext;
std::string c_type_response;

Res(ConnStream *);
Expand Down
4 changes: 3 additions & 1 deletion inc/__file_manager_utils.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@
namespace FileManager
{
// * Error cases;
std::string build_error_pages(std::map<int, std::string> &, const std::string &, const std::string &);
std::string build_error_pages(std::map<int, std::string> &, const std::string &, const std::string &, std::string &);

// * Get method
std::string read_file(const std::string);
Expand All @@ -42,4 +42,6 @@ namespace FileManager
std::string create_files(const std::vector<uint8_t> &, const std::string&, const std::string);
std::string get_random_filename(const std::vector<uint8_t>&);

std::string set_file_ext(std::string name);

};
32 changes: 32 additions & 0 deletions server_uploaded_files/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
##############
# README #
##############

Here are our recommended videos. You will find a lot of videos online - some that are not in English. This list provides you with the initial information you need.

**Welcome to 42**

1) Welcome to 42 Lisboa & 42 Porto

2) [Welcome to 42](https://www.youtube.com/watch?v=a14XiTXjOow&list=PLVQYiy6xNUxxd5TiZL87_v6JOx61umsUz)

**Introduction**
1) https://www.youtube.com/watch?v=pVlGoBE772I&list=PLVQYiy6xNUxxd5TiZL87_v6JOx61umsUz&index=2
2) https://www.youtube.com/watch?v=dMXSxS2BDE0&list=PLVQYiy6xNUxxd5TiZL87_v6JOx61umsUz&index=3
3) https://www.youtube.com/watch?v=1zN-m25vcmA&list=PLVQYiy6xNUxxd5TiZL87_v6JOx61umsUz&index=4

**Evaluations**
1)https://www.youtube.com/watch?v=iMr4VbggeSQ&list=PLxeKVrYoZO6B64HVamfG1d8Jb2XIrNuRO&index=3
2)https://www.youtube.com/watch?v=E-fZq-cRBkE&list=PLxeKVrYoZO6B64HVamfG1d8Jb2XIrNuRO&index=4
3)https://www.youtube.com/watch?v=qNUM8Bwgiss&list=PLxeKVrYoZO6B64HVamfG1d8Jb2XIrNuRO&index=5
4) https://www.youtube.com/watch?v=maue2MNY1jg&list=PLxeKVrYoZO6B64HVamfG1d8Jb2XIrNuRO&index=6

**Shell 00 & Shell 01**
1) https://www.youtube.com/watch?v=kSqNvRYcTL8&list=PLxeKVrYoZO6B64HVamfG1d8Jb2XIrNuRO&index=1
2) https://www.youtube.com/watch?v=qu8vjw5xwLA&list=PLxeKVrYoZO6B64HVamfG1d8Jb2XIrNuRO&index=2

**C00** - https://www.youtube.com/playlist?list=PLxeKVrYoZO6DNJxUBWIiTO-B6X62SMHVo
**C01** - https://www.youtube.com/playlist?list=PLxeKVrYoZO6CZlYBPJVZQ_n4AulUhjV4p
**C05** - https://www.youtube.com/playlist?list=PLxeKVrYoZO6CKf2YJ3uiQN6aSzAeqLYlu
**C08** - https://www.youtube.com/playlist?list=PLxeKVrYoZO6C_4IgEopWvNCYMI1Po3dVY
**C09** - https://www.youtube.com/watch?v=gtbmNNXcN3s
Binary file added server_uploaded_files/letter_TMR_Cloudfare.pdf
Binary file not shown.
16 changes: 15 additions & 1 deletion srcs/__utils/__file_manager_utils.cpp
Original file line number Diff line number Diff line change
@@ -1,6 +1,18 @@
#include <__file_manager_utils.hpp>
#include <string.h>

std::string FileManager::set_file_ext(std::string name)
{
size_t pos;
std::string file_ext_res = "";

pos = name.find_last_of('.');
if (pos == std::string::npos && pos >= name.length())
file_ext_res = ".txt";
else
file_ext_res = name.substr(pos);
return file_ext_res;
}

/*
* Returns a html page given a content;
Expand All @@ -22,7 +34,7 @@ std::string html_template(const std::string &content, const std::string &title)
* Returns the content of the error pages in case of success;
* Builds an error page in case none exists;
*/
std::string FileManager::build_error_pages(std::map<int, std::string> &error_pages, const std::string &code, const std::string &error_msg)
std::string FileManager::build_error_pages(std::map<int, std::string> &error_pages, const std::string &code, const std::string &error_msg, std::string &extension)
{
std::ifstream file;
std::stringstream content;
Expand All @@ -34,11 +46,13 @@ std::string FileManager::build_error_pages(std::map<int, std::string> &error_pag
{
content << "<h1>"<< code << " " << "</h1>"
<< "<p>" << error_msg << "</p>";
extension = ".html";
}
else
{
content << file.rdbuf();
file.close();
extension = set_file_ext(path);
}
return (html_template(content.str(), "Error " + code));
}
Expand Down
1 change: 0 additions & 1 deletion srcs/conn/ConnStream.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,6 @@ void ConnStream::clean_conn()
res->status_code = "";
res->content = "";
res->data = "";
res->add_ext= "";
}

void ConnStream::set_time(void)
Expand Down
84 changes: 58 additions & 26 deletions srcs/conn/Res.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ Res::Res(ConnStream *_stream) : stream(_stream)
content_type[".jpeg"] = "image/jpeg";
content_type[".jpg"] = "image/jpeg";

this->c_type_response = "text/html";
this->c_type_response = "";
}

Res::~Res() {}
Expand All @@ -37,10 +37,15 @@ int Res::build_http_response(void)
{
std::map<int, std::string> error_pages = stream->server->error_pages;
std::stringstream ss;
std::string extension = "";

if (status_code[0] != '2') // * There is an error;
this->content = FileManager::build_error_pages(error_pages, status_code, error_msg);

{
this->content = FileManager::build_error_pages(error_pages, status_code, error_msg, extension);
c_type_response = content_type[FileManager::set_file_ext(extension)];
}
if (c_type_response == "")
c_type_response = "text/plain";
ss << "HTTP/1.1 " << status_code << " " << this->status[status_code] << "\r\n";
ss << "Content-Type: " << c_type_response << "\r\n";
ss << "Content-Length: " << content.length() << "\r\n\r\n";
Expand Down Expand Up @@ -72,10 +77,17 @@ int Res::check_method(void)
int Res::send(void)
{
std::map<std::string, std::string> &cgi_path = stream->server->cgi_path;
for (const auto& pair : cgi_path) {
std::cout << "Key: " << pair.first << ", Value: " << pair.second << std::endl;
}

Req *req = stream->req;

if (!this->status_code.empty() && !this->error_msg.empty())
return (build_http_response());
// std::cout << "route id: " << req->route_id << std::endl;
// std::cout << "req->file_ext: " << req->file_ext << std::endl;
// std::cout << "req->file_path: " << req->file_path << std::endl;
try
{
if (this->check_method() == -1)
Expand Down Expand Up @@ -108,6 +120,7 @@ int Res::exec_CGI(void)
int pipe_fd[2];
int pipe_fd_aux[2];
pid_t pid;
std::cout << "DEBUG CGI" << std::endl;

ServerContext * server = stream->server;

Expand All @@ -122,13 +135,14 @@ int Res::exec_CGI(void)
if (pid == 0)
{
std::vector<std::string> request;
int dev_null;
// int dev_null;

request.push_back("request=" + data);
request.push_back("path=" + req->file_path);
request.push_back("method=" + req->method);
request.push_back("body=" + raw_body);
request.push_back("content-type=" + content_type[req->file_ext]);

std::cout << data << "\n" << req->file_path << "\n" << req->method << "\n" << raw_body << std::endl;

char **envp = new char *[request.size() + 1];
size_t i = 0;
Expand All @@ -138,16 +152,16 @@ int Res::exec_CGI(void)
std::strcpy((char *)envp[i], request[i].c_str());
}
envp[i] = NULL;

std::cout << "check cgi\n";
close(pipe_fd[0]); // Close read end
dup2(stream->fd, STDOUT_FILENO); // Redirect stdout to the write end

close(pipe_fd_aux[1]); // Close write end
dup2(pipe_fd_aux[0], STDIN_FILENO); // Redirect stdin to the read end

dev_null = open("/dev/null", O_WRONLY);
dup2(dev_null, STDERR_FILENO); // redirecting stderr to /dev/null
close(dev_null);
// dev_null = open("/dev/null", O_WRONLY);
// dup2(dev_null, STDERR_FILENO); // redirecting stderr to /dev/null
// close(dev_null);

execve(argv[0], argv, envp);
delete[] envp;
Expand Down Expand Up @@ -212,18 +226,18 @@ std::string Res::check_index(void)
return "";
}

std::string Res::set_file_ext(std::string name)
{
size_t pos;
std::string file_ext_res = "";

pos = name.find_last_of('.');
if (pos == std::string::npos && pos >= name.length())
file_ext_res = ".txt";
else
file_ext_res = name.substr(pos);
return file_ext_res;
}
// std::string Res::set_file_ext(std::string name)
// {
// size_t pos;
// std::string file_ext_res = "";

// pos = name.find_last_of('.');
// if (pos == std::string::npos && pos >= name.length())
// file_ext_res = ".txt";
// else
// file_ext_res = name.substr(pos);
// return file_ext_res;
// }

/*
* Builds the file as specified in the directory listing;
Expand All @@ -233,10 +247,10 @@ std::string Res::set_file_ext(std::string name)
void Res::exec_get(void)
{
Req * req = stream->req;

if (req->path_type == _FILE) {
content = FileManager::read_file(req->file_path);
c_type_response = content_type[set_file_ext(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;
status_code = "200";
}
if (req->path_type == _DIRECTORY) {
Expand All @@ -246,14 +260,32 @@ void Res::exec_get(void)
if (check_dir_listing() == 1)
{
content = FileManager::directory_listing(req->file_path, req->route_path, stream->server->port);
c_type_response = "text/html";
status_code = "200";
}
else if (check_index() != "")
else if (!check_index().empty())
{
std::string name = check_index();
content = FileManager::read_file(name);
c_type_response = content_type[set_file_ext(name)];
if (!name.empty())
{
if (FileManager::set_file_ext(name) == ".py") // verificar todas as keys de cgi
{
req->file_path = name;
req->file_ext = FileManager::set_file_ext(req->file_path);
std::cout << "new file p: " << req->file_path << std::endl;
std::cout << "new file ext: " << req->file_ext << std::endl;
exec_CGI();
return ;
}
else
{
content = FileManager::read_file(name);
c_type_response = content_type[FileManager::set_file_ext(name)];
}
status_code = "200";
}
else
throw HttpError("404", "Not Found");
}
else
throw HttpError("403", "Forbidden");
Expand Down

0 comments on commit d523c47

Please sign in to comment.