diff --git a/srcs/Client.cpp b/srcs/Client.cpp index cb6382a..5c5e975 100644 --- a/srcs/Client.cpp +++ b/srcs/Client.cpp @@ -77,18 +77,16 @@ Client & Client::operator=( Client const & rhs ) // https://www.tutorialspoint.com/http/http_requests.htm void Client::parse_request(std::vector &servers) { -// std::map headers; -// std::string body; - std::string copy_request; - -// DEBUG -// std::cout << "\nREQUEST ____________\n" << raw_request << "\n_____________\n"; clear_request(); // not mandatory - - _parse_request_line(copy_request); + _parse_request_line(); +// debug +print_client("first line"); if (status) return; - _parse_request_headers(copy_request); + _parse_request_headers(); +// debug +print_client("headers"); + if (status) return; assigned_server = ::_determine_process_server(this, servers); @@ -106,11 +104,11 @@ void Client::parse_request(std::vector &servers) void Client::parse_request_body() { - size_t pos; + size_t pos; pos = raw_request.find(CRLF CRLF); pos += std::string(CRLF CRLF).size(); - _request.body = message.substr(pos); + _request.body = raw_request.substr(pos); if (_request.body.size() > assigned_server->client_body_limit) status = 413; // HTTP Client Errors @@ -166,6 +164,32 @@ void Client::clear_script() _request.script.info.clear(); } +// debug +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" << raw_request << "\n__\n" + << "get_cl_fd() : " << get_cl_fd() << "\n" + << "get_cl_port() : " << get_cl_port() << "\n" + << "get_cl_ip() : " << get_cl_ip() << "\n" + << "get_rq_method_str() : " << get_rq_method_str() << "\n" + << "get_rq_uri() : " << get_rq_uri() << "\n" + << "get_rq_abs_path() : " << get_rq_abs_path() << "\n" + << "get_rq_query() : " << get_rq_query() << "\n" + << "get_rq_version() : " << get_rq_version() << "\n" + << "get_rq_body() : " << get_rq_body() << "\n" + << "get_rq_port() : " << get_rq_port() << "\n" + << "get_rq_hostname() : " << get_rq_hostname() << "\n" + << "get_rq_script_path() : " << get_rq_script_path() << "\n" + << "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"; +} /********************************************* * GETTERS @@ -209,13 +233,12 @@ void Client::_parse_request_line() { std::vector line; std::string raw_line; - int ret; raw_line = ::get_line(raw_request, 0, CRLF); line = ::split_trim(raw_line, " ", ' '); if (line.size() != 3) { - std::cerr << "err _parse_first_line(): wrong number of elements (" << ret << " instead of 3)\n"; + std::cerr << "err _parse_first_line(): wrong number of elements (" << line.size() << " instead of 3)\n"; status = 400; // "bad request" } else @@ -247,9 +270,10 @@ void Client::_parse_request_headers() headers = raw_request; // extract header part - ::extract_line(headers, 0, CRLF); - pos = headers.find(CRLF); - ::extract_line(headers, pos); // delete from pos to the end + ::extract_line(headers, 0, CRLF); // delete first line + 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 // copy result of parser into headers ret = ::parse_http_headers(raw_request, _request.headers); if (ret > 0) diff --git a/srcs/Client.hpp b/srcs/Client.hpp index 2b82f36..2e8300c 100644 --- a/srcs/Client.hpp +++ b/srcs/Client.hpp @@ -75,6 +75,8 @@ class Client void clear_request(); void clear_script(); bool fill_script_path(std::string script); + // DEBUG + void print_client(std::string message = ""); private: int _fd; diff --git a/srcs/cgi-bin/php-cgi b/srcs/cgi-bin/php-cgi index d3d3625..b8e85dc 100755 --- a/srcs/cgi-bin/php-cgi +++ b/srcs/cgi-bin/php-cgi @@ -1,7 +1,8 @@ #! /usr/bin/php +size_t parse_http_headers ( std::string headers, std::map fields ) @@ -243,8 +243,10 @@ std::map std::vector::iterator it_end; size_t err = 0; size_t pos; + std::string key; + std::string val; - list = ::split_trim(sub, CRLF, ' '); + list = ::split_trim(headers, CRLF, ' '); it_end = list.end(); for (it = list.begin(); it != it_end; it++) diff --git a/srcs/utils.hpp b/srcs/utils.hpp index 58bf2e8..4e7e624 100644 --- a/srcs/utils.hpp +++ b/srcs/utils.hpp @@ -3,6 +3,7 @@ # define UTILS_HPP # include +# include # include # include # include // atoi @@ -47,8 +48,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 & s, size_t p, std::string d); std::string get_line(std::string str, size_t pos, std::string del); -std::map - parse_http_headers (std::string headers, std::map fields ); +size_t parse_http_headers (std::string headers, std::map fields ); void throw_test(); #endif diff --git a/srcs/webserv/cgi_script.cpp b/srcs/webserv/cgi_script.cpp index 70a05ef..5f92af8 100644 --- a/srcs/webserv/cgi_script.cpp +++ b/srcs/webserv/cgi_script.cpp @@ -137,8 +137,13 @@ std::string Webserv::_exec_script(Client *client, char **env) void Webserv::_check_script_output(Client *client, std::string output) { - // TODO: it doesn't work with execve error, i don't know why yet ? +// 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); } @@ -166,8 +171,8 @@ void Webserv::_check_script_fields(Client *client, std::string output) std::map::iterator it_scr; size_t pos; - srv_fld = ::parse_http_headers(client->response); - scr_fld = ::parse_http_headers(output); + ::parse_http_headers(client->response, srv_fld); + ::parse_http_headers(output, scr_fld); // wip: compare both map to supress duplicates for (it_srv = srv_fld.begin(); it_srv != srv_fld.end(); it_srv++) {