🏗️」 wip(requests): requests parsing and basic handling of GET responses

This commit is contained in:
y-syo
2025-02-03 18:39:23 +01:00
parent 35956dc13e
commit 4fbaed787d
8 changed files with 336 additions and 13 deletions

View File

@ -6,7 +6,7 @@
# By: adjoly <adjoly@student.42angouleme.fr> +#+ +:+ +#+ #
# +#+#+#+#+#+ +#+ #
# 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:

View File

@ -0,0 +1,27 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* HttpRequest.hpp :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: mmoussou <mmoussou@student.42angouleme.fr +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2025/02/03 17:23:00 by mmoussou #+# #+# */
/* Updated: 2025/02/03 18:11:29 by mmoussou ### ########.fr */
/* */
/* ************************************************************************** */
#pragma once
#ifndef __REQUESTS_HTTP_REQUEST_HPP__
# define __REQUESTS_HTTP_REQUEST_HPP__
struct HttpRequest {
std::string method;
std::string target;
std::string protocol;
std::map<std::string, std::string> headers;
std::string body;
};
HttpRequest parseRequest(const std::string &rawRequest);
#endif

View File

@ -0,0 +1,38 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* HttpResponse.hpp :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: mmoussou <mmoussou@student.42angouleme.fr +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2025/02/03 17:21:20 by mmoussou #+# #+# */
/* Updated: 2025/02/03 18:11:27 by mmoussou ### ########.fr */
/* */
/* ************************************************************************** */
#pragma once
#ifndef __REQUESTS_HTTP_RESPONSE_HPP__
# define __REQUESTS_HTTP_RESPONSE_HPP__
#include "requests/default.hpp"
class HttpResponse {
public:
HttpResponse(const HttpRequest &request);
HttpResponse(const HttpResponse &cpy);
~HttpResponse(void);
HttpResponse &operator=(const HttpResponse &cpy);
std::string str(void) const;
private:
std::string _protocol;
size_t _status_code;
std::string _status_text;
std::map<std::string, std::string> _headers;
std::string _body;
};
#endif

View File

@ -1,20 +1,26 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* webserv.cpp :+: :+: :+: */
/* default.hpp :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: adjoly <adjoly@student.42angouleme.fr> +#+ +:+ +#+ */
/* By: mmoussou <mmoussou@student.42angouleme.fr +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2025/01/20 16:33:31 by adjoly #+# #+# */
/* Updated: 2025/01/20 16:35:02 by adjoly ### ########.fr */
/* Created: 2025/02/03 15:48:22 by mmoussou #+# #+# */
/* Updated: 2025/02/03 18:12:58 by mmoussou ### ########.fr */
/* */
/* ************************************************************************** */
#pragma once
#ifndef __REQUESTS_DEFAULT_HPP__
# define __REQUESTS_DEFAULT_HPP__
#include <map>
#include <string>
#include <fstream>
#include <sstream>
#include <iostream>
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

11
index.html Normal file
View File

@ -0,0 +1,11 @@
<!DOCTYPE html>
<html>
<style>
body {
background-color: #1e2024;
}
</style>
<body>
<h1 style="font-size: 3rem; font-weight: bold; color: #fff; text-align: center; letter-spacing: 2px; background: linear-gradient(90deg, #ff00ff, #00ffff); -webkit-background-clip: text; display: inline-block; border-bottom: 2px solid #fff;">evilge</h1>
</body>
</html>

115
src/main.cpp Normal file
View File

@ -0,0 +1,115 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* main.cpp :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: mmoussou <mmoussou@student.42angouleme.fr +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2025/02/03 15:45:07 by mmoussou #+# #+# */
/* Updated: 2025/02/03 18:36:29 by mmoussou ### ########.fr */
/* */
/* ************************************************************************** */
#include <iostream>
#include <string>
#include <cstring>
#include <sys/socket.h>
#include <netinet/in.h>
#include <unistd.h>
#include <fstream>
#include <csignal>
#include <cstdlib>
#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<html><body><h1>501 Not Implemented</h1></body></html>";
send(client_socket, response.c_str(), response.length(), 0);
}
close(client_socket);
}
close(server_socket);
return 0;
}

View File

@ -0,0 +1,55 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* HttpRequests.cpp :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: mmoussou <mmoussou@student.42angouleme.fr +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2025/02/03 16:07:01 by mmoussou #+# #+# */
/* Updated: 2025/02/03 18:20:51 by mmoussou ### ########.fr */
/* */
/* ************************************************************************** */
#include "requests/default.hpp"
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<std::string, std::string>::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;
}

View File

@ -0,0 +1,69 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* HttpResponse.cpp :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: mmoussou <mmoussou@student.42angouleme.fr +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2025/02/03 17:28:31 by mmoussou #+# #+# */
/* Updated: 2025/02/03 18:15:12 by mmoussou ### ########.fr */
/* */
/* ************************************************************************** */
#include "requests/HttpResponse.hpp"
/*std::string _protocol;
size_t _status_code;
std::string _status_text;
std::map<std::string, std::string> _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<char>(file)), std::istreambuf_iterator<char>());
}
else
{
this->_status_code = 404;
this->_status_text = "Not Found";
this->_headers["Content-Type"] = "text/html";
this->_body = "<html><body>nuh uh, get 404'd >:D</body></html>";
}
}
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<std::string, std::string>::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());
}