From 972f52ebc890003fcb11640e7a03207afe1493f2 Mon Sep 17 00:00:00 2001 From: hugogogo Date: Mon, 8 Aug 2022 15:36:16 +0200 Subject: [PATCH] client variables are const ref --- Makefile | 2 +- README.md | 28 ++++++++++++++++ srcs/Client.cpp | 8 ++--- srcs/Client.hpp | 18 +++++----- srcs/webserv/Webserv.hpp | 2 +- srcs/webserv/cgi_script.cpp | 65 ++++++++++--------------------------- srcs/webserv/response.cpp | 25 ++++++++++---- 7 files changed, 80 insertions(+), 68 deletions(-) diff --git a/Makefile b/Makefile index 1333a0c..ad045bd 100644 --- a/Makefile +++ b/Makefile @@ -1,6 +1,6 @@ NAME = webserv -CXX = clang++ +CXX = c++ CXXFLAGS = -Wall -Wextra #-Werror CXXFLAGS += $(HEADERS_D:%=-I%) diff --git a/README.md b/README.md index be6550c..889abb5 100644 --- a/README.md +++ b/README.md @@ -1,5 +1,6 @@ ## work together + #### questions - 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 ? @@ -13,6 +14,8 @@ - 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/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 - 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 +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 diff --git a/srcs/Client.cpp b/srcs/Client.cpp index 010cf2c..5224fdf 100644 --- a/srcs/Client.cpp +++ b/srcs/Client.cpp @@ -143,10 +143,10 @@ void Client::clear_script() *********************************************/ // client side -int Client::get_cl_fd() const { return _fd; } -std::string Client::get_cl_ip() const { return _ip; } -listen_socket * Client::get_cl_lsocket() const { return _lsocket; } -std::string Client::get_cl_port() const { return _port; } +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; } diff --git a/srcs/Client.hpp b/srcs/Client.hpp index 539a29b..b364054 100644 --- a/srcs/Client.hpp +++ b/srcs/Client.hpp @@ -48,10 +48,10 @@ class Client unsigned int status; // getters - int get_cl_fd() const; - std::string get_cl_port() const; - std::string get_cl_ip() const; - listen_socket * get_cl_lsocket() const; + int get_cl_fd() const; + const std::string & get_cl_port() const; + const std::string & get_cl_ip() const; + const listen_socket * get_cl_lsocket() const; // requests getters http_method get_rq_method() const; @@ -74,11 +74,11 @@ class Client bool fill_script_path(std::string script); private: - const int _fd; - const std::string _port; - const std::string _ip; - listen_socket * _lsocket; - struct Request _request; + const int _fd; + const std::string _port; + const std::string _ip; + const listen_socket * _lsocket; + struct Request _request; void _parse_request_line( std::string rline ); void _parse_request_uri( std::string uri ); diff --git a/srcs/webserv/Webserv.hpp b/srcs/webserv/Webserv.hpp index 85cd738..30a644f 100644 --- a/srcs/webserv/Webserv.hpp +++ b/srcs/webserv/Webserv.hpp @@ -93,10 +93,10 @@ class Webserv ServerConfig &_determine_process_server(Client *client); 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); - void _construct_client(Client *client); char** _set_env(Client *client); char* _dup_env(std::string var, std::string val); char* _dup_env(std::string var, int i); diff --git a/srcs/webserv/cgi_script.cpp b/srcs/webserv/cgi_script.cpp index cc4d836..b896170 100644 --- a/srcs/webserv/cgi_script.cpp +++ b/srcs/webserv/cgi_script.cpp @@ -17,9 +17,8 @@ void Webserv::_exec_cgi(Client *client) env = _set_env(client); _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; } @@ -39,6 +38,7 @@ char* Webserv::_dup_env(std::string var, int i) val = ::itos(i); str = var + "=" + val; + // TODO change strdup for something with new return ( strdup(str.c_str()) ); } @@ -74,46 +74,29 @@ void Webserv::_exec_script(Client *client, char **env) #define RD 0 #define WR 1 #define CGI_BUF_SIZE 10 - -/*1*/ #define FD_WR_TO_CHLD fd_in[WR] -/* */ #define FD_WR_TO_PRNT fd_out[WR] -/* */ #define FD_RD_FR_CHLD fd_out[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 + #define FD_WR_TO_CHLD fd_in[WR] + #define FD_WR_TO_PRNT fd_out[WR] + #define FD_RD_FR_CHLD fd_out[RD] + #define FD_RD_FR_PRNT fd_in[RD] pid_t pid; char buf[CGI_BUF_SIZE]; // WIP define buffer char * const * nll = NULL; std::string response; std::string body = client->get_rq_body(); + int fd_in[2]; + int fd_out[2]; -/*1*/ int fd_in[2]; -/* */ int fd_out[2]; -/*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); + pipe(fd_in); + pipe(fd_out); pid = fork(); if (pid == -1) std::cerr << "fork crashed" << std::endl; else if (pid == 0) { - -/*1*/ close(FD_WR_TO_CHLD); -/* */ close(FD_RD_FR_CHLD); -/*2*/ - + close(FD_WR_TO_CHLD); + close(FD_RD_FR_CHLD); dup2(FD_RD_FR_PRNT, STDIN_FILENO); dup2(FD_WR_TO_PRNT, STDOUT_FILENO); execve(client->get_rq_script_path().c_str(), nll, env); @@ -121,15 +104,11 @@ void Webserv::_exec_script(Client *client, char **env) } else { -/*1*/ close(FD_RD_FR_PRNT); -/* */ close(FD_WR_TO_PRNT); -/*2*/ - + close(FD_RD_FR_PRNT); + close(FD_WR_TO_PRNT); write(FD_WR_TO_CHLD, body.c_str(), body.size()); close(FD_WR_TO_CHLD); waitpid(-1, NULL, 0); -/*1*/ -/*2*/// lseek(fdOut, 0, SEEK_SET); memset(buf, '\0', CGI_BUF_SIZE); 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()) response = "Status: 500\r\n\r\n"; -/*1*/ -/*2*/// fclose(fIn); -/* */// fclose(fOut); -/* */// close(fdIn); -/* */// close(fdOut); - // 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 client->response += response; } -void Webserv::_construct_client(Client *client) -{ - (void)client; -} - diff --git a/srcs/webserv/response.cpp b/srcs/webserv/response.cpp index 261b959..ed08d6f 100644 --- a/srcs/webserv/response.cpp +++ b/srcs/webserv/response.cpp @@ -147,13 +147,8 @@ void Webserv::_get(Client *client, ServerConfig &server, LocationConfig &locatio // 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); + _response_correction(client); 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 ':' --> 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) { /*