Webserv
Loading...
Searching...
No Matches
network::ClientHandler Class Reference

Manages the full lifecycle of a single client connection. More...

#include <ClientHandler.hpp>

Inheritance diagram for network::ClientHandler:
network::IEventHandler

Classes

struct  CgiState
struct  SendBuffer

Public Member Functions

 ClientHandler (int clientFd, int port, std::string const &clientAddr, http::Router const &)
virtual ~ClientHandler ()
virtual void handleEvent (uint32_t events)
virtual int getFd () const
void pushToSendBuffer (const char *data, size_t length)
 Pushes data into the client's send buffer. This is called by the CGIHandler when it reads from the pipe.
bool isSendBufferFull () const
 Checks if the send buffer is over its high-water mark. This is the backpressure mechanism.
void onCgiComplete ()
 Called by CGIHandler when the CGI script finishes (EOF).
void onCgiHeadersParsed (http::Headers const &)
 Notification that CGI headers have been fully parsed.
void handleError (http::HttpStatus)
 Triggers an error response (e.g., 404, 500).
Public Member Functions inherited from network::IEventHandler
virtual ~IEventHandler ()

Private Member Functions

void handleRead ()
 Handles incoming data on the socket.
void handleDraining ()
void handleStaticResponseWrite ()
 Handles outgoing data on the socket.
void handleCgiResponseWrite ()
void generateResponse ()
 Processes a fully parsed request to generate a response.
void handleRequestParsingState (http::RequestParser::State state)
bool setupCgiHandler (http::IResponseBody *body)
void setupStaticResponse ()
void resetForNewRequest ()
 Resets the ClientHandler state for a new request (keep-alive).
void closeConnection ()
 Closes the connection and removes it from the dispatcher.
void initiateDraining ()
void finalizeConnection ()
 ClientHandler (const ClientHandler &)
ClientHandleroperator= (const ClientHandler &)
std::string getLogSignature () const

Private Attributes

int clientFd_
int port_
std::string clientAddr_
bool headersSent_
http::Router const & router_
http::Request request_
http::Response response_
http::RequestParser reqParser_
bool isKeepAlive_
bool isDraining_
std::time_t drainStartTime_
SendBuffer rspBuffer_
CgiState cgiState_

Static Private Attributes

static const size_t IO_BUFFER_SIZE = 8192

Additional Inherited Members

Protected Member Functions inherited from network::IEventHandler
 IEventHandler ()

Detailed Description

Manages the full lifecycle of a single client connection.

This class acts as a state machine for a client, handling I/O events via epoll. It reads and parses HTTP requests, dispatches them to a router, and manages sending the response back to the client, including streaming large files. Each instance corresponds to one connected client socket.

Constructor & Destructor Documentation

◆ ClientHandler() [1/2]

network::ClientHandler::ClientHandler ( int clientFd,
int port,
std::string const & clientAddr,
http::Router const & router )

◆ ~ClientHandler()

network::ClientHandler::~ClientHandler ( )
virtual

◆ ClientHandler() [2/2]

network::ClientHandler::ClientHandler ( const ClientHandler & )
private

Member Function Documentation

◆ closeConnection()

void network::ClientHandler::closeConnection ( )
private

Closes the connection and removes it from the dispatcher.

◆ finalizeConnection()

void network::ClientHandler::finalizeConnection ( )
private

◆ generateResponse()

void network::ClientHandler::generateResponse ( )
private

Processes a fully parsed request to generate a response.

◆ getFd()

int network::ClientHandler::getFd ( ) const
virtual

◆ getLogSignature()

std::string network::ClientHandler::getLogSignature ( ) const
private

◆ handleCgiResponseWrite()

void network::ClientHandler::handleCgiResponseWrite ( )
private

◆ handleDraining()

void network::ClientHandler::handleDraining ( )
private

◆ handleError()

void network::ClientHandler::handleError ( http::HttpStatus status)

Triggers an error response (e.g., 404, 500).

◆ handleEvent()

void network::ClientHandler::handleEvent ( uint32_t events)
virtual

◆ handleRead()

void network::ClientHandler::handleRead ( )
private

Handles incoming data on the socket.

◆ handleRequestParsingState()

void network::ClientHandler::handleRequestParsingState ( http::RequestParser::State state)
private

◆ handleStaticResponseWrite()

void network::ClientHandler::handleStaticResponseWrite ( )
private

Handles outgoing data on the socket.

◆ initiateDraining()

void network::ClientHandler::initiateDraining ( )
private

◆ isSendBufferFull()

bool network::ClientHandler::isSendBufferFull ( ) const

Checks if the send buffer is over its high-water mark. This is the backpressure mechanism.

◆ onCgiComplete()

void network::ClientHandler::onCgiComplete ( )

Called by CGIHandler when the CGI script finishes (EOF).

◆ onCgiHeadersParsed()

void network::ClientHandler::onCgiHeadersParsed ( http::Headers const & headers)

Notification that CGI headers have been fully parsed.

◆ operator=()

ClientHandler & network::ClientHandler::operator= ( const ClientHandler & )
private

◆ pushToSendBuffer()

void network::ClientHandler::pushToSendBuffer ( const char * data,
size_t length )

Pushes data into the client's send buffer. This is called by the CGIHandler when it reads from the pipe.

◆ resetForNewRequest()

void network::ClientHandler::resetForNewRequest ( )
private

Resets the ClientHandler state for a new request (keep-alive).

◆ setupCgiHandler()

bool network::ClientHandler::setupCgiHandler ( http::IResponseBody * body)
private

◆ setupStaticResponse()

void network::ClientHandler::setupStaticResponse ( )
private

Member Data Documentation

◆ cgiState_

CgiState network::ClientHandler::cgiState_
private

◆ clientAddr_

std::string network::ClientHandler::clientAddr_
private

◆ clientFd_

int network::ClientHandler::clientFd_
private

◆ drainStartTime_

std::time_t network::ClientHandler::drainStartTime_
private

◆ headersSent_

bool network::ClientHandler::headersSent_
private

◆ IO_BUFFER_SIZE

const size_t network::ClientHandler::IO_BUFFER_SIZE = 8192
staticprivate

◆ isDraining_

bool network::ClientHandler::isDraining_
private

◆ isKeepAlive_

bool network::ClientHandler::isKeepAlive_
private

◆ port_

int network::ClientHandler::port_
private

◆ reqParser_

http::RequestParser network::ClientHandler::reqParser_
private

◆ request_

http::Request network::ClientHandler::request_
private

◆ response_

http::Response network::ClientHandler::response_
private

◆ router_

http::Router const& network::ClientHandler::router_
private

◆ rspBuffer_

SendBuffer network::ClientHandler::rspBuffer_
private

The documentation for this class was generated from the following files: