successful merge?
This commit is contained in:
@@ -3,26 +3,25 @@
|
||||
|
||||
void Webserv::_response(Client *client)
|
||||
{
|
||||
client->status = 200; // default value
|
||||
|
||||
ServerConfig &server = _determine_process_server(client);
|
||||
_send_response(client, server);
|
||||
_send_response(client);
|
||||
if (g_last_signal)
|
||||
_handle_last_signal();
|
||||
}
|
||||
|
||||
void Webserv::_send_response(Client *client, ServerConfig &server)
|
||||
void Webserv::_send_response(Client *client)
|
||||
{
|
||||
ssize_t ret;
|
||||
|
||||
std::cerr << "send()\n";
|
||||
|
||||
_append_base_headers(client);
|
||||
_construct_response(client, server);
|
||||
if (!client->status)
|
||||
_construct_response(client);
|
||||
_insert_status_line(client);
|
||||
if (client->status >= 400)
|
||||
_error_html_response(client, server);
|
||||
_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)
|
||||
{
|
||||
@@ -31,19 +30,7 @@ void Webserv::_send_response(Client *client, ServerConfig &server)
|
||||
_close_client(client->fd);
|
||||
return ;
|
||||
}
|
||||
|
||||
// Body send (WIP for working binary files)
|
||||
if (client->body_size)
|
||||
{
|
||||
ret = ::send(client->fd, client->buf, client->body_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);
|
||||
@@ -64,19 +51,18 @@ void Webserv::_append_base_headers(Client *client)
|
||||
client->response.append("Connection: keep-alive" CRLF);
|
||||
}
|
||||
|
||||
void Webserv::_construct_response(Client *client, ServerConfig &server)
|
||||
void Webserv::_construct_response(Client *client)
|
||||
{
|
||||
// TODO : Move this in read(), stop read if content too large
|
||||
if (client->get_body().size() > server.client_body_limit)
|
||||
if (client->get_body().size() > client->assigned_server->client_body_limit)
|
||||
{
|
||||
client->status = 413;
|
||||
return;
|
||||
}
|
||||
LocationConfig &location = _determine_location(server, client->get_path());
|
||||
_process_method(client, server, location);
|
||||
_process_method(client);
|
||||
}
|
||||
|
||||
void Webserv::_process_method(Client *client, ServerConfig &server, LocationConfig &location)
|
||||
void Webserv::_process_method(Client *client)
|
||||
{
|
||||
unsigned int allow_methods = ANY_METHODS; // TEMP VARIABLE
|
||||
// after update in ConfigParser, use the "allow_methods" of location.
|
||||
@@ -91,11 +77,11 @@ void Webserv::_process_method(Client *client, ServerConfig &server, LocationConf
|
||||
switch (client->get_method())
|
||||
{
|
||||
case (GET):
|
||||
_get(client, server, location); break;
|
||||
_get(client); break;
|
||||
case (POST):
|
||||
_post(client, server, location); break;
|
||||
_post(client); break;
|
||||
case (DELETE):
|
||||
_delete(client, server, location); break;
|
||||
_delete(client); break;
|
||||
default:
|
||||
break;
|
||||
}
|
||||
@@ -119,20 +105,20 @@ void Webserv::_insert_status_line(Client *client)
|
||||
client->response.insert(0, status_line);
|
||||
}
|
||||
|
||||
void Webserv::_error_html_response(Client *client, ServerConfig &server)
|
||||
void Webserv::_error_html_response(Client *client)
|
||||
{
|
||||
if (server.error_pages[client->status].empty())
|
||||
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.c_str(), html_page.size(), "html");
|
||||
_append_body(client, html_page, "html");
|
||||
}
|
||||
else
|
||||
_get_file(client, server.error_pages[client->status]);
|
||||
_get_file(client, client->assigned_server->error_pages[client->status]);
|
||||
}
|
||||
|
||||
#define INDEX "index.html" // temp wip
|
||||
void Webserv::_get(Client *client, ServerConfig &server, LocationConfig &location)
|
||||
//#define INDEX "index.html" // temp wip
|
||||
void Webserv::_get(Client *client)
|
||||
{
|
||||
/* RULES **
|
||||
|
||||
@@ -150,11 +136,13 @@ THIS NEEDS WORK...
|
||||
|
||||
/* 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);
|
||||
// path.insert(0, location.root);
|
||||
std::cerr << "path = " << path << "\n";
|
||||
|
||||
// path = root + location.path
|
||||
@@ -266,10 +254,16 @@ void Webserv::_autoindex(Client *client, LocationConfig &location, std::string &
|
||||
|
||||
|
||||
|
||||
# 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];
|
||||
/*
|
||||
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";
|
||||
|
||||
@@ -287,7 +281,7 @@ void Webserv::_get_file(Client *client, const std::string &path)
|
||||
return ;
|
||||
}
|
||||
|
||||
ifd.open(path.c_str(), std::ios::binary | std::ios::ate); // std::ios::binary (binary for files like images ?)
|
||||
ifd.open(path.c_str(), std::ios::ate);
|
||||
if (!ifd)
|
||||
{
|
||||
std::cerr << path << ": ifd.open fail" << '\n';
|
||||
@@ -303,13 +297,12 @@ void Webserv::_get_file(Client *client, const std::string &path)
|
||||
// Then chunk
|
||||
client->status = 500; // WIP temp
|
||||
std::cerr << "File too large for non chunk body\n";
|
||||
ifd.close();
|
||||
return ;
|
||||
}
|
||||
|
||||
ifd.seekg(0, std::ios::beg);
|
||||
ifd.read(client->buf, size);
|
||||
if (!ifd)
|
||||
buf << ifd.rdbuf();
|
||||
if (!ifd || !buf)
|
||||
{
|
||||
std::cerr << path << ": ifd.read fail" << '\n';
|
||||
client->status = 500;
|
||||
@@ -317,59 +310,46 @@ void Webserv::_get_file(Client *client, const std::string &path)
|
||||
else
|
||||
{
|
||||
client->status = 200;
|
||||
client->buf[ifd.gcount()] = '\0';
|
||||
|
||||
std::string file_ext = "";
|
||||
size_t dot_pos = path.rfind(".");
|
||||
std::cerr << "dot_pos = " << dot_pos << "\n";
|
||||
if (dot_pos != std::string::npos && dot_pos + 1 < path.size())
|
||||
file_ext = path.substr(dot_pos + 1);
|
||||
std::cerr << "file_ext = " << file_ext << "\n";
|
||||
|
||||
client->body_size = ifd.gcount();
|
||||
// WIP, pass empty body argument because append to string mess up binary file
|
||||
_append_body(client, "", client->body_size, file_ext);
|
||||
std::string file_ext = _determine_file_extension(path);
|
||||
_append_body(client, buf.str(), file_ext);
|
||||
}
|
||||
|
||||
ifd.close();
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
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 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)
|
||||
{
|
||||
/*
|
||||
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.
|
||||
*/
|
||||
const std::string &mime_type = _mime_types[file_extension];
|
||||
client->response.append("Content-Type: ");
|
||||
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(CRLF);
|
||||
|
||||
client->response.append("Content-Length: ");
|
||||
std::string tmp = ::itos(body_size);
|
||||
client->response.append(tmp);
|
||||
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);
|
||||
client->response.append(CRLF);
|
||||
client->response.append(body);
|
||||
}
|
||||
|
||||
void Webserv::_post(Client *client, ServerConfig &server, LocationConfig &location)
|
||||
void Webserv::_post(Client *client)
|
||||
{
|
||||
(void)server; // To remove from arg if we determine its useless
|
||||
/*
|
||||
WIP
|
||||
https://www.rfc-editor.org/rfc/rfc9110.html#name-post
|
||||
*/
|
||||
std::string path = client->get_path();
|
||||
path.insert(0, location.root);
|
||||
path.insert(0, client->assigned_location->root);
|
||||
|
||||
/* CGI Here ? */
|
||||
|
||||
@@ -394,7 +374,7 @@ void Webserv::_post_file(Client *client, const std::string &path)
|
||||
return ;
|
||||
}
|
||||
|
||||
ofd.open(path.c_str(), std::ios::binary | std::ios::trunc);
|
||||
ofd.open(path.c_str(), std::ios::trunc);
|
||||
if (!ofd)
|
||||
{
|
||||
std::cerr << path << ": ofd.open fail" << '\n';
|
||||
@@ -402,11 +382,11 @@ void Webserv::_post_file(Client *client, const std::string &path)
|
||||
}
|
||||
else
|
||||
{
|
||||
// Used body.size() so Content-Length useless at this point ?
|
||||
// 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.write(client->get_body().c_str(), client->get_body().size());
|
||||
ofd << client->get_body();
|
||||
if (!ofd)
|
||||
{
|
||||
std::cerr << path << ": ofd.write fail" << '\n';
|
||||
@@ -422,20 +402,17 @@ void Webserv::_post_file(Client *client, const std::string &path)
|
||||
client->status = 201;
|
||||
// WIP https://www.rfc-editor.org/rfc/rfc9110.html#section-9.3.3-4
|
||||
}
|
||||
|
||||
ofd.close();
|
||||
}
|
||||
}
|
||||
|
||||
void Webserv::_delete(Client *client, ServerConfig &server, LocationConfig &location)
|
||||
void Webserv::_delete(Client *client)
|
||||
{
|
||||
(void)server; // To remove from arg if we determine its useless
|
||||
/*
|
||||
WIP
|
||||
https://www.rfc-editor.org/rfc/rfc9110.html#name-delete
|
||||
*/
|
||||
std::string path = client->get_path();
|
||||
path.insert(0, location.root);
|
||||
path.insert(0, client->assigned_location->root);
|
||||
|
||||
/* CGI Here ? */
|
||||
|
||||
@@ -466,7 +443,7 @@ void Webserv::_delete_file(Client *client, const std::string &path)
|
||||
}
|
||||
}
|
||||
|
||||
ServerConfig &Webserv::_determine_process_server(Client *client)
|
||||
ServerConfig *Webserv::_determine_process_server(Client *client)
|
||||
{
|
||||
/*
|
||||
http://nginx.org/en/docs/http/request_processing.html
|
||||
@@ -491,16 +468,16 @@ ServerConfig &Webserv::_determine_process_server(Client *client)
|
||||
++it;
|
||||
}
|
||||
if (it != _servers.end())
|
||||
return (*it);
|
||||
return (&(*it));
|
||||
else
|
||||
return (*default_server);
|
||||
return (&(*default_server));
|
||||
}
|
||||
|
||||
LocationConfig &Webserv::_determine_location(ServerConfig &server, std::string &path)
|
||||
const LocationConfig *Webserv::_determine_location(const ServerConfig &server, const std::string &path) const
|
||||
{
|
||||
// std::cout << "determin location path sent: " << path << '\n';
|
||||
|
||||
std::vector<LocationConfig>::iterator it = server.locations.begin();
|
||||
std::vector<LocationConfig>::const_iterator it = server.locations.begin();
|
||||
while (it != server.locations.end())
|
||||
{
|
||||
// std::cout << it->path << " -- ";
|
||||
@@ -514,7 +491,15 @@ LocationConfig &Webserv::_determine_location(ServerConfig &server, std::string &
|
||||
++it;
|
||||
}
|
||||
if (it != server.locations.end())
|
||||
return (*it);
|
||||
return (&(*it));
|
||||
else
|
||||
return (server.locations.front());
|
||||
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(""));
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user