From 3fe37ea45187372e99eb3b6d1ff2b3c0fc2cd5a6 Mon Sep 17 00:00:00 2001 From: LuckyLaszlo Date: Mon, 15 Aug 2022 18:16:11 +0200 Subject: [PATCH] DELETE method tested and fixed + added 404 for directories without index and autoindex + minors adjustements --- default.config | 2 +- srcs/utils.cpp | 9 +++--- srcs/utils.hpp | 4 +-- srcs/webserv/method_delete.cpp | 28 ++++++++--------- srcs/webserv/method_get.cpp | 53 +++++++++++++++----------------- srcs/webserv/method_post.cpp | 28 +++++++++++------ srcs/webserv/parsing_request.cpp | 2 -- 7 files changed, 62 insertions(+), 64 deletions(-) delete mode 100644 srcs/webserv/parsing_request.cpp diff --git a/default.config b/default.config index 2325bae..e93c393 100644 --- a/default.config +++ b/default.config @@ -47,7 +47,7 @@ server { location /the_dump { allow_methods GET DELETE; root ./www/user_files; - #autoindex on; + autoindex on; } location /redirect { diff --git a/srcs/utils.cpp b/srcs/utils.cpp index 793c46b..d85d5d5 100644 --- a/srcs/utils.cpp +++ b/srcs/utils.cpp @@ -171,18 +171,19 @@ file_type eval_file_type(const std::string &path) 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()"); 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()"); - return 403; // FORBIDDEN, file doesn't have execution permission + return 403; // FORBIDDEN, file doesn't have access permission } return 0; } diff --git a/srcs/utils.hpp b/srcs/utils.hpp index 215f690..44f5347 100644 --- a/srcs/utils.hpp +++ b/srcs/utils.hpp @@ -67,15 +67,15 @@ std::string trim(std::string str, char del); http_method str_to_http_method(std::string &str); std::string http_methods_to_str(unsigned int methods); 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); std::string str_tolower(std::string str); 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"); size_t parse_http_headers (std::string headers, std::map & fields ); void str_map_key_tolower(std::map & mp); -void throw_test(); // debug +void throw_test(); void print_special(std::string str); diff --git a/srcs/webserv/method_delete.cpp b/srcs/webserv/method_delete.cpp index 40e0e3b..16c7d94 100644 --- a/srcs/webserv/method_delete.cpp +++ b/srcs/webserv/method_delete.cpp @@ -4,7 +4,6 @@ void Webserv::_delete(Client *client, const std::string &path) { /* - WIP https://www.rfc-editor.org/rfc/rfc9110.html#name-delete */ _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) { - if (access(path.c_str(), F_OK) == -1) - { - std::perror("err access()"); - client->status = 404; - return ; - } + std::cout << "_delete_file()\n"; + client->status = ::eval_file_mode(path, W_OK); + if (client->status) + return; - if (access(path.c_str(), W_OK) == -1) - { - std::perror("err access()"); - client->status = 403; - return ; - } - - if (remove(path.c_str()) == -1) + if (std::remove(path.c_str()) == -1) { std::perror("err remove()"); - client->status = 500; + if (errno == ENOTEMPTY || errno == EEXIST) + client->status = 403; + else + client->status = 500; return ; } + + client->status = 204; + client->response.append(CRLF); } diff --git a/srcs/webserv/method_get.cpp b/srcs/webserv/method_get.cpp index d3c1783..4649dd2 100644 --- a/srcs/webserv/method_get.cpp +++ b/srcs/webserv/method_get.cpp @@ -16,9 +16,10 @@ std::string Webserv::_replace_url_root(Client *client, std::string path) // const? void Webserv::_get(Client *client, std::string &path) { - - -// Index/Autoindex block +/* + https://www.rfc-editor.org/rfc/rfc9110.html#name-get +*/ + std::cout << "_get()\n"; if (eval_file_type(path) == IS_DIR) { if (path[path.size() - 1] != '/') @@ -34,12 +35,14 @@ void Webserv::_get(Client *client, std::string &path) } if (client->assigned_location->autoindex == true) _autoindex(client, path); + else + client->status = 404; } else _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) { /* @@ -52,21 +55,11 @@ void Webserv::_get_file(Client *client, const std::string &path) std::cout << "_get_file()\n"; - if (access(path.c_str(), F_OK) == -1) - { - std::perror("err access()"); - client->status = 404; - return ; - } + client->status = ::eval_file_mode(path, R_OK); + if (client->status) + return; - if (access(path.c_str(), R_OK) == -1) - { - std::perror("err access()"); - client->status = 403; - return ; - } - - ifd.open(path.c_str(), std::ios::ate); + ifd.open(path.c_str()); if (!ifd) { std::cerr << path << ": ifd.open fail" << '\n'; @@ -74,17 +67,6 @@ void Webserv::_get_file(Client *client, const std::string &path) } 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(); 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? void Webserv::_autoindex(Client *client, const std::string &path) { diff --git a/srcs/webserv/method_post.cpp b/srcs/webserv/method_post.cpp index 9ee0aaa..ce7309b 100644 --- a/srcs/webserv/method_post.cpp +++ b/srcs/webserv/method_post.cpp @@ -5,10 +5,9 @@ void Webserv::_post(Client *client, const std::string &path) { /* - WIP 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::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 void Webserv::_upload_files(Client *client) { + std::cout << "_upload_files()\n"; std::ofstream ofd; std::vector::const_iterator body_it = client->get_rq_multi_bodys().begin(); std::string path; std::string filename; 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()) { @@ -72,13 +76,13 @@ void Webserv::_upload_files(Client *client) path.append(filename); - if (access(path.c_str(), F_OK) == -1) + 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) + if (file_existed && ::access(path.c_str(), W_OK) == -1) { std::perror("err access()"); client->status = 403; @@ -103,10 +107,15 @@ void Webserv::_upload_files(Client *client) 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 { - // client->status = 200; - client->status = 204; // DEBUG 204 + client->status = 200; client->response.append("Location: "); client->response.append("/index.html"); // WIP client->response.append(CRLF); @@ -115,12 +124,11 @@ void Webserv::_upload_files(Client *client) } else { - // client->status = 201; - client->status = 204; // DEBUG 204 + client->status = 201; 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 } -} +*/ diff --git a/srcs/webserv/parsing_request.cpp b/srcs/webserv/parsing_request.cpp deleted file mode 100644 index d83d9e3..0000000 --- a/srcs/webserv/parsing_request.cpp +++ /dev/null @@ -1,2 +0,0 @@ -#include "parsing_request.hpp" -