From 4fbaed787d16312f6b30ad081dfd5558ccd8600b Mon Sep 17 00:00:00 2001 From: y-syo Date: Mon, 3 Feb 2025 18:39:23 +0100 Subject: [PATCH 01/10] =?UTF-8?q?=E3=80=8C=F0=9F=8F=97=EF=B8=8F=E3=80=8D?= =?UTF-8?q?=20wip(requests):=20requests=20parsing=20and=20basic=20handling?= =?UTF-8?q?=20of=20GET=20responses?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- Makefile | 8 +- includes/requests/HttpRequest.hpp | 27 ++++ includes/requests/HttpResponse.hpp | 38 ++++++ .../requests/default.hpp | 26 ++-- index.html | 11 ++ src/main.cpp | 115 ++++++++++++++++++ src/requests_handling/HttpRequests.cpp | 55 +++++++++ src/requests_handling/HttpResponse.cpp | 69 +++++++++++ 8 files changed, 336 insertions(+), 13 deletions(-) create mode 100644 includes/requests/HttpRequest.hpp create mode 100644 includes/requests/HttpResponse.hpp rename src/webserv.cpp => includes/requests/default.hpp (57%) create mode 100644 index.html create mode 100644 src/main.cpp create mode 100644 src/requests_handling/HttpRequests.cpp create mode 100644 src/requests_handling/HttpResponse.cpp diff --git a/Makefile b/Makefile index 6116405..dceb576 100644 --- a/Makefile +++ b/Makefile @@ -6,7 +6,7 @@ # By: adjoly +#+ +:+ +#+ # # +#+#+#+#+#+ +#+ # # Created: 2024/10/25 16:09:27 by adjoly #+# #+# # -# Updated: 2025/01/21 13:13:53 by mmoussou ### ########.fr # +# Updated: 2025/02/03 16:43:32 by mmoussou ### ########.fr # # # # **************************************************************************** # @@ -18,6 +18,8 @@ CC = c++ OBJSDIR = obj/ +INCLUDES = ./includes + SRCS = $(shell find . -name '*.cpp') OBJS = $(addprefix $(OBJSDIR), $(SRCS:.cpp=.o)) @@ -38,12 +40,12 @@ endif all: $(NAME) $(NAME): $(OBJS) - @$(CC) $(FLAGS) -I . $(OBJS) -o $(NAME) + @$(CC) $(FLAGS) -I$(INCLUDES) $(OBJS) -o $(NAME) @printf "$(YELLOW)「✨」 feat($(NAME)): program compiled\n" $(OBJSDIR)%.o: %.cpp @mkdir -p $(@D) - @$(CC) $(FLAGS) -I . -c $< -o $@ + @$(CC) $(FLAGS) -I$(INCLUDES) -c $< -o $@ @printf "$(DELETE)$(GREEN)「🔨」 build($<): object compiled\n" clean: diff --git a/includes/requests/HttpRequest.hpp b/includes/requests/HttpRequest.hpp new file mode 100644 index 0000000..e828a51 --- /dev/null +++ b/includes/requests/HttpRequest.hpp @@ -0,0 +1,27 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* HttpRequest.hpp :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: mmoussou headers; + std::string body; +}; + +HttpRequest parseRequest(const std::string &rawRequest); + +#endif diff --git a/includes/requests/HttpResponse.hpp b/includes/requests/HttpResponse.hpp new file mode 100644 index 0000000..f9784a0 --- /dev/null +++ b/includes/requests/HttpResponse.hpp @@ -0,0 +1,38 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* HttpResponse.hpp :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: mmoussou _headers; + std::string _body; + +}; + +#endif diff --git a/src/webserv.cpp b/includes/requests/default.hpp similarity index 57% rename from src/webserv.cpp rename to includes/requests/default.hpp index b60c590..9584306 100644 --- a/src/webserv.cpp +++ b/includes/requests/default.hpp @@ -1,20 +1,26 @@ /* ************************************************************************** */ /* */ /* ::: :::::::: */ -/* webserv.cpp :+: :+: :+: */ +/* default.hpp :+: :+: :+: */ /* +:+ +:+ +:+ */ -/* By: adjoly +#+ +:+ +#+ */ +/* By: mmoussou +#include +#include +#include #include -int main(int ac, char **av, char **env) { - (void)ac; - (void)av; - (void)env; - std::cout << "test" << std::endl; -} +#include "requests/HttpRequest.hpp" +#include "requests/HttpResponse.hpp" + +#endif diff --git a/index.html b/index.html new file mode 100644 index 0000000..89abca8 --- /dev/null +++ b/index.html @@ -0,0 +1,11 @@ + + + + +

evilge

+ + diff --git a/src/main.cpp b/src/main.cpp new file mode 100644 index 0000000..fb4b71b --- /dev/null +++ b/src/main.cpp @@ -0,0 +1,115 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* main.cpp :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: mmoussou +#include +#include +#include +#include +#include +#include +#include +#include + +#include "requests/default.hpp" + +#define PORT 8080 +#define BUFFER_SIZE 4096 + +int server_socket; + +void close_socket(int signal) +{ + close(server_socket); + exit(signal); +} + +int main() { + // handle ctrl-C to close server socket + if (signal(SIGINT, close_socket) == SIG_ERR) { + std::cerr << "Error registering signal handler!" << std::endl; + return 1; + } + + // create a socket + server_socket = socket(AF_INET, SOCK_STREAM, 0); + if (server_socket == -1) { + std::cerr << "Failed to create socket" << std::endl; + return 1; + } + + // prepare the server address + sockaddr_in server_address; + std::memset(&server_address, 0, sizeof(server_address)); + server_address.sin_family = AF_INET; + server_address.sin_addr.s_addr = INADDR_ANY; + server_address.sin_port = htons(PORT); + + // bind the socket to the address + if (bind(server_socket, (sockaddr*)&server_address, sizeof(server_address)) == -1) { + std::cerr << "Failed to bind socket" << std::endl; + return 1; + } + + // listen for incoming connections + if (listen(server_socket, 5) == -1) { + std::cerr << "Failed to listen on socket" << std::endl; + return 1; + } + + std::cout << "Server is listening on port " << PORT << std::endl; + + while (true) { + // accept an incoming connection + sockaddr_in client_address; + socklen_t client_address_len = sizeof(client_address); + int client_socket = accept(server_socket, (sockaddr*)&client_address, &client_address_len); + if (client_socket == -1) { + std::cerr << "Failed to accept connection" << std::endl; + continue; + } + + // receive the HTTP request + char buffer[BUFFER_SIZE]; + std::memset(buffer, 0, BUFFER_SIZE); + ssize_t bytes_received = recv(client_socket, buffer, BUFFER_SIZE - 1, 0); + if (bytes_received == -1) { + std::cerr << "Failed to receive request" << std::endl; + close(client_socket); + continue; + } + + // parse the request + std::string received_data(buffer, bytes_received); + HttpRequest request = parseRequest(received_data); + + std::cout << "Received " << request.method << " request for " << request.target << std::endl; + + + // handle the request + if (request.method == "GET") + { + HttpResponse response(request); + send(client_socket, response.str().c_str(), response.str().length(), 0); + } + else + { + std::string response = "HTTP/1.1 501 Not Implemented\r\nContent-Type: text/html\r\n\r\n

501 Not Implemented

"; + send(client_socket, response.c_str(), response.length(), 0); + } + + close(client_socket); + } + + close(server_socket); + return 0; +} diff --git a/src/requests_handling/HttpRequests.cpp b/src/requests_handling/HttpRequests.cpp new file mode 100644 index 0000000..74fc0a3 --- /dev/null +++ b/src/requests_handling/HttpRequests.cpp @@ -0,0 +1,55 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* HttpRequests.cpp :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: mmoussou > request.method >> request.target >> request.protocol; + request.target.insert(request.target.begin(), '.'); + } + + while (std::getline(stream, line) && line != "\r") + { + size_t delimiter_index = line.find(':'); + if (delimiter_index != std::string::npos) + { + std::string key = line.substr(0, delimiter_index); + std::string value = line.substr(delimiter_index + 2); + request.headers[key] = value; + } + } + + std::ostringstream body_stream; + while (std::getline(stream, line)) + body_stream << line << "\n"; + request.body = body_stream.str(); + + std::cout << "method: " << request.method << std::endl; + std::cout << "target: " << request.target << std::endl; + std::cout << "protocol: " << request.protocol << std::endl; + std::cout << std::endl; + std::cout << "-- headers --" << std::endl; + for (std::map::const_iterator it = request.headers.begin(); it != request.headers.end(); ++it) + std::cout << it->first << ": " << it->second << std::endl; + std::cout << std::endl; + std::cout << "-- body --" << std::endl << request.body << std::endl; + + return request; +} diff --git a/src/requests_handling/HttpResponse.cpp b/src/requests_handling/HttpResponse.cpp new file mode 100644 index 0000000..ea412eb --- /dev/null +++ b/src/requests_handling/HttpResponse.cpp @@ -0,0 +1,69 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* HttpResponse.cpp :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: mmoussou _headers; +std::string _body;*/ + +HttpResponse::HttpResponse(const HttpRequest &request): _protocol(request.protocol) +{ + std::ifstream file(request.target.c_str(), std::ios::binary); + if (file) + { + this->_status_code = 200; + this->_status_text = "OK"; + this->_headers["Content-Type"] = "text/html"; + this->_body = std::string((std::istreambuf_iterator(file)), std::istreambuf_iterator()); + } + else + { + this->_status_code = 404; + this->_status_text = "Not Found"; + this->_headers["Content-Type"] = "text/html"; + this->_body = "nuh uh, get 404'd >:D"; + } +} + +HttpResponse::HttpResponse(const HttpResponse &cpy) +{ + (void) cpy; +} + +HttpResponse::~HttpResponse(void) +{ +} + +HttpResponse &HttpResponse::operator=(const HttpResponse &cpy) +{ + (void) cpy; + return (*this); +} + +std::string HttpResponse::str(void) const +{ + std::ostringstream response; + + response << this->_protocol << " " << this->_status_code << " " << this->_status_text; + response << "\r\n"; + + for (std::map::const_iterator it = this->_headers.begin(); it != this->_headers.end(); ++it) + response << it->first << ": " << it->second << "\r\n"; + + response << "\r\n"; + response << this->_body; + + return (response.str()); +} From d7baf04e5f90f28bbcb83726f2a931cf808d1461 Mon Sep 17 00:00:00 2001 From: y-syo Date: Mon, 3 Feb 2025 18:53:18 +0100 Subject: [PATCH 02/10] =?UTF-8?q?=E3=80=8C=F0=9F=93=9D=E3=80=8D=20doc(NORM?= =?UTF-8?q?.md):=20fixed=20the=20norm?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- NORM.md | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/NORM.md b/NORM.md index 1692f68..931e93c 100644 --- a/NORM.md +++ b/NORM.md @@ -2,14 +2,14 @@ ## File Structure -- Header Files (.h): +- Header Files (.hpp/.h): - Include declarations of classes, functions, constants, and macros. - Use include guards or #pragma once to prevent multiple inclusions. - Source Files (.cpp): - Contain the implementation of functions and classes. ## Naming Conventions -- Variables: camelCase (e.g., int studentAge;) +- Variables: snake_case (e.g., int student_age;) - Functions: camelCase (e.g., void calculateSum();) - Classes: PascalCase (e.g., class UserAccount {};) - Constants: ALL_CAPS_SNAKE_CASE (e.g., const int MAX_SIZE = 100;) From 935de681d9787927dfec2933653ae151f68dd5f0 Mon Sep 17 00:00:00 2001 From: y-syo Date: Tue, 11 Feb 2025 07:41:56 +0100 Subject: [PATCH 03/10] =?UTF-8?q?=E3=80=8C=F0=9F=8F=97=EF=B8=8F=E3=80=8D?= =?UTF-8?q?=20wip:=20work=20in=20progress,=20not=20done=20yet.?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- includes/requests/HttpResponse.hpp | 21 +++++++++++++-------- src/requests_handling/HttpResponse.cpp | 13 +------------ 2 files changed, 14 insertions(+), 20 deletions(-) diff --git a/includes/requests/HttpResponse.hpp b/includes/requests/HttpResponse.hpp index f9784a0..5a6cde5 100644 --- a/includes/requests/HttpResponse.hpp +++ b/includes/requests/HttpResponse.hpp @@ -6,7 +6,7 @@ /* By: mmoussou Date: Tue, 11 Feb 2025 13:52:45 +0100 Subject: [PATCH 04/10] =?UTF-8?q?=E3=80=8C=F0=9F=8F=97=EF=B8=8F=E3=80=8D?= =?UTF-8?q?=20wip(includes):=20very=20very=20wip=20of=20the=20headers=20(t?= =?UTF-8?q?his=20commit=20should=20be=20deleted=20later)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- includes/default.hpp | 20 +++++++ includes/requests/HttpRequest.hpp | 78 ++++++++++++++++++++++---- includes/requests/HttpResponse.hpp | 53 ++++++++++------- includes/requests/default.hpp | 32 +++++++++-- includes/server/default.hpp | 27 +++++++++ includes/webserv.hpp | 33 +++++++++++ src/main.cpp | 17 ++---- src/requests_handling/HttpRequests.cpp | 5 +- src/requests_handling/HttpResponse.cpp | 6 +- 9 files changed, 217 insertions(+), 54 deletions(-) create mode 100644 includes/default.hpp create mode 100644 includes/server/default.hpp diff --git a/includes/default.hpp b/includes/default.hpp new file mode 100644 index 0000000..fc55644 --- /dev/null +++ b/includes/default.hpp @@ -0,0 +1,20 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* default.hpp :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: mmoussou headers; - std::string body; }; -HttpRequest parseRequest(const std::string &rawRequest); +class Get: public http::IRequest { +public: + Get(void); + Get(std::string &data); + ~Get(void); -#endif + void parse(const http::IRequest &request); + + http::Response execute(void); + +}; + +class Post: public http::IRequest { +public: + Post(void); + Post(std::string &data); + ~Post(void); + + void parse(const http::IRequest &request); + + http::Response execute(void); + +}; + +class Delete: public http::IRequest { +public: + Delete(void); + Delete(std::string &data); + ~Delete(void); + + void parse(const http::IRequest &request); + + http::Response execute(void); + +}; + +} // -namespace http +} // -namespace webserv + +#endif // __WEBSERV_REQUESTS_HTTP_REQUEST_HPP__ diff --git a/includes/requests/HttpResponse.hpp b/includes/requests/HttpResponse.hpp index 5a6cde5..b618248 100644 --- a/includes/requests/HttpResponse.hpp +++ b/includes/requests/HttpResponse.hpp @@ -6,38 +6,49 @@ /* By: mmoussou (); + Response(); - std::string str(void) const; + * or : (if too different ? idk tbh) + Response(Get const req); + Response(Post const req); + */ + ~Response(void); -protected: - std::string _protocol; - size_t _status_code; - std::string _status_text; - std::map _headers; - std::string _body; + std::string getProtocol(void) const; + size_t getStatusCode(void) const; + std::string getStatusText(void) const; + + void setProtocol(std::string const protocol); + void setStatusCode(size_t const status_code); + void setStatusText(std::string const status_text); + +private: + std::string _protocol; + size_t _status_code; + std::string _status_text; }; -class HttpGet: public IHttpResponse { -public: - HttpGet(void); - HttpGet(const HttpRequest &request); - ~HttpResponse(void); +} // -namespace http +} // -namespace webserv - void parseRequest(const HttpRequest &request); -} - -#endif +#endif // __WEBSERV_REQUESTS_HTTP_RESPONSE_HPP__ diff --git a/includes/requests/default.hpp b/includes/requests/default.hpp index 9584306..220f40c 100644 --- a/includes/requests/default.hpp +++ b/includes/requests/default.hpp @@ -6,13 +6,13 @@ /* By: mmoussou #include @@ -20,7 +20,31 @@ #include #include +#include "default.hpp" #include "requests/HttpRequest.hpp" #include "requests/HttpResponse.hpp" -#endif +namespace webserv { +namespace http { + +class IMessage { +public: + virtual std::map getHeaders(void) const; + virtual std::string getBody(void) const; + + virtual void setHeaders(std::map const headers); + virtual void setBody(std::string const body); + + virtual void addHeader(std::string const name, std::string const value); + virtual void rmHeader(std::string const name); + +protected: + std::map _headers; + std::string _body; + +}; + +} // -namespace http +} // -namespace webserv + +#endif // __WEBSERV_REQUESTS_DEFAULT_HPP__ diff --git a/includes/server/default.hpp b/includes/server/default.hpp new file mode 100644 index 0000000..5ca19f5 --- /dev/null +++ b/includes/server/default.hpp @@ -0,0 +1,27 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* default.hpp :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: mmoussou +#include +#include +#include +#include +#include +#include +#include +#include + +namespace webserv { + + + +} //-namespace webserv + +#endif // __WEBSERV_WEBSERV_HPP__ diff --git a/src/main.cpp b/src/main.cpp index fb4b71b..190f426 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -6,20 +6,11 @@ /* By: mmoussou -#include -#include -#include -#include -#include -#include -#include -#include - +#include "webserv.hpp" #include "requests/default.hpp" #define PORT 8080 @@ -39,7 +30,7 @@ int main() { std::cerr << "Error registering signal handler!" << std::endl; return 1; } - +/* // create a socket server_socket = socket(AF_INET, SOCK_STREAM, 0); if (server_socket == -1) { @@ -110,6 +101,6 @@ int main() { close(client_socket); } - close(server_socket); + close(server_socket);*/ return 0; } diff --git a/src/requests_handling/HttpRequests.cpp b/src/requests_handling/HttpRequests.cpp index 74fc0a3..09fc1f4 100644 --- a/src/requests_handling/HttpRequests.cpp +++ b/src/requests_handling/HttpRequests.cpp @@ -6,12 +6,12 @@ /* By: mmoussou _headers; std::string _body;*/ - +/* HttpResponse::HttpResponse(const HttpRequest &request): _protocol(request.protocol) { std::ifstream file(request.target.c_str(), std::ios::binary); @@ -55,4 +55,4 @@ std::string HttpResponse::str(void) const response << this->_body; return (response.str()); -} +}*/ From 354b5f01f309a6f182b18b055ad751ef435bb926 Mon Sep 17 00:00:00 2001 From: y-syo Date: Tue, 11 Feb 2025 22:26:50 +0100 Subject: [PATCH 05/10] =?UTF-8?q?=E3=80=8C=F0=9F=8F=97=EF=B8=8F=E3=80=8D?= =?UTF-8?q?=20wip:=20work=20in=20progress,=20not=20done=20yet.?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- includes/default.hpp | 20 ------------- includes/requests/HttpIMessage.hpp | 45 ++++++++++++++++++++++++++++++ includes/requests/HttpRequest.hpp | 9 ++++-- includes/requests/HttpResponse.hpp | 6 ++-- includes/requests/default.hpp | 36 ++---------------------- includes/server/default.hpp | 4 +-- includes/webserv.hpp | 2 +- src/main.cpp | 6 ++-- 8 files changed, 64 insertions(+), 64 deletions(-) delete mode 100644 includes/default.hpp create mode 100644 includes/requests/HttpIMessage.hpp diff --git a/includes/default.hpp b/includes/default.hpp deleted file mode 100644 index fc55644..0000000 --- a/includes/default.hpp +++ /dev/null @@ -1,20 +0,0 @@ -/* ************************************************************************** */ -/* */ -/* ::: :::::::: */ -/* default.hpp :+: :+: :+: */ -/* +:+ +:+ +:+ */ -/* By: mmoussou +#include + +namespace webserv { +namespace http { + +class IMessage { +public: + virtual std::map getHeaders(void) const; + virtual std::string getBody(void) const; + + virtual void setHeaders(std::map const headers); + virtual void setBody(std::string const body); + + virtual void addHeader(std::string const name, std::string const value); + virtual void rmHeader(std::string const name); + + virtual std::string str(void) const = 0; + +protected: + std::map _headers; + std::string _body; + +}; + +} // -namespace http +} // -namespace webserv + +#endif // __WEBSERV_REQUESTS_HTTP_IMESSAGE_HPP__ diff --git a/includes/requests/HttpRequest.hpp b/includes/requests/HttpRequest.hpp index 18b60c8..7b4911c 100644 --- a/includes/requests/HttpRequest.hpp +++ b/includes/requests/HttpRequest.hpp @@ -6,7 +6,7 @@ /* By: mmoussou +#include +#include + +#include +#include namespace webserv { namespace http { diff --git a/includes/requests/HttpResponse.hpp b/includes/requests/HttpResponse.hpp index b618248..9db5b92 100644 --- a/includes/requests/HttpResponse.hpp +++ b/includes/requests/HttpResponse.hpp @@ -6,7 +6,7 @@ /* By: mmoussou namespace webserv { namespace http { @@ -41,6 +41,8 @@ public: void setStatusCode(size_t const status_code); void setStatusText(std::string const status_text); + std::string str(void) const; + private: std::string _protocol; size_t _status_code; diff --git a/includes/requests/default.hpp b/includes/requests/default.hpp index 220f40c..946cda1 100644 --- a/includes/requests/default.hpp +++ b/includes/requests/default.hpp @@ -6,7 +6,7 @@ /* By: mmoussou -#include -#include -#include -#include - -#include "default.hpp" -#include "requests/HttpRequest.hpp" -#include "requests/HttpResponse.hpp" - -namespace webserv { -namespace http { - -class IMessage { -public: - virtual std::map getHeaders(void) const; - virtual std::string getBody(void) const; - - virtual void setHeaders(std::map const headers); - virtual void setBody(std::string const body); - - virtual void addHeader(std::string const name, std::string const value); - virtual void rmHeader(std::string const name); - -protected: - std::map _headers; - std::string _body; - -}; - -} // -namespace http -} // -namespace webserv +#include +#include #endif // __WEBSERV_REQUESTS_DEFAULT_HPP__ diff --git a/includes/server/default.hpp b/includes/server/default.hpp index 5ca19f5..f648bec 100644 --- a/includes/server/default.hpp +++ b/includes/server/default.hpp @@ -6,7 +6,7 @@ /* By: mmoussou +#include #define PORT 8080 #define BUFFER_SIZE 4096 From eb825346d9e8c16fba6df42e99867fbc7ae266b0 Mon Sep 17 00:00:00 2001 From: y-syo Date: Tue, 11 Feb 2025 22:27:07 +0100 Subject: [PATCH 06/10] =?UTF-8?q?=E3=80=8C=F0=9F=93=9D=E3=80=8D=20doc(READ?= =?UTF-8?q?ME):=20fixed=20the=20typo?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- README.md | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/README.md b/README.md index 91961c5..af68a50 100644 --- a/README.md +++ b/README.md @@ -1,7 +1,7 @@ # webserv -Webserv go brrrrr \ -This is the webserver you will see in all your life star this IMMIDIATELY otherwise we will come to your house +webserv go brrrrr \ +this is the webserver you will see in all your life, star this IMMEDIATELY otherwise we will come to your house ## license From 8edf96d85d005d4a812b487118ae284d7f84597a Mon Sep 17 00:00:00 2001 From: y-syo Date: Tue, 11 Feb 2025 22:27:15 +0100 Subject: [PATCH 07/10] =?UTF-8?q?=E3=80=8C=F0=9F=93=9D=E3=80=8D=20doc(READ?= =?UTF-8?q?ME):=20fixed=20the=20typo?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- :D | 2 ++ 1 file changed, 2 insertions(+) create mode 100644 :D diff --git a/:D b/:D new file mode 100644 index 0000000..8557680 --- /dev/null +++ b/:D @@ -0,0 +1,2 @@ +[yosyo-indev eb82534] 「📝」 doc(README): fixed the typo + 1 file changed, 2 insertions(+), 2 deletions(-) From 3041ebbc9584bc62df057e6c14adca1efb3e9c07 Mon Sep 17 00:00:00 2001 From: y-syo Date: Tue, 11 Feb 2025 22:27:52 +0100 Subject: [PATCH 08/10] =?UTF-8?q?=E3=80=8C=F0=9F=94=A8=E3=80=8D=20fix:=20w?= =?UTF-8?q?tf?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- :D | 2 -- 1 file changed, 2 deletions(-) delete mode 100644 :D diff --git a/:D b/:D deleted file mode 100644 index 8557680..0000000 --- a/:D +++ /dev/null @@ -1,2 +0,0 @@ -[yosyo-indev eb82534] 「📝」 doc(README): fixed the typo - 1 file changed, 2 insertions(+), 2 deletions(-) From 6b2632534cceba1db0f2aff4032f9abf0c45193e Mon Sep 17 00:00:00 2001 From: y-syo Date: Wed, 12 Feb 2025 01:36:19 +0100 Subject: [PATCH 09/10] =?UTF-8?q?=E3=80=8C=F0=9F=8F=97=EF=B8=8F=E3=80=8D?= =?UTF-8?q?=20wip(requests):=20from=20map=20to=20multimap=20for=20multiple?= =?UTF-8?q?=20headers=20with=20same=20key(still=20tmp=20commit,=20will=20r?= =?UTF-8?q?edo=20them=20correctly=20after=20i'm=20done)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- includes/requests/HttpIMessage.hpp | 16 ++--- includes/requests/HttpRequest.hpp | 19 +++--- includes/requests/HttpResponse.hpp | 20 ++---- includes/requests/default.hpp | 4 +- includes/server/default.hpp | 6 +- includes/webserv.hpp | 4 +- src/requests_handling/HttpIMessage.cpp | 45 ++++++++++++++ src/requests_handling/HttpRequests.cpp | 85 +++++++++++++++++++++++++- src/requests_handling/HttpResponse.cpp | 68 ++++++++++++--------- 9 files changed, 197 insertions(+), 70 deletions(-) create mode 100644 src/requests_handling/HttpIMessage.cpp diff --git a/includes/requests/HttpIMessage.hpp b/includes/requests/HttpIMessage.hpp index 1b1cc19..e72e361 100644 --- a/includes/requests/HttpIMessage.hpp +++ b/includes/requests/HttpIMessage.hpp @@ -6,7 +6,7 @@ /* By: mmoussou getHeaders(void) const; - virtual std::string getBody(void) const; + virtual std::multimap getHeaders(void) const; + virtual std::string getBody(void) const; - virtual void setHeaders(std::map const headers); + virtual void setHeaders(std::multimap const headers); virtual void setBody(std::string const body); - virtual void addHeader(std::string const name, std::string const value); - virtual void rmHeader(std::string const name); + virtual void addHeader(std::string const key, std::string const value); + virtual void rmHeader(std::string const key); virtual std::string str(void) const = 0; protected: - std::map _headers; - std::string _body; + std::multimap _headers; + std::string _body; }; diff --git a/includes/requests/HttpRequest.hpp b/includes/requests/HttpRequest.hpp index 7b4911c..2f4d723 100644 --- a/includes/requests/HttpRequest.hpp +++ b/includes/requests/HttpRequest.hpp @@ -6,7 +6,7 @@ /* By: mmoussou + #include namespace webserv { @@ -22,16 +24,6 @@ namespace http { class Response: public http::IMessage { public: Response(void); - /* - * ? either : (templated) - Response(); - Response(); - - * or : (if too different ? idk tbh) - Response(Get const req); - Response(Post const req); - */ - ~Response(void); std::string getProtocol(void) const; size_t getStatusCode(void) const; @@ -44,9 +36,9 @@ public: std::string str(void) const; private: - std::string _protocol; - size_t _status_code; - std::string _status_text; + std::string _protocol; + size_t _status_code; + std::string _status_text; }; diff --git a/includes/requests/default.hpp b/includes/requests/default.hpp index 946cda1..3c8763d 100644 --- a/includes/requests/default.hpp +++ b/includes/requests/default.hpp @@ -6,7 +6,7 @@ /* By: mmoussou #include +using namespace webserv; + #endif // __WEBSERV_REQUESTS_DEFAULT_HPP__ diff --git a/includes/server/default.hpp b/includes/server/default.hpp index f648bec..1f956c6 100644 --- a/includes/server/default.hpp +++ b/includes/server/default.hpp @@ -6,7 +6,7 @@ /* By: mmoussou + +using namespace webserv; + +std::multimap http::IMessage::getHeaders(void) const +{ + return (this->_headers); +} + +std::string http::IMessage::getBody(void) const +{ + return (this->_body); +} + +void http::IMessage::setHeaders(std::multimap const headers) +{ + this->_headers = headers; +} + +void http::IMessage::setBody(std::string const body) +{ + this->_body = body; +} + +void http::IMessage::addHeader(std::string const key, std::string const value) +{ + this->_headers.insert(std::make_pair(key, value)); +} + +void http::IMessage::rmHeader(std::string const key) +{ + this->_headers.erase(key); +} diff --git a/src/requests_handling/HttpRequests.cpp b/src/requests_handling/HttpRequests.cpp index 09fc1f4..a0d0761 100644 --- a/src/requests_handling/HttpRequests.cpp +++ b/src/requests_handling/HttpRequests.cpp @@ -6,11 +6,11 @@ /* By: mmoussou /* HttpRequest parseRequest(const std::string &rawRequest) { @@ -54,3 +54,84 @@ HttpRequest parseRequest(const std::string &rawRequest) return request; } */ + +/* GET response creation +HttpResponse::HttpResponse(const HttpRequest &request): _protocol(request.protocol) +{ + std::ifstream file(request.target.c_str(), std::ios::binary); + if (file) + { + this->_status_code = 200; + this->_status_text = "OK"; + this->_headers["Content-Type"] = "text/html"; + this->_body = std::string((std::istreambuf_iterator(file)), std::istreambuf_iterator()); + } + else + { + // TODO: replace with the new predefined array of response + this->_status_code = 404; + this->_status_text = "Not Found"; + this->_headers["Content-Type"] = "text/html"; + this->_body = "nuh uh, get 404'd >:D"; + } +} +*/ + +/* +class IRequest: public http::IMessage { +public: + virtual void parse(const http::IRequest &request) = 0; + virtual http::Response execute(void) = 0; + + //std::string str(void) const; + + std::string getProtocol(void) const; + size_t getStatusCode(void) const; + std::string getStatusText(void) const; + + void setProtocol(std::string const protocol); + void setStatusCode(size_t const status_code); + void setStatusText(std::string const status_text); + +private: + std::string _method; + std::string _target; + std::string _protocol; + +}; +*/ + +using namespace webserv; + +void http::IRequest::parse(http::IRequest const &request) { (void) request; } +http::Response execute(void) { return (http::Response()); } + +std::string http::IRequest::getMethod(void) const +{ + return (this->_method); +} + +std::string http::IRequest::getTarget(void) const +{ + return (this->_target); +} + +std::string http::IRequest::getProtocol(void) const +{ + return (this->_protocol); +} + +void http::IRequest::setMethod(std::string const method) +{ + this->_method = method; +} + +void http::IRequest::setTarget(std::string const target) +{ + this->_target = target; +} + +void http::IRequest::setProtocol(std::string const protocol) +{ + this->_protocol = protocol; +} diff --git a/src/requests_handling/HttpResponse.cpp b/src/requests_handling/HttpResponse.cpp index 4445195..5857f28 100644 --- a/src/requests_handling/HttpResponse.cpp +++ b/src/requests_handling/HttpResponse.cpp @@ -6,53 +6,65 @@ /* By: mmoussou _headers; -std::string _body;*/ /* -HttpResponse::HttpResponse(const HttpRequest &request): _protocol(request.protocol) -{ - std::ifstream file(request.target.c_str(), std::ios::binary); - if (file) - { - this->_status_code = 200; - this->_status_text = "OK"; - this->_headers["Content-Type"] = "text/html"; - this->_body = std::string((std::istreambuf_iterator(file)), std::istreambuf_iterator()); - } - else - { - this->_status_code = 404; - this->_status_text = "Not Found"; - this->_headers["Content-Type"] = "text/html"; - this->_body = "nuh uh, get 404'd >:D"; - } -} +- do a map of all the status_text and get it from here, not storing them +- get error pages from an array of predefined response, that would be modified by the config +*/ -HttpResponse::~HttpResponse(void) +using namespace webserv; + +http::Response::Response(void) { } -std::string HttpResponse::str(void) const +std::string http::Response::str(void) const { std::ostringstream response; response << this->_protocol << " " << this->_status_code << " " << this->_status_text; response << "\r\n"; - for (std::map::const_iterator it = this->_headers.begin(); it != this->_headers.end(); ++it) + for (std::multimap::const_iterator it = this->_headers.begin(); it != this->_headers.end(); ++it) response << it->first << ": " << it->second << "\r\n"; response << "\r\n"; response << this->_body; return (response.str()); -}*/ +} + +std::string http::Response::getProtocol(void) const +{ + return (this->_protocol); +} + +size_t http::Response::getStatusCode(void) const +{ + return (this->_status_code); +} + +std::string http::Response::getStatusText(void) const +{ + return (this->_status_text); +} + +void http::Response::setProtocol(std::string const protocol) +{ + this->_protocol = protocol; +} + +void http::Response::setStatusCode(size_t const status_code) +{ + this->_status_code = status_code; +} + +void http::Response::setStatusText(std::string const status_text) +{ + this->_status_text = status_text; +} From b30d54d9cc06bf84000b1a510a554085785033de Mon Sep 17 00:00:00 2001 From: y-syo Date: Wed, 12 Feb 2025 11:28:52 +0100 Subject: [PATCH 10/10] =?UTF-8?q?=E3=80=8C=E2=9C=A8=E3=80=8D=20feat(reques?= =?UTF-8?q?ts):=20GET=20done,=20time=20for=20the=20two=20others=20D:?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- includes/requests/HttpRequest.hpp | 14 +- src/main.cpp | 22 +-- src/requests_handling/HttpRequests.cpp | 189 +++++++++++++------------ 3 files changed, 116 insertions(+), 109 deletions(-) diff --git a/includes/requests/HttpRequest.hpp b/includes/requests/HttpRequest.hpp index 2f4d723..830bfaa 100644 --- a/includes/requests/HttpRequest.hpp +++ b/includes/requests/HttpRequest.hpp @@ -6,7 +6,7 @@ /* By: mmoussou -/* -HttpRequest parseRequest(const std::string &rawRequest) -{ - std::istringstream stream(rawRequest); - HttpRequest request; - std::string line; - - if (std::getline(stream, line)) - { - std::istringstream line_stream(line); - line_stream >> request.method >> request.target >> request.protocol; - request.target.insert(request.target.begin(), '.'); - } - - while (std::getline(stream, line) && line != "\r") - { - size_t delimiter_index = line.find(':'); - if (delimiter_index != std::string::npos) - { - std::string key = line.substr(0, delimiter_index); - std::string value = line.substr(delimiter_index + 2); - request.headers[key] = value; - } - } - - std::ostringstream body_stream; - while (std::getline(stream, line)) - body_stream << line << "\n"; - request.body = body_stream.str(); - - std::cout << "method: " << request.method << std::endl; - std::cout << "target: " << request.target << std::endl; - std::cout << "protocol: " << request.protocol << std::endl; - std::cout << std::endl; - std::cout << "-- headers --" << std::endl; - for (std::map::const_iterator it = request.headers.begin(); it != request.headers.end(); ++it) - std::cout << it->first << ": " << it->second << std::endl; - std::cout << std::endl; - std::cout << "-- body --" << std::endl << request.body << std::endl; - - return request; -} -*/ - -/* GET response creation -HttpResponse::HttpResponse(const HttpRequest &request): _protocol(request.protocol) -{ - std::ifstream file(request.target.c_str(), std::ios::binary); - if (file) - { - this->_status_code = 200; - this->_status_text = "OK"; - this->_headers["Content-Type"] = "text/html"; - this->_body = std::string((std::istreambuf_iterator(file)), std::istreambuf_iterator()); - } - else - { - // TODO: replace with the new predefined array of response - this->_status_code = 404; - this->_status_text = "Not Found"; - this->_headers["Content-Type"] = "text/html"; - this->_body = "nuh uh, get 404'd >:D"; - } -} -*/ - -/* -class IRequest: public http::IMessage { -public: - virtual void parse(const http::IRequest &request) = 0; - virtual http::Response execute(void) = 0; - - //std::string str(void) const; - - std::string getProtocol(void) const; - size_t getStatusCode(void) const; - std::string getStatusText(void) const; - - void setProtocol(std::string const protocol); - void setStatusCode(size_t const status_code); - void setStatusText(std::string const status_text); - -private: - std::string _method; - std::string _target; - std::string _protocol; - -}; -*/ using namespace webserv; -void http::IRequest::parse(http::IRequest const &request) { (void) request; } +std::string http::IRequest::str(void) const +{ + std::ostringstream response; + + response << this->_method << " " << this->_target << " " << this->_protocol; + response << "\r\n"; + + for (std::multimap::const_iterator it = this->_headers.begin(); it != this->_headers.end(); ++it) + response << it->first << ": " << it->second << "\r\n"; + + response << "\r\n"; + response << this->_body; + + return (response.str()); +} + +void parse(std::string const &data) { (void) data; } http::Response execute(void) { return (http::Response()); } std::string http::IRequest::getMethod(void) const @@ -135,3 +62,83 @@ void http::IRequest::setProtocol(std::string const protocol) { this->_protocol = protocol; } + +// ------------------------------------------------------------------ + +http::Get::Get(void) +{ +} + +http::Get::Get(std::string &data) +{ + this->parse(data); +} + +void http::Get::parse(std::string const &data) +{ + std::istringstream stream(data); + std::string line; + + if (std::getline(stream, line)) + { + std::istringstream line_stream(line); + line_stream >> this->_method >> this->_target >> this->_protocol; + this->_target.insert(this->_target.begin(), '.'); + } + + while (std::getline(stream, line) && line != "\r") + { + size_t delimiter_index = line.find(':'); + if (delimiter_index != std::string::npos) + { + std::string key = line.substr(0, delimiter_index); + std::string value = line.substr(delimiter_index + 2); + this->_headers.insert(std::make_pair(key, value)); + } + } + + std::ostringstream body_stream; + while (std::getline(stream, line)) + body_stream << line << "\n"; + this->_body = body_stream.str(); + + /* + std::cout << "method: " << this->_method << std::endl; + std::cout << "target: " << this->_target << std::endl; + std::cout << "protocol: " << this->_protocol << std::endl; + std::cout << std::endl; + std::cout << "-- headers --" << std::endl; + for (std::map::const_iterator it = this->_headers.begin(); it != this->_headers.end(); ++it) + std::cout << it->first << ": " << it->second << std::endl; + std::cout << std::endl; + std::cout << "-- body --" << std::endl << this->_body << std::endl; + */ +} + +http::Response http::Get::execute(void) +{ + http::Response response; + + try + { + std::ifstream file(this->_target.c_str(), std::ios::binary); + response.setProtocol(this->_protocol); + response.setStatusCode(200); + response.setStatusText("OK"); + response.addHeader("Content-Type", "text/html"); + response.setBody(std::string((std::istreambuf_iterator(file)), std::istreambuf_iterator())); + } + catch (...) + { + // TODO: replace with a predefined array of error pages + response.setProtocol(this->_protocol); + response.setStatusCode(404); + response.setStatusText("Not Found"); + response.addHeader("Content-Type", "text/html"); + response.setBody("nuh uh, get 404'd >:D"); + } + + return (response); +} + +// ------------------------------------------------------------------