diff --git a/includes/requests/Errors.hpp b/includes/requests/Errors.hpp index c57a05d..a31b50b 100644 --- a/includes/requests/Errors.hpp +++ b/includes/requests/Errors.hpp @@ -6,7 +6,7 @@ /* By: mmoussou ); static std::map message; private: diff --git a/includes/server/Client.hpp b/includes/server/Client.hpp index a13a55a..f1d5efc 100644 --- a/includes/server/Client.hpp +++ b/includes/server/Client.hpp @@ -6,7 +6,7 @@ /* By: adjoly +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2025/04/14 14:14:39 by adjoly #+# #+# */ -/* Updated: 2025/04/14 15:00:41 by adjoly ### ########.fr */ +/* Updated: 2025/04/17 12:46:04 by mmoussou ### ########.fr */ /* */ /* ************************************************************************** */ @@ -18,16 +18,26 @@ #include #include +namespace webserv { +namespace server { + class Client { public: - Client(int, sockaddr_in); + Client(int, sockaddr_in, config::Config *); ~Client(void); void answer(void); + private: + void _getRequest(std::string); + int _fd; struct sockaddr_in _client_addr; http::IRequest *_request; http::Response *_response; - config::Config *_conf; + config::Server *_conf; + Logger *_log; }; + +} // -namespace server +} // -namespace webserv diff --git a/src/server/Client.cpp b/src/server/Client.cpp new file mode 100644 index 0000000..29d6e85 --- /dev/null +++ b/src/server/Client.cpp @@ -0,0 +1,105 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* Client.cpp :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: mmoussou + +using namespace server; + +/*class Client { + public: + Client(int, sockaddr_in, config::Config *); + ~Client(void); + + void answer(void); + + private: + void getRequest(void); + + int _fd; + struct sockaddr_in _client_addr; + http::IRequest *_request; + http::Response *_response; + config::Server *_conf; + std::string _request_method; +};*/ + +Client::Client(int fd, sockaddr_in socket, config::Servr *conf, Logger *log) +{ + this->_fd = fd; + this->_client_addr = socket; + this->_conf = conf; + this->_log = log; + + std::string received_data; + char buffer[BUFFER_SIZE]; + ssize_t bytes_received; + do + { + std::memset(buffer, 0, BUFFER_SIZE); + bytes_received = recv(fd, buffer, BUFFER_SIZE - 1, 0); + if (bytes_received == -1) + { + _log->error("failed to receive request"); + continue; + } + received_data += std::string(buffer, bytes_received); + } + while (buffer[bytes_received]); + + + this->getRequest(request_str); +} + +void Client::_getRequest(std::string request_str) +{ + std::string method = request_str.substr(0, request_str.substr(0, 4).find_last_not_of(" ") + 1); + + if (method == "GET") + { + _log->info("get request received"); + this->_request = new http::Get(request_str); + } + else if (method == "DELETE") + { + _log->info("delete request received"); + this->_request = new http::Delete(request_str); + } + else if (method == "POST") + { + _log->info("post request received"); + this->_request = new http::Post(request_str); + } + else + { + _log->info("unsupported request received"); + this->_request = new http::Get(); + this->_request->setMethod("501"); + } +} + +void Client::answer(void) +{ + std::string response; + + if (this->_request == "GET" || this->_request == "DELETE" || this->_request == "POST") + response = this->_request.execute().str(); + else + response = "HTTP/1.1 501 Not Implemented\r\nContent-Type: text/html\r\n\r\n

501 Not Implemented

"; + send(this->_fd, response.c_str(), response.length(), 0); +} + + +Client::~Client(void) +{ + delete this->_request; + delete this->_response; +} diff --git a/upload.html b/upload.html deleted file mode 100644 index a9e575f..0000000 --- a/upload.html +++ /dev/null @@ -1,90 +0,0 @@ - - - - upload - - - - - -
-
- -

-

-
- - - - -