From 031932e88794c796f7c0ea06483e0ede95364975 Mon Sep 17 00:00:00 2001 From: hugogogo Date: Mon, 1 Aug 2022 00:07:51 +0200 Subject: [PATCH] added trim in utils, added parse request header, mv pdf in docs --- .../webserv.en.subject.pdf | Bin .../webserv.fr.subject.pdf | Bin .../webserv_correction.pdf | Bin headers/Client.hpp | 2 +- headers/Webserv.hpp | 6 +- headers/utils.hpp | 3 +- index.html | 13 --- srcs/utils.cpp | 24 ++++-- srcs/webserv/request.cpp | 81 ++++++++++++++++++ srcs/webserv/response.cpp | 8 +- 10 files changed, 107 insertions(+), 30 deletions(-) rename webserv.en.subject.pdf => docs/webserv.en.subject.pdf (100%) rename webserv.fr.subject.pdf => docs/webserv.fr.subject.pdf (100%) rename webserv_correction.pdf => docs/webserv_correction.pdf (100%) delete mode 100644 index.html diff --git a/webserv.en.subject.pdf b/docs/webserv.en.subject.pdf similarity index 100% rename from webserv.en.subject.pdf rename to docs/webserv.en.subject.pdf diff --git a/webserv.fr.subject.pdf b/docs/webserv.fr.subject.pdf similarity index 100% rename from webserv.fr.subject.pdf rename to docs/webserv.fr.subject.pdf diff --git a/webserv_correction.pdf b/docs/webserv_correction.pdf similarity index 100% rename from webserv_correction.pdf rename to docs/webserv_correction.pdf diff --git a/headers/Client.hpp b/headers/Client.hpp index 2536012..65a9b46 100644 --- a/headers/Client.hpp +++ b/headers/Client.hpp @@ -26,4 +26,4 @@ struct Client }; -#endif \ No newline at end of file +#endif diff --git a/headers/Webserv.hpp b/headers/Webserv.hpp index bc18b87..7f867e2 100644 --- a/headers/Webserv.hpp +++ b/headers/Webserv.hpp @@ -107,7 +107,11 @@ class Webserv // void _serve_file(Client *client, std::string page); void _exec_cgi_script(Client *client); - void _parse_request(); + void _parse_request(Client *client); + void _parse_request_line(Client *client, std::string rline); + void _parse_request_headers + ( Client *client + , std::vector list); // // END TMP HUGO TEST CGI }; diff --git a/headers/utils.hpp b/headers/utils.hpp index 9aa6162..ab47fa4 100644 --- a/headers/utils.hpp +++ b/headers/utils.hpp @@ -7,7 +7,8 @@ # include std::vector split(std::string input, char delimiter); -char* itoa(int n); +std::string itoa(int n); +std::string trim(std::string str, char c); #endif diff --git a/index.html b/index.html deleted file mode 100644 index ae36576..0000000 --- a/index.html +++ /dev/null @@ -1,13 +0,0 @@ - - - - - - - -
- -
- - - diff --git a/srcs/utils.cpp b/srcs/utils.cpp index 6423231..72027ef 100644 --- a/srcs/utils.cpp +++ b/srcs/utils.cpp @@ -3,9 +3,9 @@ std::vector split(std::string input, char delimiter) { - std::vector answer; - std::stringstream ss(input); - std::string temp; + std::vector answer; + std::stringstream ss(input); + std::string temp; while (getline(ss, temp, delimiter)) answer.push_back(temp); @@ -13,13 +13,19 @@ std::vector split(std::string input, char delimiter) return answer; } -char* itoa(int n) +std::string trim(std::string str, char c) { - std::stringstream strs; - char * str; + str = str.substr(str.find_first_not_of(c)); + str = str.substr(0, str.find_last_not_of(c) + 1); - strs << n; - str = (char*)(strs.str().c_str()); - return (str); + return str; +} + +std::string itoa(int n) +{ + std::stringstream strs; + + strs << n; + return ( strs.str() ); } diff --git a/srcs/webserv/request.cpp b/srcs/webserv/request.cpp index 17c372b..0e3870f 100644 --- a/srcs/webserv/request.cpp +++ b/srcs/webserv/request.cpp @@ -32,6 +32,87 @@ void Webserv::_read_request(Client *client) buf[ret] = '\0'; client->raw_request.append(buf); + _parse_request(client); _epoll_update(client->fd, EPOLLOUT, EPOLL_CTL_MOD, client); } + +// http headers : +// 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 Webserv::_parse_request(Client *client) +{ + std::string sub; + std::vector list; + size_t pos; + + pos = (client->raw_request).find("\r\n\r\n"); + sub = (client->raw_request).substr(0, pos); + list = split(sub, '\n'); + // request_line + _parse_request_line(client, *list.begin()); + list.erase(list.begin()); + // headers + _parse_request_headers(client, list); + // message-body + client->request.insert( std::pair("Body-Message", &client->raw_request[pos + 4]) ); + + // TMP DEBUG +// std::map::iterator itm; +// std::map::iterator itm_end; +// std::cout << "\n====== REQUEST HEADERS\n"; +// for (itm = client->request.begin(), itm_end = client->request.end(); itm != itm_end; itm++) +// std::cout << "[" << itm->first << "] [" << itm->second << "]\n"; +// std::cout << "\n====== END\n"; +} + +void Webserv::_parse_request_line(Client *client, std::string rline) +{ + std::vector sline; + std::string tmp; + + sline = split(rline, ' '); + if (sline.size() != 3) + { + std::cerr << "err _parse_request_line(): "; + throw std::runtime_error("bad request-line header"); + } + // method + tmp = ::trim(sline[0], ' '); + tmp = ::trim(tmp, '\r'); + client->request.insert( std::pair("Method", tmp) ); + // TODO uri in request_line : + // https://www.rfc-editor.org/rfc/rfc7230#section-5.3 + // https://stackoverflow.com/questions/40311306/when-is-absoluteuri-used-from-the-http-request-specs + tmp = ::trim(sline[1], ' '); + tmp = ::trim(tmp, '\r'); + client->request.insert( std::pair("Request-URI", tmp) ); + // http version + tmp = ::trim(sline[2], ' '); + tmp = ::trim(tmp, '\r'); + client->request.insert( std::pair("HTTP-Version", tmp) ); +} + +void Webserv::_parse_request_headers + ( Client *client + , std::vector list) +{ + std::string key; + std::string val; + std::vector::iterator it; + size_t pos; + + for (it = list.begin(); it != list.end(); it++) + { + pos = (*it).find(':'); + key = (*it).substr( 0, pos ); + key = ::trim(key, ' '); + key = ::trim(key, '\r'); + val = (*it).substr( pos + 1 ); + val = ::trim(val, ' '); + val = ::trim(val, '\r'); + client->request.insert( std::pair(key, val) ); + } +} + diff --git a/srcs/webserv/response.cpp b/srcs/webserv/response.cpp index 26a23bd..dbd0219 100644 --- a/srcs/webserv/response.cpp +++ b/srcs/webserv/response.cpp @@ -83,8 +83,8 @@ void Webserv::_insert_status_line(Client *client) void Webserv::_get_ressource(Client *client) { std::ifstream ifd; // For chunk, ifstream directly in struct CLient for multiples read without close() ? - char buf[MAX_FILESIZE+1]; - char *tmp; + char buf[MAX_FILESIZE+1]; + const char *tmp; // Mini parsing à l'arrache du PATH std::string path; @@ -128,10 +128,8 @@ void Webserv::_get_ressource(Client *client) client->response.append("Content-Type: text/html; charset=UTF-8\r\n"); client->response.append("Content-Length: "); - // tmp = ::ft_itoa(ifd.gcount()); - tmp = ::itoa(ifd.gcount()); + tmp = ::itoa(ifd.gcount()).c_str(); client->response.append(tmp); - // delete tmp; client->response.append("\r\n"); // Body