correct extract_line to avoid duplicate with erase or substr, and to resolve pbm of returning trailing delim

This commit is contained in:
hugogogo
2022-08-11 17:33:42 +02:00
parent ff77dfd298
commit ad2b5a629a
3 changed files with 91 additions and 55 deletions

View File

@@ -78,9 +78,13 @@ Client & Client::operator=( Client const & rhs )
void Client::parse_request(std::vector<ServerConfig> &servers) void Client::parse_request(std::vector<ServerConfig> &servers)
{ {
clear_request(); // not mandatory clear_request(); // not mandatory
// debug
print_client("before");
_parse_request_line(); _parse_request_line();
// debug // debug
print_client("first line"); print_client("first line");
if (status) if (status)
return; return;
_parse_request_headers(); _parse_request_headers();
@@ -170,22 +174,23 @@ void Client::print_client(std::string message)
std::map<std::string, std::string>::iterator it; std::map<std::string, std::string>::iterator it;
std::cout << "\n=== DEBUG PRINT CLIENT ===\n"; std::cout << "\n=== DEBUG PRINT CLIENT ===\n";
std::cout << "\n" << message << ":\n----------\n" std::cout << "\n" << message << ":\n----------\n" << "raw_request:\n__\n";
<< "raw_request:\n__\n" << raw_request << "\n__\n" ::print_special(raw_request);
<< "get_cl_fd() : " << get_cl_fd() << "\n" std::cout << "\n__\n"
<< "get_cl_port() : " << get_cl_port() << "\n" << "get_cl_fd() : [" << get_cl_fd() << "]\n"
<< "get_cl_ip() : " << get_cl_ip() << "\n" << "get_cl_port() : [" << get_cl_port() << "]\n"
<< "get_rq_method_str() : " << get_rq_method_str() << "\n" << "get_cl_ip() : [" << get_cl_ip() << "]\n"
<< "get_rq_uri() : " << get_rq_uri() << "\n" << "get_rq_method_str() : [" << get_rq_method_str() << "]\n"
<< "get_rq_abs_path() : " << get_rq_abs_path() << "\n" << "get_rq_uri() : [" << get_rq_uri() << "]\n"
<< "get_rq_query() : " << get_rq_query() << "\n" << "get_rq_abs_path() : [" << get_rq_abs_path() << "]\n"
<< "get_rq_version() : " << get_rq_version() << "\n" << "get_rq_query() : [" << get_rq_query() << "]\n"
<< "get_rq_body() : " << get_rq_body() << "\n" << "get_rq_version() : [" << get_rq_version() << "]\n"
<< "get_rq_port() : " << get_rq_port() << "\n" << "get_rq_body() : [" << get_rq_body() << "]\n"
<< "get_rq_hostname() : " << get_rq_hostname() << "\n" << "get_rq_port() : [" << get_rq_port() << "]\n"
<< "get_rq_script_path() : " << get_rq_script_path() << "\n" << "get_rq_hostname() : [" << get_rq_hostname() << "]\n"
<< "get_rq_script_info() : " << get_rq_script_info() << "\n" << "get_rq_script_path() : [" << get_rq_script_path() << "]\n"
<< "headers : " << "\n"; << "get_rq_script_info() : [" << get_rq_script_info() << "]\n"
<< "headers :\n";
for (it = _request.headers.begin(); it != _request.headers.end(); it++) for (it = _request.headers.begin(); it != _request.headers.end(); it++)
std::cout << " " << it->first << ": " << it->second << "\n"; std::cout << " " << it->first << ": " << it->second << "\n";
std::cout << "\n=== END DEBUG ===\n\n"; std::cout << "\n=== END DEBUG ===\n\n";
@@ -235,7 +240,13 @@ void Client::_parse_request_line()
std::string raw_line; std::string raw_line;
raw_line = ::get_line(raw_request, 0, CRLF); raw_line = ::get_line(raw_request, 0, CRLF);
line = ::split_trim(raw_line, " ", ' ');
// DEBUG
std::cout << "raw_line:[";
::print_special(raw_line);
std::cout << "]\n";
line = ::split_trim(raw_line, " ");
if (line.size() != 3) if (line.size() != 3)
{ {
std::cerr << "err _parse_first_line(): wrong number of elements (" << line.size() << " instead of 3)\n"; std::cerr << "err _parse_first_line(): wrong number of elements (" << line.size() << " instead of 3)\n";

View File

@@ -30,25 +30,27 @@ std::vector<std::string> split(std::string input, char delimiter)
} }
std::vector<std::string> std::vector<std::string>
split_trim(std::string input, std::string delim = "\n", char ctrim = '\0') split_trim(std::string input, std::string delim, char ctrim)
{ {
std::vector<std::string> split_str; std::vector<std::string> split_str;
std::string tmp; std::string tmp;
int start = 0; size_t start = 0;
int end; size_t end = 0;
size_t len = 0;
end = input.find(delim); while (end != std::string::npos)
while (end != -1)
{ {
tmp = input.substr(start, end - start); end = input.find(delim, start);
len = end - start;
if (end == std::string::npos)
len = end;
tmp = input.substr(start, len);
if (ctrim != '\0') if (ctrim != '\0')
tmp = trim(tmp, ctrim); tmp = trim(tmp, ctrim);
if (tmp.size() != 0) if (tmp.size() != 0)
split_str.push_back( tmp ); split_str.push_back( tmp );
start = end + delim.size(); start = end + delim.size();
end = input.find(delim, start);
} }
split_str.push_back( input.substr(start, end - start) );
return split_str; return split_str;
} }
@@ -70,6 +72,19 @@ std::string trim(std::string str, char del)
return str; return str;
} }
//// trim a set of char
//std::string trim(std::string str, std::string del)
//{
// std::string new_str;
//
// while (new_str.compare(str) != 0)
// {
// for (size_t i = 0; i < del.size(); i++)
// trim(str, del[i]);
// }
// return str;
//}
std::string itos(int n) std::string itos(int n)
{ {
std::stringstream strs; std::stringstream strs;
@@ -160,7 +175,11 @@ int path_is_valid(std::string path)
} }
void replace_all_substr(std::string &str, const std::string &ori_substr, const std::string &new_substr) void
replace_all_substr(
std::string &str,
const std::string &ori_substr,
const std::string &new_substr)
{ {
if (ori_substr.empty()) if (ori_substr.empty())
return; return;
@@ -184,13 +203,13 @@ std::string str_tolower(std::string str)
// identify a line in a string, by delim (ex. '\n') // identify a line in a string, by delim (ex. '\n')
// delete this line from the string // delete this line from the string
// and return the deleted line // and return the deleted line
// if delim is empty, the extraction begin exactly at pos, and end at npos
std::string std::string
extract_line(std::string & str, size_t pos = 0, std::string delim = "") extract_line(std::string & str, size_t pos, std::string delim)
{ {
std::string del_str; std::string del_str;
size_t begin; size_t begin;
size_t end; size_t end;
size_t len;
begin = str.rfind(delim, pos); begin = str.rfind(delim, pos);
if (begin == std::string::npos) if (begin == std::string::npos)
@@ -199,38 +218,23 @@ std::string
begin += delim.size(); begin += delim.size();
end = str.find(delim, pos); end = str.find(delim, pos);
len = end;
if (end != std::string::npos) if (end != std::string::npos)
end += delim.size(); len = end - begin;
if (delim.empty())
end = std::string::npos;
del_str = str.substr(begin, end - begin); del_str = str.substr(begin, len);
str.erase(begin, end - begin); str.erase(begin, len);
return del_str; return del_str;
} }
// get a line in a string, by delim // get a line in a string, by delim
// same as extract, except it doesn't delete it // same as extract, except it doesn't delete it
std::string get_line(std::string str, size_t pos = 0, std::string delim = "") std::string get_line(std::string str, size_t pos, std::string delim)
{ {
std::string ret_str; std::string ret;
size_t begin;
size_t end;
begin = str.rfind(delim, pos); ret = ::extract_line(str, pos, delim);
if (begin == std::string::npos) return ret;
begin = 0;
else
begin += delim.size();
end = str.find(delim, pos);
if (end != std::string::npos)
end += delim.size();
if (delim.empty())
end = std::string::npos;
ret_str = str.substr(begin, end - begin);
return ret_str;
} }
size_t size_t
@@ -271,6 +275,24 @@ size_t
return err; return err;
} }
// DEBUG
void print_special(std::string str)
{
char c;
for (size_t i = 0; i < str.size(); i++)
{
c = str[i];
if (c == '\r')
std::cout << "\\r";
else if (c == '\n')
std::cout << "\\n" << "\n";
else
std::cout << c;
fflush(stdout);
}
}
bool operator==(const listen_socket& lhs, int fd) bool operator==(const listen_socket& lhs, int fd)
{ return lhs.fd == fd; } { return lhs.fd == fd; }

View File

@@ -10,6 +10,7 @@
# include <sys/stat.h> // stat() # include <sys/stat.h> // stat()
# include <cctype> // tolower # include <cctype> // tolower
# include <algorithm> // transform # include <algorithm> // transform
# include <cstdio> // fflush
# define CR "\r" # define CR "\r"
# define LF "\n" # define LF "\n"
@@ -36,7 +37,7 @@ bool operator==(const listen_socket& lhs, int fd);
bool operator==(int fd, const listen_socket& rhs); bool operator==(int fd, const listen_socket& rhs);
std::vector<std::string> split(std::string input, char delimiter); std::vector<std::string> split(std::string input, char delimiter);
std::vector<std::string> split_trim(std::string s, std::string d, char c); std::vector<std::string> split_trim(std::string input, std::string delim = "\n", char ctrim = '\0');
bool isNumeric(std::string str); bool isNumeric(std::string str);
bool isNumeric_btw(int low, int high, std::string str); bool isNumeric_btw(int low, int high, std::string str);
std::string itos(int n); std::string itos(int n);
@@ -46,9 +47,11 @@ std::string http_methods_to_str(unsigned int methods);
int path_is_valid(std::string path); int path_is_valid(std::string path);
void replace_all_substr(std::string &str, const std::string &ori_substr, const std::string &new_substr); void replace_all_substr(std::string &str, const std::string &ori_substr, const std::string &new_substr);
std::string str_tolower(std::string str); std::string str_tolower(std::string str);
std::string extract_line(std::string & s, size_t p, std::string d); 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, std::string del); 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<std::string, std::string> fields ); size_t parse_http_headers (std::string headers, std::map<std::string, std::string> fields );
void throw_test(); void throw_test();
// debug
void print_special(std::string str);
#endif #endif