http message parsin wip but really better

This commit is contained in:
hugogogo
2022-08-11 00:28:01 +02:00
parent 11f71ea74f
commit 1d67e6988d
8 changed files with 112 additions and 81 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;
@@ -176,29 +184,91 @@ std::string str_tolower(std::string str)
// 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)
// if delim is empty, the extraction begin exactly at pos, and end at npos
std::string
extract_line(std::string & str, size_t pos = 0, std::string delim = "")
{
std::string del_str;
size_t begin;
size_t end;
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 = str.find(delim, pos);
if (end != std::string::npos)
end += delim.size();
if (delim.empty())
end = std::string::npos;
del_str = (*str).substr(begin, end - begin);
(*str).erase(begin, end - begin);
del_str = str.substr(begin, end - begin);
str.erase(begin, end - begin);
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 = 0, std::string delim = "")
{
std::string ret_str;
size_t begin;
size_t end;
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 += delim.size();
if (delim.empty())
end = std::string::npos;
ret_str = str.substr(begin, end - begin);
return ret_str;
}
std::map<std::string, std::string>
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;
list = ::split_trim(sub, 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;
}
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;
}
bool operator==(const listen_socket& lhs, int fd)
{ return lhs.fd == fd; }