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
#### 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 :
! TODO : change all the '\n' by '\r\n'
! TODO : there is at least one header field followed by '\r\n\r\n' :
- "Content-Type"
- "Location"
- "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 : if no Location field && no Status field -> status code = 200
! TODO?: handle Location field, either :
- local : start with '/' --> rerun the request with new uri
- client : start with '<scheme>:' --> send back status code 302
#### what cgi ?
- a basic form with "name" and "something", that return a html page with that
- a script called by a file extension in URI
-> TODO : there is no field duplicate (resolve conflicts)
-> TODO : if status field, change server status for this one
-> TODO : if no Location field && no Status field -> status code = 200
#### questions
- 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 {
allow_methods GET;
root ./www/html;
cgi_ext php cgi
}
location /board/content {

View File

@@ -36,10 +36,10 @@ Client::~Client() {
Client::Client( Client const & src )
: body_size(0)
, status(0)
, _fd ( src.get_cl_fd() )
, _port ( src.get_cl_port() )
, _ip ( src.get_cl_ip() )
, _lsocket ( src.get_cl_lsocket() )
, _fd ( src._fd )
, _port ( src._port )
, _ip ( src._ip )
, _lsocket ( src._lsocket )
{
raw_request = src.raw_request;
response = src.response;
@@ -143,10 +143,10 @@ void Client::clear_script()
*********************************************/
// client side
int Client::get_cl_fd() const { return _fd; }
const std::string &Client::get_cl_ip() const { return _ip; }
const std::string &Client::get_cl_port() const { return _port; }
const listen_socket *Client::get_cl_lsocket() const { return _lsocket; }
int Client::get_cl_fd() const { return _fd; }
const std::string & Client::get_cl_ip() const { return _ip; }
const std::string & Client::get_cl_port() const { return _port; }
const listen_socket * Client::get_cl_lsocket() const { return _lsocket; }
// requette
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);
private:
const int _fd;
const std::string _port;
const std::string _ip;
const listen_socket * _lsocket;
int _fd;
std::string _port;
std::string _ip;
listen_socket * _lsocket;
struct Request _request;
void _parse_request_line( std::string rline );

View File

@@ -95,12 +95,12 @@ class Webserv
LocationConfig &_determine_location(ServerConfig &server, std::string const &path);
void _response_correction(Client *client);
// cgi_script.cpp
bool _is_cgi(Client *client);
void _exec_cgi(Client *client);
char** _set_env(Client *client);
char* _dup_env(std::string var, std::string val);
char* _dup_env(std::string var, int i);
void _exec_script(Client *client, char **env);
bool _is_cgi(Client *client);
std::string _exec_cgi(Client *client);
char** _set_env(Client *client);
char* _dup_env(std::string var, std::string val);
char* _dup_env(std::string var, int i);
std::string _exec_script(Client *client, char **env);
// epoll_update.cpp
int _epoll_update(int fd, uint32_t events, int op);
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;
}
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);
_exec_script(client, env);
script_output = _exec_script(client, env);
for (int i = 0; env[i]; i++)
delete[] env[i];
delete[] env;
return script_output;
}
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[1] = _dup_env("CONTENT_LENGTH" , client->get_rq_body().size());
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[5] = _dup_env("PATH_TRANSLATED"); // not supported
env[6] = _dup_env("QUERY_STRING" , client->get_rq_query());
@@ -69,7 +72,7 @@ char** Webserv::_set_env(Client *client)
return env;
}
void Webserv::_exec_script(Client *client, char **env)
std::string Webserv::_exec_script(Client *client, char **env)
{
#define RD 0
#define WR 1
@@ -82,7 +85,7 @@ void Webserv::_exec_script(Client *client, char **env)
pid_t pid;
char buf[CGI_BUF_SIZE]; // WIP define buffer
char * const * nll = NULL;
std::string response;
std::string script_output;
std::string body = client->get_rq_body();
int fd_in[2];
int fd_out[2];
@@ -113,19 +116,16 @@ void Webserv::_exec_script(Client *client, char **env)
memset(buf, '\0', CGI_BUF_SIZE);
while (read(FD_RD_FR_CHLD, buf, CGI_BUF_SIZE - 1) > 0)
{
response += buf;
script_output += buf;
memset(buf, '\0', CGI_BUF_SIZE);
}
}
if (response.empty())
response = "Status: 500\r\n\r\n";
if (script_output.empty())
script_output = "Status: 500\r\n\r\n";
// DEBUG
std::cout << "\n_______response_______\n"
<< response
<< "\n_____end response_____\n";
std::cout << "\n______response______\n" << script_output << "\n____end response____\n";
// TODO: see how this must be handled
client->response += response;
return script_output;
}