Wip chunked decoding

+ Need to test normal body parsing
+ path_is_valid() renamed eval_file_type()
+ replaced atoi with strtol/strtoul
This commit is contained in:
LuckyLaszlo
2022-08-12 05:50:00 +02:00
parent ab0bc2c4c0
commit 400efbe720
18 changed files with 185 additions and 82 deletions

View File

@@ -23,7 +23,7 @@
# include <algorithm> // find
# include <string> // string
# include <cstdio> // perror, remove
# include <cstdlib> // atoi (athough it's already cover by <string>)
# include <cstdlib> // strtol, strtoul
# include <dirent.h> // opendir()
# include "Client.hpp"

View File

@@ -146,7 +146,7 @@ void Webserv::_check_script_status(Client *client, std::string output)
if (pos != std::string::npos)
{
status_pos = pos + std::string("Status:").size();
client->status = atoi(output.c_str() + status_pos);
client->status = std::strtoul(output.c_str() + status_pos, NULL, 10);
::del_line_in_str(&output, pos, CRLF);
}
client->status = 200;

View File

@@ -76,7 +76,7 @@ void Webserv::_reopen_lsocket(std::vector<listen_socket>::iterator it)
// HUGO ADD END
try {
_bind(it->fd, std::atoi(it->port.c_str()), it->host);
_bind(it->fd, std::strtoul(it->port.c_str(), NULL, 10), it->host);
_listen(it->fd, 42); // 42 arbitrary
}
catch (const std::exception& e) {

View File

@@ -43,6 +43,7 @@
# define S405 "405 Method Not Allowed"
# define S408 "408 Request Timeout"
# define S413 "413 Content Too Large"
# define S415 "415 Unsupported Media Type"
# define S500 "500 Internal Server Error"
# define S501 "501 Not Implemented"

View File

@@ -48,7 +48,7 @@ void Webserv::init_virtual_servers(std::vector<ServerConfig>* servers)
//
// HUGO ADD END
_bind(new_socket.fd, std::atoi(it->port.c_str()), it->host);
_bind(new_socket.fd, std::strtoul(it->port.c_str(), NULL, 10), it->host);
_listen(new_socket.fd, 42); // 42 arbitrary
if (_epoll_update(new_socket.fd, EPOLLIN, EPOLL_CTL_ADD) == -1)

View File

@@ -1,7 +1,6 @@
#include "Webserv.hpp"
// TODO : path_is_valid() Macro for return value
void Webserv::_get(Client *client)
{
std::string path = client->get_rq_abs_path();
@@ -30,14 +29,14 @@ void Webserv::_get(Client *client)
// END TMP HUGO
// Index/Autoindex block
if (path_is_valid(path) == IS_DIR)
if (eval_file_type(path) == IS_DIR)
{
std::cout << "made it to Index/Autoindex\n";
if (path[path.size() - 1] != '/')
path.push_back('/');
for (size_t i = 0; i < client->assigned_location->index.size(); i++)
{
if (path_is_valid(path + client->assigned_location->index[i]) == 2)
if (eval_file_type(path + client->assigned_location->index[i]) == IS_FILE)
{
path.append(client->assigned_location->index[i]);
_get_file(client, path);

View File

@@ -25,12 +25,14 @@ void Webserv::_request(Client *client)
}
else if (ret == READ_COMPLETE)
{
if (client->body_complete)
std::cerr << "______BODY\n" << client->get_rq_body() << "\n______\n"; // DEBUG
_epoll_update(client->get_cl_fd(), EPOLLOUT, EPOLL_CTL_MOD);
client->request_complete = true;
}
}
int Webserv::_read_request(Client *client) // Messy, Need refactoring
int Webserv::_read_request(Client *client)
{
char buf[BUFSIZE];
ssize_t ret;
@@ -41,8 +43,6 @@ int Webserv::_read_request(Client *client) // Messy, Need refactoring
if (ret == -1)
{
std::perror("err recv()");
std::cerr << "client ptr =" << client << "\n"; // DEBUG
std::cerr << "client.fd =" << client->get_cl_fd() << "\n"; // DEBUG
return READ_CLOSE;
}
if (ret == 0)
@@ -50,41 +50,32 @@ int Webserv::_read_request(Client *client) // Messy, Need refactoring
std::cerr << "recv() read 0, then close client" << "\n"; // DEBUG
return READ_CLOSE;
}
client->raw_request.append(buf, ret);
if (!client->header_complete)
{
if (client->raw_request.find(CRLF CRLF) != std::string::npos)
client->parse_request(_servers);
if (client->status)
return READ_COMPLETE;
if (client->header_complete)
{
client->header_complete = true;
client->parse_request(_servers);
std::cerr << client->get_rq_method_str() << " " << client->get_rq_uri() << " " << client->get_rq_version() << "\n"; // DEBUG
if (client->status)
return READ_COMPLETE;
if (client->get_rq_headers("Content-Type").empty() && client->get_rq_headers("Content-Length").empty()) // No body case
return READ_COMPLETE;
if (client->get_rq_headers("Content-Type").empty()
&& client->get_rq_headers("Content-Length").empty()
&& client->get_rq_headers("Transfer-Encoding").empty())
return READ_COMPLETE; // No body case
}
else if (client->raw_request.size() > MAX_HEADER_SIZE)
{
// 413 or 400 ? 413 seems common among http server, but don't fit perfectly.
{ // 413 or 400 ? 413 seems common among http server, but don't fit perfectly.
client->status = 413;
return READ_COMPLETE;
}
}
else if (client->header_complete)
{
client->read_body_size += ret;
if (client->read_body_size > client->assigned_server->client_body_limit)
{
client->status = 413;
// client->read_body_size += ret; // Not accurate, part of body could have been read with headers, unused for now
client->parse_request_body();
if (client->status || client->body_complete)
return READ_COMPLETE;
}
if ((int)client->read_body_size >= ::atoi(client->get_rq_headers("Content-Length").c_str()))
{
client->parse_request_body();
return READ_COMPLETE;
}
}
return READ_IN_PROGRESS;

View File

@@ -20,8 +20,9 @@ void Webserv::run()
nfds = ::epoll_wait(_epfd, events, MAX_EVENTS, TIMEOUT);
if (nfds == -1)
{
int errno_copy = errno;
std::perror("err epoll_wait()");
if (errno == EINTR)
if (errno_copy == EINTR)
g_run = false;
else
throw std::runtime_error("Epoll wait");