better telnet tester, merging

This commit is contained in:
Eric LAZO
2022-08-14 21:41:46 +02:00
30 changed files with 605 additions and 163 deletions

View File

@@ -92,7 +92,7 @@ void Client::parse_request_headers(std::vector<ServerConfig> &servers)
_parse_request_fields();
// DEBUG
print_client("headers");
// print_client("headers");
if (status)
return;
@@ -101,10 +101,10 @@ print_client("headers");
_check_request_errors();
if (status)
return;
_parse_port_hostname(this->get_rq_headers("Host")); // use getter for headers because it works case insensitive
_parse_port_hostname(get_rq_headers("Host")); // use getter for headers because it works case insensitive
// DEBUG
std::cerr << get_rq_method_str() << " " << get_rq_target() << " " << get_rq_version() << "\n";
// std::cerr << get_rq_method_str() << " " << get_rq_target() << " " << get_rq_version() << "\n";
// dont clear raw_request, we need it for future reparsing of body
// see call of parse_request() in _read_request()
@@ -113,6 +113,7 @@ std::cerr << get_rq_method_str() << " " << get_rq_target() << " " << get_rq_vers
void Client::parse_request_body()
{
std::cerr << "parse_request_body()\n";
size_t pos;
pos = raw_request.find(CRLF CRLF);
@@ -120,14 +121,38 @@ void Client::parse_request_body()
{
std::cerr << "parse_request_body() bad call, header incomplete\n";
// QUESTION from hugo : don't we change the status here ?
// RESPONSE from luke : C'est vrai. Peut-être mettre un 500, c'etait plus du debug à la base.
// C'est seulement si on appelle la fonction au mauvais endroit, avant d'avoir un header complet, que ça arrive.
return;
}
pos += CRLF_SIZE*2;
// Chunked decoding WIP. Dont work.
if (!get_rq_headers("Transfer-Encoding").empty()
&& get_rq_headers("Transfer-Encoding") == "chunked")
{
// Chunked decoding WIP. How to test this ? dont know how to send chunks with telnet.
_parse_chunked_body(pos + CRLF_SIZE*2);
}
else if (raw_request.size() - pos >= std::strtoul(get_rq_headers("Content-Length").c_str(), NULL, 10))
{
if (get_rq_headers("Content-Type").find("multipart/form-data") != NPOS)
_parse_multipart_body(pos);
else
_request.body = raw_request.substr(pos + CRLF_SIZE*2);
body_complete = true;
}
// std::cerr << "Content-Length = " << std::strtoul(get_rq_headers("Content-Length").c_str(), NULL, 10) << "\n";
// std::cerr << "raw_request.size() - pos = " << raw_request.size() - pos << "\n";
// _request.body = raw_request.substr(pos);
// std::cerr << "_request.body.size() = " << _request.body.size() << "\n";
///////////////
// Body checks
if (_request.body.size() > assigned_server->client_body_limit)
status = 413; // HTTP Client Errors
}
void Client::_parse_chunked_body(size_t pos)
{
size_t chunk_size = 1;
size_t chunk_field_end = 0;
char *endptr = NULL;
@@ -142,28 +167,29 @@ void Client::parse_request_body()
pos = 0;
while (chunk_size != 0)
{
if (pos > _request.body.size())
/* if (pos > _request.body.size())
{
std::cerr << "parse_request_body(), pos > size()\n";
// status = 400;
return;
}
} */
/*
if (pos == _request.body.size())
{
std::cerr << "parse_request_body(), will reread till last chunk\n";
return;
}
} */
endptr_copy = endptr;
/* endptr_copy = endptr; */
(void)endptr_copy;
chunk_size = std::strtoul(&_request.body[pos], &endptr, 16);
if (chunk_size == LONG_MAX && errno == ERANGE)
status = 413;
if (endptr == endptr_copy)
/* if (chunk_size == LONG_MAX && errno == ERANGE)
status = 413; */
/* if (endptr == endptr_copy)
{
std::cerr << "parse_request_body(), no conversion possible\n";
return;
}
} */
chunk_field_end = _request.body.find(CRLF, pos);
@@ -181,48 +207,26 @@ void Client::parse_request_body()
_request.headers.erase("Transfer-Encoding");
body_complete = true;
}
else
{
if (raw_request.size() - pos >= std::strtoul(get_rq_headers("Content-Length").c_str(), NULL, 10))
{
_request.body = raw_request.substr(pos);
body_complete = true;
}
/* Should be equivalent */
// _request.body = raw_request.substr(pos);
// if (_request.body.size() >= std::strtoul(get_rq_headers("Content-Length").c_str(), NULL, 10))
// body_complete = true;
}
///////////////
// Body checks
if (_request.body.size() > assigned_server->client_body_limit)
status = 413; // HTTP Client Errors
}
// TODO HUGO : faire la fonction, mdr.
void Client::fill_script_path(const std::string &path, size_t pos)
void Client::fill_script_path(std::string &path, size_t pos)
{
(void)path;
(void)pos;
/* size_t pos;
size_t len = path.size();
std::string path = this->get_rq_abs_path();
std::string tmp;
pos = path.find(script);
if (pos == 0)
/*DEBUG*/ std::cout << "\n" << B_PURPLE << "debug path dot" << RESET << "\npath:[" << path << "]\n" << "&path[pos]:[" << &path[pos] << "]\n";
if (path[0] == '.')
{
tmp = path.substr(0, pos + len);
_request.script.path = "./srcs" + tmp; // TODO: root path ?
_request.script.path = "./srcs" + tmp; // TODO: root path ?
_request.script.info = path.substr(pos + len);
return true;
path.erase(0, 1);
pos--;
}
return false; */
/*DEBUG*/ std::cout << "path:[" << path << "]\n" << "&path[pos]:[" << &path[pos] << "]\n";
_request.script.path = path.substr(0, pos);
/*DEBUG*/ std::cout << "script_path:[" << _request.script.path << "]\n";
_request.script.info = path.substr(pos);
/*DEBUG*/ std::cout << "script_info:[" << _request.script.info << "]\n" << B_PURPLE << "end debug path dot" << RESET << "\n";
}
void Client::clear()
@@ -247,6 +251,7 @@ void Client::clear_request()
_request.version.clear();
_request.headers.clear();
_request.body.clear();
_request.multi_bodys.clear();
_request.abs_path.clear();
_request.query.clear();
_request.port.clear();
@@ -368,7 +373,7 @@ void Client::_parse_request_fields()
// delete first line
pos = headers.find(CRLF);
if (pos != NPOS)
headers.erase(0, pos + std::string(CRLF).size());
headers.erase(0, pos + CRLF_SIZE);
// delete body part
pos = headers.find(CRLF CRLF);
if (pos != NPOS)
@@ -407,6 +412,9 @@ void Client::_parse_port_hostname(std::string host)
void Client::_check_request_errors()
{
std::cerr << "Content-Length=" << get_rq_headers("Content-Length") << "\n";
std::cerr << "strtoul=" << std::strtoul(get_rq_headers("Content-Length").c_str(), NULL, 10) << "\n";
std::cerr << "client_body_limit=" << assigned_server->client_body_limit << "\n";
///////////////////////
// Request line checks
if (_request.method == UNKNOWN)
@@ -431,13 +439,13 @@ void Client::_check_request_errors()
//////////////////
// Headers checks
else if (!this->get_rq_headers("Content-Length").empty()
&& std::strtoul(this->get_rq_headers("Content-Length").c_str(), NULL, 10) > assigned_server->client_body_limit)
else if (!get_rq_headers("Content-Length").empty()
&& std::strtoul(get_rq_headers("Content-Length").c_str(), NULL, 10) > assigned_server->client_body_limit)
status = 413;
else if (!this->get_rq_headers("Transfer-Encoding").empty()
&& this->get_rq_headers("Transfer-Encoding") != "chunked" )
else if (!get_rq_headers("Transfer-Encoding").empty()
&& get_rq_headers("Transfer-Encoding") != "chunked" )
status = 501;
else if (!this->get_rq_headers("Content-Encoding").empty())
else if (!get_rq_headers("Content-Encoding").empty())
{
status = 415;
response.append("Accept-Encoding:"); // empty, no encoding accepted