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

@@ -152,7 +152,7 @@ void Webserv::_check_script_status(Client *client, std::string output)
{
status_pos = pos + std::string("Status:").size();
client->status = atoi(output.c_str() + status_pos);
::del_line_in_str(&output, pos, CRLF);
::extract_line(output, pos, CRLF);
}
else
client->status = 200;
@@ -176,7 +176,7 @@ void Webserv::_check_script_fields(Client *client, std::string output)
if (it_srv->first == it_scr->first)
{
pos = client->response.find(it_srv->first);
::del_line_in_str(&client->response, pos, CRLF);
::extract_line(client->response, pos, CRLF);
}
}
}

View File

@@ -1,48 +1,6 @@
#include "parsing_message_http.hpp"
/*
- 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::map<std::string, std::string>
parse_http_headers(std::string message)
{
std::map<std::string, std::string> headers;
std::vector<std::string> list;
std::vector<std::string>::iterator it;
std::string sub;
std::string key;
std::string val;
size_t pos;
pos = (message).find(CRLF CRLF);
sub = (message).substr(0, pos);
list = ::split(sub, '\n');
if ( maybe_http_first_line( *list.begin() ) )
list.erase(list.begin());
for (it = list.begin(); it != list.end(); it++)
{
// TODO: if pattern is not "NAME: value" return error
pos = (*it).find(':');
key = (*it).substr( 0, pos );
key = ::trim(key, ' ');
key = ::trim(key, '\r');
key = ::str_tolower(key);
val = (*it).substr( pos + 1 );
val = ::trim(val, ' ');
val = ::trim(val, '\r');
headers.insert( std::pair<std::string, std::string>(key, val) );
}
return headers;
}
std::string
parse_http_body(std::string message)
{
@@ -57,18 +15,3 @@ std::string
return body;
}
bool maybe_http_first_line(std::string str)
{
// method SP target SP version https://www.rfc-editor.org/rfc/rfc7230.html#section-3.1.1
// version SP status SP reason https://www.rfc-editor.org/rfc/rfc7230.html#section-3.1.2
std::vector<std::string> sline;
sline = ::split(str, ' ');
if (sline.size() != 3)
return false;
if (sline[0].find(':') != std::string::npos)
return false;
return true;
}

View File

@@ -9,14 +9,13 @@
# include "utils.hpp"
std::map<std::string, std::string>
parse_http_headers(std::string message);
parse_http_headers (
std::string headers,
std::map<std::string, std::string> fields )
std::string
parse_http_body(std::string message);
bool
maybe_http_first_line(std::string);
// http message structure :
//
// start-line