Skip to content

Commit

Permalink
New file organization
Browse files Browse the repository at this point in the history
  • Loading branch information
ratavare committed May 28, 2024
1 parent e4de991 commit ca13d75
Show file tree
Hide file tree
Showing 20 changed files with 50 additions and 128 deletions.
2 changes: 1 addition & 1 deletion cgi-bin/php/index.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

$response = "";
$type = "";
$upload_dir = "uploads/";
$upload_dir = "www/uploads/";


if ($_SERVER['method'] === 'GET') {
Expand Down
8 changes: 1 addition & 7 deletions cgi-bin/py/index.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
def POST():
raw_body = sys.stdin.buffer.read()

upload_dir = "uploads/"
upload_dir = "www/uploads/"
if not os.path.exists(upload_dir):
os.mkdir(upload_dir)
entries = os.listdir(upload_dir)
Expand Down Expand Up @@ -115,12 +115,6 @@ def GET():
return res

if __name__ == "__main__" :

# while True:
# print("This is an infinite loop with a delay")

# print("check main\n")
# path = os.environ.get('path')
method = os.environ.get('method')

response = ""
Expand Down
23 changes: 10 additions & 13 deletions config.yml
Original file line number Diff line number Diff line change
Expand Up @@ -10,32 +10,29 @@ server:
server_name: example.com
max_cbsize: 2000m
max_conn: 10
# * Root definition
root: /pages
index: index.html
root: /www # * Root definition
index: pages/index.html
http_methods: GET POST
# * Error pages map;
error_pages:
error_pages: # * Error pages map;
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:
cgi: # * CGI extension exec. path map;
.py: /usr/bin/python3
# .php: /usr/bin/php
# * Server routing;
route /main: # to do: HTTP redirect.
root: /pages
route /main:
root: /www/pages
redirect: adeus
index: favicon.ico # default file to open if the request is the dir
index: favicon.ico
dir_listing: off
http_methods: GET POST
route /up:
root: /pages/uploads
root: /www/uploads
dir_listing: on
http_methods: GET

http_methods: GET POST
# * CGI routing;
route /py:
root: /cgi-bin/py
Expand Down
2 changes: 1 addition & 1 deletion inc/Parser.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ typedef struct s_route {
s_route();
std::string path;
std::string redir;
std::list<std::string> httpMethods; // TODO: HTTP REDIRECT
std::list<std::string> httpMethods;
std::list<std::string> index;
std::string rroot;
int dirListing;
Expand Down
Binary file removed pages/uploads/1716908240046_Test cases.docx
Binary file not shown.
Binary file removed pages/uploads/1716908240048_test_cases.docx
Binary file not shown.
48 changes: 0 additions & 48 deletions pages/uploads/1716908240051_README.md

This file was deleted.

Binary file removed pages/uploads/ola.png
Binary file not shown.
13 changes: 1 addition & 12 deletions srcs/conn/Req.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -83,20 +83,9 @@ bool Req::validate_route_name(std::string name, std::string filePath) {
return false;
}

/*
* Reformular esta funcao, ela deve tentar igualar todos o campos do path
* a uma route, ex:
* srcs/conn/Req.cpp, deve tentar ver se srcs/conn ou apenas srcs/ faz
* parte de alguma route. Se nao fizer automaticamente assume-se a Server_Route
* fazendo com que neste caso o new_path passaria a pages/srcs/conn/Req.cpp
* o que e um path invalido e levaria a um erro 403.
*/

void Req::expand_file_path()
{
std::vector<t_location> routes = this->stream->server->routes;
// std::cout << "Entered expand with file_path: " << this->file_path << "$" << std::endl;
// std::cout << "referer: " << referer << "$" << std::endl;

if (!referer.empty() && referer.find('.') == std::string::npos && method == "GET") {
if (referer[referer.size() - 1] != '/')
Expand Down Expand Up @@ -211,7 +200,7 @@ void Req::parser(size_t end_header_pos)
method = request_line_tokens[0];
URL = request_line_tokens[1];
http = request_line_tokens[2];

std::cout << method << " " << URL << " " << http << std::endl;
set_referer(message_header);

set_URL_data(URL);
Expand Down
19 changes: 3 additions & 16 deletions srcs/conn/Res.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -229,19 +229,6 @@ 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;
// }

/*
* Builds the file as specified in the directory listing;
* Read the speficied file;
Expand All @@ -251,6 +238,7 @@ int Res::exec_get(void)
{
Req * req = stream->req;

// std::cout << "GET file_path: " << req->file_path << std::endl;
if (req->path_type == _FILE) {
content = FileManager::read_file(req->file_path);
c_type_response = content_type[FileManager::set_file_ext(req->file_path)];
Expand Down Expand Up @@ -328,7 +316,6 @@ int Res::exec_post(void)
if (stream->server->cgi_path.find(FileManager::set_file_ext(name)) != stream->server->cgi_path.end()) {
req->file_path = "./" + name;
req->file_ext = FileManager::set_file_ext(req->file_path);
std::cout << "AAAAAAAAAA" << std::endl;
return (exec_CGI());
}
}
Expand All @@ -338,7 +325,7 @@ int Res::exec_post(void)
throw HttpError("406", "We can't execute this type of request");

boundary = get_boundary(content_type);
std::cout << "file_path: " << req->file_path << std::endl;
// std::cout << "file_path: " << req->file_path << std::endl;
DIR* dir = opendir(req->file_path.c_str());
if (!dir)
throw HttpError("500", "Internal Server Error");
Expand All @@ -347,6 +334,6 @@ int Res::exec_post(void)

this->status_code = FileManager::create_files(req->raw_body, boundary, req->file_path);
if (this->status_code == "201")
this->content = "What should be the content when we upload a file?";
this->content = "What should be the content when we upload a file? i dunno arthur but change this message plz :(";
return 0;
}
10 changes: 6 additions & 4 deletions srcs/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -6,16 +6,18 @@
const char* RED_TEXT = "\033[31m";
const char* RESET_COLOR = "\033[0m";

int main(void)
int main(int argc, char **argv)
{
if (argc != 2)
return (std::cout << "Invalid argument number" << std::endl, 1);
Lexer *lex = new Lexer;
Parser *par = new Parser;
try
{
lex->tokenize("config.yml");
Lexer::printTokens(lex->getTokens()); // * Print tokens.
lex->tokenize(argv[1]);
// Lexer::printTokens(lex->getTokens()); // * Print tokens.
par->parse(lex->getTokens());
par->printServerNodes(par->getServerNodesIt()); // * Print server nodes.
// par->printServerNodes(par->getServerNodesIt()); // * Print server nodes.
WebServer server(par->getServerNodes());
server.listen();
}
Expand Down
Loading

0 comments on commit ca13d75

Please sign in to comment.