Merge branch 'hugo3'

+ script output verification works (field duplicate and status)
+ parsing_message_http :
  - the file is deleted
  - of the three functions it contained, only one still exist
  - 'parse_http_headers' is now in utils
+ changes in utils :
  - http headers parsing doesn't change key case itself
  - a new function change key case tolower case in map<str, str>
  - added split_trim() that perform a split and a trim at once
  - resolved pbm in trim
  - added print_special to print special char '\r' and '\n'
  - del_line became extract_line, because it returns the deleted line
  - added get line, that does the same without deleting
  - moved http_header in utils, and made it more consistent
+ in Client :
  - parse_request is now named parse_request_headers to work with parse body
  - private function _parse_request_headers is then now _parse_request_fields
    (because it doesn't take care of request first line, but only fields)
  - added a debug function 'print_client'
This commit is contained in:
hugogogo
2022-08-12 14:05:11 +02:00
19 changed files with 374 additions and 184 deletions

View File

@@ -9,6 +9,14 @@ void throw_test()
throw std::bad_alloc();
}
// notice : the use of getline make it such as
// it doesn't identify multiple delim as one :
// " something \n else " -> 1 - something
// 2 - else
// is not the same as :
// " something \n\n else " -> 1 - something
// 2 -
// 3 - else
std::vector<std::string> split(std::string input, char delimiter)
{
std::vector<std::string> answer;
@@ -21,25 +29,62 @@ std::vector<std::string> split(std::string input, char delimiter)
return answer;
}
std::string trim(std::string str, char c)
std::vector<std::string>
split_trim(std::string input, std::string delim, char ctrim)
{
std::vector<std::string> split_str;
std::string tmp;
size_t start = 0;
size_t end = 0;
size_t len = 0;
while (end != std::string::npos)
{
end = input.find(delim, start);
len = end - start;
if (end == std::string::npos)
len = end;
tmp = input.substr(start, len);
if (ctrim != '\0')
tmp = trim(tmp, ctrim);
if (tmp.size() != 0)
split_str.push_back( tmp );
start = end + delim.size();
}
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;
}
//// 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::stringstream strs;
@@ -127,7 +172,11 @@ file_type eval_file_type(const 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())
return;
@@ -148,27 +197,116 @@ 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;
size_t len;
begin = (*str).rfind(delim, pos);
begin = str.rfind(delim, pos);
if (begin == std::string::npos)
begin = 0;
else
begin += delim.size();
end = (*str).find(delim, pos);
if (end == std::string::npos)
end = 0;
else
end += delim.size();
end = str.find(delim, pos);
len = end;
if (end != std::string::npos)
len = end - begin;
(*str).erase(begin, end - begin);
del_str = str.substr(begin, len);
str.erase(begin, len);
return del_str;
}
// get a line in a string, by delim
// same as extract, except it doesn't delete it
std::string get_line(std::string str, size_t pos, std::string delim)
{
std::string ret;
ret = ::extract_line(str, pos, delim);
return ret;
}
size_t
parse_http_headers (
std::string headers,
std::map<std::string, std::string> & fields )
{
std::vector<std::string> list;
std::vector<std::string>::iterator it;
std::vector<std::string>::iterator it_end;
size_t err = 0;
size_t pos;
std::string key;
std::string val;
list = ::split_trim(headers, CRLF, ' ');
it_end = list.end();
for (it = list.begin(); it != it_end; it++)
{
pos = (*it).find(':');
if (pos == std::string::npos)
{
err++;
continue;
}
key = (*it).substr(0, pos);
if ( key.find(' ') != std::string::npos )
{
err++;
continue;
}
// bad idea, in cgi we need to have the original value
// key = ::str_tolower(key); // to make "key" case_insensitive
val = (*it).substr(pos + 1);
val = ::trim(val, ' ');
fields.insert( std::pair<std::string, std::string>(key, val) );
}
return err;
}
void str_map_key_tolower(std::map<std::string, std::string> & mp)
{
std::map<std::string, std::string> new_mp;
std::map<std::string, std::string>::iterator it;
std::string key;
std::string value;
for (it = mp.begin(); it != mp.end(); it++)
{
key = it->first;
value = it->second;
key = ::str_tolower(key);
new_mp.insert( std::pair<std::string, std::string>(key, value) );
}
mp.swap(new_mp);
}
// 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 << YELLOW << "\\r" << RESET;
else if (c == '\n')
std::cout << YELLOW << "\\n" << RESET << "\n";
else
std::cout << c;
fflush(stdout);
}
}
bool operator==(const listen_socket& lhs, int fd)
{ return lhs.fd == fd; }