default and custom error pages response
+ refactoring in response.cpp (functions split) + added Client::clear() + added replace_all_substr() in utils.cpp
This commit is contained in:
@@ -3,6 +3,8 @@
|
||||
|
||||
void Webserv::_response(Client *client)
|
||||
{
|
||||
client->status = 200; // default value
|
||||
|
||||
ServerConfig &server = _determine_process_server(client);
|
||||
_send_response(client, server);
|
||||
if (g_last_signal)
|
||||
@@ -14,9 +16,12 @@ void Webserv::_send_response(Client *client, ServerConfig &server)
|
||||
ssize_t ret;
|
||||
|
||||
std::cerr << "send()\n";
|
||||
// std::cerr << "RAW_REQUEST\n|\n" << client->raw_request << "|\n"; // DEBUG
|
||||
|
||||
_append_base_headers(client);
|
||||
_construct_response(client, server);
|
||||
_insert_status_line(client);
|
||||
if (client->status >= 400)
|
||||
_error_html_response(client, server);
|
||||
|
||||
ret = ::send(client->fd, client->response.c_str(), client->response.size(), 0);
|
||||
if (ret == -1)
|
||||
@@ -27,30 +32,38 @@ void Webserv::_send_response(Client *client, ServerConfig &server)
|
||||
return ;
|
||||
}
|
||||
|
||||
if (client->raw_request.find("Connection: close") != std::string::npos)
|
||||
if (client->get_headers("Connection") == "close")
|
||||
_close_client(client->fd);
|
||||
else
|
||||
{
|
||||
_epoll_update(client->fd, EPOLLIN, EPOLL_CTL_MOD);
|
||||
client->raw_request.clear();
|
||||
client->response.clear();
|
||||
client->clear();
|
||||
}
|
||||
}
|
||||
|
||||
void Webserv::_construct_response(Client *client, ServerConfig &server)
|
||||
void Webserv::_append_base_headers(Client *client)
|
||||
{
|
||||
LocationConfig &location = _determine_location(server, client->get_path());
|
||||
|
||||
client->status = 200; // default value
|
||||
|
||||
client->response.append("Server: Webserv/0.1\r\n");
|
||||
|
||||
if (client->get_headers("Connection") == "close")
|
||||
client->response.append("Connection: close\r\n");
|
||||
else
|
||||
client->response.append("Connection: keep-alive\r\n");
|
||||
}
|
||||
|
||||
void Webserv::_construct_response(Client *client, ServerConfig &server)
|
||||
{
|
||||
if (client->get_body().size() > server.client_body_limit)
|
||||
{
|
||||
client->status = 413;
|
||||
return;
|
||||
}
|
||||
LocationConfig &location = _determine_location(server, client->get_path());
|
||||
_process_method(client, server, location);
|
||||
}
|
||||
|
||||
void Webserv::_process_method(Client *client, ServerConfig &server, LocationConfig &location)
|
||||
{
|
||||
unsigned int allow_methods = ALL_METHODS; // TEMP VARIABLE
|
||||
// after update in ConfigParser, use the "allow_methods" of location.
|
||||
// TODO in ConfigParser : by default if no field in config file, "allow_methods" must be set to ALL_METHODS
|
||||
@@ -61,12 +74,20 @@ void Webserv::_construct_response(Client *client, ServerConfig &server)
|
||||
}
|
||||
else if (allow_methods & client->get_method())
|
||||
{
|
||||
if (client->get_method() & GET)
|
||||
_get_ressource(client, server, location);
|
||||
else if (client->get_method() & POST)
|
||||
_post(client, server, location);
|
||||
else if (client->get_method() & DELETE)
|
||||
_delete(client, server, location);
|
||||
switch (client->get_method())
|
||||
{
|
||||
case (GET):
|
||||
_get(client, server, location);
|
||||
break;
|
||||
case (POST):
|
||||
_post(client, server, location);
|
||||
break;
|
||||
case (DELETE):
|
||||
_delete(client, server, location);
|
||||
break;
|
||||
default:
|
||||
break;
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
@@ -75,58 +96,38 @@ void Webserv::_construct_response(Client *client, ServerConfig &server)
|
||||
client->response.append(::http_methods_to_str(allow_methods));
|
||||
client->response.append("\r\n");
|
||||
}
|
||||
|
||||
_insert_status_line(client, server);
|
||||
}
|
||||
|
||||
// https://www.iana.org/assignments/http-status-codes/http-status-codes.xhtml
|
||||
#define HTML_ERROR(STATUS) "\r\n<!DOCTYPE html><html><head><title>"STATUS"</title></head><body><h1 style=\"text-align:center\">"STATUS"</h1><hr><p style=\"text-align:center\">Le Webserv/0.1</p></body></html>"
|
||||
#define S200 "200 OK"
|
||||
#define S400 "400 Bad Request"
|
||||
#define S404 "404 Not Found"
|
||||
#define S500 "500 Internal Server Error"
|
||||
void Webserv::_insert_status_line(Client *client, ServerConfig &server)
|
||||
void Webserv::_insert_status_line(Client *client)
|
||||
{
|
||||
std::string status_line;
|
||||
|
||||
status_line.append("HTTP/1.1 ");
|
||||
// WIP, maybe make a map for status response
|
||||
switch (client->status)
|
||||
{
|
||||
case (200):
|
||||
status_line.append(S200);
|
||||
break;
|
||||
case (400):
|
||||
status_line.append(S400);
|
||||
client->response.append(HTML_ERROR(S400));
|
||||
break;
|
||||
case (404):
|
||||
status_line.append(S404);
|
||||
client->response.append(HTML_ERROR(S404));
|
||||
break;
|
||||
case (500):
|
||||
status_line.append(S500);
|
||||
client->response.append(HTML_ERROR(S500));
|
||||
break;
|
||||
}
|
||||
status_line.append(_http_status[client->status]);
|
||||
status_line.append("\r\n");
|
||||
|
||||
client->response.insert(0, status_line);
|
||||
}
|
||||
|
||||
#define ROOT "www"
|
||||
#define INDEX "index.html"
|
||||
#define MAX_FILESIZE 1000000 // (1Mo)
|
||||
void Webserv::_get_ressource(Client *client, ServerConfig &server, LocationConfig &location)
|
||||
void Webserv::_error_html_response(Client *client, ServerConfig &server)
|
||||
{
|
||||
if (server.error_pages[client->status].empty())
|
||||
{
|
||||
std::string html_page = HTML_ERROR;
|
||||
::replace_all_substr(html_page, STATUS_PLACEHOLDER, _http_status[client->status]);
|
||||
_append_body(client, html_page.c_str(), html_page.size());
|
||||
}
|
||||
else
|
||||
_get_file(client, server.error_pages[client->status]);
|
||||
}
|
||||
|
||||
#define INDEX "index.html" // temp wip
|
||||
void Webserv::_get(Client *client, ServerConfig &server, LocationConfig &location)
|
||||
{
|
||||
std::ifstream ifd; // For chunk, ifstream directly in struct CLient for multiples read without close() ?
|
||||
char buf[MAX_FILESIZE+1];
|
||||
std::string tmp;
|
||||
std::string path = client->get_path();
|
||||
|
||||
if (path == "/") // TODO ; With server config
|
||||
if (path == "/") // TODO : index and autoindex
|
||||
path.append(INDEX);
|
||||
path.insert(0, ROOT);
|
||||
path.insert(0, location.root);
|
||||
|
||||
std::cerr << "path = " << path << "\n";
|
||||
|
||||
@@ -140,7 +141,15 @@ void Webserv::_get_ressource(Client *client, ServerConfig &server, LocationConfi
|
||||
//
|
||||
// END TMP HUGO
|
||||
|
||||
|
||||
_get_file(client, path);
|
||||
}
|
||||
|
||||
#define MAX_FILESIZE 1000000 // (1Mo)
|
||||
void Webserv::_get_file(Client *client, const std::string &path)
|
||||
{
|
||||
std::ifstream ifd; // For chunk, ifstream directly in struct CLient for multiples read without close() ?
|
||||
char buf[MAX_FILESIZE+1];
|
||||
|
||||
if (access(path.c_str(), R_OK) == -1)
|
||||
{
|
||||
std::perror("err access()");
|
||||
@@ -171,21 +180,32 @@ void Webserv::_get_ressource(Client *client, ServerConfig &server, LocationConfi
|
||||
ifd.read(buf, size);
|
||||
buf[ifd.gcount()] = '\0';
|
||||
|
||||
client->response.append("Content-Type: text/html; charset=UTF-8\r\n"); // TODO : determine Content-Type
|
||||
|
||||
client->response.append("Content-Length: ");
|
||||
tmp = ::itos(ifd.gcount());
|
||||
client->response.append(tmp);
|
||||
client->response.append("\r\n");
|
||||
|
||||
// Body
|
||||
client->response.append("\r\n");
|
||||
client->response.append(buf);
|
||||
_append_body(client, buf, ifd.gcount());
|
||||
|
||||
ifd.close();
|
||||
}
|
||||
}
|
||||
|
||||
void Webserv::_append_body(Client *client, const char *body, size_t body_size)
|
||||
{
|
||||
/*
|
||||
TODO : determine Content-Type
|
||||
how ? read the body ?
|
||||
or before in other way (like based and file extension) and pass here as argument ?
|
||||
http://nginx.org/en/docs/http/ngx_http_core_module.html#types
|
||||
Need to look "conf/mime.types" of nginx. Maybe make a map<> based on that.
|
||||
*/
|
||||
client->response.append("Content-Type: text/html; charset=UTF-8\r\n");
|
||||
|
||||
client->response.append("Content-Length: ");
|
||||
std::string tmp = ::itos(body_size);
|
||||
client->response.append(tmp);
|
||||
client->response.append("\r\n");
|
||||
|
||||
client->response.append("\r\n");
|
||||
client->response.append(body);
|
||||
}
|
||||
|
||||
void Webserv::_post(Client *client, ServerConfig &server, LocationConfig &location)
|
||||
{
|
||||
/*
|
||||
|
||||
Reference in New Issue
Block a user