CGI discussion and a little bit of work done

This commit is contained in:
LuckyLaszlo
2022-08-12 18:08:39 +02:00
parent cade79c37f
commit b44acafefe
9 changed files with 61 additions and 59 deletions

View File

@@ -1,10 +1,13 @@
IN 42 SUBJECT AND/OR PRIORITY : IN 42 SUBJECT AND/OR PRIORITY :
- CGI - CGI (TODO HUGO)
- chunked request (response not mandatory it seems) - chunked request (WIP, a bit difficult)
- Need to test normal body parsing - 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 ! - Ecrire des tests !
- handle redirection (Work, but weird behavior need deeper test) - handle redirection (Work, but weird behavior need deeper test)
- upload files with config "upload_dir"
- _determine_location() review (New version to complete and test) - _determine_location() review (New version to complete and test)
- replace std::string::npos with macro NPOS ? - replace std::string::npos with macro NPOS ?
----------------------------- -----------------------------

View File

@@ -104,7 +104,7 @@ print_client("headers");
_parse_port_hostname(this->get_rq_headers("Host")); // use getter for headers because it works case insensitive _parse_port_hostname(this->get_rq_headers("Host")); // use getter for headers because it works case insensitive
// DEBUG // 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 // dont clear raw_request, we need it for future reparsing of body
// see call of parse_request() in _read_request() // see call of parse_request() in _read_request()
@@ -202,10 +202,13 @@ void Client::parse_request_body()
status = 413; // HTTP Client Errors 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; (void)path;
int len = script.size(); (void)pos;
/* size_t pos;
size_t len = path.size();
std::string path = this->get_rq_abs_path(); std::string path = this->get_rq_abs_path();
std::string tmp; std::string tmp;
@@ -219,7 +222,7 @@ bool Client::fill_script_path(std::string script)
_request.script.info = path.substr(pos + len); _request.script.info = path.substr(pos + len);
return true; return true;
} }
return false; return false; */
} }
void Client::clear() void Client::clear()
@@ -240,7 +243,7 @@ void Client::clear_request()
{ {
clear_script(); clear_script();
_request.method = UNKNOWN; _request.method = UNKNOWN;
_request.uri.clear(); _request.target.clear();
_request.version.clear(); _request.version.clear();
_request.headers.clear(); _request.headers.clear();
_request.body.clear(); _request.body.clear();
@@ -269,7 +272,7 @@ void Client::print_client(std::string message)
<< "get_cl_port() : [" << get_cl_port() << "]\n" << "get_cl_port() : [" << get_cl_port() << "]\n"
<< "get_cl_ip() : [" << get_cl_ip() << "]\n" << "get_cl_ip() : [" << get_cl_ip() << "]\n"
<< "get_rq_method_str() : [" << get_rq_method_str() << "]\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_abs_path() : [" << get_rq_abs_path() << "]\n"
<< "get_rq_query() : [" << get_rq_query() << "]\n" << "get_rq_query() : [" << get_rq_query() << "]\n"
<< "get_rq_version() : [" << get_rq_version() << "]\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; } http_method Client::get_rq_method() const { return _request.method; }
std::string Client::get_rq_method_str() const std::string Client::get_rq_method_str() const
{ return ::http_methods_to_str(_request.method); } { 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_abs_path() const { return _request.abs_path; }
std::string Client::get_rq_query() const { return _request.query; } std::string Client::get_rq_query() const { return _request.query; }
std::string Client::get_rq_version() const { return _request.version; } std::string Client::get_rq_version() const { return _request.version; }
@@ -338,22 +341,22 @@ void Client::_parse_request_line()
else else
{ {
_request.method = str_to_http_method(line[0]); _request.method = str_to_http_method(line[0]);
_request.uri = line[1]; _request.target = line[1];
_parse_request_uri(line[1]); _parse_request_target(line[1]);
_request.version = line[2]; _request.version = line[2];
} }
} }
void Client::_parse_request_uri( std::string uri ) void Client::_parse_request_target( std::string target )
{ {
size_t pos; size_t pos;
pos = uri.find("?"); pos = target.find("?");
if (pos != std::string::npos) if (pos != std::string::npos)
_request.query = uri.substr(pos + 1); _request.query = target.substr(pos + 1);
else else
_request.query = ""; _request.query = "";
_request.abs_path = uri.substr(0, pos); _request.abs_path = target.substr(0, pos);
} }
void Client::_parse_request_fields() void Client::_parse_request_fields()

View File

@@ -21,7 +21,7 @@ struct Script
struct Request struct Request
{ {
http_method method; http_method method;
std::string uri; std::string target;
std::string abs_path; std::string abs_path;
std::string query; std::string query;
std::string version; std::string version;
@@ -60,7 +60,7 @@ class Client
// requests getters // requests getters
http_method get_rq_method() const; http_method get_rq_method() const;
std::string get_rq_method_str() 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_abs_path() const;
std::string get_rq_query() const; std::string get_rq_query() const;
std::string get_rq_version() const; std::string get_rq_version() const;
@@ -76,7 +76,7 @@ class Client
void clear(); void clear();
void clear_request(); void clear_request();
void clear_script(); void clear_script();
bool fill_script_path(std::string script); void fill_script_path(const std::string &path, size_t pos);
// DEBUG // DEBUG
void print_client(std::string message = ""); void print_client(std::string message = "");
@@ -89,7 +89,7 @@ class Client
void _parse_request_line(); void _parse_request_line();
void _parse_request_fields(); 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 _parse_port_hostname(std::string host);
void _check_request_errors(); void _check_request_errors();

View File

@@ -80,7 +80,7 @@ class Webserv
int _send_response(Client *client); int _send_response(Client *client);
void _append_base_headers(Client *client); void _append_base_headers(Client *client);
void _construct_response(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 _insert_status_line(Client *client);
void _error_html_response(Client *client); void _error_html_response(Client *client);
void _append_body(Client *client, const std::string &body, const std::string &file_extension = ""); void _append_body(Client *client, const std::string &body, const std::string &file_extension = "");
@@ -92,17 +92,17 @@ class Webserv
// move later // move later
std::string _replace_url_root(Client *client, std::string path); 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 _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 // method_post.cpp
void _post(Client *client); void _post(Client *client, const std::string &path);
void _post_file(Client *client, const std::string &path); void _post_file(Client *client, const std::string &path);
// method_delete.cpp // method_delete.cpp
void _delete(Client *client); void _delete(Client *client, const std::string &path);
void _delete_file(Client *client, const std::string &path); void _delete_file(Client *client, const std::string &path);
// cgi_script.cpp // cgi_script.cpp
bool _is_cgi(Client *client); size_t _cgi_pos(Client *client, std::string &path);
std::string _exec_cgi(Client *client); std::string _exec_cgi(Client *client);
char** _set_env(Client *client); char** _set_env(Client *client);
char* _dup_env(std::string var, std::string val); char* _dup_env(std::string var, std::string val);

View File

@@ -1,14 +1,21 @@
#include "Webserv.hpp" #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 size_t pos = NPOS;
if (client->fill_script_path("/cgi-bin/php-cgi")) std::vector<std::string>::const_iterator it;
return true; it = client->assigned_location->cgi_ext.begin();
if (client->fill_script_path("/cgi-bin/cgi_cpp.cgi")) while (it != client->assigned_location->cgi_ext.end())
return true; {
pos = std::min(path.find(*it) + it->size(), pos);
++it;
}
if (pos == NPOS)
return false; return false;
else
return true;
} }
std::string Webserv::_exec_cgi(Client *client) std::string Webserv::_exec_cgi(Client *client)

View File

@@ -1,17 +1,12 @@
#include "Webserv.hpp" #include "Webserv.hpp"
void Webserv::_delete(Client *client) void Webserv::_delete(Client *client, const std::string &path)
{ {
/* /*
WIP WIP
https://www.rfc-editor.org/rfc/rfc9110.html#name-delete 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); _delete_file(client, path);
} }

View File

@@ -3,7 +3,7 @@
std::string Webserv::_replace_url_root(Client *client, std::string path) 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 == "/") if (client->assigned_location->path == "/")
path.insert(0, client->assigned_location->root); path.insert(0, client->assigned_location->root);
else else
@@ -13,7 +13,7 @@ std::string Webserv::_replace_url_root(Client *client, std::string path)
} }
// const? // 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? // 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"; std::cout << "made it to _autoindex\n";

View File

@@ -2,17 +2,12 @@
#include "Webserv.hpp" #include "Webserv.hpp"
void Webserv::_post(Client *client) void Webserv::_post(Client *client, const std::string &path)
{ {
/* /*
WIP WIP
https://www.rfc-editor.org/rfc/rfc9110.html#name-post 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); _post_file(client, path);
} }

View File

@@ -69,22 +69,21 @@ void Webserv::_append_base_headers(Client *client)
void Webserv::_construct_response(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 path = _replace_url_root(client, client->get_rq_abs_path());
std::string script_output; /* size_t pos = _cgi_pos(client, path);
if (_is_cgi(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); _check_script_output(client, script_output);
client->response += script_output; client->response += script_output;
return; 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 << "assigned_location->path = " << client->assigned_location->path << "\n"; // debug
std::cerr << "allow_methods = " << client->assigned_location->allow_methods << "\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()) switch (client->get_rq_method())
{ {
case (GET): case (GET):
_get(client); break; _get(client, path); break;
case (POST): case (POST):
_post(client); break; _post(client, path); break;
case (DELETE): case (DELETE):
_delete(client); break; _delete(client, path); break;
default: default:
break; break;
} }