diff --git a/Makefile b/Makefile index afc17fd..7298ae8 100644 --- a/Makefile +++ b/Makefile @@ -1,6 +1,6 @@ NAME = webserv -CXX = c++ +CXX = clang++ CXXFLAGS = -Wall -Wextra #-Werror CXXFLAGS += $(HEADERS_D:%=-I%) @@ -30,7 +30,6 @@ SRCS = main.cpp \ utils.cpp \ cgi_script.cpp \ Client.cpp \ - parsing_message_http.cpp \ OBJS_D = builds OBJS = $(SRCS:%.cpp=$(OBJS_D)/%.o) diff --git a/README.md b/README.md index 2f66508..831a28f 100644 --- a/README.md +++ b/README.md @@ -9,20 +9,6 @@ ? - a script called by a file extension in URI #### questions -- in client, fd is in privat, so accesible by getter, is it ok ? -- in client.cpp i fill the port, is there a default one in case it's not in the request ? -- timeout server but still works ? -- path contains double "//" from `Webserv::_get()` in response.cpp -- cgi path ? defined in config ? and root path ? : - - `Client.cpp : fill_script_path()` - - `cgi.cpp : is_cgi()` - - `cgi.cpp : set_env()` -- what if the uri contains a php file, and the config said php must be handled by cgi, but the path to this php in the uri is wrong ? - - is it ok ? `http://my_site.com/cgi-bin/php-cgi` (real path) - - is it ok ? `http://my_site.com/php-cgi` (reconstruct path ?) - - is it ok ? `http://my_site.com/something/php-cgi` (what about 'something' ?) - - is it ok ? `http://my_site.com/something/cgi-bin/php-cgi` (real path with 'something' before ? ) -- I don't save the STDIN and STDOUT before dup2 in child process, is it wrong ? - the response page is received long after the cgi-script is done, why ? --- @@ -104,18 +90,6 @@ [rfc 3875](https://www.rfc-editor.org/rfc/rfc3875) #### output cgi script : -! TODO : change all the '\n' by '\r\n' -! TODO : there is at least one header field followed by '\r\n\r\n' : - - "Content-Type" - - "Location" - - "Status" -! TODO : there is no space between filed name and ":" -! TODO?: handle Location field, either : - - local : start with '/' --> rerun the request with new uri - - client : start with ':' --> send back status code 302 --> TODO : there is no field duplicate (resolve conflicts) --> TODO : if status field, change server status for this one --> TODO : if no Location field && no Status field -> status code = 200 #### summary : - the cgi-script will send back at least one header field followed by an empty line @@ -125,13 +99,18 @@ - "Status" - the cgi-script may send back more header fields - the server must check and modify few things : - - there is no duplicate in headers fields (if there is, resolve conflict) - - there is no space between the field name and the ":" - - the newlines are of form "\r\n", and not "\n" only - - if the location field is not present, then if the status field is not present either, then the status code is 200 - - the cgi-script can return a location field, of two types : - - local redirection : start with a "/", the server must answer as if this was the request uri - - client redirection : start with ":", the server must send back a status 302 with this uri to the client + - there is no field duplicate (resolve conflicts) + - there is no space between filed name and ":" + - change all the '\n' by '\r\n' + - if no Location field && no Status field -> status code = 200 + - handle Location field, either : + - local : start with '/' --> rerun the request with new uri + - client : start with ':' --> send back status code 302 + - there is at least one header field followed by '\r\n\r\n' : + - "Content-Type" + - "Location" + - "Status" + - if status field, change server status for this one - to pass the body-message to the cgi-script, we write it into the temporary fd on which the script read it's standard input [3.1: server responsabilities](https://www.rfc-editor.org/rfc/rfc3875#section-3.1) @@ -262,6 +241,46 @@ SERVER_SOFTWARE : the server software you're using (e.g. Apache 1.3) REDIRECT_STATUS : for exemple, 200 ``` + +--- +## http errors + +#### HTTP Client Errors +- 400 Bad Request This error code indicates that the request cannot be processed because of wrong syntax usage by the client. +- 401 Unauthorized This error code indicates that the client is not authorized to receive the requested data, without authentication. A login name and password based authentication might be required to access the requested data. +- 403 Forbidden There is no way you can access the requested data. A 403 error announces that the data is off limits. +- 404 Not Found This error indicates that the resources requested by the client are currently unavailable. +- 405 Method Not Allowed This error indicates wrong usage of request method. Depending on the kind of data requested, the appropriate request method must be chosen. +- 406 Not Acceptable When the data provided by a web server does not match the specifications made in ‘Accept’ header of the client HTTP request, this error is the result. +- 407 Proxy Authentication Required This error clearly indicates that an authentication from the proxy server is required to gain access to requested resources. +- 408 Request Timeout This type of error indicates that the client was delayed in making a request, within the specified time allocated to it, by the server. +- 409 Conflict This error code is displayed when the server perceives a conflict between two requests made simultaneously by different clients, for the same resource. +- 410 Gone This error code indicates that the requested data is no longer hosted on the server and therefore further requests made for it, would be futile. +- 411 Length Required If the request made by the client does not include information about the length of the requested data or resource, this error code is displayed. +- 412 Precondition Failed Some requests made by clients come attached with conditions that need to be satisfied by the server, before data transaction may happen. If these conditions are not met, error 412 results. +- 413 Request Entity Too Large When a client makes a request which is too overwhelming for the server’s resources to handle, it presents this error code. +- 414 Requested URI Too Long A Uniform Resource Identifier (URI) is a character string used to describe a data stream or resource on a server. Error 414 occurs when the server is unable to process the URI, because of limited resources and long string length. +- 415 Unsupported Media Type A server may be designed to allow only certain formats for media files. When error 415 is displayed, it indicates that the format of file being uploaded through a client request, does not match the requisite format. +- 416 Request Range Not Satisfiable Sometimes, a client may request for only a small part of a file, instead of asking for the entire file. If this request is not specified properly and the part of the file requested does not exist, this error is displayed. +- 417 Expectation Failed This error code is displayed when the server cannot meet the specifications provided in the request. +- 422 Unprocessable Entity This error is displayed when the request made, cannot be processed due to an error in semantic structure. +- 423 Locked This error is displayed when a requested piece of data or resource has been locked, making it inaccessible for a server. +- 424 Failed Dependency A server may process a succession of requests from a client with the fulfillment of each, dependent on the one made before. This error is displayed when a request made before is not fulfilled, due to which the current request cannot be processed. +- 426 Upgrade Required This error signifies that the client may need to switch over to a secure protocol like TLS to get the request processed. +- 444 No Response This error signifies that the server has simply rejected the client request and terminated connection. +- 449 Retry With This is a request made by the server to the client, to make the request again after executing certain actions or making specific changes in request. This is an error code introduced by Microsoft. +- 499 Client Closed Request When client terminates a connection made with the server, while its processing the associated request, this error code is displayed. +- 450 Blocked By Windows Parental Controls Another error code introduced by Microsoft, this one is displayed when a URL is blocked by parental control settings on the web browsers. + +#### HTTP Server Errors +- 500 Internal Server Error A generic message displayed by the server, when the problem with the request cannot be specified by any other appropriate code. +- 501 Not Implemented This error indicates the inability of the server to process a request, as it hasn’t been configured to respond to the request method used. +- 502 Bad Gateway Sometimes, hosted pages on web servers are transmitted to client via proxy servers. If the proxy server (to which a client has sent a request), fails connecting with the web server (known as the upstream server), error 502 results. +- 503 Service Unavailable When the server is already overloaded with multiple requests, it will temporarily stop entertaining new requests, by displaying a 503 error code. +- 504 Gateway Timeout When the request made by a proxy server to the web server hosting a resource times out, error 504 is reported. +- 505 HTTP Version Not Supported An error code seen rarely, it is displayed when the web server does not support the protocol version of the client request. + + --- ## ressources diff --git a/YoupiBanane/Yeah/not_happy.bad_extension b/YoupiBanane/Yeah/not_happy.bad_extension index e69de29..70cf8a7 100644 --- a/YoupiBanane/Yeah/not_happy.bad_extension +++ b/YoupiBanane/Yeah/not_happy.bad_extension @@ -0,0 +1 @@ +inside YoupiBanane/Yeah/not_happy.bad_extension diff --git a/YoupiBanane/nop/other.pouic b/YoupiBanane/nop/other.pouic index e69de29..e8d843d 100644 --- a/YoupiBanane/nop/other.pouic +++ b/YoupiBanane/nop/other.pouic @@ -0,0 +1 @@ +inside YoupiBanane/nop/other.pouic diff --git a/YoupiBanane/nop/youpi.bad_extension b/YoupiBanane/nop/youpi.bad_extension index e69de29..a2f4e0a 100644 --- a/YoupiBanane/nop/youpi.bad_extension +++ b/YoupiBanane/nop/youpi.bad_extension @@ -0,0 +1 @@ +inside YoupiBanane/nop/youpi.bad_extension diff --git a/YoupiBanane/youpi.bad_extension b/YoupiBanane/youpi.bad_extension index e69de29..b3b2172 100644 --- a/YoupiBanane/youpi.bad_extension +++ b/YoupiBanane/youpi.bad_extension @@ -0,0 +1 @@ +inside YoupiBanane/youpi.bad_extension diff --git a/YoupiBanane/youpi.bla b/YoupiBanane/youpi.bla index e69de29..d5bdbde 100644 --- a/YoupiBanane/youpi.bla +++ b/YoupiBanane/youpi.bla @@ -0,0 +1 @@ +inside YoupiBanane/youpi.bla diff --git a/srcs/Client.cpp b/srcs/Client.cpp index e388257..1182e5d 100644 --- a/srcs/Client.cpp +++ b/srcs/Client.cpp @@ -79,11 +79,8 @@ 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) { - std::map headers; - std::string body; - if (raw_request.find(CRLF CRLF) == NPOS) return ; header_complete = true; @@ -92,27 +89,37 @@ void Client::parse_request(std::vector &servers) _parse_request_line(); if (status) return; - _parse_request_headers(); + _parse_request_fields(); + +// DEBUG +print_client("headers"); + + if (status) + return; assigned_server = ::_determine_process_server(this, servers); assigned_location = ::_determine_location(*assigned_server, _request.abs_path); _check_request_errors(); if (status) return; - _parse_port_hostname(this->get_rq_headers("Host")); + _parse_port_hostname(this->get_rq_headers("Host")); // use getter for headers because it works case insensitive - std::cerr << get_rq_method_str() << " " << get_rq_uri() << " " << get_rq_version() << "\n"; // DEBUG -/* dont clear raw_request, we need it for future reparsing of body - see call of parse_request() in _read_request() */ +// DEBUG +std::cerr << get_rq_method_str() << " " << get_rq_uri() << " " << get_rq_version() << "\n"; + +// dont clear raw_request, we need it for future reparsing of body +// see call of parse_request() in _read_request() // raw_request.clear(); } void Client::parse_request_body() { size_t pos; + pos = raw_request.find(CRLF CRLF); if (pos == NPOS) { std::cerr << "parse_request_body() bad call, header incomplete\n"; + // QUESTION from hugo : don't we change the status here ? return; } pos += CRLF_SIZE*2; @@ -192,7 +199,7 @@ void Client::parse_request_body() /////////////// // Body checks if (_request.body.size() > assigned_server->client_body_limit) - status = 413; + status = 413; // HTTP Client Errors } bool Client::fill_script_path(std::string script) @@ -247,6 +254,33 @@ void Client::clear_script() _request.script.info.clear(); } +// debug +void Client::print_client(std::string message) +{ + std::map::iterator it; + + std::cout << "\n=== DEBUG PRINT CLIENT ===\n"; + std::cout << message << ":\n----------\n\n" << "raw_request:\n__\n"; + ::print_special(raw_request); + std::cout << "\n__\n" + << "get_cl_fd() : [" << get_cl_fd() << "]\n" + << "get_cl_port() : [" << get_cl_port() << "]\n" + << "get_cl_ip() : [" << get_cl_ip() << "]\n" + << "get_rq_method_str() : [" << get_rq_method_str() << "]\n" + << "get_rq_uri() : [" << get_rq_uri() << "]\n" + << "get_rq_abs_path() : [" << get_rq_abs_path() << "]\n" + << "get_rq_query() : [" << get_rq_query() << "]\n" + << "get_rq_version() : [" << get_rq_version() << "]\n" + << "get_rq_body() : [" << get_rq_body() << "]\n" + << "get_rq_port() : [" << get_rq_port() << "]\n" + << "get_rq_hostname() : [" << get_rq_hostname() << "]\n" + << "get_rq_script_path() : [" << get_rq_script_path() << "]\n" + << "get_rq_script_info() : [" << get_rq_script_info() << "]\n" + << "headers :\n"; + for (it = _request.headers.begin(); it != _request.headers.end(); it++) + std::cout << " " << it->first << ": [" << it->second << "]\n"; + std::cout << "\n=== END PRINT CLIENT ===\n\n"; +} /********************************************* * GETTERS @@ -290,12 +324,13 @@ std::string Client::get_rq_headers(const std::string & key) const void Client::_parse_request_line() { std::vector line; - int ret; + std::string raw_line; - ret = ::parse_http_first_line(raw_request, line); - if (ret != 3) + raw_line = ::get_line(raw_request, 0, CRLF); + line = ::split_trim(raw_line, " "); + if (line.size() != 3) { - std::cerr << "err _parse_first_line(): wrong number of elements (" << ret << " instead of 3)\n"; + std::cerr << "err _parse_first_line(): wrong number of elements (" << line.size() << " instead of 3)\n"; status = 400; // "bad request" } else @@ -319,10 +354,32 @@ 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() { - // TODO: check error and adjust status - _request.headers = ::parse_http_headers(raw_request); + std::string headers; + size_t pos; + int ret; + + headers = raw_request; + // delete first line + pos = headers.find(CRLF); + if (pos != std::string::npos) + headers.erase(0, pos + std::string(CRLF).size()); + // delete body part + pos = headers.find(CRLF CRLF); + if (pos != std::string::npos) + headers.erase(pos); + else { + std::cerr << "err _parse_request_fields(): request header doesn't end with empty line\n"; + status = 400; // "bad request" + } + // copy result of parser into headers + ret = ::parse_http_headers(headers, _request.headers); + if (ret > 0) { + std::cerr << "err _parse_request_fields(): " << ret << " fields are bad formated\n"; + status = 400; // "bad request" + } + ::str_map_key_tolower(_request.headers); } void Client::_parse_port_hostname(std::string host) @@ -349,12 +406,12 @@ void Client::_check_request_errors() /////////////////////// // Request line checks if (_request.method == UNKNOWN) - status = 501; + status = 501; // HTTP Client Errors else if (_request.version.compare(0, sizeof("HTTP/1") - 1, "HTTP/1") != 0) - status = 505; + status = 505; // HTTP Client Errors else if (!(assigned_location->allow_methods & _request.method)) { - status = 405; + status = 405; // HTTP Client Errors response.append("Allow: "); response.append(::http_methods_to_str(assigned_location->allow_methods)); response.append(CRLF); @@ -396,3 +453,4 @@ bool operator==(const Client& lhs, int fd) { return lhs.get_cl_fd() == fd; } bool operator==(int fd, const Client& rhs) { return fd == rhs.get_cl_fd(); } + diff --git a/srcs/Client.hpp b/srcs/Client.hpp index b87c84c..594434c 100644 --- a/srcs/Client.hpp +++ b/srcs/Client.hpp @@ -11,7 +11,6 @@ # include // htonl, htons, ntohl, ntohs, inet_addr, inet_ntoa # include "utils.hpp" # include "ServerConfig.hpp" -# include "parsing_message_http.hpp" struct Script { @@ -72,12 +71,14 @@ 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(); void clear_script(); bool fill_script_path(std::string script); + // DEBUG + void print_client(std::string message = ""); private: int _fd; @@ -87,7 +88,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/cgi-bin/php-cgi b/srcs/cgi-bin/php-cgi index d3d3625..fa20b65 100755 --- a/srcs/cgi-bin/php-cgi +++ b/srcs/cgi-bin/php-cgi @@ -1,7 +1,8 @@ #! /usr/bin/php - - 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 1e1b359..6b71864 100644 --- a/srcs/utils.cpp +++ b/srcs/utils.cpp @@ -9,6 +9,14 @@ void throw_test() throw std::bad_alloc(); } +// notice : the use of getline make it such as +// it doesn't identify multiple delim as one : +// " something \n else " -> 1 - something +// 2 - else +// is not the same as : +// " something \n\n else " -> 1 - something +// 2 - +// 3 - else std::vector split(std::string input, char delimiter) { std::vector answer; @@ -21,25 +29,62 @@ std::vector split(std::string input, char delimiter) return answer; } -std::string trim(std::string str, char c) +std::vector + split_trim(std::string input, std::string delim, char ctrim) +{ + std::vector split_str; + std::string tmp; + size_t start = 0; + size_t end = 0; + size_t len = 0; + + while (end != std::string::npos) + { + end = input.find(delim, start); + len = end - start; + if (end == std::string::npos) + len = end; + tmp = input.substr(start, len); + if (ctrim != '\0') + tmp = trim(tmp, ctrim); + if (tmp.size() != 0) + split_str.push_back( tmp ); + start = end + delim.size(); + } + return split_str; +} + +std::string trim(std::string str, char del) { size_t pos; - // delete leadings c - pos = str.find_first_not_of(c); + // delete leadings del + pos = str.find_first_not_of(del); if (pos == std::string::npos) - return str; + pos = str.size(); str = str.substr(pos); - // delete endings c - pos = str.find_last_not_of(c); - if (pos == std::string::npos) - return str; - str = str.substr(0, pos + 1); + // delete trailing del + pos = str.find_last_not_of(del); + if (pos != std::string::npos) + str = str.substr(0, pos + 1); return str; } +//// trim a set of char +//std::string trim(std::string str, std::string del) +//{ +// std::string new_str; +// +// while (new_str.compare(str) != 0) +// { +// for (size_t i = 0; i < del.size(); i++) +// trim(str, del[i]); +// } +// return str; +//} + std::string itos(int n) { std::stringstream strs; @@ -127,7 +172,11 @@ file_type eval_file_type(const std::string &path) } -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) { if (ori_substr.empty()) return; @@ -148,27 +197,116 @@ std::string str_tolower(std::string str) return str; } -void del_line_in_str(std::string * str, size_t pos, std::string delim) +// identify a line in a string, by delim (ex. '\n') +// delete this line from the string +// and return the deleted line +std::string + extract_line(std::string & str, size_t pos, std::string delim) { - size_t begin; - size_t end; + std::string del_str; + size_t begin; + size_t end; + size_t len; - begin = (*str).rfind(delim, pos); + begin = str.rfind(delim, pos); if (begin == std::string::npos) begin = 0; else begin += delim.size(); - end = (*str).find(delim, pos); - if (end == std::string::npos) - end = 0; - else - end += delim.size(); + end = str.find(delim, pos); + len = end; + if (end != std::string::npos) + len = end - begin; - (*str).erase(begin, end - begin); + del_str = str.substr(begin, len); + str.erase(begin, len); + return del_str; } +// get a line in a string, by delim +// same as extract, except it doesn't delete it +std::string get_line(std::string str, size_t pos, std::string delim) +{ + std::string ret; + ret = ::extract_line(str, pos, delim); + return ret; +} + +size_t + parse_http_headers ( + std::string headers, + std::map & fields ) +{ + std::vector list; + std::vector::iterator it; + std::vector::iterator it_end; + size_t err = 0; + size_t pos; + std::string key; + std::string val; + + list = ::split_trim(headers, CRLF, ' '); + + it_end = list.end(); + for (it = list.begin(); it != it_end; it++) + { + pos = (*it).find(':'); + if (pos == std::string::npos) + { + err++; + continue; + } + key = (*it).substr(0, pos); + if ( key.find(' ') != std::string::npos ) + { + err++; + continue; + } +// bad idea, in cgi we need to have the original value +// key = ::str_tolower(key); // to make "key" case_insensitive + val = (*it).substr(pos + 1); + val = ::trim(val, ' '); + fields.insert( std::pair(key, val) ); + } + return err; +} + +void str_map_key_tolower(std::map & mp) +{ + std::map new_mp; + std::map::iterator it; + std::string key; + std::string value; + + for (it = mp.begin(); it != mp.end(); it++) + { + key = it->first; + value = it->second; + key = ::str_tolower(key); + new_mp.insert( std::pair(key, value) ); + } + mp.swap(new_mp); +} + +// DEBUG +void print_special(std::string str) +{ + char c; + + for (size_t i = 0; i < str.size(); i++) + { + c = str[i]; + if (c == '\r') + std::cout << YELLOW << "\\r" << RESET; + else if (c == '\n') + std::cout << YELLOW << "\\n" << RESET << "\n"; + else + std::cout << c; + fflush(stdout); + } +} bool operator==(const listen_socket& lhs, int fd) { return lhs.fd == fd; } diff --git a/srcs/utils.hpp b/srcs/utils.hpp index 7667c5e..90e734f 100644 --- a/srcs/utils.hpp +++ b/srcs/utils.hpp @@ -3,6 +3,7 @@ # define UTILS_HPP # include +# include # include # include # include // strtol, strtoul @@ -11,7 +12,8 @@ # include // stat() # include // tolower # include // transform -# include // perror +# include // perror, fflush +# include "colors.h" // for debug print_special # define CR "\r" # define LF "\n" @@ -53,16 +55,22 @@ bool operator==(const listen_socket& lhs, int fd); bool operator==(int fd, const listen_socket& rhs); std::vector split(std::string input, char delimiter); +std::vector split_trim(std::string input, std::string delim = "\n", char ctrim = '\0'); bool isNumeric(std::string str); bool isNumeric_btw(int low, int high, std::string str); std::string itos(int n); -std::string trim(std::string str, char c); +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); void replace_all_substr(std::string &str, const std::string &ori_substr, const std::string &new_substr); std::string str_tolower(std::string str); -void del_line_in_str(std::string * str, size_t pos, std::string delim); +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 print_special(std::string str); #endif diff --git a/srcs/webserv/Webserv.hpp b/srcs/webserv/Webserv.hpp index b0c5339..ff3c986 100644 --- a/srcs/webserv/Webserv.hpp +++ b/srcs/webserv/Webserv.hpp @@ -104,9 +104,9 @@ class Webserv char* _dup_env(std::string var, std::string val); char* _dup_env(std::string var, int i); std::string _exec_script(Client *client, char **env); - void _check_script_output(Client *client, std::string output); - void _check_script_status(Client *client, std::string output); - void _check_script_fields(Client *client, std::string output); + void _check_script_output(Client *client, std::string & output); + void _check_script_status(Client *client, std::string & output); + void _check_script_fields(Client *client, std::string & output); // epoll_update.cpp int _epoll_update(int fd, uint32_t events, int op); int _epoll_update(int fd, uint32_t events, int op, void *ptr); diff --git a/srcs/webserv/cgi_script.cpp b/srcs/webserv/cgi_script.cpp index 7e237dd..a337fa8 100644 --- a/srcs/webserv/cgi_script.cpp +++ b/srcs/webserv/cgi_script.cpp @@ -89,6 +89,8 @@ std::string Webserv::_exec_script(Client *client, char **env) std::string body = client->get_rq_body(); int fd_in[2]; int fd_out[2]; + int save_in = dup(STDIN_FILENO); + int save_out = dup(STDOUT_FILENO); pipe(fd_in); pipe(fd_out); @@ -127,17 +129,22 @@ std::string Webserv::_exec_script(Client *client, char **env) } if (script_output.empty()) script_output = "Status: 500\r\n\r\n"; + + dup2(save_in, STDIN_FILENO); + dup2(save_out, STDOUT_FILENO); return script_output; } -void Webserv::_check_script_output(Client *client, std::string output) +void Webserv::_check_script_output(Client *client, std::string & output) { - // TODO: it doesn't work with execve error, i don't know why yet ? _check_script_status(client, output); _check_script_fields(client, output); + // _check_script_empty_lines(client, output); + // _check_script_space_colons(client, output); + // _check_script_new_lines(client, output); } -void Webserv::_check_script_status(Client *client, std::string output) +void Webserv::_check_script_status(Client *client, std::string & output) { size_t pos; int status_pos; @@ -147,30 +154,42 @@ void Webserv::_check_script_status(Client *client, std::string output) { status_pos = pos + std::string("Status:").size(); client->status = std::strtoul(output.c_str() + status_pos, NULL, 10); - ::del_line_in_str(&output, pos, CRLF); + ::extract_line(output, pos, CRLF); } - client->status = 200; + else + client->status = 200; } -void Webserv::_check_script_fields(Client *client, std::string output) +void Webserv::_check_script_fields(Client *client, std::string & output) { std::map srv_fld; // server_field std::map scr_fld; // script_field std::map::iterator it_srv; std::map::iterator it_scr; + std::string tmp; size_t pos; - srv_fld = parse_http_headers(client->response); - scr_fld = parse_http_headers(output); - // wip: compare both map to supress duplicates + // put server headers in map + tmp = client->response; + pos = tmp.find(CRLF CRLF); + if (pos != std::string::npos) + tmp.erase(pos); + ::parse_http_headers(tmp, srv_fld); + // put script headers in map + tmp = output; + pos = tmp.find(CRLF CRLF); + if (pos != std::string::npos) + tmp.erase(pos); + ::parse_http_headers(tmp, scr_fld); + // compare both map to supress duplicates for (it_srv = srv_fld.begin(); it_srv != srv_fld.end(); it_srv++) { for (it_scr = scr_fld.begin(); it_scr != scr_fld.end(); it_scr++) { - if (it_srv->first == it_scr->first) + if (str_tolower(it_srv->first) == str_tolower(it_scr->first)) { pos = client->response.find(it_srv->first); - ::del_line_in_str(&client->response, pos, CRLF); + ::extract_line(client->response, pos, CRLF); } } } diff --git a/srcs/webserv/method_get.cpp b/srcs/webserv/method_get.cpp index b4b9baa..318b5d7 100644 --- a/srcs/webserv/method_get.cpp +++ b/srcs/webserv/method_get.cpp @@ -18,9 +18,6 @@ void Webserv::_get(Client *client) if (_is_cgi(client)) { script_output = _exec_cgi(client); - // DEBUG - std::cout << "\n____script_output____\n" << script_output << "\n_______________\n"; - // wip check output of script _check_script_output(client, script_output); client->response += script_output; return; @@ -48,8 +45,6 @@ void Webserv::_get(Client *client) } else _get_file(client, path); - - } # define MAX_FILESIZE 1000000 // (1Mo) diff --git a/srcs/webserv/parsing_message_http.cpp b/srcs/webserv/parsing_message_http.cpp index 63e67cd..2712f20 100644 --- a/srcs/webserv/parsing_message_http.cpp +++ b/srcs/webserv/parsing_message_http.cpp @@ -1,64 +1,6 @@ #include "parsing_message_http.hpp" -size_t - parse_http_first_line(std::string message, std::vector &line) -{ - std::vector sline; - std::string sub; - std::string tmp; - size_t pos; - size_t ret; - - // TODO: check for err in substr - pos = message.find(CRLF); - sub = message.substr(0, pos); - sline = ::split(sub, ' '); - ret = sline.size(); - if (ret != 3) - return ret; - for (int i = 0; i < 3; i++) - { - tmp = ::trim(sline[i], ' '); - tmp = ::trim(tmp, '\r'); - line.push_back(tmp); - } - return ret; -} - -std::map - parse_http_headers(std::string message) -{ - std::map headers; - std::vector list; - std::vector::iterator it; - std::string sub; - std::string key; - std::string val; - size_t pos; - - pos = (message).find(CRLF CRLF); - sub = (message).substr(0, pos); - list = ::split(sub, '\n'); - if ( maybe_http_first_line( *list.begin() ) ) - list.erase(list.begin()); - - for (it = list.begin(); it != list.end(); it++) - { - // TODO: if pattern is not "NAME: value" return error - pos = (*it).find(':'); - key = (*it).substr( 0, pos ); - key = ::trim(key, ' '); - key = ::trim(key, '\r'); - key = ::str_tolower(key); - val = (*it).substr( pos + 1 ); - val = ::trim(val, ' '); - val = ::trim(val, '\r'); - headers.insert( std::pair(key, val) ); - } - return headers; -} - std::string parse_http_body(std::string message) { @@ -73,18 +15,3 @@ std::string return body; } -bool maybe_http_first_line(std::string str) -{ -// method SP target SP version https://www.rfc-editor.org/rfc/rfc7230.html#section-3.1.1 -// version SP status SP reason https://www.rfc-editor.org/rfc/rfc7230.html#section-3.1.2 - - std::vector sline; - - sline = ::split(str, ' '); - if (sline.size() != 3) - return false; - if (sline[0].find(':') != std::string::npos) - return false; - return true; -} - diff --git a/srcs/webserv/parsing_message_http.hpp b/srcs/webserv/parsing_message_http.hpp index 67cd473..d1e77ed 100644 --- a/srcs/webserv/parsing_message_http.hpp +++ b/srcs/webserv/parsing_message_http.hpp @@ -8,18 +8,14 @@ # include # include "utils.hpp" -size_t - parse_http_first_line(std::string message, std::vector &line); - std::map - parse_http_headers(std::string message); + parse_http_headers ( + std::string headers, + std::map fields ) std::string parse_http_body(std::string message); -bool - maybe_http_first_line(std::string); - // http message structure : // // start-line diff --git a/srcs/webserv/request.cpp b/srcs/webserv/request.cpp index 24f2a80..09b5c6e 100644 --- a/srcs/webserv/request.cpp +++ b/srcs/webserv/request.cpp @@ -54,7 +54,7 @@ int Webserv::_read_request(Client *client) if (!client->header_complete) { - client->parse_request(_servers); + client->parse_request_headers(_servers); if (client->status) return READ_COMPLETE; if (client->header_complete)