🏗️」 wip(requests): work in progress, not done yet.

This commit is contained in:
y-syo
2025-02-14 17:11:33 +01:00
parent 00f85cbed7
commit 28713b407a
4 changed files with 106 additions and 21 deletions

View File

@ -6,7 +6,7 @@
/* By: mmoussou <mmoussou@student.42angouleme.fr +#+ +:+ +#+ */ /* By: mmoussou <mmoussou@student.42angouleme.fr +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */ /* +#+#+#+#+#+ +#+ */
/* Created: 2025/02/11 22:13:38 by mmoussou #+# #+# */ /* Created: 2025/02/11 22:13:38 by mmoussou #+# #+# */
/* Updated: 2025/02/12 00:55:12 by mmoussou ### ########.fr */ /* Updated: 2025/02/14 17:10:42 by mmoussou ### ########.fr */
/* */ /* */
/* ************************************************************************** */ /* ************************************************************************** */
@ -37,6 +37,8 @@ protected:
std::multimap<std::string, std::string> _headers; std::multimap<std::string, std::string> _headers;
std::string _body; std::string _body;
static const std::map<int, std::string> _response_status_codes;
}; };
} // -namespace http } // -namespace http

View File

@ -6,7 +6,7 @@
/* By: mmoussou <mmoussou@student.42angouleme.fr +#+ +:+ +#+ */ /* By: mmoussou <mmoussou@student.42angouleme.fr +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */ /* +#+#+#+#+#+ +#+ */
/* Created: 2025/02/03 17:23:00 by mmoussou #+# #+# */ /* Created: 2025/02/03 17:23:00 by mmoussou #+# #+# */
/* Updated: 2025/02/12 08:56:21 by mmoussou ### ########.fr */ /* Updated: 2025/02/14 15:43:32 by mmoussou ### ########.fr */
/* */ /* */
/* ************************************************************************** */ /* ************************************************************************** */
@ -14,6 +14,7 @@
#ifndef __WEBSERV_REQUESTS_HTTP_REQUEST_HPP__ #ifndef __WEBSERV_REQUESTS_HTTP_REQUEST_HPP__
# define __WEBSERV_REQUESTS_HTTP_REQUEST_HPP__ # define __WEBSERV_REQUESTS_HTTP_REQUEST_HPP__
#include <ctime>
#include <fstream> #include <fstream>
#include <sstream> #include <sstream>
#include <iostream> #include <iostream>

View File

@ -6,7 +6,7 @@
/* By: mmoussou <mmoussou@student.42angouleme.fr +#+ +:+ +#+ */ /* By: mmoussou <mmoussou@student.42angouleme.fr +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */ /* +#+#+#+#+#+ +#+ */
/* Created: 2025/02/03 15:45:07 by mmoussou #+# #+# */ /* Created: 2025/02/03 15:45:07 by mmoussou #+# #+# */
/* Updated: 2025/02/12 10:02:11 by mmoussou ### ########.fr */ /* Updated: 2025/02/14 12:48:51 by mmoussou ### ########.fr */
/* */ /* */
/* ************************************************************************** */ /* ************************************************************************** */
@ -20,20 +20,24 @@ int server_socket;
void close_socket(int signal) void close_socket(int signal)
{ {
std::cerr << std::endl << "closing..." << std::endl;
close(server_socket); close(server_socket);
exit(signal); exit(signal);
} }
int main() { int main()
{
// handle ctrl-C to close server socket // handle ctrl-C to close server socket
if (signal(SIGINT, close_socket) == SIG_ERR) { if (signal(SIGPIPE, SIG_IGN) == SIG_ERR || signal(SIGINT, close_socket) == SIG_ERR || signal(SIGQUIT, close_socket) == SIG_ERR)
std::cerr << "Error registering signal handler!" << std::endl; {
std::cerr << "Error registering signal handlers!" << std::endl;
return 1; return 1;
} }
// create a socket // create a socket
server_socket = socket(AF_INET, SOCK_STREAM, 0); server_socket = socket(AF_INET, SOCK_STREAM, 0);
if (server_socket == -1) { if (server_socket == -1)
{
std::cerr << "Failed to create socket" << std::endl; std::cerr << "Failed to create socket" << std::endl;
return 1; return 1;
} }
@ -45,21 +49,21 @@ int main() {
server_address.sin_addr.s_addr = INADDR_ANY; server_address.sin_addr.s_addr = INADDR_ANY;
server_address.sin_port = htons(PORT); server_address.sin_port = htons(PORT);
// bind the socket to the address if (bind(server_socket, (sockaddr*)&server_address, sizeof(server_address)) == -1)
if (bind(server_socket, (sockaddr*)&server_address, sizeof(server_address)) == -1) { {
std::cerr << "Failed to bind socket" << std::endl; std::cerr << "Failed to bind socket" << std::endl;
return 1; return 1;
} }
if (listen(server_socket, 5) == -1)
// listen for incoming connections {
if (listen(server_socket, 5) == -1) {
std::cerr << "Failed to listen on socket" << std::endl; std::cerr << "Failed to listen on socket" << std::endl;
return 1; return 1;
} }
std::cout << "Server is listening on port " << PORT << std::endl; std::cout << "Server is listening on port " << PORT << std::endl;
while (true) { while (true)
{
// accept an incoming connection // accept an incoming connection
sockaddr_in client_address; sockaddr_in client_address;
socklen_t client_address_len = sizeof(client_address); socklen_t client_address_len = sizeof(client_address);
@ -73,7 +77,8 @@ int main() {
char buffer[BUFFER_SIZE]; char buffer[BUFFER_SIZE];
std::memset(buffer, 0, BUFFER_SIZE); std::memset(buffer, 0, BUFFER_SIZE);
ssize_t bytes_received = recv(client_socket, buffer, BUFFER_SIZE - 1, 0); ssize_t bytes_received = recv(client_socket, buffer, BUFFER_SIZE - 1, 0);
if (bytes_received == -1) { if (bytes_received == -1)
{
std::cerr << "Failed to receive request" << std::endl; std::cerr << "Failed to receive request" << std::endl;
close(client_socket); close(client_socket);
continue; continue;
@ -81,17 +86,14 @@ int main() {
// parse the request // parse the request
std::string received_data(buffer, bytes_received); std::string received_data(buffer, bytes_received);
//HttpRequest request = parseRequest(received_data);
http::Get request(received_data); http::Get request(received_data);
std::cout << "Received " << request.getMethod() << " request for " << request.getTarget() << std::endl; std::cout << "Received " << request.getMethod() << " request for " << request.getTarget() << std::endl;
// handle the request // handle the request
std::string response; std::string response;
if (request.getMethod() == "GET") if (request.getMethod() == "GET" || request.getMethod() == "POST")
{
response = request.execute().str(); response = request.execute().str();
}
else else
{ {
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>"; 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>";

View File

@ -6,7 +6,7 @@
/* By: mmoussou <mmoussou@student.42angouleme.fr +#+ +:+ +#+ */ /* By: mmoussou <mmoussou@student.42angouleme.fr +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */ /* +#+#+#+#+#+ +#+ */
/* Created: 2025/02/03 16:07:01 by mmoussou #+# #+# */ /* Created: 2025/02/03 16:07:01 by mmoussou #+# #+# */
/* Updated: 2025/02/12 10:04:33 by mmoussou ### ########.fr */ /* Updated: 2025/02/14 15:46:44 by mmoussou ### ########.fr */
/* */ /* */
/* ************************************************************************** */ /* ************************************************************************** */
@ -102,7 +102,8 @@ void http::Get::parse(std::string const &data)
body_stream << line << "\n"; body_stream << line << "\n";
this->_body = body_stream.str(); this->_body = body_stream.str();
/* ///*
std::cout << "-- start-line --" << std::endl;
std::cout << "method: " << this->_method << std::endl; std::cout << "method: " << this->_method << std::endl;
std::cout << "target: " << this->_target << std::endl; std::cout << "target: " << this->_target << std::endl;
std::cout << "protocol: " << this->_protocol << std::endl; std::cout << "protocol: " << this->_protocol << std::endl;
@ -112,7 +113,7 @@ void http::Get::parse(std::string const &data)
std::cout << it->first << ": " << it->second << std::endl; std::cout << it->first << ": " << it->second << std::endl;
std::cout << std::endl; std::cout << std::endl;
std::cout << "-- body --" << std::endl << this->_body << std::endl; std::cout << "-- body --" << std::endl << this->_body << std::endl;
*/ //*/
} }
http::Response http::Get::execute(void) http::Response http::Get::execute(void)
@ -142,3 +143,82 @@ http::Response http::Get::execute(void)
} }
// ------------------------------------------------------------------ // ------------------------------------------------------------------
http::Delete::Delete(void)
{
}
http::Delete::Delete(std::string &data)
{
this->parse(data);
}
void http::Delete::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 << "-- start-line --" << std::endl;
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<std::string, std::string>::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::Delete::execute(void)
{
http::Response response;
try
{
if (std::remove(this->_target.c_str()))
throw;
response.setProtocol(this->_protocol);
response.setStatusCode(204);
response.setStatusText("No Content");
time_t now = std::time(NULL);
response.addHeader("Date", std::string(std::ctime(&now)));
}
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("<html><body>nuh uh, get 404'd >:D</body></html>");
}
return (response);
}