From c7905ebd19e1576218c8204e23853b676a12cb08 Mon Sep 17 00:00:00 2001 From: hugogogo Date: Wed, 10 Aug 2022 17:12:28 +0200 Subject: [PATCH] changed del_line_in_str to etract_line --- srcs/utils.cpp | 20 ++++++++++++++++---- srcs/utils.hpp | 2 +- srcs/webserv/cgi_script.cpp | 7 ++++--- 3 files changed, 21 insertions(+), 8 deletions(-) diff --git a/srcs/utils.cpp b/srcs/utils.cpp index 75c60a0..d8e0c11 100644 --- a/srcs/utils.cpp +++ b/srcs/utils.cpp @@ -151,10 +151,14 @@ std::string str_tolower(std::string str) return str; } -void del_line_in_str(std::string * str, size_t pos, std::string delim) +// identify a line in a string, by delim (ex. '\n') +// delete this line from the string +// and return the deleted line +std::string extract_line(std::string * str, size_t pos, std::string delim) { - size_t begin; - size_t end; + std::string del_str; + size_t begin; + size_t end; begin = (*str).rfind(delim, pos); if (begin == std::string::npos) @@ -168,10 +172,18 @@ void del_line_in_str(std::string * str, size_t pos, std::string delim) else end += delim.size(); + del_str = (*str).substr(begin, end - begin); (*str).erase(begin, end - begin); + return del_str; } - +// transform a str, like a http header, into a map +// with delim +// and perform an action on keys and values +// action receives address of keys and values, and return bool error : +// bool action(&keys, &values) +//std::map +// str_to_map(str, delim, action = NULL) bool operator==(const listen_socket& lhs, int fd) { return lhs.fd == fd; } diff --git a/srcs/utils.hpp b/srcs/utils.hpp index 861dd3f..ea38236 100644 --- a/srcs/utils.hpp +++ b/srcs/utils.hpp @@ -44,7 +44,7 @@ std::string http_methods_to_str(unsigned int methods); int path_is_valid(std::string path); 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 del_line_in_str(std::string * str, size_t pos, std::string delim); +std::string extract_line(std::string * str, size_t pos, std::string delim); void throw_test(); #endif diff --git a/srcs/webserv/cgi_script.cpp b/srcs/webserv/cgi_script.cpp index 1e47585..01c56a7 100644 --- a/srcs/webserv/cgi_script.cpp +++ b/srcs/webserv/cgi_script.cpp @@ -154,7 +154,8 @@ void Webserv::_check_script_status(Client *client, std::string output) client->status = atoi(output.c_str() + status_pos); ::del_line_in_str(&output, pos, CRLF); } - client->status = 200; + else + client->status = 200; } void Webserv::_check_script_fields(Client *client, std::string output) @@ -165,8 +166,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); + 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++) {