From a1fff0f8c2d241f9673918a6ea5f447b94dd2b18 Mon Sep 17 00:00:00 2001 From: hugogogo Date: Thu, 11 Aug 2022 17:46:27 +0200 Subject: [PATCH] added colors, for print_special + renamed client's public function parse_request() into parse_request_headers() --- srcs/Client.cpp | 19 ++++--------------- srcs/Client.hpp | 4 ++-- srcs/colors.h | 25 +++++++++++++++++++++++++ srcs/utils.cpp | 4 ++-- srcs/utils.hpp | 1 + srcs/webserv/request.cpp | 2 +- 6 files changed, 35 insertions(+), 20 deletions(-) create mode 100644 srcs/colors.h diff --git a/srcs/Client.cpp b/srcs/Client.cpp index c04ce05..a4d58b8 100644 --- a/srcs/Client.cpp +++ b/srcs/Client.cpp @@ -75,22 +75,17 @@ Client & Client::operator=( Client const & rhs ) // https://developer.mozilla.org/en-US/docs/Web/HTTP/Headers // https://www.ibm.com/docs/en/cics-ts/5.3?topic=protocol-http-requests // https://www.tutorialspoint.com/http/http_requests.htm -void Client::parse_request(std::vector &servers) +void Client::parse_request_headers(std::vector &servers) { clear_request(); // not mandatory -// debug -print_client("before"); - _parse_request_line(); // debug print_client("first line"); - if (status) return; - _parse_request_headers(); + _parse_request_fields(); // debug print_client("headers"); - if (status) return; assigned_server = ::_determine_process_server(this, servers); @@ -174,7 +169,7 @@ void Client::print_client(std::string message) std::map::iterator it; std::cout << "\n=== DEBUG PRINT CLIENT ===\n"; - std::cout << "\n" << message << ":\n----------\n" << "raw_request:\n__\n"; + std::cout << "\n" << message << ":----------\n\n" << "raw_request:\n__\n"; ::print_special(raw_request); std::cout << "\n__\n" << "get_cl_fd() : [" << get_cl_fd() << "]\n" @@ -240,12 +235,6 @@ void Client::_parse_request_line() std::string raw_line; raw_line = ::get_line(raw_request, 0, CRLF); - -// DEBUG -std::cout << "raw_line:["; -::print_special(raw_line); -std::cout << "]\n"; - line = ::split_trim(raw_line, " "); if (line.size() != 3) { @@ -273,7 +262,7 @@ void Client::_parse_request_uri( std::string uri ) _request.abs_path = uri.substr(0, pos); } -void Client::_parse_request_headers() +void Client::_parse_request_fields() { std::string headers; size_t pos; diff --git a/srcs/Client.hpp b/srcs/Client.hpp index 2e8300c..a6cc82f 100644 --- a/srcs/Client.hpp +++ b/srcs/Client.hpp @@ -69,7 +69,7 @@ class Client std::string get_rq_script_info() const; std::string get_rq_headers(const std::string & key) const; - void parse_request(std::vector &servers); + void parse_request_headers(std::vector &servers); void parse_request_body(); void clear(); void clear_request(); @@ -86,7 +86,7 @@ class Client struct Request _request; void _parse_request_line(); - void _parse_request_headers(); + void _parse_request_fields(); void _parse_request_uri( std::string uri ); void _parse_port_hostname(std::string host); diff --git a/srcs/colors.h b/srcs/colors.h new file mode 100644 index 0000000..0374e42 --- /dev/null +++ b/srcs/colors.h @@ -0,0 +1,25 @@ +#ifndef COLORS_H +# define COLORS_H + +# define GRAY "\e[0;30m" +# define RED "\e[0;31m" +# define GREEN "\e[0;32m" +# define YELLOW "\e[0;33m" +# define BLUE "\e[0;34m" +# define PURPLE "\e[0;35m" +# define CYAN "\e[0;36m" +# define WHITE "\e[0;37m" + +# define B_GRAY "\e[1;30m" +# define B_RED "\e[1;31m" +# define B_GREEN "\e[1;32m" +# define B_YELLOW "\e[1;33m" +# define B_BLUE "\e[1;34m" +# define B_PURPLE "\e[1;35m" +# define B_CYAN "\e[1;36m" +# define B_WHITE "\e[1;37m" + +# define RESET "\e[0m" + +#endif + diff --git a/srcs/utils.cpp b/srcs/utils.cpp index fe1af34..80db088 100644 --- a/srcs/utils.cpp +++ b/srcs/utils.cpp @@ -284,9 +284,9 @@ void print_special(std::string str) { c = str[i]; if (c == '\r') - std::cout << "\\r"; + std::cout << YELLOW << "\\r" << RESET; else if (c == '\n') - std::cout << "\\n" << "\n"; + std::cout << YELLOW << "\\n" << RESET << "\n"; else std::cout << c; fflush(stdout); diff --git a/srcs/utils.hpp b/srcs/utils.hpp index 6e325cf..bc47588 100644 --- a/srcs/utils.hpp +++ b/srcs/utils.hpp @@ -11,6 +11,7 @@ # include // tolower # include // transform # include // fflush +# include "colors.h" # define CR "\r" # define LF "\n" diff --git a/srcs/webserv/request.cpp b/srcs/webserv/request.cpp index d3d075a..941df21 100644 --- a/srcs/webserv/request.cpp +++ b/srcs/webserv/request.cpp @@ -56,7 +56,7 @@ int Webserv::_read_request(Client *client) // Messy, Need refactoring if (client->raw_request.find(CRLF CRLF) != std::string::npos) { client->header_complete = true; - client->parse_request(_servers); + client->parse_request_headers(_servers); std::cerr << client->get_rq_method_str() << " " << client->get_rq_uri() << " " << client->get_rq_version() << "\n"; // DEBUG if (client->status) return READ_COMPLETE;