better telnet tester, merging

This commit is contained in:
Eric LAZO
2022-08-14 21:41:46 +02:00
30 changed files with 605 additions and 163 deletions

View File

@@ -25,12 +25,14 @@
# include <cstdio> // perror, remove
# include <cstdlib> // strtol, strtoul
# include <dirent.h> // opendir()
# include <locale> // isalpha, local
# include "Client.hpp"
# include "ServerConfig.hpp"
# include "utils.hpp"
# include "http_status.hpp"
# include "autoindex.hpp"
# include "colors.h"
extern bool g_run;
extern int g_last_signal;
@@ -97,12 +99,13 @@ class Webserv
void _autoindex(Client *client, const std::string &path);
// method_post.cpp
void _post(Client *client, const std::string &path);
void _post_file(Client *client, const std::string &path);
void _upload_files(Client *client);
// method_delete.cpp
void _delete(Client *client, const std::string &path);
void _delete_file(Client *client, const std::string &path);
// cgi_script.cpp
size_t _cgi_pos(Client *client, std::string &path);
bool _is_cgi(Client *client, std::string path);
size_t _cgi_pos(Client *client, std::string &path, size_t pos);
std::string _exec_cgi(Client *client);
char** _set_env(Client *client);
char* _dup_env(std::string var, std::string val);

View File

@@ -1,21 +1,72 @@
#include "Webserv.hpp"
// TODO HUGO : go ameliorer la recherche comme on a dit.
size_t Webserv::_cgi_pos(Client *client, std::string &path)
bool Webserv::_is_cgi(Client *client, std::string path)
{
size_t pos = NPOS;
std::vector<std::string>::const_iterator it;
it = client->assigned_location->cgi_ext.begin();
while (it != client->assigned_location->cgi_ext.end())
std::string script_path;
size_t file_type;
size_t file_mode;
size_t pos = 0;
while (pos != NPOS)
{
pos = std::min(path.find(*it) + it->size(), pos);
++it;
pos = _cgi_pos(client, path, pos);
/*DEBUG*/ std::cout << "pos:" << pos << "\n&path[pos]:" << &path[pos] << "\n" << B_YELLOW << "fin debug _cgi_pos()\n\n" << RESET;
if (pos == NPOS)
break;
client->fill_script_path(path, pos);
script_path = "." + client->get_rq_script_path();
file_type = ::eval_file_type(script_path);
if (file_type == IS_DIR) // but what if it's a symlink ?
continue;
if (file_type == IS_FILE)
{
file_mode = ::eval_file_mode( script_path, X_OK );
if (!file_mode)
return true;
}
}
if (pos == NPOS)
return false;
else
return true;
client->clear_script();
client->clear_script();
client->status = file_mode; // 404 not_found OR 403 forbidden
return false;
}
size_t Webserv::_cgi_pos(Client *client, std::string &path, size_t pos)
{
std::vector<std::string> v_ext;
std::vector<std::string>::const_iterator it;
std::vector<std::string>::const_iterator it_end;
size_t len;
std::locale loc; // for isalpha()
/*DEBUG*/ it = client->assigned_location->cgi_ext.begin(); std::cout << B_YELLOW << "\nDEBUG _cgi_pos()\n" << RESET << "vector_ext.size():[" << client->assigned_location->cgi_ext.size() << "]\n\n";
v_ext = client->assigned_location->cgi_ext;
if (v_ext.empty())
return NPOS;
/*DEBUG*/ std::cout << "ext:[" << *it << "]\n" << "path:[" << path << "]\n\n";
it_end = client->assigned_location->cgi_ext.end();
while (pos < path.size())
{
/*DEBUG*/ std::cout << "\nwhile\n";
if (path.compare(pos, 2, "./") == 0)
pos += 2;
/*DEBUG*/ std::cout << "&path[pos]:[" << &path[pos] << "]\n";
pos = path.find('.', pos);
if (pos == NPOS)
return pos;
it = client->assigned_location->cgi_ext.begin();
for ( ; it != it_end; ++it)
{
/*DEBUG*/ std::cout << " for\n"; std::cout << " &path[pos]:[" << &path[pos] << "]\n" << " *it:[" << *it << "]\n" << " (*it).size():[" << (*it).size() << "]\n" << " path.substr(pos, (*it).size()):[" << path.substr(pos + 1, (*it).size()) << "]\n\n";
len = (*it).size();
if (path.compare(pos + 1, len, *it) == 0)
if ( !std::isalpha(path[pos + 1 + len], loc) )
return pos + 1 + len;
}
pos++;
}
return NPOS;
}
std::string Webserv::_exec_cgi(Client *client)
@@ -98,6 +149,7 @@ std::string Webserv::_exec_script(Client *client, char **env)
int fd_out[2];
int save_in = dup(STDIN_FILENO);
int save_out = dup(STDOUT_FILENO);
std::string path;
pipe(fd_in);
pipe(fd_out);
@@ -111,12 +163,13 @@ std::string Webserv::_exec_script(Client *client, char **env)
close(FD_RD_FR_CHLD);
dup2(FD_RD_FR_PRNT, STDIN_FILENO);
dup2(FD_WR_TO_PRNT, STDOUT_FILENO);
// DEBUG
std::cerr << "execve:\n";
execve(client->get_rq_script_path().c_str(), nll, env);
path = "." + client->get_rq_script_path();
/*DEBUG*/std::cerr << "execve\n" << "path:[" << path << "]\n";
execve(path.c_str(), nll, env);
// for tests execve crash :
//execve("wrong", nll, env);
std::cerr << "execve crashed.\n";
// TODO HUGO : check errno
}
else
{

View File

@@ -112,9 +112,11 @@ void Webserv::_init_http_status_map()
_http_status.insert(status_pair(405, S405));
_http_status.insert(status_pair(408, S408));
_http_status.insert(status_pair(413, S413));
_http_status.insert(status_pair(415, S415));
_http_status.insert(status_pair(500, S500));
_http_status.insert(status_pair(501, S501));
_http_status.insert(status_pair(505, S505));
}
void Webserv::_init_mime_types_map()

View File

@@ -3,12 +3,13 @@
std::string Webserv::_replace_url_root(Client *client, std::string path)
{
std::cerr << "path before = " << path << "\n"; // DEBUG
std::cerr << "assigned_location->path = " << client->assigned_location->path << "\n"; // debug
std::cerr << "path before = " << path << "\n"; // DEBUG
if (client->assigned_location->path == "/")
path.insert(0, client->assigned_location->root);
else
path.replace(0, client->assigned_location->path.size(), client->assigned_location->root);
std::cerr << "path after = " << path << "\n"; // DEBUG
std::cerr << "path after = " << path << "\n"; // DEBUG
return path;
}
@@ -20,7 +21,6 @@ void Webserv::_get(Client *client, std::string &path)
// Index/Autoindex block
if (eval_file_type(path) == IS_DIR)
{
std::cout << "made it to Index/Autoindex\n";
if (path[path.size() - 1] != '/')
path.push_back('/');
for (size_t i = 0; i < client->assigned_location->index.size(); i++)
@@ -50,7 +50,7 @@ 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() ?
std::stringstream buf;
std::cout << "made it to get_file\n";
std::cout << "_get_file()\n";
if (access(path.c_str(), F_OK) == -1)
{
@@ -103,7 +103,7 @@ void Webserv::_get_file(Client *client, const std::string &path)
// const?
void Webserv::_autoindex(Client *client, const std::string &path)
{
std::cout << "made it to _autoindex\n";
std::cout << "_autoindex()\n";
// std::cout << "client target: " << client->get_rq_target() << '\n';
@@ -113,10 +113,9 @@ void Webserv::_autoindex(Client *client, const std::string &path)
DIR *dir;
struct dirent *ent;
// std::cout << "location root: " << client->assigned_location->root << " location path: "
// << client->assigned_location->path << '\n';
// std::cout << "location root: " << client->assigned_location->root << " location path: " << client->assigned_location->path << '\n';
std::cout << "Path in auto is: " << path << '\n';
// std::cout << "Path in auto is: " << path << '\n';
if ( (dir = opendir(path.c_str()) ) != NULL)
{
dir_list.append(AUTOINDEX_START);

View File

@@ -8,51 +8,119 @@ void Webserv::_post(Client *client, const std::string &path)
WIP
https://www.rfc-editor.org/rfc/rfc9110.html#name-post
*/
_post_file(client, path);
(void)path;
std::cout << "_post()\n";
std::cerr << "upload_dir = " << client->assigned_location->upload_dir << "\n";
if (client->get_rq_abs_path() != client->assigned_location->path)
client->status = 404; // 404 ? J'ai un doute.
else if (client->assigned_location->upload_dir.empty())
client->status = 404; // 404 ? J'ai un doute.
else if (client->get_rq_multi_bodys().empty())
{
client->status = 415;
client->response.append("Accept: multipart/form-data"); // empty, no encoding accepted
client->response.append(CRLF);
}
else
_upload_files(client);
}
void Webserv::_post_file(Client *client, const std::string &path)
#define DEFAULT_NAME "unnamed_file"
// TODO : Loop for multi body
void Webserv::_upload_files(Client *client)
{
std::ofstream ofd;
std::vector<MultipartBody>::const_iterator body_it = client->get_rq_multi_bodys().begin();
std::string path;
std::string filename;
size_t pos;
bool file_existed;
if (access(path.c_str(), F_OK) == -1)
file_existed = false;
else
file_existed = true;
// How to determine status 403 for file that dont already exist ?
if (file_existed && access(path.c_str(), W_OK) == -1)
while (body_it != client->get_rq_multi_bodys().end())
{
std::perror("err access()");
client->status = 403;
return ;
}
if (body_it->body.empty())
{
++body_it;
continue;
}
// Content-Disposition: form-data; name="upload_file"; filename="camion.jpg"
::print_map(body_it->headers);
filename = client->get_rq_multi_bodys_headers("Content-Disposition", body_it);
std::cerr << "filename ="<< filename << "\n";
pos = filename.find("filename=");
if (pos != NPOS)
{
filename = filename.substr(pos + sizeof("filename=")-1);
std::cerr << "filename ="<< filename << "\n";
// A l'arrache pour enlever les "
filename.erase(0, 1);
std::cerr << "filename ="<< filename << "\n";
filename.erase(filename.size()-1, 1);
std::cerr << "filename ="<< filename << "\n";
std::cerr << "filename ="<< filename << "\n";
if (filename.empty())
filename = DEFAULT_NAME;
}
else
{
filename = DEFAULT_NAME;
}
std::cerr << "filename ="<< filename << "\n";
path = client->assigned_location->upload_dir; // Assume there a final '/'
path.append(filename);
ofd.open(path.c_str(), std::ios::trunc);
if (!ofd)
{
std::cerr << path << ": ofd.open fail" << '\n';
client->status = 500;
}
else
{
// Content-Length useless at this point ?
ofd << client->get_rq_body();
if (access(path.c_str(), F_OK) == -1)
file_existed = false;
else
file_existed = true;
// How to determine status 403 for file that dont already exist ? access() on the upload_dir ?
if (file_existed && access(path.c_str(), W_OK) == -1)
{
std::perror("err access()");
client->status = 403;
return ;
}
ofd.open(path.c_str(), std::ios::trunc);
if (!ofd)
{
std::cerr << path << ": ofd.open fail" << '\n';
client->status = 500;
return;
}
ofd << body_it->body;
if (!ofd)
{
std::cerr << path << ": ofd.write fail" << '\n';
client->status = 500;
return;
}
else if (file_existed)
{
client->status = 200;
// WIP https://www.rfc-editor.org/rfc/rfc9110.html#name-200-ok
}
else
{
client->status = 201;
// WIP https://www.rfc-editor.org/rfc/rfc9110.html#section-9.3.3-4
}
++body_it;
ofd.close();
}
if (file_existed) // with multi body it doesn't make much sense
{
// client->status = 200;
client->status = 204; // DEBUG 204
client->response.append("Location: ");
client->response.append("/index.html"); // WIP
client->response.append(CRLF);
client->response.append(CRLF);
// WIP https://www.rfc-editor.org/rfc/rfc9110.html#name-200-ok
}
else
{
// client->status = 201;
client->status = 204; // DEBUG 204
client->response.append("Location: ");
client->response.append("/index.html"); // WIP
client->response.append(CRLF);
client->response.append(CRLF);
// WIP https://www.rfc-editor.org/rfc/rfc9110.html#section-9.3.3-4
}
}

View File

@@ -25,7 +25,7 @@ void Webserv::_request(Client *client)
}
else if (ret == READ_COMPLETE)
{
if (client->body_complete)
if (client->body_complete && client->get_rq_multi_bodys().empty()) // DEBUG
std::cerr << "______BODY\n" << client->get_rq_body() << "\n______\n"; // DEBUG
_epoll_update(client->get_cl_fd(), EPOLLOUT, EPOLL_CTL_MOD);
client->request_complete = true;
@@ -37,7 +37,6 @@ int Webserv::_read_request(Client *client)
char buf[BUFSIZE];
ssize_t ret;
std::cerr << "call recv()" << "\n" ;
ret = ::recv(client->get_cl_fd(), buf, BUFSIZE, 0);
std::cerr << "recv() on fd(" << client->get_cl_fd() << ") returned = " << ret << "\n" ;
if (ret == -1)
@@ -51,6 +50,8 @@ int Webserv::_read_request(Client *client)
return READ_CLOSE;
}
client->raw_request.append(buf, ret);
// ::print_special(client->raw_request);
// std::cerr << "__raw_request__\n" << client->raw_request << "\n______\n"; // DEBUG
// print_special(client->raw_request);
@@ -63,8 +64,7 @@ int Webserv::_read_request(Client *client)
{
if (client->get_rq_headers("Content-Type").empty()
&& client->get_rq_headers("Content-Length").empty()
// && client->get_rq_headers("Transfer-Encoding").empty()
)
&& client->get_rq_headers("Transfer-Encoding").empty())
return READ_COMPLETE; // No body case
}
else if (client->raw_request.size() > MAX_HEADER_SIZE)
@@ -73,7 +73,7 @@ int Webserv::_read_request(Client *client)
return READ_COMPLETE;
}
}
else if (client->header_complete)
if (client->header_complete)
{
// client->read_body_size += ret; // Not accurate, part of body could have been read with headers, unused for now
client->parse_request_body();

View File

@@ -21,7 +21,10 @@ void Webserv::_response(Client *client)
}
else if (ret == SEND_COMPLETE)
{
if (client->get_rq_headers("Connection") == "close" || client->status == 408)
if (client->get_rq_headers("Connection") == "close"
|| client->status == 400 // TODO: Refactoring
|| client->status == 408
|| client->status == 413)
_close_client(client->get_cl_fd());
else
{
@@ -61,32 +64,35 @@ void Webserv::_append_base_headers(Client *client)
{
client->response.append("Server: Webserv/0.1" CRLF);
if (client->get_rq_headers("Connection") == "close")
if (client->get_rq_headers("Connection") == "close"
|| client->status == 400 // TODO: Refactoring
|| client->status == 408
|| client->status == 413)
client->response.append("Connection: close" CRLF);
else
client->response.append("Connection: keep-alive" CRLF);
}
// TODO HUGO : wip
void Webserv::_construct_response(Client *client)
{
std::string path = _replace_url_root(client, client->get_rq_abs_path());
std::string path;
std::string script_output;
/* size_t pos = _cgi_pos(client, path);
if (pos != NPOS)
path = _replace_url_root(client, client->get_rq_abs_path());
if (_is_cgi(client, path))
{
client->fill_script_path(path, pos);
std::string script_output = _exec_cgi(client);
script_output = _exec_cgi(client);
_check_script_output(client, script_output);
client->response += script_output;
return;
} */
}
_process_method(client, path);
}
void Webserv::_process_method(Client *client, std::string &path)
{
std::cerr << "assigned_location->path = " << client->assigned_location->path << "\n"; // debug
std::cerr << "allow_methods = " << client->assigned_location->allow_methods << "\n"; // debug
std::cerr << "allow_methods = " << http_methods_to_str(client->assigned_location->allow_methods) << "\n"; // debug
switch (client->get_rq_method())
{
@@ -182,8 +188,6 @@ ServerConfig *_determine_process_server(Client *client, std::vector<ServerConfig
// Temporary Global Scope. Probably move to Client in the future.
const LocationConfig *_determine_location(const ServerConfig &server, const std::string &path)
{
std::cout << "determin location path sent: " << path << '\n';
/* RULES ***
If a path coresponds exactly to a location, use that one
@@ -206,12 +210,8 @@ If we get a url that ends in / ignore the last /
for (std::vector<LocationConfig>::const_iterator it = server.locations.begin(); it != server.locations.end(); it++)
{
// std::cout << it->path << " -- ";
if (it->path.size() > uri.size())
{
// std::cout << "skipping this one\n";
continue ;
}
if (uri.compare(0, it->path.size(), it->path) == 0)
{

View File

@@ -12,7 +12,12 @@ void Webserv::_timeout()
std::cerr << "timeout request fd " << it->get_cl_fd() << "\n";
it->status = 408;
_epoll_update(it->get_cl_fd(), EPOLLOUT, EPOLL_CTL_MOD);
// DEBUG, close without repsonse 408
/* _close_client(it->get_cl_fd());
it = _clients.begin(); */
}
// else // DEBUG
++it;
}
}