basic HTTP GET response
This commit is contained in:
@@ -6,9 +6,9 @@
|
||||
# include <string>
|
||||
# include <map>
|
||||
|
||||
class Client
|
||||
struct Client
|
||||
{
|
||||
public:
|
||||
// public:
|
||||
// Client(Placeholder);
|
||||
// Client();
|
||||
// Client(Client const &src);
|
||||
@@ -18,9 +18,11 @@ class Client
|
||||
int fd;
|
||||
std::string raw_request;
|
||||
std::map<std::string, std::string> request;
|
||||
std::map<std::string, std::string> response;
|
||||
// std::map<std::string, std::string> response;
|
||||
std::string response;
|
||||
unsigned int status;
|
||||
|
||||
private:
|
||||
// private:
|
||||
|
||||
};
|
||||
|
||||
|
||||
134
srcs/Webserv.cpp
134
srcs/Webserv.cpp
@@ -53,7 +53,7 @@ void Webserv::init_virtual_servers() // ADD config param
|
||||
throw std::runtime_error("Socket init");
|
||||
}
|
||||
|
||||
_bind(_socket_fd, 80);
|
||||
_bind(_socket_fd, 4040);
|
||||
_listen(_socket_fd, 512); // 512 arbitrary
|
||||
|
||||
if (_epoll_update(_socket_fd, EPOLLIN, EPOLL_CTL_ADD) == -1)
|
||||
@@ -93,9 +93,9 @@ void Webserv::start()
|
||||
if ((events[i].data.fd == _socket_fd) && (events[i].events & EPOLLIN))
|
||||
_accept_connection(events[i].data.fd);
|
||||
else if (events[i].events & EPOLLIN)
|
||||
_read_request(static_cast<Client*>(events[i].data.ptr));
|
||||
_request(static_cast<Client*>(events[i].data.ptr));
|
||||
else if (events[i].events & EPOLLOUT)
|
||||
_send_response(static_cast<Client*>(events[i].data.ptr));
|
||||
_response(static_cast<Client*>(events[i].data.ptr)); // NEED TO BREAK HERE IF SIGINT (or throw in _handle_signal ?)
|
||||
++i;
|
||||
_actual_client = NULL;
|
||||
}
|
||||
@@ -131,6 +131,12 @@ void Webserv::_accept_connection(int fd)
|
||||
|
||||
//////////
|
||||
// READ //
|
||||
|
||||
void Webserv::_request(Client *client)
|
||||
{
|
||||
_read_request(client);
|
||||
}
|
||||
|
||||
void Webserv::_read_request(Client *client)
|
||||
{
|
||||
char buf[BUFSIZE+1];
|
||||
@@ -164,22 +170,32 @@ void Webserv::_read_request(Client *client)
|
||||
|
||||
///////////
|
||||
// WRITE //
|
||||
|
||||
void Webserv::_response(Client *client)
|
||||
{
|
||||
_send_response(client);
|
||||
client->raw_request.clear();
|
||||
client->response.clear();
|
||||
}
|
||||
|
||||
void Webserv::_send_response(Client *client)
|
||||
{
|
||||
ssize_t ret;
|
||||
_actual_client = client;
|
||||
|
||||
std::cerr << "send()\n";
|
||||
std::cerr << "RAW_REQUEST\n|\n" << client->raw_request << "|\n";
|
||||
std::cerr << "RAW_REQUEST\n|\n" << client->raw_request << "|\n"; // DEBUG
|
||||
|
||||
ret = ::send(client->fd, MSG_TEST, sizeof MSG_TEST - 1, 0);
|
||||
_construct_response(client);
|
||||
|
||||
ret = ::send(client->fd, client->response.data(), client->response.size(), 0);
|
||||
if (ret == -1)
|
||||
{
|
||||
std::perror("err send(): ");
|
||||
if (g_last_signal)
|
||||
_handle_last_signal();
|
||||
// else
|
||||
// _close_client(client->fd);
|
||||
// _close_client(client->fd);
|
||||
return ;
|
||||
}
|
||||
|
||||
@@ -188,8 +204,111 @@ void Webserv::_send_response(Client *client)
|
||||
// _close_client(client->fd);
|
||||
// else
|
||||
// _epoll_update(client->fd, EPOLLIN, EPOLL_CTL_MOD, client);
|
||||
}
|
||||
|
||||
client->raw_request.clear();
|
||||
void Webserv::_construct_response(Client *client)
|
||||
{
|
||||
client->status = 200;
|
||||
client->response.append("Server: Webserv/0.1\r\n");
|
||||
|
||||
client->response.append("Connection: close\r\n");
|
||||
|
||||
_get_ressource(client);
|
||||
|
||||
_insert_status_line(client);
|
||||
}
|
||||
|
||||
#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)
|
||||
{
|
||||
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("200 OK");
|
||||
break;
|
||||
case (404):
|
||||
status_line.append("404 Not Found");
|
||||
client->response.append(E404);
|
||||
break;
|
||||
case (500):
|
||||
status_line.append("500 Internal Server Error");
|
||||
client->response.append(E500);
|
||||
break;
|
||||
}
|
||||
status_line.append("\r\n");
|
||||
|
||||
client->response.insert(0, status_line);
|
||||
}
|
||||
|
||||
#define ROOT "website"
|
||||
#define INDEX "index.html"
|
||||
#define FILENAME "rfc2119_no_link.html"
|
||||
#define MAX_FILESIZE 1000000 // (1Mo)
|
||||
void Webserv::_get_ressource(Client *client)
|
||||
{
|
||||
std::ifstream ifd; // For chunk, ifstream directly in struct CLient for multiples read without close() ?
|
||||
char buf[MAX_FILESIZE+1];
|
||||
char *tmp;
|
||||
|
||||
// Mini parsing à l'arrache du PATH
|
||||
std::string path;
|
||||
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);
|
||||
|
||||
if (access(path.data(), 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 ?)
|
||||
if (!ifd)
|
||||
{
|
||||
std::cerr << path << ": open fail" << '\n';
|
||||
client->status = 500;
|
||||
}
|
||||
else
|
||||
{
|
||||
// WIP : Chunk or not chunk (if filesize too big)
|
||||
std::streampos size = ifd.tellg();
|
||||
if (size > MAX_FILESIZE)
|
||||
{
|
||||
// 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(buf, size);
|
||||
buf[ifd.gcount()] = '\0';
|
||||
|
||||
client->response.append("Content-Type: text/html; charset=UTF-8\r\n");
|
||||
|
||||
client->response.append("Content-Length: ");
|
||||
tmp = ::ft_itoa(ifd.gcount());
|
||||
client->response.append(tmp);
|
||||
delete tmp;
|
||||
client->response.append("\r\n");
|
||||
|
||||
// Body
|
||||
client->response.append("\r\n");
|
||||
client->response.append(buf);
|
||||
|
||||
|
||||
ifd.close();
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -238,6 +357,7 @@ void Webserv::_handle_last_signal()
|
||||
else if (g_last_signal == SIGINT)
|
||||
{
|
||||
g_run = false;
|
||||
// maybe a throw here instead of "g_run" ?
|
||||
}
|
||||
g_last_signal = 0;
|
||||
}
|
||||
|
||||
@@ -24,6 +24,10 @@
|
||||
# include "Client.hpp"
|
||||
# include "Server.hpp"
|
||||
# include <csignal> // signal
|
||||
# include <cstdlib> // itoa
|
||||
# include <fstream> // ifstream
|
||||
char *ft_itoa(int n);
|
||||
# include <unistd.h> // access
|
||||
|
||||
# define BUFSIZE 8192
|
||||
# define TIMEOUT 3000
|
||||
@@ -67,8 +71,16 @@ class Webserv
|
||||
std::vector<Client> _clients;
|
||||
|
||||
void _accept_connection(int fd);
|
||||
|
||||
void _request(Client *client);
|
||||
void _read_request(Client *client);
|
||||
|
||||
void _response(Client *client);
|
||||
void _send_response(Client *client);
|
||||
void _construct_response(Client *client);
|
||||
void _insert_status_line(Client *client);
|
||||
void _get_ressource(Client *client);
|
||||
|
||||
|
||||
int _epoll_update(int fd, uint32_t events, int op);
|
||||
int _epoll_update(int fd, uint32_t events, int op, void *ptr);
|
||||
|
||||
50
srcs/ft_itoa.cpp
Normal file
50
srcs/ft_itoa.cpp
Normal file
@@ -0,0 +1,50 @@
|
||||
|
||||
#include <string.h>
|
||||
|
||||
static int eval_is_negative(int *n)
|
||||
{
|
||||
if (*n < 0)
|
||||
{
|
||||
*n = *n * -1;
|
||||
return (1);
|
||||
}
|
||||
return (0);
|
||||
}
|
||||
|
||||
static int eval_digit_nbr(int n)
|
||||
{
|
||||
int digit_nbr;
|
||||
|
||||
if (n == 0)
|
||||
return (1);
|
||||
digit_nbr = 0;
|
||||
while (n != 0)
|
||||
{
|
||||
digit_nbr++;
|
||||
n = n / 10;
|
||||
}
|
||||
return (digit_nbr);
|
||||
}
|
||||
|
||||
char *ft_itoa(int n)
|
||||
{
|
||||
int i;
|
||||
char *str;
|
||||
int is_negative;
|
||||
|
||||
if (n == -2147483648)
|
||||
return (strdup("-2147483648"));
|
||||
is_negative = eval_is_negative(&n);
|
||||
i = eval_digit_nbr(n) + is_negative;
|
||||
str = new char[i+1];
|
||||
if (is_negative)
|
||||
str[0] = '-';
|
||||
str[i] = '\0';
|
||||
while (i > 0 + is_negative)
|
||||
{
|
||||
i--;
|
||||
str[i] = (n % 10) + '0';
|
||||
n = n / 10;
|
||||
}
|
||||
return (str);
|
||||
}
|
||||
Reference in New Issue
Block a user