DELETE method tested and fixed

+ added 404 for directories without index and autoindex
+ minors adjustements
This commit is contained in:
LuckyLaszlo
2022-08-15 18:16:11 +02:00
parent a284e400c1
commit 3fe37ea451
7 changed files with 62 additions and 64 deletions

View File

@@ -47,7 +47,7 @@ server {
location /the_dump { location /the_dump {
allow_methods GET DELETE; allow_methods GET DELETE;
root ./www/user_files; root ./www/user_files;
#autoindex on; autoindex on;
} }
location /redirect { location /redirect {

View File

@@ -171,18 +171,19 @@ file_type eval_file_type(const std::string &path)
return (IS_OTHER); return (IS_OTHER);
} }
size_t eval_file_mode(std::string path, int mode) // rename in "eval_file_access" ?
size_t eval_file_mode(const std::string &path, int mode)
{ {
if (access(path.c_str(), F_OK) == -1) if (::access(path.c_str(), F_OK) == -1)
{ {
std::perror("err access()"); std::perror("err access()");
return 404; // NOT_FOUND, file doesn't exist return 404; // NOT_FOUND, file doesn't exist
} }
if (access(path.c_str(), mode) == -1) if (::access(path.c_str(), mode) == -1)
{ {
std::perror("err access()"); std::perror("err access()");
return 403; // FORBIDDEN, file doesn't have execution permission return 403; // FORBIDDEN, file doesn't have access permission
} }
return 0; return 0;
} }

View File

@@ -67,15 +67,15 @@ std::string trim(std::string str, char del);
http_method str_to_http_method(std::string &str); http_method str_to_http_method(std::string &str);
std::string http_methods_to_str(unsigned int methods); std::string http_methods_to_str(unsigned int methods);
file_type eval_file_type(const std::string &path); file_type eval_file_type(const std::string &path);
size_t eval_file_mode(std::string path, int mode); size_t eval_file_mode(const std::string &path, int mode);
void replace_all_substr(std::string &str, const std::string &ori_substr, const std::string &new_substr); void replace_all_substr(std::string &str, const std::string &ori_substr, const std::string &new_substr);
std::string str_tolower(std::string str); std::string str_tolower(std::string str);
std::string extract_line(std::string & str, size_t pos = 0, std::string delim = "\n"); std::string extract_line(std::string & str, size_t pos = 0, std::string delim = "\n");
std::string get_line (std::string str, size_t pos = 0, std::string delim = "\n"); std::string get_line (std::string str, size_t pos = 0, std::string delim = "\n");
size_t parse_http_headers (std::string headers, std::map<std::string, std::string> & fields ); size_t parse_http_headers (std::string headers, std::map<std::string, std::string> & fields );
void str_map_key_tolower(std::map<std::string, std::string> & mp); void str_map_key_tolower(std::map<std::string, std::string> & mp);
void throw_test();
// debug // debug
void throw_test();
void print_special(std::string str); void print_special(std::string str);

View File

@@ -4,7 +4,6 @@
void Webserv::_delete(Client *client, const std::string &path) void Webserv::_delete(Client *client, const std::string &path)
{ {
/* /*
WIP
https://www.rfc-editor.org/rfc/rfc9110.html#name-delete https://www.rfc-editor.org/rfc/rfc9110.html#name-delete
*/ */
_delete_file(client, path); _delete_file(client, path);
@@ -12,24 +11,21 @@ void Webserv::_delete(Client *client, const std::string &path)
void Webserv::_delete_file(Client *client, const std::string &path) void Webserv::_delete_file(Client *client, const std::string &path)
{ {
if (access(path.c_str(), F_OK) == -1) std::cout << "_delete_file()\n";
{ client->status = ::eval_file_mode(path, W_OK);
std::perror("err access()"); if (client->status)
client->status = 404; return;
return ;
}
if (access(path.c_str(), W_OK) == -1) if (std::remove(path.c_str()) == -1)
{
std::perror("err access()");
client->status = 403;
return ;
}
if (remove(path.c_str()) == -1)
{ {
std::perror("err remove()"); std::perror("err remove()");
if (errno == ENOTEMPTY || errno == EEXIST)
client->status = 403;
else
client->status = 500; client->status = 500;
return ; return ;
} }
client->status = 204;
client->response.append(CRLF);
} }

View File

@@ -16,9 +16,10 @@ std::string Webserv::_replace_url_root(Client *client, std::string path)
// const? // const?
void Webserv::_get(Client *client, std::string &path) void Webserv::_get(Client *client, std::string &path)
{ {
/*
https://www.rfc-editor.org/rfc/rfc9110.html#name-get
// Index/Autoindex block */
std::cout << "_get()\n";
if (eval_file_type(path) == IS_DIR) if (eval_file_type(path) == IS_DIR)
{ {
if (path[path.size() - 1] != '/') if (path[path.size() - 1] != '/')
@@ -34,12 +35,14 @@ void Webserv::_get(Client *client, std::string &path)
} }
if (client->assigned_location->autoindex == true) if (client->assigned_location->autoindex == true)
_autoindex(client, path); _autoindex(client, path);
else
client->status = 404;
} }
else else
_get_file(client, path); _get_file(client, path);
} }
# define MAX_FILESIZE 1 * MB # define MAX_FILESIZE 1 * MB // unused
void Webserv::_get_file(Client *client, const std::string &path) void Webserv::_get_file(Client *client, const std::string &path)
{ {
/* /*
@@ -52,21 +55,11 @@ void Webserv::_get_file(Client *client, const std::string &path)
std::cout << "_get_file()\n"; std::cout << "_get_file()\n";
if (access(path.c_str(), F_OK) == -1) client->status = ::eval_file_mode(path, R_OK);
{ if (client->status)
std::perror("err access()"); return;
client->status = 404;
return ;
}
if (access(path.c_str(), R_OK) == -1) ifd.open(path.c_str());
{
std::perror("err access()");
client->status = 403;
return ;
}
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';
@@ -74,17 +67,6 @@ void Webserv::_get_file(Client *client, const std::string &path)
} }
else else
{ {
/* std::streampos size = ifd.tellg();
// WIP Low priority : Chunk or not chunk (if filesize too big)
if (size > MAX_FILESIZE)
{
// Then chunk
client->status = 500; // WIP temp
std::cerr << "File too large for non chunk body\n";
return ;
} */
ifd.seekg(0, std::ios::beg);
buf << ifd.rdbuf(); buf << ifd.rdbuf();
if (!ifd || !buf) if (!ifd || !buf)
{ {
@@ -100,6 +82,19 @@ void Webserv::_get_file(Client *client, const std::string &path)
} }
} }
/*
// WIP Low priority : Chunk or not chunk (if filesize > MAX_FILESIZE)
// WITH flag "std::ios::ate" for open()
std::streampos size = ifd.tellg();
ifd.seekg(0, std::ios::beg);
if (size > MAX_FILESIZE)
{
// Chunked GET here
return ;
}
*/
// const? // const?
void Webserv::_autoindex(Client *client, const std::string &path) void Webserv::_autoindex(Client *client, const std::string &path)
{ {

View File

@@ -5,10 +5,9 @@
void Webserv::_post(Client *client, const std::string &path) void Webserv::_post(Client *client, const std::string &path)
{ {
/* /*
WIP
https://www.rfc-editor.org/rfc/rfc9110.html#name-post https://www.rfc-editor.org/rfc/rfc9110.html#name-post
*/ */
(void)path; (void)path; // unused, we use "assigned_location->upload_dir" instead
std::cout << "_post()\n"; std::cout << "_post()\n";
std::cerr << "upload_dir = " << client->assigned_location->upload_dir << "\n"; std::cerr << "upload_dir = " << client->assigned_location->upload_dir << "\n";
@@ -31,12 +30,17 @@ void Webserv::_post(Client *client, const std::string &path)
// TODO : Loop for multi body // TODO : Loop for multi body
void Webserv::_upload_files(Client *client) void Webserv::_upload_files(Client *client)
{ {
std::cout << "_upload_files()\n";
std::ofstream ofd; std::ofstream ofd;
std::vector<MultipartBody>::const_iterator body_it = client->get_rq_multi_bodys().begin(); std::vector<MultipartBody>::const_iterator body_it = client->get_rq_multi_bodys().begin();
std::string path; std::string path;
std::string filename; std::string filename;
size_t pos; size_t pos;
bool file_existed; bool file_existed = false;
client->status = ::eval_file_mode(client->assigned_location->upload_dir, W_OK);
if (client->status)
return;
while (body_it != client->get_rq_multi_bodys().end()) while (body_it != client->get_rq_multi_bodys().end())
{ {
@@ -72,13 +76,13 @@ void Webserv::_upload_files(Client *client)
path.append(filename); path.append(filename);
if (access(path.c_str(), F_OK) == -1) if (::access(path.c_str(), F_OK) == -1)
file_existed = false; file_existed = false;
else else
file_existed = true; file_existed = true;
// How to determine status 403 for file that dont already exist ? access() on the upload_dir ? // 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) if (file_existed && ::access(path.c_str(), W_OK) == -1)
{ {
std::perror("err access()"); std::perror("err access()");
client->status = 403; client->status = 403;
@@ -103,10 +107,15 @@ void Webserv::_upload_files(Client *client)
ofd.close(); ofd.close();
} }
client->status = 204;
client->response.append(CRLF);
// https://www.rfc-editor.org/rfc/rfc9110.html#name-204-no-content
}
/*
if (file_existed) // with multi body it doesn't make much sense if (file_existed) // with multi body it doesn't make much sense
{ {
// client->status = 200; client->status = 200;
client->status = 204; // DEBUG 204
client->response.append("Location: "); client->response.append("Location: ");
client->response.append("/index.html"); // WIP client->response.append("/index.html"); // WIP
client->response.append(CRLF); client->response.append(CRLF);
@@ -115,12 +124,11 @@ void Webserv::_upload_files(Client *client)
} }
else else
{ {
// client->status = 201; client->status = 201;
client->status = 204; // DEBUG 204
client->response.append("Location: "); client->response.append("Location: ");
client->response.append("/index.html"); // WIP client->response.append("/index.html"); // WIP
client->response.append(CRLF); client->response.append(CRLF);
client->response.append(CRLF); client->response.append(CRLF);
// WIP https://www.rfc-editor.org/rfc/rfc9110.html#section-9.3.3-4 // WIP https://www.rfc-editor.org/rfc/rfc9110.html#section-9.3.3-4
} }
} */

View File

@@ -1,2 +0,0 @@
#include "parsing_request.hpp"