Moved most request errors check in Client

+ bugfix of "Bad file descriptor" due to empty operator=() in Client
+ added a condition in _determine_location_COOP_FIX()
+ udpate memo.txt
+ need fix for index/autoindex (location "/list" dont work)
This commit is contained in:
LuckyLaszlo
2022-08-10 07:19:05 +02:00
parent 58f67bf42e
commit 86f7740984
13 changed files with 134 additions and 118 deletions

View File

@@ -37,7 +37,7 @@ Client::~Client() {
return;
}
// WIP not sure fo what is more logic here
/* // WIP not sure fo what is more logic here
Client::Client( Client const & src )
: status ( src.status ),
header_complete ( src.header_complete ),
@@ -54,8 +54,9 @@ Client::Client( Client const & src )
// buf = strdup(src.buf); // TODO: this doesn't work
return;
}
*/
// WIP placeholder because of const values
/* // WIP placeholder because of const values
Client & Client::operator=( Client const & rhs )
{
if ( this != &rhs )
@@ -64,7 +65,7 @@ Client & Client::operator=( Client const & rhs )
}
return *this;
}
*/
/*********************************************
* PUBLIC MEMBER FUNCTIONS
@@ -74,19 +75,37 @@ Client & Client::operator=( Client const & rhs )
// https://developer.mozilla.org/en-US/docs/Web/HTTP/Headers
// https://www.ibm.com/docs/en/cics-ts/5.3?topic=protocol-http-requests
// https://www.tutorialspoint.com/http/http_requests.htm
void Client::parse_request()
void Client::parse_request(std::vector<ServerConfig> &servers)
{
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";
clear_request(); // not mandatory
_parse_request_line();
if (status)
return;
_parse_request_headers();
_parse_request_body();
assigned_server = ::_determine_process_server(this, servers);
assigned_location = ::_determine_location(*assigned_server, _request.abs_path);
_check_request_errors();
if (status)
return;
_parse_port_hostname(this->get_rq_headers("Host"));
raw_request.clear();
/* dont clear raw_request, we need it for future reparsing of body
see call of parse_request() in _read_request() */
// raw_request.clear();
}
void Client::parse_request_body()
{
// TODO: check error and adjust status
_request.body = ::parse_http_body(raw_request);
if (_request.body.size() > assigned_server->client_body_limit)
status = 413;
}
bool Client::fill_script_path(std::string script)
@@ -216,12 +235,6 @@ void Client::_parse_request_headers()
_request.headers = ::parse_http_headers(raw_request);
}
void Client::_parse_request_body()
{
// TODO: check error and adjust status
_request.body = ::parse_http_body(raw_request);
}
void Client::_parse_port_hostname(std::string host)
{
size_t pos;
@@ -241,6 +254,41 @@ void Client::_parse_port_hostname(std::string host)
_request.hostname = host.substr(0, pos);
}
void Client::_check_request_errors()
{
//////////////////////
// Request line checks
if (_request.method == UNKNOWN)
status = 501;
else if (_request.version.compare(0, sizeof("HTTP/1") - 1, "HTTP/1") != 0)
status = 505;
else if (!(assigned_location->allow_methods & _request.method))
{
status = 405;
response.append("Allow: ");
response.append(::http_methods_to_str(assigned_location->allow_methods));
response.append(CRLF);
}
else if (assigned_location->redirect_status)
{ // Weird behavior. Sometimes, the web browser seems to wait for a complete response until timeout.
// (for codes 301, 302, 303, 307, and 308)
status = assigned_location->redirect_status;
response.append("Location: ");
response.append(assigned_location->redirect_uri);
response.append(CRLF CRLF);
}
if (status)
return;
/////////////////
// Headers checks
if (!this->get_rq_headers("Content-Length").empty()
&& ::atoi(this->get_rq_headers("Content-Length").c_str()) > (int)assigned_server->client_body_limit)
status = 413;
return;
}
/*********************************************
* OVERLOAD