diff --git a/srcs/Client.cpp b/srcs/Client.cpp index a4d58b8..94259af 100644 --- a/srcs/Client.cpp +++ b/srcs/Client.cpp @@ -79,12 +79,10 @@ void Client::parse_request_headers(std::vector &servers) { clear_request(); // not mandatory _parse_request_line(); -// debug -print_client("first line"); if (status) return; _parse_request_fields(); -// debug +// DEBUG print_client("headers"); if (status) return; @@ -93,8 +91,7 @@ print_client("headers"); _check_request_errors(); if (status) return; - // use getter for headers because it works case insensitive - _parse_port_hostname(this->get_rq_headers("Host")); + _parse_port_hostname(this->get_rq_headers("Host")); // use getter for headers because it works case insensitive // dont clear raw_request, we need it for future reparsing of body // see call of parse_request() in _read_request() @@ -169,7 +166,7 @@ void Client::print_client(std::string message) std::map::iterator it; std::cout << "\n=== DEBUG PRINT CLIENT ===\n"; - std::cout << "\n" << message << ":----------\n\n" << "raw_request:\n__\n"; + std::cout << message << ":\n----------\n\n" << "raw_request:\n__\n"; ::print_special(raw_request); std::cout << "\n__\n" << "get_cl_fd() : [" << get_cl_fd() << "]\n" @@ -187,8 +184,8 @@ void Client::print_client(std::string message) << "get_rq_script_info() : [" << get_rq_script_info() << "]\n" << "headers :\n"; for (it = _request.headers.begin(); it != _request.headers.end(); it++) - std::cout << " " << it->first << ": " << it->second << "\n"; - std::cout << "\n=== END DEBUG ===\n\n"; + std::cout << " " << it->first << ": [" << it->second << "]\n"; + std::cout << "\n=== END PRINT CLIENT ===\n\n"; } /********************************************* @@ -269,16 +266,22 @@ void Client::_parse_request_fields() int ret; headers = raw_request; - // extract header part - ::extract_line(headers, 0, CRLF); // delete first line + // delete first line + pos = headers.find(CRLF); + if (pos != std::string::npos) + headers.erase(0, pos + std::string(CRLF).size()); + // delete body part pos = headers.find(CRLF CRLF); -// ::extract_line(headers, pos); // delete from empty line to the end - headers.erase(pos); //delete from empty line to the end + if (pos != std::string::npos) + headers.erase(pos); + else { + std::cerr << "err _parse_request_fields(): request header doesn't end with empty line\n"; + status = 400; // "bad request" + } // copy result of parser into headers - ret = ::parse_http_headers(raw_request, _request.headers); - if (ret > 0) - { - std::cerr << "err _parse_request_headers(): " << ret << " field are bad formated\n"; + ret = ::parse_http_headers(headers, _request.headers); + if (ret > 0) { + std::cerr << "err _parse_request_fields(): " << ret << " fields are bad formated\n"; status = 400; // "bad request" } } diff --git a/srcs/cgi-bin/php-cgi b/srcs/cgi-bin/php-cgi index b8e85dc..6c90e0c 100755 --- a/srcs/cgi-bin/php-cgi +++ b/srcs/cgi-bin/php-cgi @@ -2,6 +2,7 @@ fields ) + std::map & fields ) { std::vector list; std::vector::iterator it; diff --git a/srcs/utils.hpp b/srcs/utils.hpp index bc47588..2a766d5 100644 --- a/srcs/utils.hpp +++ b/srcs/utils.hpp @@ -50,7 +50,7 @@ void replace_all_substr(std::string &str, const std::string &ori_substr, co std::string str_tolower(std::string str); std::string extract_line(std::string & str, size_t pos = 0, std::string delim = "\n"); std::string get_line (std::string str, size_t pos = 0, std::string delim = "\n"); -size_t parse_http_headers (std::string headers, std::map fields ); +size_t parse_http_headers (std::string headers, std::map & fields ); void throw_test(); // debug void print_special(std::string str); diff --git a/srcs/webserv/Webserv.hpp b/srcs/webserv/Webserv.hpp index 35c082d..bbaf9f5 100644 --- a/srcs/webserv/Webserv.hpp +++ b/srcs/webserv/Webserv.hpp @@ -104,9 +104,9 @@ class Webserv 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); - void _check_script_output(Client *client, std::string output); - void _check_script_status(Client *client, std::string output); - void _check_script_fields(Client *client, std::string output); + void _check_script_output(Client *client, std::string & output); + void _check_script_status(Client *client, std::string & output); + void _check_script_fields(Client *client, std::string & output); // 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); diff --git a/srcs/webserv/cgi_script.cpp b/srcs/webserv/cgi_script.cpp index 5f92af8..d15b391 100644 --- a/srcs/webserv/cgi_script.cpp +++ b/srcs/webserv/cgi_script.cpp @@ -106,9 +106,9 @@ std::string Webserv::_exec_script(Client *client, char **env) dup2(FD_WR_TO_PRNT, STDOUT_FILENO); // DEBUG std::cerr << "execve:\n"; - //execve(client->get_rq_script_path().c_str(), nll, env); + execve(client->get_rq_script_path().c_str(), nll, env); // for tests execve crash : - execve("wrong", nll, env); + //execve("wrong", nll, env); std::cerr << "execve crashed.\n"; } else @@ -135,19 +135,13 @@ std::string Webserv::_exec_script(Client *client, char **env) return script_output; } -void Webserv::_check_script_output(Client *client, std::string output) +void Webserv::_check_script_output(Client *client, std::string & output) { -// DEBUG -std::cout << "outpu:__________\n" << output << "__________\n" - << "\nstatus:" << client->status << "__________\n"; _check_script_status(client, output); -// DEBUG -std::cout << "outpu:__________\n" << output << "__________\n" - << "\nstatus:" << client->status << "__________\n"; _check_script_fields(client, output); } -void Webserv::_check_script_status(Client *client, std::string output) +void Webserv::_check_script_status(Client *client, std::string & output) { size_t pos; int status_pos; @@ -163,27 +157,79 @@ void Webserv::_check_script_status(Client *client, std::string output) client->status = 200; } -void Webserv::_check_script_fields(Client *client, std::string output) +void Webserv::_check_script_fields(Client *client, std::string & output) { std::map srv_fld; // server_field std::map scr_fld; // script_field std::map::iterator it_srv; std::map::iterator it_scr; + std::string tmp; size_t pos; - ::parse_http_headers(client->response, srv_fld); - ::parse_http_headers(output, scr_fld); - // wip: compare both map to supress duplicates + // put server headers in map + tmp = client->response; + pos = tmp.find(CRLF CRLF); + if (pos != std::string::npos) + tmp.erase(pos); + ::parse_http_headers(tmp, srv_fld); + // put script headers in map + tmp = output; + pos = tmp.find(CRLF CRLF); + if (pos != std::string::npos) + tmp.erase(pos); + ::parse_http_headers(tmp, scr_fld); + // compare both map to supress duplicates + +// debug +std::map::iterator it; +std::cout << "\n\n+++++\ndebug comparison:\nBEFORE\nserver headers:\n"; +for (it = srv_fld.begin(); it != srv_fld.end(); it++) { + std::cout << " " << it->first << ": [" << it->second << "]\n"; } +std::cout << "\nscript headers:\n"; +for (it = scr_fld.begin(); it != scr_fld.end(); it++) { + std::cout << " " << it->first << ": [" << it->second << "]\n"; } +// en debug + for (it_srv = srv_fld.begin(); it_srv != srv_fld.end(); it_srv++) { for (it_scr = scr_fld.begin(); it_scr != scr_fld.end(); it_scr++) { - if (it_srv->first == it_scr->first) + if (str_tolower(it_srv->first) == str_tolower(it_scr->first)) { pos = client->response.find(it_srv->first); ::extract_line(client->response, pos, CRLF); + // debug + std::cout << "helloooooooooooooooooooooooooooooo\n"; + std::cout << pos << "\n"; } } } + +// debug + srv_fld.clear(); + scr_fld.clear(); + // put server headers in map + tmp = client->response; + pos = tmp.find(CRLF CRLF); + if (pos != std::string::npos) + tmp.erase(pos); + ::parse_http_headers(tmp, srv_fld); + // put script headers in map + tmp = output; + pos = tmp.find(CRLF CRLF); + if (pos != std::string::npos) + tmp.erase(pos); + ::parse_http_headers(tmp, scr_fld); + // compare both map to supress duplicates + +std::cout << "\nAFTER\nserver headers:\n"; +for (it = srv_fld.begin(); it != srv_fld.end(); it++) { + std::cout << " " << it->first << ": [" << it->second << "]\n"; } +std::cout << "\nscript headers:\n"; +for (it = scr_fld.begin(); it != scr_fld.end(); it++) { + std::cout << " " << it->first << ": [" << it->second << "]\n"; } +std::cout << "\nend debug comparison\n+++++\n\n"; +// end debug + } diff --git a/srcs/webserv/method_get.cpp b/srcs/webserv/method_get.cpp index 7c97cc5..d6088a7 100644 --- a/srcs/webserv/method_get.cpp +++ b/srcs/webserv/method_get.cpp @@ -61,9 +61,6 @@ Where does cgi fit in in all this ??? if (_is_cgi(client)) { script_output = _exec_cgi(client); - // DEBUG - std::cout << "\n____script_output____\n" << script_output << "\n_______________\n"; - // wip check output of script _check_script_output(client, script_output); client->response += script_output; return;