debug new request parsing
This commit is contained in:
@@ -77,18 +77,16 @@ Client & Client::operator=( Client const & rhs )
|
||||
// https://www.tutorialspoint.com/http/http_requests.htm
|
||||
void Client::parse_request(std::vector<ServerConfig> &servers)
|
||||
{
|
||||
// std::map<std::string, std::string> headers;
|
||||
// std::string body;
|
||||
std::string copy_request;
|
||||
|
||||
// DEBUG
|
||||
// std::cout << "\nREQUEST ____________\n" << raw_request << "\n_____________\n";
|
||||
clear_request(); // not mandatory
|
||||
|
||||
_parse_request_line(copy_request);
|
||||
_parse_request_line();
|
||||
// debug
|
||||
print_client("first line");
|
||||
if (status)
|
||||
return;
|
||||
_parse_request_headers(copy_request);
|
||||
_parse_request_headers();
|
||||
// debug
|
||||
print_client("headers");
|
||||
|
||||
if (status)
|
||||
return;
|
||||
assigned_server = ::_determine_process_server(this, servers);
|
||||
@@ -106,11 +104,11 @@ void Client::parse_request(std::vector<ServerConfig> &servers)
|
||||
|
||||
void Client::parse_request_body()
|
||||
{
|
||||
size_t pos;
|
||||
size_t pos;
|
||||
|
||||
pos = raw_request.find(CRLF CRLF);
|
||||
pos += std::string(CRLF CRLF).size();
|
||||
_request.body = message.substr(pos);
|
||||
_request.body = raw_request.substr(pos);
|
||||
|
||||
if (_request.body.size() > assigned_server->client_body_limit)
|
||||
status = 413; // HTTP Client Errors
|
||||
@@ -166,6 +164,32 @@ void Client::clear_script()
|
||||
_request.script.info.clear();
|
||||
}
|
||||
|
||||
// debug
|
||||
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" << raw_request << "\n__\n"
|
||||
<< "get_cl_fd() : " << get_cl_fd() << "\n"
|
||||
<< "get_cl_port() : " << get_cl_port() << "\n"
|
||||
<< "get_cl_ip() : " << get_cl_ip() << "\n"
|
||||
<< "get_rq_method_str() : " << get_rq_method_str() << "\n"
|
||||
<< "get_rq_uri() : " << get_rq_uri() << "\n"
|
||||
<< "get_rq_abs_path() : " << get_rq_abs_path() << "\n"
|
||||
<< "get_rq_query() : " << get_rq_query() << "\n"
|
||||
<< "get_rq_version() : " << get_rq_version() << "\n"
|
||||
<< "get_rq_body() : " << get_rq_body() << "\n"
|
||||
<< "get_rq_port() : " << get_rq_port() << "\n"
|
||||
<< "get_rq_hostname() : " << get_rq_hostname() << "\n"
|
||||
<< "get_rq_script_path() : " << get_rq_script_path() << "\n"
|
||||
<< "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";
|
||||
}
|
||||
|
||||
/*********************************************
|
||||
* GETTERS
|
||||
@@ -209,13 +233,12 @@ void Client::_parse_request_line()
|
||||
{
|
||||
std::vector<std::string> line;
|
||||
std::string raw_line;
|
||||
int ret;
|
||||
|
||||
raw_line = ::get_line(raw_request, 0, CRLF);
|
||||
line = ::split_trim(raw_line, " ", ' ');
|
||||
if (line.size() != 3)
|
||||
{
|
||||
std::cerr << "err _parse_first_line(): wrong number of elements (" << ret << " instead of 3)\n";
|
||||
std::cerr << "err _parse_first_line(): wrong number of elements (" << line.size() << " instead of 3)\n";
|
||||
status = 400; // "bad request"
|
||||
}
|
||||
else
|
||||
@@ -247,9 +270,10 @@ void Client::_parse_request_headers()
|
||||
|
||||
headers = raw_request;
|
||||
// extract header part
|
||||
::extract_line(headers, 0, CRLF);
|
||||
pos = headers.find(CRLF);
|
||||
::extract_line(headers, pos); // delete from pos to the end
|
||||
::extract_line(headers, 0, CRLF); // delete first line
|
||||
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
|
||||
// copy result of parser into headers
|
||||
ret = ::parse_http_headers(raw_request, _request.headers);
|
||||
if (ret > 0)
|
||||
|
||||
@@ -75,6 +75,8 @@ class Client
|
||||
void clear_request();
|
||||
void clear_script();
|
||||
bool fill_script_path(std::string script);
|
||||
// DEBUG
|
||||
void print_client(std::string message = "");
|
||||
|
||||
private:
|
||||
int _fd;
|
||||
|
||||
@@ -1,7 +1,8 @@
|
||||
#! /usr/bin/php
|
||||
|
||||
<?php
|
||||
echo "Status: 200\r\n";
|
||||
echo "false: 300\r\n";
|
||||
echo "Status: 300\r\n";
|
||||
echo "\r\n";
|
||||
echo "BEGIN PHP-CGI\n-----------\n\n";
|
||||
echo "AUTH_TYPE: " . getenv("AUTH_TYPE");
|
||||
|
||||
@@ -233,7 +233,7 @@ std::string get_line(std::string str, size_t pos = 0, std::string delim = "")
|
||||
return ret_str;
|
||||
}
|
||||
|
||||
std::map<std::string, std::string>
|
||||
size_t
|
||||
parse_http_headers (
|
||||
std::string headers,
|
||||
std::map<std::string, std::string> fields )
|
||||
@@ -243,8 +243,10 @@ std::map<std::string, std::string>
|
||||
std::vector<std::string>::iterator it_end;
|
||||
size_t err = 0;
|
||||
size_t pos;
|
||||
std::string key;
|
||||
std::string val;
|
||||
|
||||
list = ::split_trim(sub, CRLF, ' ');
|
||||
list = ::split_trim(headers, CRLF, ' ');
|
||||
|
||||
it_end = list.end();
|
||||
for (it = list.begin(); it != it_end; it++)
|
||||
|
||||
@@ -3,6 +3,7 @@
|
||||
# define UTILS_HPP
|
||||
|
||||
# include <vector>
|
||||
# include <map>
|
||||
# include <string>
|
||||
# include <sstream>
|
||||
# include <cstdlib> // atoi
|
||||
@@ -47,8 +48,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 & s, size_t p, std::string d);
|
||||
std::string get_line(std::string str, size_t pos, std::string del);
|
||||
std::map<std::string, std::string>
|
||||
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();
|
||||
|
||||
#endif
|
||||
|
||||
@@ -137,8 +137,13 @@ std::string Webserv::_exec_script(Client *client, char **env)
|
||||
|
||||
void Webserv::_check_script_output(Client *client, std::string output)
|
||||
{
|
||||
// TODO: it doesn't work with execve error, i don't know why yet ?
|
||||
// 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);
|
||||
}
|
||||
|
||||
@@ -166,8 +171,8 @@ void Webserv::_check_script_fields(Client *client, std::string output)
|
||||
std::map<std::string, std::string>::iterator it_scr;
|
||||
size_t pos;
|
||||
|
||||
srv_fld = ::parse_http_headers(client->response);
|
||||
scr_fld = ::parse_http_headers(output);
|
||||
::parse_http_headers(client->response, srv_fld);
|
||||
::parse_http_headers(output, scr_fld);
|
||||
// wip: compare both map to supress duplicates
|
||||
for (it_srv = srv_fld.begin(); it_srv != srv_fld.end(); it_srv++)
|
||||
{
|
||||
|
||||
Reference in New Issue
Block a user