From df24de46c78aa4d0e95bd4adca6c113a32a0d6c4 Mon Sep 17 00:00:00 2001 From: Hugo LAMY Date: Wed, 17 Aug 2022 20:41:22 +0200 Subject: [PATCH] works on download script --- srcs/cgi-bin/cgi_cpp_download.cpp | 2 +- srcs/cgi-bin/cgi_utils.cpp | 4 ++-- srcs/cgi-bin/cgi_utils.hpp | 2 +- srcs/webserv/cgi.cpp | 15 +++++++++++---- srcs/webserv/response.cpp | 7 ++++--- 5 files changed, 19 insertions(+), 11 deletions(-) diff --git a/srcs/cgi-bin/cgi_cpp_download.cpp b/srcs/cgi-bin/cgi_cpp_download.cpp index b0238e8..802d201 100644 --- a/srcs/cgi-bin/cgi_cpp_download.cpp +++ b/srcs/cgi-bin/cgi_cpp_download.cpp @@ -24,7 +24,7 @@ int main (int ac, char **av, char ** env) path = get_value("file", rq_body); path = "./www/" + path; - status = ::eval_file_read(path); + status = ::eval_file_access(path, R_OK); if (status) { std::cout << "Status: " << status << CRLF CRLF; diff --git a/srcs/cgi-bin/cgi_utils.cpp b/srcs/cgi-bin/cgi_utils.cpp index 9e2e7b7..2d4b803 100644 --- a/srcs/cgi-bin/cgi_utils.cpp +++ b/srcs/cgi-bin/cgi_utils.cpp @@ -181,7 +181,7 @@ void http_body += HTML_BODY_BOTTOM; } -size_t eval_file_read(const std::string &path) +size_t eval_file_access(const std::string &path, int mode) { if (::access(path.c_str(), F_OK) == -1) { @@ -189,7 +189,7 @@ size_t eval_file_read(const std::string &path) return 404; // NOT_FOUND, file doesn't exist } - if (::access(path.c_str(), R_OK) == -1) + if (::access(path.c_str(), mode) == -1) { std::perror("err access()"); return 403; // FORBIDDEN, file doesn't have access permission diff --git a/srcs/cgi-bin/cgi_utils.hpp b/srcs/cgi-bin/cgi_utils.hpp index 6ba5a7a..5eed6e4 100644 --- a/srcs/cgi-bin/cgi_utils.hpp +++ b/srcs/cgi-bin/cgi_utils.hpp @@ -62,7 +62,7 @@ void fill_body_basic(char **env, std::string & http_body, const std::string & rq_body); size_t -eval_file_read(const std::string &path); +eval_file_access(const std::string &path, int mode); #endif diff --git a/srcs/webserv/cgi.cpp b/srcs/webserv/cgi.cpp index 8524713..a16c3f1 100644 --- a/srcs/webserv/cgi.cpp +++ b/srcs/webserv/cgi.cpp @@ -241,14 +241,15 @@ void Webserv::_check_script_output(Client *client, std::string & output) _check_script_status(client, output); if (client->status >= 400 && client->status < 600) return; -//*DEBUG*/ std::cout << "\n" B_PURPLE "[script status]:" RESET "\n"; ::print_special(output); std::cout << B_PURPLE "-----------" RESET "\n\n"; +/*DEBUG*/ std::cout << "\n" B_PURPLE "[script status]:\n" << "client->status:[" << client->status << "]" RESET "\n"; ::print_special(output); std::cout << B_PURPLE "-----------" RESET "\n\n"; client->status = _check_script_fields(output, client->status); +/*DEBUG*/ std::cout << "\n" B_PURPLE "[script fields]:\n" << "client->status:[" << client->status << "]" RESET "\n"; ::print_special(output); std::cout << B_PURPLE "-----------" RESET "\n\n"; _check_fields_duplicates(client, output); -//*DEBUG*/ std::cout << "\n" B_PURPLE "[script fields]:" RESET "\n"; ::print_special(output); std::cout << B_PURPLE "-----------" RESET "\n\n"; +/*DEBUG*/ std::cout << "\n" B_PURPLE "[fields duplicates]:\n" << "client->status:[" << client->status << "]" RESET "\n"; ::print_special(output); std::cout << B_PURPLE "-----------" RESET "\n\n"; _remove_body_leading_empty_lines(output); -//*DEBUG*/ std::cout << "\n" B_PURPLE "[script empty lines]:" RESET "\n"; ::print_special(output); std::cout << B_PURPLE "-----------" RESET "\n\n"; +/*DEBUG*/ std::cout << "\n" B_PURPLE "[script empty lines]:\n" << "client->status:[" << client->status << "]" RESET "\n"; ::print_special(output); std::cout << B_PURPLE "-----------" RESET "\n\n"; _add_script_body_length_header(output); -//*DEBUG*/ std::cout << "\n" B_PURPLE "[script content length]:" RESET "\n"; ::print_special(output); std::cout << B_PURPLE "-----------" RESET "\n\n"; +/*DEBUG*/ std::cout << "\n" B_PURPLE "[script content length]:\n" << "client->status:[" << client->status << "]" RESET "\n"; ::print_special(output); std::cout << B_PURPLE "-----------" RESET "\n\n"; } void Webserv::_check_script_status(Client *client, std::string & output) @@ -273,9 +274,11 @@ size_t Webserv::_check_script_fields(const std::string & output, size_t status) std::string body; size_t pos; +std::cerr << "0\n"; pos = output.find(CRLF CRLF); if (pos == NPOS) // there is not empty line return 500; +std::cerr << "1\n"; headers = output.substr(0, pos); body = output.substr(pos + CRLF_SIZE * 2); headers = str_tolower(headers); @@ -284,14 +287,18 @@ size_t Webserv::_check_script_fields(const std::string & output, size_t status) { if (!body.empty()) // there is body return 500; +std::cerr << "2\n"; if (headers.find("location") == NPOS) // there is no location field return 500; +std::cerr << "3\n"; } else if (headers.find("location") != NPOS) // there is a location field { if (body.empty()) // there is no body return 500; +std::cerr << "4\n"; } +std::cerr << "5\n"; return status; } diff --git a/srcs/webserv/response.cpp b/srcs/webserv/response.cpp index 3d922c1..3f574b4 100644 --- a/srcs/webserv/response.cpp +++ b/srcs/webserv/response.cpp @@ -52,12 +52,12 @@ int Webserv::_send_response(Client *client) } else if (!client->cgi_output.empty()) { - // /*DEBUG*/ std::cout << "\n" B_PURPLE "[response]:" RESET "\n"; ::print_special(client->response); std::cout << B_PURPLE "-----------" RESET "\n\n"; - // /*DEBUG*/ std::cout << "\n" B_PURPLE "[script output]:" RESET "\n"; ::print_special(script_output); std::cout << B_PURPLE "-----------" RESET "\n\n"; + /*DEBUG*/ std::cout << "\n" B_PURPLE "[response]:" RESET "\n"; ::print_special(client->response); std::cout << B_PURPLE "-----------" RESET "\n\n"; + /*DEBUG*/ std::cout << "\n" B_PURPLE "[script output]:" RESET "\n"; ::print_special(client->cgi_output); std::cout << B_PURPLE "-----------" RESET "\n\n"; _check_script_output(client, client->cgi_output); // FD_CGI : adjust for client->cgi_output; if (client->status < 400) client->response += client->cgi_output; - // /*DEBUG*/ std::cout << "\n" B_PURPLE "[response + output]:" RESET "\n"; ::print_special(client->response); std::cout << B_PURPLE "-----------" RESET "\n\n"; + /*DEBUG*/ std::cout << "\n" B_PURPLE "[response + output]:" RESET "\n"; ::print_special(client->response); std::cout << B_PURPLE "-----------" RESET "\n\n"; } _insert_status_line(client); @@ -201,3 +201,4 @@ void Webserv::_append_body(Client *client, const std::string &body, const std::s client->response.append(CRLF); client->response.append(body); } +