changes var in client

This commit is contained in:
hugogogo
2022-08-08 18:06:41 +02:00
parent f10931042f
commit e1a68bc3e4
6 changed files with 44 additions and 39 deletions

View File

@@ -1,23 +1,27 @@
## work together ## work together
#### TODO
- `_is_cgi()` and `_fill_cgi_path()`
- `_cgi_output()` change status in `client->status`
- two cgi tests :
? - a basic form with "name" and "something", that return a html page with that
? - for GET and POST
? - a script called by a file extension in URI
#### output cgi script : #### output cgi script :
! TODO : change all the '\n' by '\r\n' ! TODO : change all the '\n' by '\r\n'
! TODO : there is at least one header field followed by '\r\n\r\n' : ! TODO : there is at least one header field followed by '\r\n\r\n' :
- "Content-Type" - "Content-Type"
- "Location" - "Location"
- "Status" - "Status"
-> TODO : there is no field duplicate (resolve conflicts)
-> TODO : if status field, change server status for this one
! TODO : there is no space between filed name and ":" ! TODO : there is no space between filed name and ":"
! TODO : if no Location field && no Status field -> status code = 200
! TODO?: handle Location field, either : ! TODO?: handle Location field, either :
- local : start with '/' --> rerun the request with new uri - local : start with '/' --> rerun the request with new uri
- client : start with '<scheme>:' --> send back status code 302 - client : start with '<scheme>:' --> send back status code 302
-> TODO : there is no field duplicate (resolve conflicts)
#### what cgi ? -> TODO : if status field, change server status for this one
- a basic form with "name" and "something", that return a html page with that -> TODO : if no Location field && no Status field -> status code = 200
- a script called by a file extension in URI
#### questions #### questions
- in client.cpp i fill the port, is there a default one in case it's not in the request ? - in client.cpp i fill the port, is there a default one in case it's not in the request ?

View File

@@ -16,6 +16,7 @@ server {
location /board { location /board {
allow_methods GET; allow_methods GET;
root ./www/html; root ./www/html;
cgi_ext php cgi
} }
location /board/content { location /board/content {

View File

@@ -36,10 +36,10 @@ Client::~Client() {
Client::Client( Client const & src ) Client::Client( Client const & src )
: body_size(0) : body_size(0)
, status(0) , status(0)
, _fd ( src.get_cl_fd() ) , _fd ( src._fd )
, _port ( src.get_cl_port() ) , _port ( src._port )
, _ip ( src.get_cl_ip() ) , _ip ( src._ip )
, _lsocket ( src.get_cl_lsocket() ) , _lsocket ( src._lsocket )
{ {
raw_request = src.raw_request; raw_request = src.raw_request;
response = src.response; response = src.response;
@@ -143,10 +143,10 @@ void Client::clear_script()
*********************************************/ *********************************************/
// client side // client side
int Client::get_cl_fd() const { return _fd; } int Client::get_cl_fd() const { return _fd; }
const std::string &Client::get_cl_ip() const { return _ip; } const std::string & Client::get_cl_ip() const { return _ip; }
const std::string &Client::get_cl_port() const { return _port; } const std::string & Client::get_cl_port() const { return _port; }
const listen_socket *Client::get_cl_lsocket() const { return _lsocket; } const listen_socket * Client::get_cl_lsocket() const { return _lsocket; }
// requette // requette
http_method Client::get_rq_method() const { return _request.method; } http_method Client::get_rq_method() const { return _request.method; }

View File

@@ -74,10 +74,10 @@ class Client
bool fill_script_path(std::string script); bool fill_script_path(std::string script);
private: private:
const int _fd; int _fd;
const std::string _port; std::string _port;
const std::string _ip; std::string _ip;
const listen_socket * _lsocket; listen_socket * _lsocket;
struct Request _request; struct Request _request;
void _parse_request_line( std::string rline ); void _parse_request_line( std::string rline );

View File

@@ -95,12 +95,12 @@ class Webserv
LocationConfig &_determine_location(ServerConfig &server, std::string const &path); LocationConfig &_determine_location(ServerConfig &server, std::string const &path);
void _response_correction(Client *client); void _response_correction(Client *client);
// cgi_script.cpp // cgi_script.cpp
bool _is_cgi(Client *client); bool _is_cgi(Client *client);
void _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);
char* _dup_env(std::string var, int i); char* _dup_env(std::string var, int i);
void _exec_script(Client *client, char **env); std::string _exec_script(Client *client, char **env);
// epoll_update.cpp // epoll_update.cpp
int _epoll_update(int fd, uint32_t events, int op); int _epoll_update(int fd, uint32_t events, int op);
int _epoll_update(int fd, uint32_t events, int op, void *ptr); int _epoll_update(int fd, uint32_t events, int op, void *ptr);

View File

@@ -11,16 +11,19 @@ bool Webserv::_is_cgi(Client *client)
return false; return false;
} }
void Webserv::_exec_cgi(Client *client) std::string Webserv::_exec_cgi(Client *client)
{ {
char** env; char** env;
std::string script_output;
env = _set_env(client); env = _set_env(client);
_exec_script(client, env); script_output = _exec_script(client, env);
for (int i = 0; env[i]; i++) for (int i = 0; env[i]; i++)
delete[] env[i]; delete[] env[i];
delete[] env; delete[] env;
return script_output;
} }
char* Webserv::_dup_env(std::string var, std::string val = "") char* Webserv::_dup_env(std::string var, std::string val = "")
@@ -49,7 +52,7 @@ char** Webserv::_set_env(Client *client)
env[0] = _dup_env("AUTH_TYPE"); // authentification not supported env[0] = _dup_env("AUTH_TYPE"); // authentification not supported
env[1] = _dup_env("CONTENT_LENGTH" , client->get_rq_body().size()); env[1] = _dup_env("CONTENT_LENGTH" , client->get_rq_body().size());
env[2] = _dup_env("CONTENT_TYPE" , client->get_rq_headers("Content-Type")); env[2] = _dup_env("CONTENT_TYPE" , client->get_rq_headers("Content-Type"));
env[3] = _dup_env("GATEWAY_INTERFACE" , "CGI/1.0"); env[3] = _dup_env("GATEWAY_INTERFACE" , "CGI/1.1"); // https://www.rfc-editor.org/rfc/rfc3875
env[4] = _dup_env("PATH_INFO" , client->get_rq_script_info()); env[4] = _dup_env("PATH_INFO" , client->get_rq_script_info());
env[5] = _dup_env("PATH_TRANSLATED"); // not supported env[5] = _dup_env("PATH_TRANSLATED"); // not supported
env[6] = _dup_env("QUERY_STRING" , client->get_rq_query()); env[6] = _dup_env("QUERY_STRING" , client->get_rq_query());
@@ -69,7 +72,7 @@ char** Webserv::_set_env(Client *client)
return env; return env;
} }
void Webserv::_exec_script(Client *client, char **env) std::string Webserv::_exec_script(Client *client, char **env)
{ {
#define RD 0 #define RD 0
#define WR 1 #define WR 1
@@ -82,7 +85,7 @@ void Webserv::_exec_script(Client *client, char **env)
pid_t pid; pid_t pid;
char buf[CGI_BUF_SIZE]; // WIP define buffer char buf[CGI_BUF_SIZE]; // WIP define buffer
char * const * nll = NULL; char * const * nll = NULL;
std::string response; std::string script_output;
std::string body = client->get_rq_body(); std::string body = client->get_rq_body();
int fd_in[2]; int fd_in[2];
int fd_out[2]; int fd_out[2];
@@ -113,19 +116,16 @@ void Webserv::_exec_script(Client *client, char **env)
memset(buf, '\0', CGI_BUF_SIZE); memset(buf, '\0', CGI_BUF_SIZE);
while (read(FD_RD_FR_CHLD, buf, CGI_BUF_SIZE - 1) > 0) while (read(FD_RD_FR_CHLD, buf, CGI_BUF_SIZE - 1) > 0)
{ {
response += buf; script_output += buf;
memset(buf, '\0', CGI_BUF_SIZE); memset(buf, '\0', CGI_BUF_SIZE);
} }
} }
if (response.empty()) if (script_output.empty())
response = "Status: 500\r\n\r\n"; script_output = "Status: 500\r\n\r\n";
// DEBUG // DEBUG
std::cout << "\n_______response_______\n" std::cout << "\n______response______\n" << script_output << "\n____end response____\n";
<< response
<< "\n_____end response_____\n";
// TODO: see how this must be handled return script_output;
client->response += response;
} }