LeftHookRoll
An HTTP/1.0 compliant web server, as specified by RFC1945
Loading...
Searching...
No Matches
Connection.hpp
Go to the documentation of this file.
1/**
2 * @file Connection.hpp
3 * @brief Will be spawned when we accept() a new connection, will handle reading from the socket, writing to the socket, and the state of the connection.
4 */
5
6#pragma once
7
8#define MAX_HEADER_SIZE 8192
9
10#include <string>
11#include <ctime>
12#include <netinet/in.h>
13#include <sys/types.h>
14
15#include "ServerConf.hpp"
16#include "LocationConf.hpp"
17#include "Request.hpp"
18#include "Response.hpp"
19
20/**
21 * @enum ConnectionState
22 * @brief Represents the state of the CLIENT SOCKET.
23 */
25{
26 READING, // Waiting for POLLIN on client socket. Accumulating request.
27 WRITING, // Waiting for POLLOUT on client socket. Draining the write buffer.
28 PROCESSING, // CPU-bound phase. Parsing, routing, checking permissions.
29 WAITING_FOR_CGI, // The socket is idle. We are waiting for the CGI pipe to give us data.
30 FINISHED, // Transaction complete. Ready to close socket.
31};
32
34{
35 public:
36 // Canonical Form
37 Connection();
38 Connection(int fd, const struct sockaddr_in& ipa, const ServerConf* defaultConfig);
39 Connection(const Connection& other);
40 Connection& operator=(const Connection& other);
42
43 // State Machine Actions
44 /**
45 * @brief Reads data from the client socket using recv() into _readBuffer.
46 * Transitions state to PROCESSING if the request is fully received.
47 */
48 void handleRead();
49
50 /**
51 * @brief Executes routing logic, instantiates the Response, and prepares data for sending.
52 * Transitions state to WRITING or handles CGI setup based on LocationConf.
53 */
54 void process();
55
56 /**
57 * @brief Writes data from the _writeBuffer (or Response) to the client socket using send().
58 * Transitions state to FINISHED when the transaction is completely sent.
59 */
60 void handleWrite();
61
62 // Error & Timeout Management
63 /**
64 * @brief Checks if the connection has exceeded the defined timeout threshold.
65 * @param timeoutSeconds The maximum allowed idle time in seconds.
66 * @return true if timed out, false otherwise.
67 */
68 bool hasTimedOut(int timeoutSeconds) const;
69
70 /**
71 * @brief Forces the connection into an error state, bypassing normal processing.
72 * @param statusCode The HTTP status code to generate (e.g., 400, 408, 500).
73 */
74 void triggerError(int statusCode);
75
76 // Getters & Setters
77
78 int getFd() const;
80 void setState(ConnectionState state);
81 Response* getResponse() const;
82 Request* getRequest() const;
83 const ServerConf* getServerConf() const;
84
85 /**
86 * @brief Returns the CGI output pipe fd, or -1 if no CGI is active.
87 */
88 int getCgiPipeFd() const;
89
90 /**
91 * @brief Assigns the appropriate location block after parsing the request URI.
92 * @param conf The correctly matched location configuration.
93 */
94 void setLocationConf(const LocationConf* conf);
95
96 private:
97 // Identity
99 struct sockaddr_in _IPA;
101
102 // Config
105
106 // Data
108 std::string _readBuffer;
109
112
114 std::string _writeBuffer;
115
116 // Dynamic Data
119
120 // Private Helpers
121 /**
122 * @brief Updates the _lastActivity timestamp to current time.
123 */
125
126 // handleRead sub-routines
127 void _readHeaders(const char* buf, size_t n);
128 void _readBody(const char* buf, size_t n);
129 void _readChunked(const char* buf, size_t n);
130};
ConnectionState
Represents the state of the CLIENT SOCKET.
@ PROCESSING
@ READING
@ FINISHED
@ WRITING
@ WAITING_FOR_CGI
Stores per-route configuration directives. This class encapsulates the configuration for a specific l...
Parses and stores the entire HTTP request entity (headers, body). Utilizes a State Machine to handle ...
Formulates the HTTP response and manages non-blocking data transmission.
Stores per-server configuration directives. This class encapsulates the configuration for a specific ...
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 _state
ConnectionState getState() const
std::string _writeBuffer
Connection & operator=(const Connection &other)
Response * getResponse() const
size_t _writeBufferSize
const ServerConf * _serverConf
bool hasTimedOut(int timeoutSeconds) const
Checks if the connection has exceeded the defined timeout threshold.
void setState(ConnectionState state)
struct sockaddr_in _IPA
std::string _readBuffer
Request * _request
Response * _response
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
int getFd() const
void triggerError(int statusCode)
Forces the connection into an error state, bypassing normal processing.
const ServerConf * getServerConf() const
time_t _lastActivity
size_t _readBufferSize
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...
size_t _totalBytesRead
void _readBody(const char *buf, size_t n)