added multipart upload file,
it works, but need some adjustements, refactoring and testing
This commit is contained in:
@@ -101,7 +101,7 @@ void Client::parse_request_headers(std::vector<ServerConfig> &servers)
|
||||
_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";
|
||||
@@ -121,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;
|
||||
@@ -183,32 +207,10 @@ void Client::parse_request_body()
|
||||
|
||||
_request.headers.erase("Transfer-Encoding");
|
||||
body_complete = true;
|
||||
}
|
||||
else
|
||||
{
|
||||
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";
|
||||
|
||||
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)
|
||||
{
|
||||
@@ -254,6 +256,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();
|
||||
@@ -376,7 +379,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)
|
||||
@@ -415,6 +418,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)
|
||||
@@ -439,13 +445,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
|
||||
|
||||
Reference in New Issue
Block a user