mirror of
https://github.com/KeyZox71/webserv.git
synced 2025-03-15 05:36:51 +01:00
「🏗️」 wip(requests): work in progress, not done yet.
This commit is contained in:
@ -6,7 +6,7 @@
|
||||
/* By: mmoussou <mmoussou@student.42angouleme.fr +#+ +:+ +#+ */
|
||||
/* +#+#+#+#+#+ +#+ */
|
||||
/* 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::string _body;
|
||||
|
||||
static const std::map<int, std::string> _response_status_codes;
|
||||
|
||||
};
|
||||
|
||||
} // -namespace http
|
||||
|
@ -6,7 +6,7 @@
|
||||
/* By: mmoussou <mmoussou@student.42angouleme.fr +#+ +:+ +#+ */
|
||||
/* +#+#+#+#+#+ +#+ */
|
||||
/* 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__
|
||||
# define __WEBSERV_REQUESTS_HTTP_REQUEST_HPP__
|
||||
|
||||
#include <ctime>
|
||||
#include <fstream>
|
||||
#include <sstream>
|
||||
#include <iostream>
|
||||
|
34
src/main.cpp
34
src/main.cpp
@ -6,7 +6,7 @@
|
||||
/* By: mmoussou <mmoussou@student.42angouleme.fr +#+ +:+ +#+ */
|
||||
/* +#+#+#+#+#+ +#+ */
|
||||
/* 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)
|
||||
{
|
||||
std::cerr << std::endl << "closing..." << std::endl;
|
||||
close(server_socket);
|
||||
exit(signal);
|
||||
}
|
||||
|
||||
int main() {
|
||||
int main()
|
||||
{
|
||||
// handle ctrl-C to close server socket
|
||||
if (signal(SIGINT, close_socket) == SIG_ERR) {
|
||||
std::cerr << "Error registering signal handler!" << std::endl;
|
||||
if (signal(SIGPIPE, SIG_IGN) == SIG_ERR || signal(SIGINT, close_socket) == SIG_ERR || signal(SIGQUIT, close_socket) == SIG_ERR)
|
||||
{
|
||||
std::cerr << "Error registering signal handlers!" << std::endl;
|
||||
return 1;
|
||||
}
|
||||
|
||||
// create a socket
|
||||
server_socket = socket(AF_INET, SOCK_STREAM, 0);
|
||||
if (server_socket == -1) {
|
||||
if (server_socket == -1)
|
||||
{
|
||||
std::cerr << "Failed to create socket" << std::endl;
|
||||
return 1;
|
||||
}
|
||||
@ -45,21 +49,21 @@ int main() {
|
||||
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) {
|
||||
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) {
|
||||
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) {
|
||||
while (true)
|
||||
{
|
||||
// accept an incoming connection
|
||||
sockaddr_in client_address;
|
||||
socklen_t client_address_len = sizeof(client_address);
|
||||
@ -73,7 +77,8 @@ int main() {
|
||||
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) {
|
||||
if (bytes_received == -1)
|
||||
{
|
||||
std::cerr << "Failed to receive request" << std::endl;
|
||||
close(client_socket);
|
||||
continue;
|
||||
@ -81,17 +86,14 @@ int main() {
|
||||
|
||||
// parse the request
|
||||
std::string received_data(buffer, bytes_received);
|
||||
//HttpRequest request = parseRequest(received_data);
|
||||
http::Get request(received_data);
|
||||
|
||||
std::cout << "Received " << request.getMethod() << " request for " << request.getTarget() << std::endl;
|
||||
|
||||
// handle the request
|
||||
std::string response;
|
||||
if (request.getMethod() == "GET")
|
||||
{
|
||||
if (request.getMethod() == "GET" || request.getMethod() == "POST")
|
||||
response = request.execute().str();
|
||||
}
|
||||
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>";
|
||||
|
@ -6,7 +6,7 @@
|
||||
/* By: mmoussou <mmoussou@student.42angouleme.fr +#+ +:+ +#+ */
|
||||
/* +#+#+#+#+#+ +#+ */
|
||||
/* 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";
|
||||
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;
|
||||
@ -112,7 +113,7 @@ void http::Get::parse(std::string const &data)
|
||||
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)
|
||||
@ -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);
|
||||
}
|
||||
|
||||
|
Reference in New Issue
Block a user