From 53a548e31418b46572a4914cb4100a758a49a694 Mon Sep 17 00:00:00 2001 From: hugogogo Date: Tue, 9 Aug 2022 14:57:05 +0200 Subject: [PATCH] wip script output verification working + trim secure pos segfault + concat script message with server message --- Makefile | 2 +- srcs/utils.cpp | 18 +++++++++++---- srcs/utils.hpp | 2 +- srcs/webserv/cgi_script.cpp | 33 ++++++++++++++++++++------- srcs/webserv/parsing_message_http.cpp | 19 +++++++++++++-- srcs/webserv/parsing_message_http.hpp | 3 +++ srcs/webserv/response.cpp | 5 +++- 7 files changed, 65 insertions(+), 17 deletions(-) diff --git a/Makefile b/Makefile index e1117aa..f570826 100644 --- a/Makefile +++ b/Makefile @@ -5,7 +5,7 @@ CXX = c++ CXXFLAGS = -Wall -Wextra #-Werror CXXFLAGS += $(HEADERS_D:%=-I%) CXXFLAGS += -std=c++98 -CXXFLAGS += -g +CXXFLAGS += -g3 CXXFLAGS += -MMD -MP #header dependencie #CXXFLAGS += -O3 diff --git a/srcs/utils.cpp b/srcs/utils.cpp index 66d8f74..e72d576 100644 --- a/srcs/utils.cpp +++ b/srcs/utils.cpp @@ -15,9 +15,19 @@ std::vector split(std::string input, char delimiter) std::string trim(std::string str, char c) { - // TODO: protect substr - str = str.substr(str.find_first_not_of(c)); - str = str.substr(0, str.find_last_not_of(c) + 1); + size_t pos; + + // delete leadings c + pos = str.find_first_not_of(c); + if (pos == std::string::npos) + return str; + str = str.substr(pos); + + // delete endings c + pos = str.find_last_not_of(c); + if (pos == std::string::npos) + return str; + str = str.substr(0, pos + 1); return str; } @@ -107,7 +117,7 @@ std::string str_tolower(std::string str) return str; } -void delete_line_in_string(std::string * str, size_t pos, std::string delim) +void del_line_in_str(std::string * str, size_t pos, std::string delim) { size_t begin; size_t end; diff --git a/srcs/utils.hpp b/srcs/utils.hpp index e75f64a..fdb037e 100644 --- a/srcs/utils.hpp +++ b/srcs/utils.hpp @@ -49,6 +49,6 @@ http_method str_to_http_method(std::string &str); std::string http_methods_to_str(unsigned int methods); void replace_all_substr(std::string &str, const std::string &ori_substr, const std::string &new_substr); std::string str_tolower(std::string str); -void delete_line_in_string(std::string * str, size_t pos, std::string delim); +void del_line_in_str(std::string * str, size_t pos, std::string delim); #endif diff --git a/srcs/webserv/cgi_script.cpp b/srcs/webserv/cgi_script.cpp index ec38d1b..be03cbe 100644 --- a/srcs/webserv/cgi_script.cpp +++ b/srcs/webserv/cgi_script.cpp @@ -102,8 +102,11 @@ std::string Webserv::_exec_script(Client *client, char **env) 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); - execve("truc", nll, env); + // DEBUG + std::cerr << "execve:\n"; + execve(client->get_rq_script_path().c_str(), nll, env); + // for tests execve crash : + //execve("wrong", nll, env); std::cerr << "execve crashed.\n"; } else @@ -144,18 +147,32 @@ void Webserv::_check_script_status(Client *client, std::string output) { status_pos = pos + std::string("Status:").size(); client->status = atoi(output.c_str() + status_pos); - ::delete_line_in_string(&output, pos, CRLF); + ::del_line_in_str(&output, pos, CRLF); } client->status = 200; } void Webserv::_check_script_fields(Client *client, std::string output) { - std::map server_fields; - std::map script_fields; + std::map srv_fld; // server_field + std::map scr_fld; // script_field + std::map::iterator it_srv; + std::map::iterator it_scr; + size_t pos; - server_fields = parse_http_headers(client->response); - script_fields = parse_http_headers(output); - // TODO: compare both map to supress duplicates + srv_fld = parse_http_headers(client->response); + scr_fld = parse_http_headers(output); + // wip: compare both map to supress duplicates + 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) + { + pos = client->response.find(it_srv->first); + ::del_line_in_str(&client->response, pos, CRLF); + } + } + } } diff --git a/srcs/webserv/parsing_message_http.cpp b/srcs/webserv/parsing_message_http.cpp index d66ee6c..63e67cd 100644 --- a/srcs/webserv/parsing_message_http.cpp +++ b/srcs/webserv/parsing_message_http.cpp @@ -19,7 +19,7 @@ size_t return ret; for (int i = 0; i < 3; i++) { - tmp = ::trim(sline[0], ' '); + tmp = ::trim(sline[i], ' '); tmp = ::trim(tmp, '\r'); line.push_back(tmp); } @@ -40,7 +40,7 @@ std::map pos = (message).find(CRLF CRLF); sub = (message).substr(0, pos); list = ::split(sub, '\n'); - // TODO: if (list.begin() is "first line") + if ( maybe_http_first_line( *list.begin() ) ) list.erase(list.begin()); for (it = list.begin(); it != list.end(); it++) @@ -73,3 +73,18 @@ std::string return body; } +bool maybe_http_first_line(std::string str) +{ +// method SP target SP version https://www.rfc-editor.org/rfc/rfc7230.html#section-3.1.1 +// version SP status SP reason https://www.rfc-editor.org/rfc/rfc7230.html#section-3.1.2 + + std::vector sline; + + sline = ::split(str, ' '); + if (sline.size() != 3) + return false; + if (sline[0].find(':') != std::string::npos) + return false; + return true; +} + diff --git a/srcs/webserv/parsing_message_http.hpp b/srcs/webserv/parsing_message_http.hpp index 34b955c..67cd473 100644 --- a/srcs/webserv/parsing_message_http.hpp +++ b/srcs/webserv/parsing_message_http.hpp @@ -17,6 +17,9 @@ std::map std::string parse_http_body(std::string message); +bool + maybe_http_first_line(std::string); + // http message structure : // // start-line diff --git a/srcs/webserv/response.cpp b/srcs/webserv/response.cpp index fd3cc19..0a04a77 100644 --- a/srcs/webserv/response.cpp +++ b/srcs/webserv/response.cpp @@ -149,8 +149,11 @@ void Webserv::_get(Client *client, ServerConfig &server, LocationConfig &locatio 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); - std::cout << "_____________status:" << client->status << "\n"; + client->response += script_output; return; } //