diff --git a/memo.txt b/memo.txt index 88c5890..5ad3c53 100644 --- a/memo.txt +++ b/memo.txt @@ -1,10 +1,13 @@ IN 42 SUBJECT AND/OR PRIORITY : -- CGI -- chunked request (response not mandatory it seems) +- CGI (TODO HUGO) +- chunked request (WIP, a bit difficult) + - Need to test normal body parsing +- basic html upload page for testing request of web browser +- upload files with config "upload_dir" + - Ecrire des tests ! - handle redirection (Work, but weird behavior need deeper test) -- upload files with config "upload_dir" - _determine_location() review (New version to complete and test) - replace std::string::npos with macro NPOS ? ----------------------------- diff --git a/srcs/Client.cpp b/srcs/Client.cpp index f4679b1..f0310e8 100644 --- a/srcs/Client.cpp +++ b/srcs/Client.cpp @@ -104,7 +104,7 @@ print_client("headers"); _parse_port_hostname(this->get_rq_headers("Host")); // use getter for headers because it works case insensitive // DEBUG -std::cerr << get_rq_method_str() << " " << get_rq_uri() << " " << get_rq_version() << "\n"; +std::cerr << get_rq_method_str() << " " << get_rq_target() << " " << get_rq_version() << "\n"; // dont clear raw_request, we need it for future reparsing of body // see call of parse_request() in _read_request() @@ -202,10 +202,13 @@ void Client::parse_request_body() status = 413; // HTTP Client Errors } -bool Client::fill_script_path(std::string script) +// TODO HUGO : faire la fonction, mdr. +void Client::fill_script_path(const std::string &path, size_t pos) { - size_t pos; - int len = script.size(); + (void)path; + (void)pos; +/* size_t pos; + size_t len = path.size(); std::string path = this->get_rq_abs_path(); std::string tmp; @@ -219,7 +222,7 @@ bool Client::fill_script_path(std::string script) _request.script.info = path.substr(pos + len); return true; } - return false; + return false; */ } void Client::clear() @@ -240,7 +243,7 @@ void Client::clear_request() { clear_script(); _request.method = UNKNOWN; - _request.uri.clear(); + _request.target.clear(); _request.version.clear(); _request.headers.clear(); _request.body.clear(); @@ -269,7 +272,7 @@ void Client::print_client(std::string message) << "get_cl_port() : [" << get_cl_port() << "]\n" << "get_cl_ip() : [" << get_cl_ip() << "]\n" << "get_rq_method_str() : [" << get_rq_method_str() << "]\n" - << "get_rq_uri() : [" << get_rq_uri() << "]\n" + << "get_rq_target() : [" << get_rq_target() << "]\n" << "get_rq_abs_path() : [" << get_rq_abs_path() << "]\n" << "get_rq_query() : [" << get_rq_query() << "]\n" << "get_rq_version() : [" << get_rq_version() << "]\n" @@ -298,7 +301,7 @@ const listen_socket * Client::get_cl_lsocket() const { return _lsocket; } http_method Client::get_rq_method() const { return _request.method; } std::string Client::get_rq_method_str() const { return ::http_methods_to_str(_request.method); } -std::string Client::get_rq_uri() const { return _request.uri; } +std::string Client::get_rq_target() const { return _request.target; } std::string Client::get_rq_abs_path() const { return _request.abs_path; } std::string Client::get_rq_query() const { return _request.query; } std::string Client::get_rq_version() const { return _request.version; } @@ -338,22 +341,22 @@ void Client::_parse_request_line() else { _request.method = str_to_http_method(line[0]); - _request.uri = line[1]; - _parse_request_uri(line[1]); + _request.target = line[1]; + _parse_request_target(line[1]); _request.version = line[2]; } } -void Client::_parse_request_uri( std::string uri ) +void Client::_parse_request_target( std::string target ) { size_t pos; - pos = uri.find("?"); + pos = target.find("?"); if (pos != std::string::npos) - _request.query = uri.substr(pos + 1); + _request.query = target.substr(pos + 1); else _request.query = ""; - _request.abs_path = uri.substr(0, pos); + _request.abs_path = target.substr(0, pos); } void Client::_parse_request_fields() diff --git a/srcs/Client.hpp b/srcs/Client.hpp index 594434c..d7dfc8b 100644 --- a/srcs/Client.hpp +++ b/srcs/Client.hpp @@ -21,7 +21,7 @@ struct Script struct Request { http_method method; - std::string uri; + std::string target; std::string abs_path; std::string query; std::string version; @@ -60,7 +60,7 @@ class Client // requests getters http_method get_rq_method() const; std::string get_rq_method_str() const; - std::string get_rq_uri() const; + std::string get_rq_target() const; std::string get_rq_abs_path() const; std::string get_rq_query() const; std::string get_rq_version() const; @@ -76,7 +76,7 @@ class Client void clear(); void clear_request(); void clear_script(); - bool fill_script_path(std::string script); + void fill_script_path(const std::string &path, size_t pos); // DEBUG void print_client(std::string message = ""); @@ -89,7 +89,7 @@ class Client void _parse_request_line(); void _parse_request_fields(); - void _parse_request_uri( std::string uri ); + void _parse_request_target( std::string target ); void _parse_port_hostname(std::string host); void _check_request_errors(); diff --git a/srcs/webserv/Webserv.hpp b/srcs/webserv/Webserv.hpp index b8e31f4..12964a3 100644 --- a/srcs/webserv/Webserv.hpp +++ b/srcs/webserv/Webserv.hpp @@ -80,7 +80,7 @@ class Webserv int _send_response(Client *client); void _append_base_headers(Client *client); void _construct_response(Client *client); - void _process_method(Client *client); + void _process_method(Client *client, std::string &path); void _insert_status_line(Client *client); void _error_html_response(Client *client); void _append_body(Client *client, const std::string &body, const std::string &file_extension = ""); @@ -92,17 +92,17 @@ class Webserv // move later std::string _replace_url_root(Client *client, std::string path); - void _get(Client *client); + void _get(Client *client, std::string &path); void _get_file(Client *client, const std::string &path); - void _autoindex(Client *client, std::string &path); + void _autoindex(Client *client, const std::string &path); // method_post.cpp - void _post(Client *client); + void _post(Client *client, const std::string &path); void _post_file(Client *client, const std::string &path); // method_delete.cpp - void _delete(Client *client); + void _delete(Client *client, const std::string &path); void _delete_file(Client *client, const std::string &path); // cgi_script.cpp - bool _is_cgi(Client *client); + size_t _cgi_pos(Client *client, std::string &path); std::string _exec_cgi(Client *client); char** _set_env(Client *client); char* _dup_env(std::string var, std::string val); diff --git a/srcs/webserv/cgi_script.cpp b/srcs/webserv/cgi_script.cpp index a337fa8..99568c5 100644 --- a/srcs/webserv/cgi_script.cpp +++ b/srcs/webserv/cgi_script.cpp @@ -1,14 +1,21 @@ #include "Webserv.hpp" -bool Webserv::_is_cgi(Client *client) +// TODO HUGO : go ameliorer la recherche comme on a dit. +size_t Webserv::_cgi_pos(Client *client, std::string &path) { - // TODO see how it works with config - if (client->fill_script_path("/cgi-bin/php-cgi")) + size_t pos = NPOS; + std::vector::const_iterator it; + it = client->assigned_location->cgi_ext.begin(); + while (it != client->assigned_location->cgi_ext.end()) + { + pos = std::min(path.find(*it) + it->size(), pos); + ++it; + } + if (pos == NPOS) + return false; + else return true; - if (client->fill_script_path("/cgi-bin/cgi_cpp.cgi")) - return true; - return false; } std::string Webserv::_exec_cgi(Client *client) diff --git a/srcs/webserv/method_delete.cpp b/srcs/webserv/method_delete.cpp index 0e89bfc..40e0e3b 100644 --- a/srcs/webserv/method_delete.cpp +++ b/srcs/webserv/method_delete.cpp @@ -1,17 +1,12 @@ #include "Webserv.hpp" -void Webserv::_delete(Client *client) +void Webserv::_delete(Client *client, const std::string &path) { /* WIP https://www.rfc-editor.org/rfc/rfc9110.html#name-delete */ - std::string path = client->get_rq_abs_path(); - path.insert(0, client->assigned_location->root); - - /* CGI Here ? */ - _delete_file(client, path); } diff --git a/srcs/webserv/method_get.cpp b/srcs/webserv/method_get.cpp index 20edb07..a0bcd7c 100644 --- a/srcs/webserv/method_get.cpp +++ b/srcs/webserv/method_get.cpp @@ -3,7 +3,7 @@ std::string Webserv::_replace_url_root(Client *client, std::string path) { - std::cerr << "path before = " << client_path << "\n"; // DEBUG + std::cerr << "path before = " << path << "\n"; // DEBUG if (client->assigned_location->path == "/") path.insert(0, client->assigned_location->root); else @@ -13,7 +13,7 @@ std::string Webserv::_replace_url_root(Client *client, std::string path) } // const? -void Webserv::_get(Client *client) +void Webserv::_get(Client *client, std::string &path) { @@ -101,7 +101,7 @@ void Webserv::_get_file(Client *client, const std::string &path) } // const? -void Webserv::_autoindex(Client *client, std::string &path) +void Webserv::_autoindex(Client *client, const std::string &path) { std::cout << "made it to _autoindex\n"; diff --git a/srcs/webserv/method_post.cpp b/srcs/webserv/method_post.cpp index 1653e0a..525b144 100644 --- a/srcs/webserv/method_post.cpp +++ b/srcs/webserv/method_post.cpp @@ -2,17 +2,12 @@ #include "Webserv.hpp" -void Webserv::_post(Client *client) +void Webserv::_post(Client *client, const std::string &path) { /* WIP https://www.rfc-editor.org/rfc/rfc9110.html#name-post */ - std::string path = client->get_rq_abs_path(); - path.insert(0, client->assigned_location->root); - - /* CGI Here ? */ - _post_file(client, path); } diff --git a/srcs/webserv/response.cpp b/srcs/webserv/response.cpp index 423405b..0584e57 100644 --- a/srcs/webserv/response.cpp +++ b/srcs/webserv/response.cpp @@ -69,22 +69,21 @@ void Webserv::_append_base_headers(Client *client) void Webserv::_construct_response(Client *client) { - /* Switch between normal behavior or CGI here ? - maybe better than in _get(), _post(), ...*/ std::string path = _replace_url_root(client, client->get_rq_abs_path()); - std::string script_output; - if (_is_cgi(path)) +/* size_t pos = _cgi_pos(client, path); + if (pos != NPOS) { - script_output = _exec_cgi(client); + client->fill_script_path(path, pos); + std::string script_output = _exec_cgi(client); _check_script_output(client, script_output); client->response += script_output; return; - } - _process_method(client); + } */ + _process_method(client, path); } -void Webserv::_process_method(Client *client) +void Webserv::_process_method(Client *client, std::string &path) { std::cerr << "assigned_location->path = " << client->assigned_location->path << "\n"; // debug std::cerr << "allow_methods = " << client->assigned_location->allow_methods << "\n"; // debug @@ -92,11 +91,11 @@ void Webserv::_process_method(Client *client) switch (client->get_rq_method()) { case (GET): - _get(client); break; + _get(client, path); break; case (POST): - _post(client); break; + _post(client, path); break; case (DELETE): - _delete(client); break; + _delete(client, path); break; default: break; }