_request() and _response() refactoring

+ Wip redirection (weird behavior)
+ 505 HTTP Version Not Supported
This commit is contained in:
LuckyLaszlo
2022-08-09 01:46:59 +02:00
parent a44b9b493a
commit 643b09c4f7
8 changed files with 118 additions and 60 deletions

View File

@@ -1,14 +1,37 @@
#include "Webserv.hpp"
enum send_return
{
SEND_IN_PROGRESS, // unused
SEND_COMPLETE,
SEND_CLOSE,
};
void Webserv::_response(Client *client)
{
_send_response(client);
int ret = _send_response(client);
if (g_last_signal)
_handle_last_signal();
if (ret == SEND_CLOSE)
{
_close_client(client->fd);
}
else if (ret == SEND_COMPLETE)
{
if (client->get_headers("Connection") == "close")
_close_client(client->fd);
else
{
_epoll_update(client->fd, EPOLLIN, EPOLL_CTL_MOD);
client->clear();
}
}
}
void Webserv::_send_response(Client *client)
int Webserv::_send_response(Client *client)
{
ssize_t ret;
@@ -27,18 +50,11 @@ void Webserv::_send_response(Client *client)
{
std::perror("err send()");
std::cerr << "client.fd =" << client->fd << "\n"; // DEBUG
_close_client(client->fd);
return ;
return SEND_CLOSE;
}
std::cerr << "ret send() = " << ret << "\n"; // DEBUG
if (client->get_headers("Connection") == "close")
_close_client(client->fd);
else
{
_epoll_update(client->fd, EPOLLIN, EPOLL_CTL_MOD);
client->clear();
}
return SEND_COMPLETE;
}
void Webserv::_append_base_headers(Client *client)
@@ -59,6 +75,24 @@ void Webserv::_construct_response(Client *client)
client->status = 413;
return;
}
/* if (client->assigned_location->redirect_status)
{
// (for codes 301, 302, 303, 307, and 308)
client->status = client->assigned_location->redirect_status;
client->response.append("Location: ");
client->response.append(client->assigned_location->redirect_uri);
client->response.append(CRLF);
} */
if (client->get_path().find("redirect_test") != std::string::npos) // Test block
{ // Weird behavior. The web browser seems to wait for a complete response until timeout.
// (for codes 301, 302, 303, 307, and 308)
client->status = 307;
client->response.append("Location: ");
client->response.append("https://www.rfc-editor.org/rfc/rfc3875#section-3.3");
client->response.append(CRLF);
client->response.append(CRLF);
return ;
}
_process_method(client);
}
@@ -105,7 +139,7 @@ void Webserv::_insert_status_line(Client *client)
void Webserv::_error_html_response(Client *client)
{
if (client->assigned_server->error_pages[client->status].empty())
if (!client->assigned_server || client->assigned_server->error_pages[client->status].empty())
{
std::string html_page = HTML_ERROR;
::replace_all_substr(html_page, STATUS_PLACEHOLDER, _http_status[client->status]);