You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

231 lines
7.5 KiB

2 years ago
/**
* @file Client.cpp
* @brief The client sockets class which keep keep clients information and handle answer to them.
* @author Narnaud
* @version 0.1
* @date 2023-01-12
*/
2 years ago
#include "webserv.hpp"
2 years ago
inline string get_extension(string str) {
size_t pos = str.rfind('.');
if (pos != string::npos) return str.substr(pos);
else return "";
}
2 years ago
Client::Client(int fd, ip_port_t ip_port, Master *parent) : _fd(fd), _ip_port(ip_port), _parent(parent) {
2 years ago
init();
cout << "New connection, socket fd is " << fd << ", ip is : " << _ip_port.ip << ", port : " << _ip_port.port << "\n";
}
2 years ago
Client::~Client(void) {
close(_fd);
2 years ago
cout << "Host disconnected, ip " << _ip_port.ip << ", port " << _ip_port.port << "\n";
}
2 years ago
void Client::init(void) {
_server = NULL;
_route = NULL;
_method = _uri = _host = _header = _body = "";
_len = 0;
_last_chunk = false;
_request.clear();
2 years ago
}
2 years ago
bool Client::getRequest(Env *env, string paquet) {
if (paquet.length() < 1) send_error(403);
if (DEBUG) debug_block("Paquet: ", paquet);
2 years ago
if (header_pick("Method:", 0) != "") return getBody(paquet);
2 years ago
vec_string lines = split(paquet, "\r\n");
for (vec_string::iterator it = lines.begin(); it < lines.end(); it++) {
2 years ago
size_t pos = paquet.find("\r\n");
2 years ago
if (pos != string::npos) paquet.erase(0, pos + 2);
else paquet.clear();
2 years ago
_header += *it + (it + 1 != lines.end() ? "\r\n" : "");
2 years ago
if (_header.find("\r\n\r\n") != string::npos)
2 years ago
return !parseHeader(env) ? false : (_len != 0 ? getBody(paquet) : true);
2 years ago
}
return false;
}
bool Client::getBody(string paquet) {
2 years ago
vec_string lines = split(paquet, "\r\n");
2 years ago
vec_string::iterator it;
2 years ago
2 years ago
for (it = lines.begin(); it < lines.end(); it++) {
2 years ago
if (DEBUG) cout << "Remaining length: " << _len << "\n";
if ((*it).length() && _len <= 0 && header_pick("Transfer-Encoding:", 0) == "chunked") {
2 years ago
_len = std::strtol((*it).c_str(), 0, 16) + 2;
2 years ago
_last_chunk = _len == 2 ? true : false;
2 years ago
} else if (_len > 0 || it != lines.begin()) {
_body += *it + "\r\n";
_len -= ((*it).length() + 2);
}
2 years ago
}
2 years ago
// if (_body.size())
_body.resize(_body.length() - 2);
2 years ago
_len += 2;
2 years ago
return (_last_chunk && _len == 0) ? true : false;
2 years ago
}
bool Client::parseHeader(Env *env) {
2 years ago
vec_string lines, method, line;
2 years ago
if (DEBUG) cout << "Parsing header...\n";
2 years ago
lines = split(_header, "\r\n");
method = split(lines.at(0), " ");
_request["Method:"] = method;
2 years ago
if (lines.size() > 0) {
2 years ago
for (vec_string::iterator it = lines.begin() + 1; it < lines.end(); it++) {
line = split(*it, " ");
2 years ago
_request[line.at(0)] = vec_string(line.begin() + 1, line.end());
2 years ago
}
}
_method = header_pick("Method:", 0);
2 years ago
if ((_method == "POST" || _method == "PUT") && header_pick("Content-Length:", 0) == "" &&
header_pick("Transfer-Encoding:", 0) != "chunked")
2 years ago
return (send_error(400), false);
vec_string uri_split = split(header_pick("Method:", 1), "?");
_uri = uri_split.at(0);
2 years ago
if (uri_split.size() > 1) _query = uri_split.at(1);
_host = header_pick("Host:", 0);
2 years ago
_env = env;
_server = _parent->choose_server(env, _host);
_route = _server->choose_route(_uri);
2 years ago
if (DEBUG) debug_header();
2 years ago
string len = header_pick("Content-Length:", 0).c_str();
if (len != "") {
_len = std::atoi(len.c_str());
2 years ago
_last_chunk = true;
2 years ago
int max_len = _route->_client_max_body_size > 0 ? _route->_client_max_body_size
: _server->_client_max_body_size > 0 ? _server->_client_max_body_size
: INT_MAX;
if (_len > max_len) return (send_error(413), false);
} else _len = 0;
2 years ago
return true;
}
2 years ago
string Client::header_pick(string key, size_t id) { return _request[key].size() <= id ? "" : _request[key].at(id); }
2 years ago
bool Client::check_method(void) {
vec_string allowed;
2 years ago
if ((_route && (allowed = _route->_allowed_methods).size() > 0) ||
2 years ago
(_server && (allowed = _server->_allowed_methods).size() > 0) || ((allowed = _env->_allowed_methods).size() > 0))
return std::find(allowed.begin(), allowed.end(), _method) < allowed.end() ? true : false;
else if (_method == "GET" || _method == "POST" || _method == "DELETE" || _method == "PUT") return (true);
return (false);
}
2 years ago
void Client::handleRequest(void) {
if (DEBUG) {
debug_block("Header: ", _header);
debug_block("Body: ", _body);
}
string ret;
2 years ago
string req_path = _route->getRoot() + _uri;
std::cout << "||-> Request for " << req_path << " received <-||\n";
string cgi_path = _route->_cgi.size() ? _route->_cgi[get_extension(req_path)]
: _server->_cgi.size() ? _server->_cgi[get_extension(req_path)]
: "";
if (DEBUG) cout << "Path: " << req_path << "\n";
if (!check_method()) send_error(405);
2 years ago
else {
2 years ago
if ((ret = _route->getIndex(_uri, req_path)) == "") ret = read_file(req_path);
2 years ago
if (ret == "404") {
2 years ago
if (_method == "POST" || _method == "PUT") create_file(req_path);
else send_error(404);
} else if (ret == "403") send_error(403);
else if (_method == "DELETE") std::remove(req_path.c_str());
else if (cgi_path != "") cgi(cgi_path, req_path);
else send_answer("HTTP/1.1 200 OK\r\n" + ret);
2 years ago
}
2 years ago
}
void Client::create_file(string path) {
std::ofstream file(path.c_str());
2 years ago
if (!file.good()) send_error(403);
2 years ago
else {
file << _body;
file.close();
send_answer("HTTP/1.1 201 Accepted\r\nContent-Length: 0\r\n\r\n");
}
2 years ago
}
/**
* @brief Launch cgi binary to parse the file requested by the client.
*
* @param cgi_path The cgi binary location specified in configuration file according to the file requested.
* @param path The path to the file requested.
*/
2 years ago
void Client::cgi(string cgi_path, string path) {
int status;
int fd[2];
2 years ago
std::stringstream ss;
2 years ago
string ret;
2 years ago
2 years ago
if (!std::ifstream(cgi_path.c_str()).good()) return send_error(404);
2 years ago
pipe(fd);
2 years ago
if (fork() == 0) {
2 years ago
const char **args = new const char *[cgi_path.length() + path.length() + 2];
args[0] = cgi_path.c_str();
2 years ago
args[1] = path.c_str();
args[2] = NULL;
2 years ago
string path_info = "PATH_INFO=" + _route->getRoot();
string query = "QUERY_STRING=" + _query;
2 years ago
const char **env = new const char *[path_info.length() + query.length() + 2];
env[0] = path_info.c_str();
2 years ago
env[1] = query.c_str();
env[2] = NULL;
2 years ago
dup2(fd[1], STDOUT_FILENO);
close(fd[1]);
close(fd[0]);
2 years ago
execve(cgi_path.c_str(), (char **)args, (char **)env);
2 years ago
}
close(fd[1]);
2 years ago
waitpid(-1, &status, 0);
2 years ago
char buffer[10000];
buffer[read(fd[0], buffer, 10000)] = 0;
ret = string(buffer);
2 years ago
ss << "HTTP/1.1 200 OK\r\nContent-Length: " << ret.length() - ret.find("\r\n\r\n") - 4 << "\r\n\r\n" << ret;
2 years ago
send_answer(ss.str());
}
2 years ago
/**
* @brief Send an error answer to the client.
*
* @param error_code The HTTP response code to send.
*/
void Client::send_error(int error_code, string opt = "") {
2 years ago
switch (error_code) {
case 301:
return send_answer("HTTP/1.1 301 Moved Permanently\r\nLocation: " + opt + "\r\n\r\n");
case 400:
2 years ago
return send_answer("HTTP/1.1 400 Bad Request\r\nContent-Length: 0\r\n\r\n");
2 years ago
case 403:
2 years ago
return send_answer("HTTP/1.1 403 Forbidden\r\nContent-Length: 0\r\n\r\n");
2 years ago
case 404:
2 years ago
return send_answer("HTTP/1.1 404 Not Found\r\nContent-Length: 0\r\n\r\n");
2 years ago
case 405:
2 years ago
return send_answer("HTTP/1.1 405 Method Not Allowed\r\nConnection: "
2 years ago
"close\r\nContent-Length: 0\r\n\r\n");
case 413:
2 years ago
return send_answer("HTTP/1.1 413 Payload Too "
"Large\r\nConnection: close\r\nContent-Length: 0\r\n\r\n");
2 years ago
}
}
/**
* @brief Send an answer to the client.
*
* @param msg The HTTP message to send.
*/
2 years ago
void Client::send_answer(string msg) {
2 years ago
if (DEBUG) debug_block("ANSWER: ", msg);
#ifdef __linux__
2 years ago
send(_fd, msg.c_str(), msg.length(), MSG_NOSIGNAL);
2 years ago
#elif __APPLE__
2 years ago
write(_fd, msg.c_str(), msg.length());
2 years ago
#endif
2 years ago
init();
2 years ago
}