wip debug comparison fields output script

This commit is contained in:
hugogogo
2022-08-11 19:42:09 +02:00
parent a1fff0f8c2
commit 3a58b5d921
7 changed files with 86 additions and 39 deletions

View File

@@ -79,12 +79,10 @@ void Client::parse_request_headers(std::vector<ServerConfig> &servers)
{
clear_request(); // not mandatory
_parse_request_line();
// debug
print_client("first line");
if (status)
return;
_parse_request_fields();
// debug
// DEBUG
print_client("headers");
if (status)
return;
@@ -93,8 +91,7 @@ print_client("headers");
_check_request_errors();
if (status)
return;
// use getter for headers because it works case insensitive
_parse_port_hostname(this->get_rq_headers("Host"));
_parse_port_hostname(this->get_rq_headers("Host")); // use getter for headers because it works case insensitive
// dont clear raw_request, we need it for future reparsing of body
// see call of parse_request() in _read_request()
@@ -169,7 +166,7 @@ void Client::print_client(std::string message)
std::map<std::string, std::string>::iterator it;
std::cout << "\n=== DEBUG PRINT CLIENT ===\n";
std::cout << "\n" << message << ":----------\n\n" << "raw_request:\n__\n";
std::cout << message << ":\n----------\n\n" << "raw_request:\n__\n";
::print_special(raw_request);
std::cout << "\n__\n"
<< "get_cl_fd() : [" << get_cl_fd() << "]\n"
@@ -187,8 +184,8 @@ void Client::print_client(std::string message)
<< "get_rq_script_info() : [" << get_rq_script_info() << "]\n"
<< "headers :\n";
for (it = _request.headers.begin(); it != _request.headers.end(); it++)
std::cout << " " << it->first << ": " << it->second << "\n";
std::cout << "\n=== END DEBUG ===\n\n";
std::cout << " " << it->first << ": [" << it->second << "]\n";
std::cout << "\n=== END PRINT CLIENT ===\n\n";
}
/*********************************************
@@ -269,16 +266,22 @@ void Client::_parse_request_fields()
int ret;
headers = raw_request;
// extract header part
::extract_line(headers, 0, CRLF); // delete first line
// delete first line
pos = headers.find(CRLF);
if (pos != std::string::npos)
headers.erase(0, pos + std::string(CRLF).size());
// delete body part
pos = headers.find(CRLF CRLF);
// ::extract_line(headers, pos); // delete from empty line to the end
headers.erase(pos); //delete from empty line to the end
if (pos != std::string::npos)
headers.erase(pos);
else {
std::cerr << "err _parse_request_fields(): request header doesn't end with empty line\n";
status = 400; // "bad request"
}
// copy result of parser into headers
ret = ::parse_http_headers(raw_request, _request.headers);
if (ret > 0)
{
std::cerr << "err _parse_request_headers(): " << ret << " field are bad formated\n";
ret = ::parse_http_headers(headers, _request.headers);
if (ret > 0) {
std::cerr << "err _parse_request_fields(): " << ret << " fields are bad formated\n";
status = 400; // "bad request"
}
}

View File

@@ -2,6 +2,7 @@
<?php
echo "false: 300\r\n";
echo "server: Webserv/0.2\r\n";
echo "Status: 300\r\n";
echo "\r\n";
echo "BEGIN PHP-CGI\n-----------\n\n";

View File

@@ -240,7 +240,7 @@ std::string get_line(std::string str, size_t pos, std::string delim)
size_t
parse_http_headers (
std::string headers,
std::map<std::string, std::string> fields )
std::map<std::string, std::string> & fields )
{
std::vector<std::string> list;
std::vector<std::string>::iterator it;

View File

@@ -50,7 +50,7 @@ void replace_all_substr(std::string &str, const std::string &ori_substr, co
std::string str_tolower(std::string str);
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 = 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();
// debug
void print_special(std::string str);

View File

@@ -104,9 +104,9 @@ class Webserv
char* _dup_env(std::string var, std::string val);
char* _dup_env(std::string var, int i);
std::string _exec_script(Client *client, char **env);
void _check_script_output(Client *client, std::string output);
void _check_script_status(Client *client, std::string output);
void _check_script_fields(Client *client, std::string output);
void _check_script_output(Client *client, std::string & output);
void _check_script_status(Client *client, std::string & output);
void _check_script_fields(Client *client, std::string & output);
// epoll_update.cpp
int _epoll_update(int fd, uint32_t events, int op);
int _epoll_update(int fd, uint32_t events, int op, void *ptr);

View File

@@ -106,9 +106,9 @@ std::string Webserv::_exec_script(Client *client, char **env)
dup2(FD_WR_TO_PRNT, STDOUT_FILENO);
// DEBUG
std::cerr << "execve:\n";
//execve(client->get_rq_script_path().c_str(), nll, env);
execve(client->get_rq_script_path().c_str(), nll, env);
// for tests execve crash :
execve("wrong", nll, env);
//execve("wrong", nll, env);
std::cerr << "execve crashed.\n";
}
else
@@ -135,19 +135,13 @@ std::string Webserv::_exec_script(Client *client, char **env)
return script_output;
}
void Webserv::_check_script_output(Client *client, std::string output)
void Webserv::_check_script_output(Client *client, std::string & output)
{
// DEBUG
std::cout << "outpu:__________\n" << output << "__________\n"
<< "\nstatus:" << client->status << "__________\n";
_check_script_status(client, output);
// DEBUG
std::cout << "outpu:__________\n" << output << "__________\n"
<< "\nstatus:" << client->status << "__________\n";
_check_script_fields(client, output);
}
void Webserv::_check_script_status(Client *client, std::string output)
void Webserv::_check_script_status(Client *client, std::string & output)
{
size_t pos;
int status_pos;
@@ -163,27 +157,79 @@ void Webserv::_check_script_status(Client *client, std::string output)
client->status = 200;
}
void Webserv::_check_script_fields(Client *client, std::string output)
void Webserv::_check_script_fields(Client *client, std::string & output)
{
std::map<std::string, std::string> srv_fld; // server_field
std::map<std::string, std::string> scr_fld; // script_field
std::map<std::string, std::string>::iterator it_srv;
std::map<std::string, std::string>::iterator it_scr;
std::string tmp;
size_t pos;
::parse_http_headers(client->response, srv_fld);
::parse_http_headers(output, scr_fld);
// wip: compare both map to supress duplicates
// put server headers in map
tmp = client->response;
pos = tmp.find(CRLF CRLF);
if (pos != std::string::npos)
tmp.erase(pos);
::parse_http_headers(tmp, srv_fld);
// put script headers in map
tmp = output;
pos = tmp.find(CRLF CRLF);
if (pos != std::string::npos)
tmp.erase(pos);
::parse_http_headers(tmp, scr_fld);
// compare both map to supress duplicates
// debug
std::map<std::string, std::string>::iterator it;
std::cout << "\n\n+++++\ndebug comparison:\nBEFORE\nserver headers:\n";
for (it = srv_fld.begin(); it != srv_fld.end(); it++) {
std::cout << " " << it->first << ": [" << it->second << "]\n"; }
std::cout << "\nscript headers:\n";
for (it = scr_fld.begin(); it != scr_fld.end(); it++) {
std::cout << " " << it->first << ": [" << it->second << "]\n"; }
// en debug
for (it_srv = srv_fld.begin(); it_srv != srv_fld.end(); it_srv++)
{
for (it_scr = scr_fld.begin(); it_scr != scr_fld.end(); it_scr++)
{
if (it_srv->first == it_scr->first)
if (str_tolower(it_srv->first) == str_tolower(it_scr->first))
{
pos = client->response.find(it_srv->first);
::extract_line(client->response, pos, CRLF);
// debug
std::cout << "helloooooooooooooooooooooooooooooo\n";
std::cout << pos << "\n";
}
}
}
// debug
srv_fld.clear();
scr_fld.clear();
// put server headers in map
tmp = client->response;
pos = tmp.find(CRLF CRLF);
if (pos != std::string::npos)
tmp.erase(pos);
::parse_http_headers(tmp, srv_fld);
// put script headers in map
tmp = output;
pos = tmp.find(CRLF CRLF);
if (pos != std::string::npos)
tmp.erase(pos);
::parse_http_headers(tmp, scr_fld);
// compare both map to supress duplicates
std::cout << "\nAFTER\nserver headers:\n";
for (it = srv_fld.begin(); it != srv_fld.end(); it++) {
std::cout << " " << it->first << ": [" << it->second << "]\n"; }
std::cout << "\nscript headers:\n";
for (it = scr_fld.begin(); it != scr_fld.end(); it++) {
std::cout << " " << it->first << ": [" << it->second << "]\n"; }
std::cout << "\nend debug comparison\n+++++\n\n";
// end debug
}

View File

@@ -61,9 +61,6 @@ Where does cgi fit in in all this ???
if (_is_cgi(client))
{
script_output = _exec_cgi(client);
// DEBUG
std::cout << "\n____script_output____\n" << script_output << "\n_______________\n";
// wip check output of script
_check_script_output(client, script_output);
client->response += script_output;
return;