1#include "../includes/Connection.hpp"
2#include "../includes/ServerConf.hpp"
10#include "../includes/FatalExceptions.hpp"
16 _lastActivity(time(NULL)),
34 _lastActivity(time(NULL)),
35 _serverConf(defaultConfig),
44 long long maxBody = 0;
52 : _acceptFD(other._acceptFD),
54 _lastActivity(other._lastActivity),
55 _serverConf(other._serverConf),
56 _locationConf(other._locationConf),
57 _readBufferSize(other._readBufferSize),
58 _readBuffer(other._readBuffer),
61 _writeBufferSize(other._writeBufferSize),
62 _writeBuffer(other._writeBuffer),
64 _totalBytesRead(other._totalBytesRead)
138 std::string leftover =
_readBuffer.substr(headerEnd);
149 if (!leftover.empty())
174 std::string chunk(buf, n);
186 ssize_t n = recv(
_acceptFD, buf,
sizeof(buf), 0);
191 ": " << strerror(errno) << std::endl;
200 size_t len =
static_cast<size_t>(n);
254 std::cerr <<
"client runtime error on fd " <<
_acceptFD <<
": " << e.
what() << std::endl;
261 catch (
const std::exception& e)
263 std::cerr <<
"unexpected runtime error on fd " <<
_acceptFD <<
": " << e.
what() << std::endl;
286 std::ostringstream oss;
ConnectionState
Represents the state of the CLIENT SOCKET.
ReqState
Represents the current network-reading phase of the HTTP request.
Exception type for failures scoped to a single client request.
virtual const char * what() const
int getStatusCode() const
void handleWrite()
Writes data from the _writeBuffer (or Response) to the client socket using send()....
void setLocationConf(const LocationConf *conf)
Assigns the appropriate location block after parsing the request URI.
ConnectionState getState() const
Connection & operator=(const Connection &other)
Response * getResponse() const
const ServerConf * _serverConf
bool hasTimedOut(int timeoutSeconds) const
Checks if the connection has exceeded the defined timeout threshold.
void setState(ConnectionState state)
Request * getRequest() const
void process()
Executes routing logic, instantiates the Response, and prepares data for sending. Transitions state t...
void _updateActivityTimer()
Updates the _lastActivity timestamp to current time.
const LocationConf * _locationConf
void triggerError(int statusCode)
Forces the connection into an error state, bypassing normal processing.
const ServerConf * getServerConf() const
void _readChunked(const char *buf, size_t n)
int getCgiPipeFd() const
Returns the CGI output pipe fd, or -1 if no CGI is active.
void _readHeaders(const char *buf, size_t n)
void handleRead()
Reads data from the client socket using recv() into _readBuffer. Transitions state to PROCESSING if t...
void _readBody(const char *buf, size_t n)
size_t getSize() const
Gets total bytes currently stored (whether in RAM or File).
void append(const char *data, size_t length)
Appends raw byte data to the store. Automatically transitions from RAM to FILE_MODE if _bufferLimit i...
bool isChunkedDone(const std::string &newData) const
Checks if the incoming chunked data contains the terminal "\r\n\r\n". Used to transition from REQ_CHU...
DataStore & getBodyStore()
Returns a reference to the DataStore to allow direct writing from recv().
ReqState getReqState() const
std::string getStatusCode() const
size_t parseHeaders(const std::string &rawBuffer)
Parses the raw buffer containing the HTTP Request-Line and Headers. Transitions state to REQ_BODY,...
long long getContentLength() const
bool processBodySlice()
Unified method to prepare the body for the Response/CGI phase. For Content-Length bodies,...
BuildPhase getBuildPhase() const
void setResponsePhrase(const std::string &phrase)
void buildErrorPage(const std::string &code, const ServerConf &config)
Fast-tracks the response to an error state. Loads the appropriate error page from config or default H...
bool buildResponse(Request &req, const ServerConf &config)
Analyzes the Request and Location settings to prepare the response. Sets the status code,...
int getCgiOutputFd() const
Returns the CGI output pipe fd for epoll registration.
bool sendSlice(int fd)
Sends a slice of the response to the client socket. To be called during a POLLOUT event.
void setStatusCode(const std::string &code)
size_t getMaxBodySize() const
std::string ipv4ToString(const struct ::sockaddr_in &addr)