From b2d081f548516db82b51cf8e61e320b24e38e0e8 Mon Sep 17 00:00:00 2001 From: nicolas-arnaud Date: Wed, 9 Nov 2022 15:13:00 +0100 Subject: [PATCH] save 22-11-9-1 --- Makefile | 2 +- includes/Socket.hpp | 10 ++-- includes/webserv.hpp | 10 ++-- srcs/load/Route.cpp | 13 +++--- srcs/load/Socket.cpp | 106 +++++++++++++++++++++++-------------------- 5 files changed, 75 insertions(+), 66 deletions(-) diff --git a/Makefile b/Makefile index b92949f..d59aeba 100644 --- a/Makefile +++ b/Makefile @@ -10,7 +10,7 @@ CXXFLAGS= -g -I includes -Werror -Wextra -Wall -std=c++98 all : $(NAME) $(NAME): $(OBJS) - $(CXX) -g $(OBJS) -o $(NAME) + $(CXX) -g -fsanitize=address $(OBJS) -o $(NAME) clean: rm -rf $(OBJS) diff --git a/includes/Socket.hpp b/includes/Socket.hpp index 20aa479..faaaaf2 100644 --- a/includes/Socket.hpp +++ b/includes/Socket.hpp @@ -7,15 +7,13 @@ class Socket { Socket *_parent; std::vector _childs; struct sockaddr_in _address; - string _tmp; string _header; string _content; - string _method; - string _uri; - string _host; - string _extension; + std::map > _request; - int answer(Env *env, string request); + bool getRequest(string paquet); + bool parseHeader(); + int answer(Env *env); void send_answer(string msg); bool waitHeader(); public: diff --git a/includes/webserv.hpp b/includes/webserv.hpp index 23423be..2874854 100644 --- a/includes/webserv.hpp +++ b/includes/webserv.hpp @@ -32,6 +32,11 @@ using std::cout; using std::strerror; using std::string; +typedef struct listen_s { + string ip; + int port; +} listen_t; + class Env; class Server; class Socket; @@ -41,11 +46,6 @@ class JSONNode; typedef std::map JSONObject; typedef std::vector JSONList; -typedef struct listen_s { - string ip; - int port; -} listen_t; - void *ft_memset(void *b, int c, size_t len); bool isInt(string str); std::vector split(string str, char delim); diff --git a/srcs/load/Route.cpp b/srcs/load/Route.cpp index a3ebf63..0134fa7 100644 --- a/srcs/load/Route.cpp +++ b/srcs/load/Route.cpp @@ -60,7 +60,7 @@ string Route::getIndex(string uri, string path) { std::cerr << "stat() error on " << path << ": " << strerror(errno) << "\n"; } - content << "
    "; + content << "
"; closedir(dir); } if (!_autoindex) @@ -76,15 +76,15 @@ string Route::correctUri(string uri) { std::vector::iterator it; std::vector::iterator it2; - // cout << "Correcting request: " << uri << " with root: " << _root << "\n"; - ret << _root; + cout << "Correcting request: " << uri << " with root: " << _root << "\n"; + ret << "./" << _root; std::vector loc_split = split(_location, '/'); std::vector uri_split = split(uri, '/'); it2 = uri_split.begin(); for (it = loc_split.begin(); it < loc_split.end(); it++) { - while (*it2 == "") + while ( it2 < uri_split.end() && *it2 == "") it2++; - while (*it == "") + while (it < loc_split.end() && *it == "") it++; if (it != loc_split.end()) it2++; @@ -93,6 +93,7 @@ string Route::correctUri(string uri) { while (it2 < uri_split.end()) { ret << "/" << *(it2++); } - // cout << "resutlt: " << ret.str() << "\n"; + + cout << "result: " << ret.str() << "\n"; return ret.str(); } diff --git a/srcs/load/Socket.cpp b/srcs/load/Socket.cpp index 67002c9..37b0ba0 100644 --- a/srcs/load/Socket.cpp +++ b/srcs/load/Socket.cpp @@ -93,73 +93,83 @@ void Socket::refresh(Env *env) { _childs.erase(it); } else { buffer[valread] = '\0'; - (*it)->answer(env, buffer); + if ((*it)->getRequest(buffer)) + (*it)->answer(env); } } } } -bool Socket::waitHeader() { - if (_tmp.length() < 1) - return false; - std::vector lines = split(_tmp, '\n'); - bool is_valid = false; - for (std::vector::iterator it = lines.begin(); it < lines.end(); - it++) { - if (*it == "\r") - is_valid = true; +bool Socket::parseHeader() { + std::vector lines = split(_header, '\n'); + std::vector line; + if (lines.size() > 0) { + for (std::vector::iterator it = lines.begin() + 1; it < lines.end(); it++) { + line = split(*it, ' '); + cout << line.at(0) << "scraped from header\n"; + _request[line.at(0)] = std::vector(line.begin() + 1, line.end()); + } } - if (!is_valid) + std::vector method = split(lines.at(0), ' '); + if (method.at(0) == "POST" && _request.find("Content-Length:") == _request.end() && _request.find("Transfer-Encoding:") ==_request.end()) return false; - _header = _tmp; - _tmp = ""; + _request["Method:"] = method; return true; } -int Socket::answer(Env *env, string request) { - _tmp += request; - cout << "|===|request|===>" << _tmp << "|===||\n"; - if (_header == "") { - waitHeader(); +bool Socket::getRequest(string paquet) { + cout << "|===|paquet|===>" << paquet << "|===||\n"; + if (paquet.length() < 1) //HTTPS? + return false; + std::vector lines = split(paquet, '\n'); + long chunk_len = (_content.length() > 0 && _request["Transfer-Encoding:"].size() && _request["Transfer-Encoding:"].at(0) == "chunked") ? std::strtol(lines.at(0).substr(1).c_str(), 0, 16) : -1; + cout << "Chunk length: " << chunk_len << "\n"; + for (std::vector::iterator it = lines.begin(); it < lines.end(); it++) { + if (*it == "\r" && _content.length() == 0) + this->parseHeader(); + if (*it != "\r" && _content.length() == 0) + _header += *it + "\n"; + else if (chunk_len == -1 || it != lines.begin()) + _content += *it + "\n"; } - std::vector lines = split(_header, '\n'); - std::vector head = split(lines.at(0), ' '); - this->_method = head.at(0); - this->_uri = head.at(1); - for (std::vector::iterator it = lines.begin(); it < lines.end(); it++) - if (it->find("Host:") != string::npos) - this->_host = it->substr(6); - cout << "Method: " << this->_method << "\n"; - cout << "URI: " << this->_uri << "\n"; - cout << "Host: " << this->_host << "\n"; + cout << "Header: \n-|" << _header << "|-\n"; + cout << "Content: \n-|" << _content << "|-\n"; + if (_content.length() > 0) { + _content.resize(_content.length() - 1); + if ( _request["Method:"].at(0) == "GET" || (chunk_len == 0 || std::strtoul(_request["Content-Length:"].at(0).c_str(), 0, 10) <= _content.length() ) ) { + cout << "Request received\n"; + return true; + } + } else + _header.resize(_header.length() - 1); + return false; +} + +int Socket::answer(Env *env) { + cout << "Method: " << _request["Method:"].at(0) << "\n"; + cout << "URI: " << _request["Method:"].at(1) << "\n"; + cout << "Host: " << _request["Host:"].at(0) << "\n"; string ret; std::stringstream answer; answer << "HTTP/1.1"; - Server *server = env->choose_server(_parent, split(lines.at(1), ' ').at(1)); - Route *route = server->get_route(this->_uri); - std::vector headers; - - if ((head.at(0) != "GET" && head.at(0) != "POST" && - head.at(0) != "DELETE") || - head.size() < 2) + Server *server = env->choose_server(_parent, _request["Host:"].at(0)); + Route *route = server->get_route(_request["Method:"].at(1)); + string method = _request["Method:"].at(0); + std::vector allowed; + if (method != "GET" && method != "POST" && method != "DELETE") send_answer( "HTTP/1.1 405 Method Not Allowed\r\nContent-length: 0\r\n\r\n"); - else if ((headers = route->getHeadersLst()).size() > 0) { - if (std::find(headers.begin(), headers.end(), head.at(0)) == - headers.end()) - send_answer( - "HTTP/1.1 405 Method Not Allowed\r\nContent-length: 0\r\n\r\n"); - } else if ((headers = server->getHeadersLst()).size() > 0) { - if (std::find(headers.begin(), headers.end(), head.at(0)) == - headers.end()) - send_answer( - "HTTP/1.1 405 Method Not Allowed\r\nContent-length: 0\r\n\r\n"); + else if ((allowed = route->getHeadersLst()).size() > 0) { + if (std::find(allowed.begin(), allowed.end(), method) == allowed.end()) + send_answer("HTTP/1.1 405 Method Not Allowed\r\nContent-length: 0\r\n\r\n"); + } else if ((allowed = server->getHeadersLst()).size() > 0) { + if (std::find(allowed.begin(), allowed.end(), method) == allowed.end()) + send_answer("HTTP/1.1 405 Method Not Allowed\r\nContent-length: 0\r\n\r\n"); } - - string path = route->correctUri(this->_uri); + string path = route->correctUri(_request["Method:"].at(1)); cout << "Path: " << path << "\n"; - ret = route->getIndex(this->_uri, path); + ret = route->getIndex(_request["Method:"].at(1), path); if (ret == "") { cout << "No index: lf file\n"; ret = read_file(path);