506 lines
12 KiB
C++
506 lines
12 KiB
C++
|
|
#include "Webserv.hpp"
|
|
|
|
void Webserv::_response(Client *client)
|
|
{
|
|
_send_response(client);
|
|
if (g_last_signal)
|
|
_handle_last_signal();
|
|
}
|
|
|
|
void Webserv::_send_response(Client *client)
|
|
{
|
|
ssize_t ret;
|
|
|
|
std::cerr << "send()\n";
|
|
|
|
_append_base_headers(client);
|
|
if (!client->status)
|
|
_construct_response(client);
|
|
_insert_status_line(client);
|
|
if (client->status >= 400)
|
|
_error_html_response(client);
|
|
|
|
std::cerr << "client->response.size() = " << client->response.size() << "\n"; // DEBUG
|
|
ret = ::send(client->fd, client->response.c_str(), client->response.size(), 0);
|
|
if (ret == -1)
|
|
{
|
|
std::perror("err send()");
|
|
std::cerr << "client.fd =" << client->fd << "\n"; // DEBUG
|
|
_close_client(client->fd);
|
|
return ;
|
|
}
|
|
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();
|
|
}
|
|
}
|
|
|
|
void Webserv::_append_base_headers(Client *client)
|
|
{
|
|
client->response.append("Server: Webserv/0.1" CRLF);
|
|
|
|
if (client->get_headers("Connection") == "close")
|
|
client->response.append("Connection: close" CRLF);
|
|
else
|
|
client->response.append("Connection: keep-alive" CRLF);
|
|
}
|
|
|
|
void Webserv::_construct_response(Client *client)
|
|
{
|
|
// TODO : Move this in read(), stop read if content too large
|
|
if (client->get_body().size() > client->assigned_server->client_body_limit)
|
|
{
|
|
client->status = 413;
|
|
return;
|
|
}
|
|
_process_method(client);
|
|
}
|
|
|
|
void Webserv::_process_method(Client *client)
|
|
{
|
|
unsigned int allow_methods = ANY_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 ANY_METHODS
|
|
|
|
if (client->get_method() == UNKNOWN)
|
|
{
|
|
client->status = 501;
|
|
}
|
|
else if (allow_methods & client->get_method())
|
|
{
|
|
switch (client->get_method())
|
|
{
|
|
case (GET):
|
|
_get(client); break;
|
|
case (POST):
|
|
_post(client); break;
|
|
case (DELETE):
|
|
_delete(client); break;
|
|
default:
|
|
break;
|
|
}
|
|
}
|
|
else
|
|
{
|
|
client->status = 405;
|
|
client->response.append("Allow: ");
|
|
client->response.append(::http_methods_to_str(allow_methods));
|
|
client->response.append(CRLF);
|
|
}
|
|
}
|
|
|
|
void Webserv::_insert_status_line(Client *client)
|
|
{
|
|
std::string status_line;
|
|
|
|
status_line.append("HTTP/1.1 ");
|
|
status_line.append(_http_status[client->status]);
|
|
status_line.append(CRLF);
|
|
client->response.insert(0, status_line);
|
|
}
|
|
|
|
void Webserv::_error_html_response(Client *client)
|
|
{
|
|
if (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]);
|
|
_append_body(client, html_page, "html");
|
|
}
|
|
else
|
|
_get_file(client, client->assigned_server->error_pages[client->status]);
|
|
}
|
|
|
|
//#define INDEX "index.html" // temp wip
|
|
void Webserv::_get(Client *client)
|
|
{
|
|
/* RULES **
|
|
|
|
if path is a valid dir check if index is specified and serve that
|
|
if no index and autoindex, server that
|
|
if file, server that!
|
|
|
|
WHere does cgi fit in in all this ???
|
|
|
|
THIS NEEDS WORK...
|
|
|
|
*/
|
|
(void)server; // To remove from arg if we determine its useless
|
|
std::string path = client->get_path();
|
|
|
|
/* if (path == "/") // TODO : index and autoindex
|
|
path.append(INDEX);
|
|
<<<<<<< HEAD
|
|
path.insert(0, location.root);
|
|
*/
|
|
path.insert(0, client->assigned_location->root);
|
|
|
|
// that was actually a horrible idea...
|
|
// path.insert(0, location.root);
|
|
std::cerr << "path = " << path << "\n";
|
|
|
|
// path = root + location.path
|
|
// we will tack on an index if there is a valid one
|
|
// or autoindex if allowed
|
|
// or let _get_file sort out the error otherwise.
|
|
|
|
if (path_is_valid(path) == 1)
|
|
{
|
|
std::cout << "path is valid\n";
|
|
if (path[path.size() - 1] != '/')
|
|
path.push_back('/');
|
|
for (size_t i = 0; i < location.index.size(); i++)
|
|
{
|
|
std::cout << "location path: " << location.path << '\n';
|
|
std::cout << "location index: " << location.index[i] << '\n';
|
|
// std::cout << "path with index: " << path + location.index[i] << '\n';
|
|
if (path_is_valid(path + location.index[i]) == 2)
|
|
{
|
|
std::cout << "found a valid index\n";
|
|
path.append(location.index[i]);
|
|
break ; // what if index and autoindex in a single location?
|
|
// does this completely fail?
|
|
// do this instead of break?
|
|
//_get_file(client, path);
|
|
}
|
|
}
|
|
if (location.autoindex == true)
|
|
{
|
|
_autoindex(client, location, path);
|
|
return ;
|
|
}
|
|
}
|
|
// else
|
|
// _get_file(client, path);
|
|
// what about cgi ???
|
|
|
|
|
|
|
|
// TMP HUGO
|
|
//
|
|
if (_is_cgi(client))
|
|
{
|
|
_exec_cgi(client);
|
|
return;
|
|
}
|
|
//
|
|
// END TMP HUGO
|
|
|
|
_get_file(client, path);
|
|
}
|
|
|
|
|
|
// i only sort of need &path...
|
|
// def can improve but works for now...
|
|
void Webserv::_autoindex(Client *client, LocationConfig &location, std::string &path)
|
|
{
|
|
std::cout << "made it to _autoindex\n";
|
|
|
|
std::string dir_list;
|
|
DIR *dir;
|
|
struct dirent *ent;
|
|
|
|
std::cout << "location root: " << location.root << " location path: " \
|
|
<< location.path << '\n';
|
|
|
|
// if ((dir = opendir (path.c_str())) != NULL)
|
|
if ((dir = opendir ((location.root + location.path).c_str())) != NULL)
|
|
{
|
|
/* print all the files and directories within directory */
|
|
dir_list.append(AUTOINDEX_START);
|
|
dir_list.append(location.path);
|
|
dir_list.append(AUTOINDEX_MID1);
|
|
dir_list.append(location.path);
|
|
dir_list.append(AUTOINDEX_MID2);
|
|
while ((ent = readdir (dir)) != NULL)
|
|
{
|
|
if (strcmp(".", ent->d_name) == 0)
|
|
continue ;
|
|
dir_list.append("<a href=\"");
|
|
dir_list.append(location.path.c_str());
|
|
dir_list.append(ent->d_name);
|
|
dir_list.append("\">");
|
|
dir_list.append(ent->d_name);
|
|
dir_list.append("</a>");
|
|
dir_list.append("\r\n"); // is this right?
|
|
}
|
|
|
|
// <a href="http://nginx.org/">nginx.org</a>.<br/>
|
|
// <a href="/test/test_deeper/index1.html">index1.html</a>
|
|
|
|
// apparently this is more than good enough!
|
|
// <a href="/test/test_deeper/..">..</a>
|
|
|
|
dir_list.append(AUTOINDEX_END);
|
|
std::cout << "\n\n" << dir_list << '\n';
|
|
closedir (dir);
|
|
_append_body(client, dir_list.c_str(), dir_list.size(), "html");
|
|
}
|
|
else
|
|
{
|
|
// in theory not possible cuz we already checked...
|
|
/* could not open directory */
|
|
// perror ("");
|
|
std::cout << "could not open dir\n";
|
|
return ;
|
|
}
|
|
}
|
|
|
|
|
|
|
|
# define MAX_FILESIZE 1000000 // (1Mo)
|
|
void Webserv::_get_file(Client *client, const std::string &path)
|
|
{
|
|
/*
|
|
std::ios::binary
|
|
https://gcc.gnu.org/onlinedocs/libstdc++/manual/fstreams.html#std.io.filestreams.binary
|
|
tldr : its seems to not be so simple to do read/write of binary file in a portable way.
|
|
*/
|
|
std::ifstream ifd; // For chunk, ifstream directly in struct CLient for multiples read without close() ?
|
|
std::stringstream buf;
|
|
|
|
std::cout << "made it to get_file\n";
|
|
|
|
if (access(path.c_str(), F_OK) == -1)
|
|
{
|
|
std::perror("err access()");
|
|
client->status = 404;
|
|
return ;
|
|
}
|
|
|
|
if (access(path.c_str(), R_OK) == -1)
|
|
{
|
|
std::perror("err access()");
|
|
client->status = 403;
|
|
return ;
|
|
}
|
|
|
|
ifd.open(path.c_str(), std::ios::ate);
|
|
if (!ifd)
|
|
{
|
|
std::cerr << path << ": ifd.open fail" << '\n';
|
|
client->status = 500;
|
|
}
|
|
else
|
|
{
|
|
std::streampos size = ifd.tellg();
|
|
|
|
// WIP : Chunk or not chunk (if filesize too big)
|
|
if (size > MAX_FILESIZE)
|
|
{
|
|
// Then chunk
|
|
client->status = 500; // WIP temp
|
|
std::cerr << "File too large for non chunk body\n";
|
|
return ;
|
|
}
|
|
|
|
ifd.seekg(0, std::ios::beg);
|
|
buf << ifd.rdbuf();
|
|
if (!ifd || !buf)
|
|
{
|
|
std::cerr << path << ": ifd.read fail" << '\n';
|
|
client->status = 500;
|
|
}
|
|
else
|
|
{
|
|
client->status = 200;
|
|
std::string file_ext = _determine_file_extension(path);
|
|
_append_body(client, buf.str(), file_ext);
|
|
}
|
|
}
|
|
}
|
|
|
|
|
|
//void Webserv::_append_body(Client *client, const char *body, size_t body_size, const std::string &file_extension)
|
|
void Webserv::_append_body(Client *client, const std::string &body, const std::string &file_extension)
|
|
{
|
|
const std::string &mime_type = _mime_types[file_extension];
|
|
|
|
client->response.append("Content-Type: ");
|
|
if (mime_type.empty())
|
|
client->response.append(MIME_TYPE_DEFAULT);
|
|
else
|
|
{
|
|
client->response.append(mime_type);
|
|
if (mime_type.find("text/") != std::string::npos)
|
|
client->response.append("; charset=UTF-8");
|
|
}
|
|
client->response.append(CRLF);
|
|
|
|
client->response.append("Content-Length: ");
|
|
std::string tmp = ::itos(body.size());
|
|
client->response.append(tmp);
|
|
client->response.append(CRLF);
|
|
|
|
client->response.append(CRLF);
|
|
client->response.append(body);
|
|
}
|
|
|
|
void Webserv::_post(Client *client)
|
|
{
|
|
/*
|
|
WIP
|
|
https://www.rfc-editor.org/rfc/rfc9110.html#name-post
|
|
*/
|
|
std::string path = client->get_path();
|
|
path.insert(0, client->assigned_location->root);
|
|
|
|
/* CGI Here ? */
|
|
|
|
_post_file(client, path);
|
|
}
|
|
|
|
void Webserv::_post_file(Client *client, const std::string &path)
|
|
{
|
|
std::ofstream ofd;
|
|
|
|
bool file_existed;
|
|
if (access(path.c_str(), F_OK) == -1)
|
|
file_existed = false;
|
|
else
|
|
file_existed = true;
|
|
|
|
// How to determine status 403 for file that dont already exist ?
|
|
if (file_existed && access(path.c_str(), W_OK) == -1)
|
|
{
|
|
std::perror("err access()");
|
|
client->status = 403;
|
|
return ;
|
|
}
|
|
|
|
ofd.open(path.c_str(), std::ios::trunc);
|
|
if (!ofd)
|
|
{
|
|
std::cerr << path << ": ofd.open fail" << '\n';
|
|
client->status = 500;
|
|
}
|
|
else
|
|
{
|
|
// Content-Length useless at this point ?
|
|
// Maybe usefull in _read_request() for rejecting too big content.
|
|
// Need to _determine_process_server() as soon as possible,
|
|
// like in _read_request() for stopping read if body is too big ?
|
|
ofd << client->get_body();
|
|
if (!ofd)
|
|
{
|
|
std::cerr << path << ": ofd.write fail" << '\n';
|
|
client->status = 500;
|
|
}
|
|
else if (file_existed)
|
|
{
|
|
client->status = 200;
|
|
// WIP https://www.rfc-editor.org/rfc/rfc9110.html#name-200-ok
|
|
}
|
|
else
|
|
{
|
|
client->status = 201;
|
|
// WIP https://www.rfc-editor.org/rfc/rfc9110.html#section-9.3.3-4
|
|
}
|
|
}
|
|
}
|
|
|
|
void Webserv::_delete(Client *client)
|
|
{
|
|
/*
|
|
WIP
|
|
https://www.rfc-editor.org/rfc/rfc9110.html#name-delete
|
|
*/
|
|
std::string path = client->get_path();
|
|
path.insert(0, client->assigned_location->root);
|
|
|
|
/* CGI Here ? */
|
|
|
|
_delete_file(client, path);
|
|
}
|
|
|
|
void Webserv::_delete_file(Client *client, const std::string &path)
|
|
{
|
|
if (access(path.c_str(), F_OK) == -1)
|
|
{
|
|
std::perror("err access()");
|
|
client->status = 404;
|
|
return ;
|
|
}
|
|
|
|
if (access(path.c_str(), W_OK) == -1)
|
|
{
|
|
std::perror("err access()");
|
|
client->status = 403;
|
|
return ;
|
|
}
|
|
|
|
if (remove(path.c_str()) == -1)
|
|
{
|
|
std::perror("err remove()");
|
|
client->status = 500;
|
|
return ;
|
|
}
|
|
}
|
|
|
|
ServerConfig *Webserv::_determine_process_server(Client *client)
|
|
{
|
|
/*
|
|
http://nginx.org/en/docs/http/request_processing.html
|
|
_determine_process_server() should be complete.
|
|
TODO : test it
|
|
*/
|
|
|
|
std::string &server_name = client->get_headers("Host");
|
|
std::vector<ServerConfig>::iterator it = _servers.begin();
|
|
std::vector<ServerConfig>::iterator default_server = _servers.end();
|
|
|
|
while (it != _servers.end())
|
|
{
|
|
if (it->host == client->lsocket->host
|
|
&& it->port == client->lsocket->port)
|
|
{
|
|
if ( std::find(it->server_name.begin(), it->server_name.end(), server_name) != it->server_name.end() )
|
|
break;
|
|
else if (default_server == _servers.end())
|
|
default_server = it;
|
|
}
|
|
++it;
|
|
}
|
|
if (it != _servers.end())
|
|
return (&(*it));
|
|
else
|
|
return (&(*default_server));
|
|
}
|
|
|
|
const LocationConfig *Webserv::_determine_location(const ServerConfig &server, const std::string &path) const
|
|
{
|
|
// std::cout << "determin location path sent: " << path << '\n';
|
|
|
|
std::vector<LocationConfig>::const_iterator it = server.locations.begin();
|
|
while (it != server.locations.end())
|
|
{
|
|
// std::cout << it->path << " -- ";
|
|
// if (it->path.compare(0, path.size(), path) == 0)
|
|
if (it->path.compare(0, it->path.size(), path) == 0)
|
|
break;
|
|
// kinda gross i know but... have to deal with when there's a / at the end
|
|
if (it->path[it->path.size() - 1] == '/' \
|
|
&& it->path.compare(0, it->path.size() - 1, path) == 0)
|
|
break;
|
|
++it;
|
|
}
|
|
if (it != server.locations.end())
|
|
return (&(*it));
|
|
else
|
|
return (&(server.locations.front()));
|
|
}
|
|
|
|
std::string Webserv::_determine_file_extension(const std::string &path) const
|
|
{
|
|
size_t dot_pos = path.rfind(".");
|
|
if (dot_pos != std::string::npos && dot_pos + 1 < path.size())
|
|
return ( path.substr(dot_pos + 1) );
|
|
return (std::string(""));
|
|
}
|