add extern function for http message parsing

+ wip compare fields server and script
+ g tout cassey
This commit is contained in:
hugogogo
2022-08-09 11:19:46 +02:00
parent ae9a9b37f1
commit 3dad938e3c
13 changed files with 225 additions and 86 deletions

View File

@@ -70,22 +70,15 @@ Client & Client::operator=( Client const & rhs )
// https://www.tutorialspoint.com/http/http_requests.htm
void Client::parse_request()
{
std::string sub;
std::vector<std::string> list;
size_t pos;
std::map<std::string, std::string> headers;
std::string body;
// DEBUG
std::cout << "\nREQUEST _____________________\n"
<< raw_request
<< "\n\n";
std::cout << "\nREQUEST ____________\n" << raw_request << "\n_____________\n";
pos = (raw_request).find(CRLF CRLF);
sub = (raw_request).substr(0, pos);
list = split(sub, '\n');
_parse_request_line(*list.begin());
list.erase(list.begin());
_parse_request_headers(list);
_parse_request_body(pos + 4);
_parse_request_line();
_parse_request_headers();
_parse_request_body();
_parse_port_hostname(this->get_rq_headers("Host"));
raw_request.clear();
}
@@ -176,30 +169,24 @@ std::string Client::get_rq_headers(const std::string & key) const
* PRIVATE MEMBER FUNCTIONS
*********************************************/
void Client::_parse_request_line( std::string rline )
void Client::_parse_request_line()
{
std::vector<std::string> sline;
std::string tmp;
std::vector<std::string> line;
int ret;
sline = split(rline, ' ');
if (sline.size() != 3)
ret = ::parse_http_first_line(raw_request, line);
if (ret != 3)
{
std::cerr << "err _parse_request_line(): ";
throw std::runtime_error("bad request-line header");
std::cerr << "err _parse_first_line(): wrong number of elements (" << ret << " instead of 3)\n";
status = 400; // "bad request"
}
else
{
_request.method = str_to_http_method(line[0]);
_request.uri = line[1];
_parse_request_uri(line[1]);
_request.version = line[2];
}
// method
tmp = ::trim(sline[0], ' ');
tmp = ::trim(tmp, '\r');
_request.method = str_to_http_method(tmp);
// uri
tmp = ::trim(sline[1], ' ');
tmp = ::trim(tmp, '\r');
_request.uri = tmp;
_parse_request_uri( tmp );
// http version
tmp = ::trim(sline[2], ' ');
tmp = ::trim(tmp, '\r');
_request.version = tmp;
}
void Client::_parse_request_uri( std::string uri )
@@ -214,32 +201,16 @@ void Client::_parse_request_uri( std::string uri )
_request.abs_path = uri.substr(0, pos);
}
void Client::_parse_request_headers( std::vector<std::string> list )
void Client::_parse_request_headers()
{
std::string key;
std::string val;
std::vector<std::string>::iterator it;
size_t pos;
for (it = list.begin(); it != list.end(); it++)
{
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');
_request.headers.insert( std::pair<std::string, std::string>(key, val) );
}
// TODO: check error and adjust status
_request.headers = ::parse_http_headers(raw_request);
}
void Client::_parse_request_body( size_t pos )
void Client::_parse_request_body()
{
std::string body = &raw_request[pos];
_request.body = body;
// TODO: check error and adjust status
_request.body = ::parse_http_body(raw_request);
}
void Client::_parse_port_hostname(std::string host)
@@ -247,10 +218,7 @@ void Client::_parse_port_hostname(std::string host)
size_t pos;
if (host == "")
{
std::cerr << "no host\n";
throw std::runtime_error("no host in request");
}
pos = host.find(':');
// port :