client variables are const ref

This commit is contained in:
hugogogo
2022-08-08 15:36:16 +02:00
parent da1f4b6e37
commit 972f52ebc8
7 changed files with 80 additions and 68 deletions

View File

@@ -1,6 +1,6 @@
NAME = webserv NAME = webserv
CXX = clang++ CXX = c++
CXXFLAGS = -Wall -Wextra #-Werror CXXFLAGS = -Wall -Wextra #-Werror
CXXFLAGS += $(HEADERS_D:%=-I%) CXXFLAGS += $(HEADERS_D:%=-I%)

View File

@@ -1,5 +1,6 @@
## work together ## work together
#### 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 ?
- timeout server but still works ? - timeout server but still works ?
@@ -13,6 +14,8 @@
- is it ok ? `http://my_site.com/php-cgi` (reconstruct path ?) - is it ok ? `http://my_site.com/php-cgi` (reconstruct path ?)
- is it ok ? `http://my_site.com/something/php-cgi` (what about 'something' ?) - is it ok ? `http://my_site.com/something/php-cgi` (what about 'something' ?)
- is it ok ? `http://my_site.com/something/cgi-bin/php-cgi` (real path with 'something' before ? ) - is it ok ? `http://my_site.com/something/cgi-bin/php-cgi` (real path with 'something' before ? )
- I don't save the STDIN and STDOUT before dup2 in child process, is it wrong ?
- the response page is received long after the cgi-script is done, why ?
#### notifications #### notifications
- i changed the Client getters in two categories : - i changed the Client getters in two categories :
@@ -25,6 +28,31 @@
- the header fields names, as key in map, are stored in lowercase, and getters are case-insensitives - the header fields names, as key in map, are stored in lowercase, and getters are case-insensitives
respsonse.cpp
```
_response()
{
_determine_process_server()
_send_response()
{
_append_base_headers()
_construct_response()
{
_process_method()
{
_get()
{
_exec_cgi()
}
}
_insert_status_line()
::send(headers)
::send(body)
}
}
}
```
--- ---
## man ## man

View File

@@ -144,9 +144,9 @@ void Client::clear_script()
// client side // client side
int Client::get_cl_fd() const { return _fd; } int Client::get_cl_fd() const { return _fd; }
std::string Client::get_cl_ip() const { return _ip; } const std::string &Client::get_cl_ip() const { return _ip; }
listen_socket * Client::get_cl_lsocket() const { return _lsocket; } const std::string &Client::get_cl_port() const { return _port; }
std::string Client::get_cl_port() const { return _port; } 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

@@ -49,9 +49,9 @@ class Client
// getters // getters
int get_cl_fd() const; int get_cl_fd() const;
std::string get_cl_port() const; const std::string & get_cl_port() const;
std::string get_cl_ip() const; const std::string & get_cl_ip() const;
listen_socket * get_cl_lsocket() const; const listen_socket * get_cl_lsocket() const;
// requests getters // requests getters
http_method get_rq_method() const; http_method get_rq_method() const;
@@ -77,7 +77,7 @@ class Client
const int _fd; const int _fd;
const std::string _port; const std::string _port;
const std::string _ip; const std::string _ip;
listen_socket * _lsocket; const 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

@@ -93,10 +93,10 @@ class Webserv
ServerConfig &_determine_process_server(Client *client); ServerConfig &_determine_process_server(Client *client);
LocationConfig &_determine_location(ServerConfig &server, std::string const &path); LocationConfig &_determine_location(ServerConfig &server, std::string const &path);
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); void _exec_cgi(Client *client);
void _construct_client(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);

View File

@@ -17,9 +17,8 @@ void Webserv::_exec_cgi(Client *client)
env = _set_env(client); env = _set_env(client);
_exec_script(client, env); _exec_script(client, env);
// _construct_response(client);
for (size_t i = 0; env[i]; i++) for (int i = 0; env[i]; i++)
delete[] env[i]; delete[] env[i];
delete[] env; delete[] env;
} }
@@ -39,6 +38,7 @@ char* Webserv::_dup_env(std::string var, int i)
val = ::itos(i); val = ::itos(i);
str = var + "=" + val; str = var + "=" + val;
// TODO change strdup for something with new
return ( strdup(str.c_str()) ); return ( strdup(str.c_str()) );
} }
@@ -74,46 +74,29 @@ void Webserv::_exec_script(Client *client, char **env)
#define RD 0 #define RD 0
#define WR 1 #define WR 1
#define CGI_BUF_SIZE 10 #define CGI_BUF_SIZE 10
#define FD_WR_TO_CHLD fd_in[WR]
/*1*/ #define FD_WR_TO_CHLD fd_in[WR] #define FD_WR_TO_PRNT fd_out[WR]
/* */ #define FD_WR_TO_PRNT fd_out[WR] #define FD_RD_FR_CHLD fd_out[RD]
/* */ #define FD_RD_FR_CHLD fd_out[RD] #define FD_RD_FR_PRNT fd_in[RD]
/* */ #define FD_RD_FR_PRNT fd_in[RD]
/*2*/// #define FD_WR_TO_CHLD fdIn
/* */// #define FD_WR_TO_PRNT fdOut
/* */// #define FD_RD_FR_CHLD fdOut
/* */// #define FD_RD_FR_PRNT fdIn
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 response;
std::string body = client->get_rq_body(); std::string body = client->get_rq_body();
int fd_in[2];
int fd_out[2];
/*1*/ int fd_in[2]; pipe(fd_in);
/* */ int fd_out[2]; pipe(fd_out);
/*2*/// FILE *fIn;
/* */// FILE *fOut;
/* */// long fdIn;
/* */// long fdOut;
/*1*/ pipe(fd_in);
/* */ pipe(fd_out);
/*2*/// fIn = tmpfile();
/* */// fOut = tmpfile();
/* */// fdIn = fileno(fIn);
/* */// fdOut = fileno(fOut);
pid = fork(); pid = fork();
if (pid == -1) if (pid == -1)
std::cerr << "fork crashed" << std::endl; std::cerr << "fork crashed" << std::endl;
else if (pid == 0) else if (pid == 0)
{ {
close(FD_WR_TO_CHLD);
/*1*/ close(FD_WR_TO_CHLD); close(FD_RD_FR_CHLD);
/* */ close(FD_RD_FR_CHLD);
/*2*/
dup2(FD_RD_FR_PRNT, STDIN_FILENO); dup2(FD_RD_FR_PRNT, STDIN_FILENO);
dup2(FD_WR_TO_PRNT, STDOUT_FILENO); dup2(FD_WR_TO_PRNT, STDOUT_FILENO);
execve(client->get_rq_script_path().c_str(), nll, env); execve(client->get_rq_script_path().c_str(), nll, env);
@@ -121,15 +104,11 @@ void Webserv::_exec_script(Client *client, char **env)
} }
else else
{ {
/*1*/ close(FD_RD_FR_PRNT); close(FD_RD_FR_PRNT);
/* */ close(FD_WR_TO_PRNT); close(FD_WR_TO_PRNT);
/*2*/
write(FD_WR_TO_CHLD, body.c_str(), body.size()); write(FD_WR_TO_CHLD, body.c_str(), body.size());
close(FD_WR_TO_CHLD); close(FD_WR_TO_CHLD);
waitpid(-1, NULL, 0); waitpid(-1, NULL, 0);
/*1*/
/*2*/// lseek(fdOut, 0, SEEK_SET);
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)
@@ -141,20 +120,12 @@ void Webserv::_exec_script(Client *client, char **env)
if (response.empty()) if (response.empty())
response = "Status: 500\r\n\r\n"; response = "Status: 500\r\n\r\n";
/*1*/
/*2*/// fclose(fIn);
/* */// fclose(fOut);
/* */// close(fdIn);
/* */// close(fdOut);
// DEBUG // DEBUG
std::cout << "\n______response________\n" << response << "\n________________________\n"; std::cout << "\n_______response_______\n"
<< response
<< "\n_____end response_____\n";
// TODO: see how this must be handled // TODO: see how this must be handled
client->response += response; client->response += response;
} }
void Webserv::_construct_client(Client *client)
{
(void)client;
}

View File

@@ -147,13 +147,8 @@ void Webserv::_get(Client *client, ServerConfig &server, LocationConfig &locatio
// //
if (_is_cgi(client)) if (_is_cgi(client))
{ {
// DEBUG
std::cout << "\nSCRIPT PATH _________________"
<< "\npath:" << client->get_rq_script_path()
<< "\npath_info:" << client->get_rq_script_info()
<< "\n\n";
_exec_cgi(client); _exec_cgi(client);
_response_correction(client);
return; return;
} }
// //
@@ -229,6 +224,24 @@ void Webserv::_get_file(Client *client, const std::string &path)
} }
} }
// WIP HUGO
void Webserv::_response_correction(Client *client)
{
// 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 : 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
(void)client;
}
void Webserv::_append_body(Client *client, const char *body, size_t body_size, const std::string &file_extension) void Webserv::_append_body(Client *client, const char *body, size_t body_size, const std::string &file_extension)
{ {
/* /*