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

@@ -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);

View File

@@ -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<std::string>::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)

View File

@@ -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);
}

View File

@@ -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";

View File

@@ -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);
}

View File

@@ -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;
}