From 7fbaa44dae2b18f2ed510d68a7e1a49ef7bff1b9 Mon Sep 17 00:00:00 2001 From: Matisse Chenavas Date: Tue, 27 Aug 2024 14:57:21 +0200 Subject: [PATCH] to test state --- site/upload/test.html | 1 + site/upload/test.jpg | 1 + site/upload/test.txt | 1 + src/ConnectionHandler.cpp | 17 ++----- src/ConnectionHandler.hpp | 1 - src/EventData.cpp | 99 +++++++++++++++++++++++++++++++++++++-- src/EventData.hpp | 27 ++++++++++- src/HTTPResponse.cpp | 2 +- src/Logger.cpp | 4 ++ src/Logger.hpp | 2 +- src/Server.cpp | 19 +++++++- src/Server.hpp | 1 + src/Worker.cpp | 31 ++++++++---- src/Worker.hpp | 2 + 14 files changed, 176 insertions(+), 32 deletions(-) create mode 100644 site/upload/test.html create mode 100644 site/upload/test.jpg create mode 100644 site/upload/test.txt diff --git a/site/upload/test.html b/site/upload/test.html new file mode 100644 index 0000000..7c5eb33 --- /dev/null +++ b/site/upload/test.html @@ -0,0 +1 @@ + Festava Live - Bootstrap 5 CSS Template
Festava Live Presents

Night Live 2023

Let's begin
10 - 12th, Dec 2023
National Center, United States

About Festava 2022

Festava Live is free CSS template provided by TemplateMo website. This layout is built on Bootstrap v5.2.2 CSS library. You are free to use this template for your commercial website.

Once in Lifetime Experience

You are not allowed to redistribute the template ZIP file on any other website without a permission.

Whole Night Party

Please tell your friends about our website. Thank you.

a happy moment

your amazing festival experience with us

Meet Artists

Name: Madona

Birthdate: August 16, 1958

Music: Pop, R&B


Youtube Channel: Madona Official

Name: Rihana

Birthdate: Feb 20, 1988

Music: Country


Youtube Channel: Rihana Official

Name: Bruno Bros

Birthdate: October 8, 1985

Music: Pop


Youtube Channel: Bruno Official

Event Schedule

Date Wednesday Thursday Friday
Day 1

Pop Night

5:00 - 7:00 PM

By Adele

Rock & Roll

7:00 - 11:00 PM

By Rihana

Day 2

DJ Night

6:30 - 9:30 PM

By Rihana

Day 3

Country Music

4:30 - 7:30 PM

By Rihana

Free Styles

6:00 - 10:00 PM

By Members

Plans, you' love

Early Bird $120

Including good things:

Save up to 50%

  • platform for potential customers
  • digital experience
  • high-quality sound
  • standard content
Buy Ticket

Standard $240

What makes a premium festava?

  • platform for potential customers
  • digital experience
  • high-quality sound
  • premium content
  • live chat support
Buy Ticket

Interested? Let's talk

\ No newline at end of file diff --git a/site/upload/test.jpg b/site/upload/test.jpg new file mode 100644 index 0000000..8872073 --- /dev/null +++ b/site/upload/test.jpg @@ -0,0 +1 @@ +=Exif    ?  deYι--2{]eVu ٶCOi?9՛#~ðl o?G顺1:pl;Hi/ugocV*6 ]{j޹=VY~{.>ldn=Ry etGh~C ykH}DׁV~K6)k./}R_X:ek}F0SƺH߻3~U]ickc F ֫q]Գ^ds9w~u>oXC]͹;.Shs$mmQ-방UI#56h3cC}mq#=_S?B_^k0x &M0N)|U>}/mڲj,,1[ذ,̥r-hSt.xn}zW;e5nߤk_*ĺn6D0H NIŮ;KzL~\OƟ$P_1q-Z<=I?of=cí`۴3WOU>?,{cs=Prq Inq֟>Jesn]Mk1UL{>ꀮkh!tx9הhzTlvߣYD nc[ `oθ\Fsk+c0uc};)\;3vk+̝y=mz~6.ƪG`7F "&Photoshop 3.0ptionsltvectorDataboolESliceTypeliceHorzAlignleftOutsetlong    \ No newline at end of file diff --git a/site/upload/test.txt b/site/upload/test.txt new file mode 100644 index 0000000..fcda0eb --- /dev/null +++ b/site/upload/test.txt @@ -0,0 +1 @@ +{kashdalskjd} \ No newline at end of file diff --git a/src/ConnectionHandler.cpp b/src/ConnectionHandler.cpp index eca50a7..ea24ab9 100644 --- a/src/ConnectionHandler.cpp +++ b/src/ConnectionHandler.cpp @@ -44,7 +44,6 @@ ConnectionHandler::ConnectionHandler( _startTime(time(NULL)), _cgiHandler(NULL), _cgiState(NONE), - _step(0), _events(events) { } @@ -252,8 +251,7 @@ void ConnectionHandler::_processRequest(struct epoll_event& event) { void ConnectionHandler::_processExecutingState() { if (_cgiState == NONE) { - _log.error("CONNECTION_HANDLER(" + Utils::to_string(_step) + - "): CGIHandler is NULL or in invalid state"); + _log.error("CONNECTION_HANDLER: CGIHandler is NULL or in invalid state"); _setConnectionStatus(SENDING); return; } @@ -263,7 +261,6 @@ void ConnectionHandler::_processExecutingState() { } int ConnectionHandler::processConnection(struct epoll_event& event) { - _step++; if ((time(NULL) - _startTime) > TIMEOUT) { HTTPResponse::sendResponse(HTTPResponse::GATEWAY_TIMEOUT, _clientSocket); @@ -287,16 +284,14 @@ int ConnectionHandler::processConnection(struct epoll_event& event) { _sendResponse(); } catch (const Exception& e) { _setConnectionStatus(CLOSED); - _log.error(std::string("CONNECTION_HANDLER(" + Utils::to_string(_step) + - "): " + " Status: " + getStatusString() + + _log.error(std::string("CONNECTION_HANDLER Status: " + getStatusString() + " Exception caught: " + e.what())); } switch (_connectionStatus) { case READING: event.events = EPOLLIN | EPOLLET | EPOLLONESHOT; if (epoll_ctl(_epollSocket, EPOLL_CTL_MOD, _clientSocket, &event) == -1) { - _log.error(std::string("CONNECTION_HANDLER(" + Utils::to_string(_step) + - "): epoll_ctl: ") + + _log.error(std::string("CONNECTION_HANDLER: epoll_ctl: ") + strerror(errno)); close(_clientSocket); } @@ -304,8 +299,7 @@ int ConnectionHandler::processConnection(struct epoll_event& event) { case EXECUTING: if (!_cgiHandler) { - throw Exception("CONNECTION_HANDLER(" + Utils::to_string(_step) + - "): CGIHandler is NULL"); + throw Exception("CONNECTION_HANDLER: CGIHandler is NULL"); } _events.push(event); break; @@ -316,8 +310,7 @@ int ConnectionHandler::processConnection(struct epoll_event& event) { case SENDING: event.events = EPOLLOUT | EPOLLET | EPOLLONESHOT; if (epoll_ctl(_epollSocket, EPOLL_CTL_MOD, _clientSocket, &event) == -1) { - _log.error(std::string("CONNECTION_HANDLER(" + Utils::to_string(_step) + - "): epoll_ctl: ") + + _log.error(std::string("CONNECTION_HANDLER: epoll_ctl: ") + strerror(errno)); close(_clientSocket); } diff --git a/src/ConnectionHandler.hpp b/src/ConnectionHandler.hpp index 9f86876..52ec39b 100644 --- a/src/ConnectionHandler.hpp +++ b/src/ConnectionHandler.hpp @@ -94,7 +94,6 @@ class ConnectionHandler { time_t _startTime; CGIHandler* _cgiHandler; CGIState _cgiState; - int _step; EventQueue& _events; void _receiveRequest(struct epoll_event& event); diff --git a/src/EventData.cpp b/src/EventData.cpp index ac34291..ae93301 100644 --- a/src/EventData.cpp +++ b/src/EventData.cpp @@ -11,9 +11,98 @@ /* ************************************************************************** */ #include "EventData.hpp" +#include "EventData.hpp" + +EventData::EventData(int newFd, ConnectionHandler* ptr, pid_t newThreadId, bool newIsListening, bool newRecordTime, bool newOpened) { + pthread_mutex_init(&mutex, NULL); + setFd(newFd); + setHandler(ptr); + setThreadId(newThreadId); + setIsListening(newIsListening); + setRecordTime(newRecordTime); + setStartTime(0); + setOpened(newOpened); + } + +int EventData::getFd(){ + pthread_mutex_lock(&mutex); + int ret = fd; + pthread_mutex_unlock(&mutex); + return ret; +} +ConnectionHandler* EventData::getHandler(){ + pthread_mutex_lock(&mutex); + ConnectionHandler* ret = handler; + pthread_mutex_unlock(&mutex); + return ret; +} +pid_t EventData::getThreadId(){ + pthread_mutex_lock(&mutex); + pid_t ret = threadId; + pthread_mutex_unlock(&mutex); + return ret; +} +bool EventData::getIsListening(){ + pthread_mutex_lock(&mutex); + bool ret = isListening; + pthread_mutex_unlock(&mutex); + return ret; +} +bool EventData::getRecordTime(){ + pthread_mutex_lock(&mutex); + bool ret = recordTime; + pthread_mutex_unlock(&mutex); + return ret; +} +time_t EventData::getStartTime(){ + pthread_mutex_lock(&mutex); + time_t ret = startTime; + pthread_mutex_unlock(&mutex); + return ret; +} +bool EventData::getOpened(){ + pthread_mutex_lock(&mutex); + bool ret = opened; + pthread_mutex_unlock(&mutex); + return ret; +} + +void EventData::setFd(int newFd){ + pthread_mutex_lock(&mutex); + fd = newFd; + pthread_mutex_unlock(&mutex); +} +void EventData::setHandler(ConnectionHandler* newHandler){ + pthread_mutex_lock(&mutex); + handler = newHandler; + pthread_mutex_unlock(&mutex); +} +void EventData::setThreadId(pid_t id){ + pthread_mutex_lock(&mutex); + threadId = id; + pthread_mutex_unlock(&mutex); +} +void EventData::setIsListening(bool newIsListening){ + pthread_mutex_lock(&mutex); + isListening = newIsListening; + pthread_mutex_unlock(&mutex); +} +void EventData::setRecordTime(bool record){ + pthread_mutex_lock(&mutex); + recordTime = record; + pthread_mutex_unlock(&mutex); +} +void EventData::setStartTime(time_t time){ + pthread_mutex_lock(&mutex); + startTime = time; + pthread_mutex_unlock(&mutex); +} +void EventData::setOpened(bool newOpened){ + pthread_mutex_lock(&mutex); + opened = newOpened; + pthread_mutex_unlock(&mutex); +} -EventData::EventData(int fd, - ConnectionHandler* ptr, - pid_t threadId, - bool isListening) - : fd(fd), handler(ptr), threadId(threadId), isListening(isListening) {} +EventData::~EventData() { + pthread_mutex_destroy(&mutex); +} diff --git a/src/EventData.hpp b/src/EventData.hpp index 79c1969..91802d5 100644 --- a/src/EventData.hpp +++ b/src/EventData.hpp @@ -13,6 +13,8 @@ #ifndef EVENTDATA_HPP #define EVENTDATA_HPP +#include + #include "ConnectionHandler.hpp" class ConnectionHandler; @@ -22,11 +24,34 @@ struct EventData { ConnectionHandler* handler; pid_t threadId; bool isListening; + bool recordTime; + time_t startTime; + bool opened; + pthread_mutex_t mutex; + + int getFd(); + ConnectionHandler* getHandler(); + pid_t getThreadId(); + bool getIsListening(); + bool getRecordTime(); + time_t getStartTime(); + bool getOpened(); + + void setFd(int fd); + void setHandler(ConnectionHandler* handler); + void setThreadId(pid_t pid); + void setIsListening(bool isListening); + void setRecordTime(bool record); + void setStartTime(time_t time); + void setOpened(bool opened); EventData(int fd, ConnectionHandler* ptr, pid_t threadId = -1, - bool isListening = false); + bool isListening = false, + bool recordTime = true, + bool opened = true); + ~EventData(void); }; #endif diff --git a/src/HTTPResponse.cpp b/src/HTTPResponse.cpp index d00035f..fae712a 100644 --- a/src/HTTPResponse.cpp +++ b/src/HTTPResponse.cpp @@ -371,7 +371,7 @@ int HTTPResponse::sendResponse(int statusCode, int clientSocket) { std::string body = defaultErrorPage(statusCode); headers += "Content-Length: " + Utils::to_string(body.length()) + "\r\n\r\n"; std::string response = statusLine + headers + body; - if (send(clientSocket, response.c_str(), response.length(), 0) == -1) { + if (send(clientSocket, response.c_str(), response.length(), 0) != 0) { return -1; } return 0; diff --git a/src/Logger.cpp b/src/Logger.cpp index e1d8e40..91fc643 100644 --- a/src/Logger.cpp +++ b/src/Logger.cpp @@ -22,6 +22,8 @@ Logger* Logger::_instance = NULL; +pthread_mutex_t Logger::_mutex; + Logger::Logger() : _progLogFile(new std::ofstream()) { pthread_mutex_init(&_mutex, NULL); } @@ -35,6 +37,8 @@ Logger& Logger::getInstance() { void Logger::deleteInstance() { if (_instance != NULL) { + pthread_mutex_lock(&_mutex); + pthread_mutex_unlock(&_mutex); delete _instance; _instance = NULL; } diff --git a/src/Logger.hpp b/src/Logger.hpp index 779475d..613d00b 100644 --- a/src/Logger.hpp +++ b/src/Logger.hpp @@ -44,7 +44,7 @@ class Logger { static Logger* _instance; - mutable pthread_mutex_t _mutex; + static pthread_mutex_t _mutex; Config _config; std::ofstream* _progLogFile; std::string _progLogFileName; diff --git a/src/Server.cpp b/src/Server.cpp index 2355048..968b070 100644 --- a/src/Server.cpp +++ b/src/Server.cpp @@ -55,6 +55,9 @@ Server::~Server() { } _listenSockets.clear(); _listenEventData.clear(); + pthread_mutex_lock(&_mutex); + _running = false; + pthread_mutex_unlock(&_mutex); pthread_mutex_destroy(&_mutex); CacheHandler::deleteInstance(); ConfigManager::deleteInstance(); @@ -81,8 +84,18 @@ void Server::workerFinished() { pthread_mutex_unlock(&_mutex); } +bool Server::_shouldRun() +{ + pthread_mutex_lock(&_mutex); + bool ret = _running; + pthread_mutex_unlock(&_mutex); + return ret; +} + void Server::start() { + pthread_mutex_lock(&_mutex); _running = true; + pthread_mutex_unlock(&_mutex); for (size_t i = 0; i < _workers.size(); i++) { try { _workers[i]->start(); @@ -97,7 +110,7 @@ void Server::start() { _log.info("SERVER: All workers started (" + Utils::to_string(_activeWorkers) + ")"); struct epoll_event events[MAX_EVENTS]; - while (_running) { + while (_shouldRun()) { int nfds = epoll_wait(_epollSocket, events, MAX_EVENTS, -1); if (nfds == 0) { _log.info("SERVER: epoll_wait: 0 events"); @@ -107,7 +120,7 @@ void Server::start() { usleep(1000); continue; } - for (int i = 0; i < nfds && _running; i++) + for (int i = 0; i < nfds && _shouldRun(); i++) _events.push(events[i]); } } @@ -122,7 +135,9 @@ void Server::stop(int signum) { _workers[i]->stop(); } _log.info("SERVER: workers stopped"); + pthread_mutex_lock(&_mutex); _running = false; + pthread_mutex_unlock(&_mutex); } } diff --git a/src/Server.hpp b/src/Server.hpp index e71aefa..10ad188 100644 --- a/src/Server.hpp +++ b/src/Server.hpp @@ -52,6 +52,7 @@ class Server { void _setupServerSockets(); void _setupWorkers(); void _setupEpoll(); + bool _shouldRun(); public: Server(); diff --git a/src/Worker.cpp b/src/Worker.cpp index b30ea56..6a20ce8 100644 --- a/src/Worker.cpp +++ b/src/Worker.cpp @@ -27,9 +27,12 @@ Worker::Worker(Server& server, _listenSockets(listenSockets), _load(0), _shouldStop(false) { + pthread_mutex_init(&_mutex, NULL); } -Worker::~Worker() {} +Worker::~Worker() { + pthread_mutex_destroy(&_mutex); +} Worker::Thread::Thread() : _thread(0) {} @@ -49,7 +52,9 @@ void Worker::start() { } void Worker::stop() { + pthread_mutex_lock(&_mutex); _shouldStop = true; + pthread_mutex_unlock(&_mutex); } pid_t Worker::getThreadId() const { @@ -72,8 +77,16 @@ void* Worker::_workerRoutine(void* arg) { return NULL; } +bool Worker::_shouldRun() +{ + pthread_mutex_lock(&_mutex); + bool ret = _shouldStop; + pthread_mutex_unlock(&_mutex); + return !ret; +} + void Worker::_runEventLoop() { - while (!_shouldStop) { + while (_shouldRun()) { struct epoll_event event; if (!_events.try_pop(event)) { @@ -81,8 +94,8 @@ void Worker::_runEventLoop() { continue; } EventData* eventData = static_cast(event.data.ptr); - if (eventData && eventData->isListening && (event.events & EPOLLIN)) { - _acceptNewConnection(eventData->fd); + if (eventData && eventData->getIsListening() && (event.events & EPOLLIN)) { + _acceptNewConnection(eventData->getFd()); } else if (eventData && event.events) _launchEventProcessing(eventData, event); @@ -98,20 +111,20 @@ void Worker::_runEventLoop() { void Worker::_launchEventProcessing(EventData* eventData, struct epoll_event& event) { int handlerStatus = 0; - if (!eventData->handler) { + if (!eventData->getHandler()) { _log.warning("WORKER (" + Utils::to_string(_threadId) + "): Handler is NULL for event fd: " + Utils::to_string(event.data.fd) + - " -> " + Utils::to_string(eventData->fd)); + " -> " + Utils::to_string(eventData->getFd())); return; } try { - handlerStatus = eventData->handler->processConnection(event); + handlerStatus = eventData->getHandler()->processConnection(event); } catch (std::exception& e) { _log.error("WORKER (" + Utils::to_string(_threadId) + "): Exception: " + e.what()); } if (handlerStatus == 1) { // Done - delete eventData->handler; + delete eventData->getHandler(); delete eventData; } } @@ -123,7 +136,7 @@ void Worker::_acceptNewConnection(int fd) { struct epoll_event event; memset(&address, 0, sizeof(address)); - while (!_shouldStop) { + while (_shouldRun()) { new_socket = accept(fd, (struct sockaddr*)&address, &addrlen); // creer un nouveau sockect d'echange d'event diff --git a/src/Worker.hpp b/src/Worker.hpp index 12b4eb2..85b5745 100644 --- a/src/Worker.hpp +++ b/src/Worker.hpp @@ -70,6 +70,7 @@ class Worker { const ListenConfig& listenConfig); void _launchEventProcessing( EventData* eventData, struct epoll_event& event); + bool _shouldRun(); Server& _server; EventQueue& _events; @@ -82,6 +83,7 @@ class Worker { int _load; bool _shouldStop; pid_t _threadId; + pthread_mutex_t _mutex; Worker(const Worker&); Worker& operator=(const Worker&);