fixed response.append(body) problem.
+ File I/O working but maybe not perfect (it seems complicated to do it with std::filebuf) + fixed Content-Type for unknown file extension
This commit is contained in:
@@ -1,13 +1,11 @@
|
|||||||
|
|
||||||
#include "Client.hpp"
|
#include "Client.hpp"
|
||||||
|
|
||||||
char Client::buf[MAX_FILESIZE+1];
|
|
||||||
|
|
||||||
/*********************************************
|
/*********************************************
|
||||||
* COPLIENS
|
* COPLIENS
|
||||||
*********************************************/
|
*********************************************/
|
||||||
|
|
||||||
Client::Client() : fd(0), body_size(0), status(0) {
|
Client::Client() : fd(0), status(0) {
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -56,7 +54,6 @@ void Client::clear()
|
|||||||
clear_request();
|
clear_request();
|
||||||
raw_request.clear();
|
raw_request.clear();
|
||||||
response.clear();
|
response.clear();
|
||||||
body_size = 0;
|
|
||||||
status = 0;
|
status = 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -17,7 +17,6 @@ struct Request
|
|||||||
std::string body;
|
std::string body;
|
||||||
};
|
};
|
||||||
|
|
||||||
# define MAX_FILESIZE 1000000 // (1Mo)
|
|
||||||
class Client
|
class Client
|
||||||
{
|
{
|
||||||
public:
|
public:
|
||||||
@@ -29,8 +28,6 @@ class Client
|
|||||||
int fd;
|
int fd;
|
||||||
std::string raw_request;
|
std::string raw_request;
|
||||||
std::string response;
|
std::string response;
|
||||||
static char buf[MAX_FILESIZE+1];
|
|
||||||
size_t body_size;
|
|
||||||
unsigned int status;
|
unsigned int status;
|
||||||
listen_socket *lsocket;
|
listen_socket *lsocket;
|
||||||
|
|
||||||
|
|||||||
@@ -38,6 +38,8 @@ void signal_handler(int signum);
|
|||||||
# define FAILURE -1
|
# define FAILURE -1
|
||||||
# define SUCCESS 1
|
# define SUCCESS 1
|
||||||
|
|
||||||
|
# define MIME_TYPE_DEFAULT "application/octet-stream"
|
||||||
|
|
||||||
class Webserv
|
class Webserv
|
||||||
{
|
{
|
||||||
public:
|
public:
|
||||||
@@ -78,7 +80,7 @@ class Webserv
|
|||||||
void _process_method(Client *client, ServerConfig &server, LocationConfig &location);
|
void _process_method(Client *client, ServerConfig &server, LocationConfig &location);
|
||||||
void _insert_status_line(Client *client);
|
void _insert_status_line(Client *client);
|
||||||
void _error_html_response(Client *client, ServerConfig &server);
|
void _error_html_response(Client *client, ServerConfig &server);
|
||||||
void _append_body(Client *client, const char *body, size_t body_size, const std::string &file_extension = "");
|
void _append_body(Client *client, const std::string &body, const std::string &file_extension = "");
|
||||||
|
|
||||||
void _get(Client *client, ServerConfig &server, LocationConfig &location);
|
void _get(Client *client, ServerConfig &server, LocationConfig &location);
|
||||||
void _get_file(Client *client, const std::string &path);
|
void _get_file(Client *client, const std::string &path);
|
||||||
@@ -90,7 +92,8 @@ class Webserv
|
|||||||
void _delete_file(Client *client, const std::string &path);
|
void _delete_file(Client *client, const std::string &path);
|
||||||
|
|
||||||
ServerConfig &_determine_process_server(Client *client);
|
ServerConfig &_determine_process_server(Client *client);
|
||||||
LocationConfig &_determine_location(ServerConfig &server, std::string &path);
|
LocationConfig &_determine_location(ServerConfig &server, const std::string &path);
|
||||||
|
std::string _determine_file_extension(const std::string &path) const;
|
||||||
// cgi_script.cpp
|
// cgi_script.cpp
|
||||||
bool _is_cgi(Client *client);
|
bool _is_cgi(Client *client);
|
||||||
void _exec_cgi(Client *client);
|
void _exec_cgi(Client *client);
|
||||||
|
|||||||
@@ -105,7 +105,10 @@ void Webserv::_init_http_status_map()
|
|||||||
|
|
||||||
void Webserv::_init_mime_types_map()
|
void Webserv::_init_mime_types_map()
|
||||||
{
|
{
|
||||||
_mime_types[""] = "application/octet-stream";
|
/*
|
||||||
|
** From : http://nginx.org/en/docs/http/ngx_http_core_module.html#types
|
||||||
|
*/
|
||||||
|
_mime_types[""] = MIME_TYPE_DEFAULT;
|
||||||
|
|
||||||
_mime_types["html"] = "text/html";
|
_mime_types["html"] = "text/html";
|
||||||
_mime_types["htm"] = "text/html";
|
_mime_types["htm"] = "text/html";
|
||||||
|
|||||||
@@ -23,6 +23,7 @@ void Webserv::_send_response(Client *client, ServerConfig &server)
|
|||||||
if (client->status >= 400)
|
if (client->status >= 400)
|
||||||
_error_html_response(client, server);
|
_error_html_response(client, server);
|
||||||
|
|
||||||
|
std::cerr << "client->response.size() = " << client->response.size() << "\n"; // DEBUG
|
||||||
ret = ::send(client->fd, client->response.c_str(), client->response.size(), 0);
|
ret = ::send(client->fd, client->response.c_str(), client->response.size(), 0);
|
||||||
if (ret == -1)
|
if (ret == -1)
|
||||||
{
|
{
|
||||||
@@ -31,19 +32,7 @@ void Webserv::_send_response(Client *client, ServerConfig &server)
|
|||||||
_close_client(client->fd);
|
_close_client(client->fd);
|
||||||
return ;
|
return ;
|
||||||
}
|
}
|
||||||
|
std::cerr << "ret send() = " << ret << "\n"; // DEBUG
|
||||||
// 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 ;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
if (client->get_headers("Connection") == "close")
|
if (client->get_headers("Connection") == "close")
|
||||||
_close_client(client->fd);
|
_close_client(client->fd);
|
||||||
@@ -125,7 +114,7 @@ void Webserv::_error_html_response(Client *client, ServerConfig &server)
|
|||||||
{
|
{
|
||||||
std::string html_page = HTML_ERROR;
|
std::string html_page = HTML_ERROR;
|
||||||
::replace_all_substr(html_page, STATUS_PLACEHOLDER, _http_status[client->status]);
|
::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
|
else
|
||||||
_get_file(client, server.error_pages[client->status]);
|
_get_file(client, server.error_pages[client->status]);
|
||||||
@@ -156,10 +145,16 @@ void Webserv::_get(Client *client, ServerConfig &server, LocationConfig &locatio
|
|||||||
_get_file(client, path);
|
_get_file(client, path);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
# define MAX_FILESIZE 1000000 // (1Mo)
|
||||||
void Webserv::_get_file(Client *client, const std::string &path)
|
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::ifstream ifd; // For chunk, ifstream directly in struct CLient for multiples read without close() ?
|
||||||
// char buf[MAX_FILESIZE+1];
|
std::stringstream buf;
|
||||||
|
|
||||||
if (access(path.c_str(), F_OK) == -1)
|
if (access(path.c_str(), F_OK) == -1)
|
||||||
{
|
{
|
||||||
@@ -175,7 +170,7 @@ void Webserv::_get_file(Client *client, const std::string &path)
|
|||||||
return ;
|
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)
|
if (!ifd)
|
||||||
{
|
{
|
||||||
std::cerr << path << ": ifd.open fail" << '\n';
|
std::cerr << path << ": ifd.open fail" << '\n';
|
||||||
@@ -196,8 +191,8 @@ void Webserv::_get_file(Client *client, const std::string &path)
|
|||||||
}
|
}
|
||||||
|
|
||||||
ifd.seekg(0, std::ios::beg);
|
ifd.seekg(0, std::ios::beg);
|
||||||
ifd.read(client->buf, size);
|
buf << ifd.rdbuf();
|
||||||
if (!ifd)
|
if (!ifd || !buf)
|
||||||
{
|
{
|
||||||
std::cerr << path << ": ifd.read fail" << '\n';
|
std::cerr << path << ": ifd.read fail" << '\n';
|
||||||
client->status = 500;
|
client->status = 500;
|
||||||
@@ -205,42 +200,31 @@ void Webserv::_get_file(Client *client, const std::string &path)
|
|||||||
else
|
else
|
||||||
{
|
{
|
||||||
client->status = 200;
|
client->status = 200;
|
||||||
client->buf[ifd.gcount()] = '\0';
|
std::string file_ext = _determine_file_extension(path);
|
||||||
|
_append_body(client, buf.str(), file_ext);
|
||||||
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);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
ifd.close();
|
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 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];
|
const std::string &mime_type = _mime_types[file_extension];
|
||||||
|
|
||||||
client->response.append("Content-Type: ");
|
client->response.append("Content-Type: ");
|
||||||
|
if (mime_type.empty())
|
||||||
|
client->response.append(MIME_TYPE_DEFAULT);
|
||||||
|
else
|
||||||
|
{
|
||||||
client->response.append(mime_type);
|
client->response.append(mime_type);
|
||||||
if (mime_type.find("text/") != std::string::npos)
|
if (mime_type.find("text/") != std::string::npos)
|
||||||
client->response.append("; charset=UTF-8");
|
client->response.append("; charset=UTF-8");
|
||||||
|
}
|
||||||
client->response.append(CRLF);
|
client->response.append(CRLF);
|
||||||
|
|
||||||
client->response.append("Content-Length: ");
|
client->response.append("Content-Length: ");
|
||||||
std::string tmp = ::itos(body_size);
|
std::string tmp = ::itos(body.size());
|
||||||
client->response.append(tmp);
|
client->response.append(tmp);
|
||||||
client->response.append(CRLF);
|
client->response.append(CRLF);
|
||||||
|
|
||||||
@@ -281,7 +265,7 @@ void Webserv::_post_file(Client *client, const std::string &path)
|
|||||||
return ;
|
return ;
|
||||||
}
|
}
|
||||||
|
|
||||||
ofd.open(path.c_str(), std::ios::binary | std::ios::trunc);
|
ofd.open(path.c_str(), std::ios::trunc);
|
||||||
if (!ofd)
|
if (!ofd)
|
||||||
{
|
{
|
||||||
std::cerr << path << ": ofd.open fail" << '\n';
|
std::cerr << path << ": ofd.open fail" << '\n';
|
||||||
@@ -289,11 +273,11 @@ void Webserv::_post_file(Client *client, const std::string &path)
|
|||||||
}
|
}
|
||||||
else
|
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.
|
// Maybe usefull in _read_request() for rejecting too big content.
|
||||||
// Need to _determine_process_server() as soon as possible,
|
// Need to _determine_process_server() as soon as possible,
|
||||||
// like in _read_request() for stopping read if body is too big ?
|
// 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)
|
if (!ofd)
|
||||||
{
|
{
|
||||||
std::cerr << path << ": ofd.write fail" << '\n';
|
std::cerr << path << ": ofd.write fail" << '\n';
|
||||||
@@ -383,7 +367,7 @@ ServerConfig &Webserv::_determine_process_server(Client *client)
|
|||||||
return (*default_server);
|
return (*default_server);
|
||||||
}
|
}
|
||||||
|
|
||||||
LocationConfig &Webserv::_determine_location(ServerConfig &server, std::string &path)
|
LocationConfig &Webserv::_determine_location(ServerConfig &server, const std::string &path)
|
||||||
{
|
{
|
||||||
/*
|
/*
|
||||||
Assume there is at least one location in vector<LocationConfig> for path "/"
|
Assume there is at least one location in vector<LocationConfig> for path "/"
|
||||||
@@ -404,3 +388,11 @@ LocationConfig &Webserv::_determine_location(ServerConfig &server, std::string &
|
|||||||
else
|
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