wip integration of client.request and ServerConfig
+ Client get return reference + wip binary flags for http_method + moved config parser files
This commit is contained in:
@@ -3,25 +3,48 @@
|
||||
|
||||
void Webserv::_response(Client *client)
|
||||
{
|
||||
_send_response(client);
|
||||
ServerConfig &server = _determine_process_server(client);
|
||||
_send_response(client, server);
|
||||
if (g_last_signal)
|
||||
_handle_last_signal();
|
||||
}
|
||||
|
||||
void Webserv::_send_response(Client *client)
|
||||
ServerConfig &Webserv::_determine_process_server(Client *client)
|
||||
{
|
||||
/*
|
||||
TODO : determine virtual server based on ip_address::port and server_name
|
||||
Ior now its just based on server_name.
|
||||
(maybe with a map< string, std::vector<ServerConfig> > where the string is "ip_adress::port")
|
||||
http://nginx.org/en/docs/http/request_processing.html
|
||||
*/
|
||||
|
||||
std::string &server_name = client->get_headers("Host");
|
||||
std::vector<ServerConfig>::iterator it = _servers.begin();
|
||||
while (it != _servers.end())
|
||||
{
|
||||
if ( std::find(it->server_name.begin(), it->server_name.end(), server_name) != it->server_name.end() )
|
||||
break;
|
||||
++it;
|
||||
}
|
||||
if (it != _servers.end())
|
||||
return (*it);
|
||||
else
|
||||
return (_servers.front());
|
||||
}
|
||||
|
||||
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
|
||||
// std::cerr << "RAW_REQUEST\n|\n" << client->raw_request << "|\n"; // DEBUG
|
||||
|
||||
_construct_response(client);
|
||||
_construct_response(client, server);
|
||||
|
||||
ret = ::send(client->fd, client->response.data(), client->response.size(), 0);
|
||||
ret = ::send(client->fd, client->response.c_str(), client->response.size(), 0);
|
||||
if (ret == -1)
|
||||
{
|
||||
std::perror("err send()");
|
||||
std::cerr << "client ptr =" << client << "\n"; // DEBUG
|
||||
std::cerr << "client.fd =" << client->fd << "\n"; // DEBUG
|
||||
_close_client(client->fd);
|
||||
return ;
|
||||
@@ -37,25 +60,29 @@ void Webserv::_send_response(Client *client)
|
||||
}
|
||||
}
|
||||
|
||||
void Webserv::_construct_response(Client *client)
|
||||
void Webserv::_construct_response(Client *client, ServerConfig &server)
|
||||
{
|
||||
client->status = 200;
|
||||
client->response.append("Server: Webserv/0.1\r\n");
|
||||
|
||||
if (client->raw_request.find("Connection: close") != std::string::npos)
|
||||
if (client->get_headers("Connection") == "close")
|
||||
client->response.append("Connection: close\r\n");
|
||||
else
|
||||
client->response.append("Connection: keep-alive\r\n");
|
||||
|
||||
_get_ressource(client);
|
||||
// TODO : Method switch (GET, POST, DELETE)
|
||||
_get_ressource(client, server);
|
||||
|
||||
_insert_status_line(client);
|
||||
_insert_status_line(client, server);
|
||||
}
|
||||
|
||||
#define E400 "\r\n<!DOCTYPE html><html><head><title>400 Bad Request</title></head><body><h1 style=\"text-align:center\">400 Bad Request</h1><hr><p style=\"text-align:center\">Le Webserv/0.1</p></body></html>"
|
||||
#define E404 "\r\n<!DOCTYPE html><html><head><title>404 Not Found</title></head><body><h1 style=\"text-align:center\">404 Not Found</h1><hr><p style=\"text-align:center\">Le Webserv/0.1</p></body></html>"
|
||||
#define E500 "\r\n<!DOCTYPE html><html><head><title>500 Internal Server Error</title></head><body><h1 style=\"text-align:center\">500 Internal Server Error</h1><hr><p style=\"text-align:center\">Le Webserv/0.1</p></body></html>"
|
||||
void Webserv::_insert_status_line(Client *client)
|
||||
// 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)
|
||||
{
|
||||
std::string status_line;
|
||||
|
||||
@@ -64,19 +91,19 @@ void Webserv::_insert_status_line(Client *client)
|
||||
switch (client->status)
|
||||
{
|
||||
case (200):
|
||||
status_line.append("200 OK");
|
||||
status_line.append(S200);
|
||||
break;
|
||||
case (400):
|
||||
status_line.append("400 Not Found");
|
||||
client->response.append(E400);
|
||||
status_line.append(S400);
|
||||
client->response.append(HTML_ERROR(S400));
|
||||
break;
|
||||
case (404):
|
||||
status_line.append("404 Not Found");
|
||||
client->response.append(E404);
|
||||
status_line.append(S404);
|
||||
client->response.append(HTML_ERROR(S404));
|
||||
break;
|
||||
case (500):
|
||||
status_line.append("500 Internal Server Error");
|
||||
client->response.append(E500);
|
||||
status_line.append(S500);
|
||||
client->response.append(HTML_ERROR(S500));
|
||||
break;
|
||||
}
|
||||
status_line.append("\r\n");
|
||||
@@ -87,11 +114,18 @@ void Webserv::_insert_status_line(Client *client)
|
||||
#define ROOT "www"
|
||||
#define INDEX "index.html"
|
||||
#define MAX_FILESIZE 1000000 // (1Mo)
|
||||
void Webserv::_get_ressource(Client *client)
|
||||
void Webserv::_get_ressource(Client *client, ServerConfig &server)
|
||||
{
|
||||
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
|
||||
path.append(INDEX);
|
||||
path.insert(0, ROOT);
|
||||
|
||||
std::cerr << "path = " << path << "\n";
|
||||
|
||||
// TMP HUGO
|
||||
//
|
||||
@@ -103,32 +137,15 @@ void Webserv::_get_ressource(Client *client)
|
||||
//
|
||||
// END TMP HUGO
|
||||
|
||||
// Mini parsing à l'arrache du PATH
|
||||
std::string path;
|
||||
try
|
||||
{
|
||||
path = client->raw_request.substr(0, client->raw_request.find("\r\n"));
|
||||
path = path.substr(0, path.rfind(" "));
|
||||
path = path.substr(path.find("/"));
|
||||
if (path == "/")
|
||||
path.append(INDEX);
|
||||
path.insert(0, ROOT);
|
||||
}
|
||||
catch (std::out_of_range& e)
|
||||
{
|
||||
std::cout << e.what() << '\n';
|
||||
client->status = 400;
|
||||
return ;
|
||||
}
|
||||
|
||||
if (access(path.data(), R_OK) == -1)
|
||||
|
||||
if (access(path.c_str(), R_OK) == -1)
|
||||
{
|
||||
std::perror("err access()");
|
||||
client->status = 404;
|
||||
return ;
|
||||
}
|
||||
|
||||
ifd.open(path.data(), std::ios::binary | std::ios::ate); // std::ios::binary (binary for files like images ?)
|
||||
ifd.open(path.c_str(), std::ios::binary | std::ios::ate); // std::ios::binary (binary for files like images ?)
|
||||
if (!ifd)
|
||||
{
|
||||
std::cerr << path << ": open fail" << '\n';
|
||||
@@ -151,7 +168,7 @@ void Webserv::_get_ressource(Client *client)
|
||||
ifd.read(buf, size);
|
||||
buf[ifd.gcount()] = '\0';
|
||||
|
||||
client->response.append("Content-Type: text/html; charset=UTF-8\r\n");
|
||||
client->response.append("Content-Type: text/html; charset=UTF-8\r\n"); // TODO : determine Content-Type
|
||||
|
||||
client->response.append("Content-Length: ");
|
||||
|
||||
|
||||
Reference in New Issue
Block a user