Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions include/globals.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
#pragma once

#include <atomic>

extern std::atomic<bool> stopServer;
2 changes: 1 addition & 1 deletion src/Server/ClientConnection.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -380,7 +380,7 @@ bool ClientConnection::_extractHeaderIfComplete(std::vector<char>& header) {
// Convert header_end_index to the signed type required for vector operations
const auto header_end_pos = static_cast<std::vector<char>::difference_type>(*headerEndIndex);

header = std::vector(_headerBuffer.begin(), _headerBuffer.begin() + header_end_pos + 1);
header = std::vector(_headerBuffer.begin(), _headerBuffer.begin() + header_end_pos + 2);
header.push_back('\0'); // Null-terminate the header

// Erase the header and the delimiter from _headerBuffer
Expand Down
7 changes: 4 additions & 3 deletions src/Server/MultiSocketWebserver.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,13 @@
#include <sys/poll.h>
#include <unistd.h>

#include <cstddef> // For size_t
#include <cstddef>

#include "ClientConnection.hpp"
#include "Logger.hpp"
#include "PollFdManager.hpp"
#include "Socket.hpp"
#include "globals.hpp"

MultiSocketWebserver::MultiSocketWebserver(std::vector<std::vector<ServerConfig>> servers_config)
: _polls(PollFdManager::getInstance()) {
Expand Down Expand Up @@ -51,9 +52,9 @@ MultiSocketWebserver::~MultiSocketWebserver() {
}

void MultiSocketWebserver::run() {
while (true) {
while (stopServer == false) {
// TODO timeout
if (const int eventCount = poll(_polls.data(), _polls.size(), 5000); eventCount == -1) {
if (const int eventCount = poll(_polls.data(), _polls.size(), 5000); eventCount == -1 && !stopServer) {
LOG_ERROR("Poll failed: " + std::string(strerror(errno)));
break;
}
Expand Down
5 changes: 3 additions & 2 deletions src/http/RequestHandler/PostRequest.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,6 @@ HttpResponse RequestHandler::handlePostMultipart() {
std::string line;
std::string contentDisposition;
std::string contentTypeFile;
LOG_ERROR(_request.getBody());
while (std::getline(partStream, line) && !line.empty()) {
if (line.find("Content-Disposition") == 0)
contentDisposition = line;
Expand All @@ -75,8 +74,10 @@ HttpResponse RequestHandler::handlePostMultipart() {
LOG_ERROR("Missing Content-Type header in part");
return buildDefaultResponse(Http::BAD_REQUEST);
}
if (contentDisposition.find("filename=") != std::string::npos)
if (contentDisposition.find("filename=") != std::string::npos) {
LOG_DEBUG("Handling file upload");
return handleFileUpload(part, contentDisposition);
}

// TODO: handle form fields - not sure if needed
}
Expand Down
15 changes: 15 additions & 0 deletions src/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -10,14 +10,19 @@
/* */
/* ************************************************************************** */

#include <atomic>
#include <csignal>
#include <exception>
#include <fstream>
#include <iostream>
#include <sstream>

#include "MultiSocketWebserver.hpp"
#include "globals.hpp"
#include "webserv.hpp"

std::atomic<bool> stopServer(false);

std::string readFile(const std::string &filename) {
std::ifstream file(filename);
if (!file.is_open()) {
Expand Down Expand Up @@ -49,13 +54,23 @@ void printServerConfigs(const std::vector<std::vector<ServerConfig>> &server_con
}
}

// Signal handler function
void signalHandler(const int signum) {
LOG_INFO("Interrupt signal (" + std::to_string(signum) + ") received. Stopping server...");
stopServer = true;
}

int main(const int argc, const char *argv[]) {
if (argc != 2) {
std::cerr << COLOR(RED, "Error: ") << "Invalid number of arguments" << std::endl;
std::cerr << "Usage: " << argv[0] << " <path_to_config_file>" << std::endl;
return 1;
}

// Register signal handler
signal(SIGINT, signalHandler);
signal(SIGTERM, signalHandler);

std::string source;
std::vector<std::vector<ServerConfig>> server_config_vectors;

Expand Down
Loading