From 9a379c835d845d93bfe06aa195f1ed7098d5f0f8 Mon Sep 17 00:00:00 2001 From: hugogogo Date: Wed, 10 Aug 2022 20:01:23 +0200 Subject: [PATCH] correction of trim in case of str only fill with delim char + added a new split, that also does trim, to split without counting newlines --- srcs/Client.cpp | 6 ++-- srcs/utils.cpp | 47 +++++++++++++++++---------- srcs/utils.hpp | 2 +- srcs/webserv/parsing_message_http.cpp | 37 ++++++++++++--------- srcs/webserv/parsing_message_http.hpp | 4 +-- 5 files changed, 59 insertions(+), 37 deletions(-) diff --git a/srcs/Client.cpp b/srcs/Client.cpp index 4068f38..3cdb737 100644 --- a/srcs/Client.cpp +++ b/srcs/Client.cpp @@ -200,10 +200,12 @@ std::string Client::get_rq_headers(const std::string & key) const void Client::_parse_request_line() { std::vector line; + std::string raw_line; int ret; - ret = ::parse_http_first_line(raw_request, line); - if (ret != 3) + raw_line = extract_line(); + line = ::parse_http_first_line(raw_line); + if (line.size() != 3) { std::cerr << "err _parse_first_line(): wrong number of elements (" << ret << " instead of 3)\n"; status = 400; // "bad request" diff --git a/srcs/utils.cpp b/srcs/utils.cpp index d8e0c11..4b8c903 100644 --- a/srcs/utils.cpp +++ b/srcs/utils.cpp @@ -21,21 +21,42 @@ std::vector split(std::string input, char delimiter) return answer; } -std::string trim(std::string str, char c) +std::vector + split_trim(std::string input, std::string delim = "\n", char ctrim = '') +{ + std::vector split_str; + std::string tmp; + size_t start = 0; + size_t end; + + end = input.find(delim); + while (end != -1) + { + tmp = input.substr(start, end - start); + tmp = trim(tmp, ctrim); + if (tmp.size() != 0) + split_str.push_back( tmp ); + start = end + delim.size(); + end = input.find(delim, start); + } + split_str.push_back( input.substr(start, end - start) ); + return split_str; +} + +std::string trim(std::string str, char del) { size_t pos; - // delete leadings c - pos = str.find_first_not_of(c); + // delete leadings del + pos = str.find_first_not_of(del); if (pos == std::string::npos) - return str; + pos = str.size(); 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); + // delete trailing del + pos = str.find_last_not_of(del); + if (pos != std::string::npos) + str = str.substr(0, pos + 1); return str; } @@ -177,14 +198,6 @@ std::string extract_line(std::string * str, size_t pos, std::string delim) 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 ea38236..587b9f3 100644 --- a/srcs/utils.hpp +++ b/srcs/utils.hpp @@ -38,7 +38,7 @@ std::vector split(std::string input, char delimiter); bool isNumeric(std::string str); bool isNumeric_btw(int low, int high, std::string str); std::string itos(int n); -std::string trim(std::string str, char c); +std::string trim(std::string str, char del); http_method str_to_http_method(std::string &str); std::string http_methods_to_str(unsigned int methods); int path_is_valid(std::string path); diff --git a/srcs/webserv/parsing_message_http.cpp b/srcs/webserv/parsing_message_http.cpp index 63e67cd..7430bba 100644 --- a/srcs/webserv/parsing_message_http.cpp +++ b/srcs/webserv/parsing_message_http.cpp @@ -1,29 +1,36 @@ #include "parsing_message_http.hpp" -size_t - parse_http_first_line(std::string message, std::vector &line) +/* + - parse_http_first_line() : + - copy first line into a vector of 3 strings + / copy line into a vector of 3 strings + - parse_http_headers(std::string message) + - + - parse_http_body(std::string message) +*/ + +std::vector + parse_http_first_line(std::string line) { std::vector sline; + std::vector line = ""; std::string sub; std::string tmp; size_t pos; - size_t ret; - // TODO: check for err in substr - pos = message.find(CRLF); - sub = message.substr(0, pos); - sline = ::split(sub, ' '); - ret = sline.size(); - if (ret != 3) - return ret; - for (int i = 0; i < 3; i++) + sline = ::split(line, ' '); + if (sline.size() == 3) { - tmp = ::trim(sline[i], ' '); - tmp = ::trim(tmp, '\r'); - line.push_back(tmp); + for (int i = 0; i < 3; i++) + { + tmp = sline[i]; + tmp = ::trim(tmp, '\r'); + tmp = ::trim(tmp, ' '); + line.push_back(tmp); + } } - return ret; + return line; } std::map diff --git a/srcs/webserv/parsing_message_http.hpp b/srcs/webserv/parsing_message_http.hpp index 67cd473..a512aa1 100644 --- a/srcs/webserv/parsing_message_http.hpp +++ b/srcs/webserv/parsing_message_http.hpp @@ -8,8 +8,8 @@ # include # include "utils.hpp" -size_t - parse_http_first_line(std::string message, std::vector &line); +std::vector + parse_http_first_line(std::string message); std::map parse_http_headers(std::string message);