LeftHookRoll
An HTTP/1.0 compliant web server, as specified by RFC1945
Loading...
Searching...
No Matches
Response.hpp
Go to the documentation of this file.
1/**
2 * @file Response.hpp
3 * @brief Formulates the HTTP response and manages non-blocking data transmission.
4 * * This class decides the course of action (uploading/creating a file,CGI,static message) and
5 * manages the state of the outgoing data stream.
6 */
7#pragma once
8
9#include <string>
10#include <sys/types.h>
11#include <map>
12#include <vector>
13
14#include "DataStore.hpp"
15#include "ServerConf.hpp"
16#include "Request.hpp"
17#include "CGIManager.hpp"
18
19/**
20 * @enum ResponseState
21 * @brief Tracks the progress of sending the response to the client.
22 */
24{
25 SENDING_RES_HEAD, // Sending the Status-Line and Headers
26 SENDING_BODY_STATIC, // Sending a static file from the DataStore
27 SENDING_BODY_CHUNKED // Sending CGI output using Chunked Transfer Coding
28};
29
30/**
31 * @enum BuildPhase
32 * @brief Tracks the incremental construction of the response body.
33 * exclusivly for POST.
34 */
42
43class Response {
44public:
45
46 Response();
47 Response(const Response& other);
48 Response& operator=(const Response& other);
49 ~Response();
50
51 /**
52 * @brief Analyzes the Request and Location settings to prepare the response.
53 * Sets the status code, phrase, and loads the DataStore with content.
54 * @return true if the response is finished, false if not.
55 */
56 bool buildResponse(Request& req, const ServerConf& config);
57
58 /**
59 * @brief Fast-tracks the response to an error state.
60 * Loads the appropriate error page from config or default HTML.
61 * @param code The HTTP status code (e.g., "404", "500").
62 */
63 void buildErrorPage(const std::string& code, const ServerConf& config);
64
65 /**
66 * @brief Sends a slice of the response to the client socket.
67 * To be called during a POLLOUT event.
68 * @param fd The client socket file descriptor.
69 * @return true if the entire response is finished, false if more data remains.
70 */
71 bool sendSlice(int fd);
72
73 const std::string& getStatusCode() const;
74 const std::string& getVersion() const;
75 const std::string& getResponsePhrase() const;
79
80 /**
81 * @brief Returns the CGI output pipe fd for epoll registration.
82 * @return The fd, or -1 if no CGI is active.
83 */
84 int getCgiOutputFd() const;
85
86 /**
87 * @brief Called by ServerManager when the CGI pipe is readable.
88 * Reads available data into _responseDataStore.
89 * @return true if CGI output is fully consumed (EOF reached), false if more data expected.
90 */
91 bool readCgiOutput();
92
93 /**
94 * @brief Called by ServerManager when the CGI process times out.
95 * Kills the process and builds a 504 error page.
96 */
97 void cgiTimeout(const ServerConf& config);
98
99 /**
100 * @brief Finalizes the CGI response after all output has been read.
101 * Parses CGI output headers, builds the HTTP response, transitions to sendable state.
102 */
103 void finalizeCgiResponse();
104
105 void setStatusCode(const std::string& code);
106 void setResponsePhrase(const std::string& phrase);
107
108 /**
109 * @brief Adds a header to the response (e.g., "Content-Type", "text/html").
110 */
111 void addHeader(const std::string& key, const std::string& value);
112 void addCookie(const Request& req);
113 const std::vector<std::string>& getSetCookies() const;
114
115private:
116 std::string _statusCode; // e.g., "200"
117 std::string _version; // e.g., "HTTP/1.1"
118 std::string _response_phrase; // e.g., "OK"
119
123 std::map<std::string, std::string> _headers;
124 std::vector<std::string> _setCookies;
125 int _fileFd; // Open FD for the file being streamed; -1 when not in use
126 size_t _fileSize; // Total byte count from stat(); used for Content-Length and end detection
127 std::vector<char> _streamBuf; // Holds the latest chunk read from _fileFd, retained across EAGAIN
128 size_t _streamBufLen; // How many bytes are currently valid in _streamBuf
129 size_t _streamBufSent; // How many of those bytes have been sent to the socket so far
132
133 // concurrent POST state.
138 std::string _postFilename;
139
141
142 // Private Helpers
143 std::string _generateHeaderString();
144 std::string _lookupReasonPhrase(const std::string& code);
145
146 void _handleGet(const Request& req, const LocationConf& loc, const ServerConf& config);
147 bool _handlePost(Request& req, const LocationConf& loc, const ServerConf& config);
148 bool _continuePostWrite(Request& req);
149 void _handleDelete(const Request& req, const LocationConf& loc, const ServerConf& config);
150 bool _handleCGI(Request& req, const LocationConf& loc, const ServerConf& config);
151
152 void _finalizeSuccess(const std::string& contentType);
153 void _serveFile(const std::string& path, const ServerConf& config);
154
155 std::string _drainDataStore();
156 void _splitCgiOutput(const std::string& raw, std::string& headers, std::string& body);
157 bool _parseCgiHeaders(const std::string& headerBlock, std::string& contentType);
158
159 bool _sendHeader(int fd);
160 bool _sendBodyStatic(int fd);
161 bool _sendBodyFile(int fd);
162 bool _sendBodyDataStore(int fd);
163 bool _sendBodyChunked(int fd);
164
165 // Serialized header line (Status-Line + Headers + blank line) cached after build
166 std::string _headerBuffer;
167};
Declares the CGIManager class responsible for executing CGI scripts and redirecting their input/outpu...
A DataStore is a flexible data structure, it is either a RAM buffer or a file on disk,...
Parses and stores the entire HTTP request entity (headers, body). Utilizes a State Machine to handle ...
ResponseState
Tracks the progress of sending the response to the client.
Definition Response.hpp:24
@ SENDING_BODY_CHUNKED
Definition Response.hpp:27
@ SENDING_RES_HEAD
Definition Response.hpp:25
@ SENDING_BODY_STATIC
Definition Response.hpp:26
BuildPhase
Tracks the incremental construction of the response body. exclusivly for POST.
Definition Response.hpp:36
@ BUILD_IDLE
Definition Response.hpp:37
@ BUILD_CGI_RUNNING
Definition Response.hpp:39
@ BUILD_POST_WRITING
Definition Response.hpp:38
@ BUILD_DONE
Definition Response.hpp:40
Stores per-server configuration directives. This class encapsulates the configuration for a specific ...
CGIManager * _cgiInstance
Definition Response.hpp:130
BuildPhase getBuildPhase() const
Definition Response.cpp:867
std::map< std::string, std::string > _headers
Definition Response.hpp:123
bool _sendBodyDataStore(int fd)
Definition Response.cpp:492
std::vector< char > _streamBuf
Definition Response.hpp:127
const std::string & getVersion() const
Definition Response.cpp:864
size_t _streamBufLen
Definition Response.hpp:128
void setResponsePhrase(const std::string &phrase)
void _finalizeSuccess(const std::string &contentType)
Definition Response.cpp:820
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...
Definition Response.cpp:364
void addCookie(const Request &req)
int _fileFd
Definition Response.hpp:125
const std::vector< std::string > & getSetCookies() const
bool buildResponse(Request &req, const ServerConf &config)
Analyzes the Request and Location settings to prepare the response. Sets the status code,...
Definition Response.cpp:298
size_t _totalBytesSent
Definition Response.hpp:122
std::string _generateHeaderString()
std::string _statusCode
Definition Response.hpp:116
bool _sendBodyChunked(int fd)
std::string _drainDataStore()
Definition Response.cpp:994
DataStore _responseDataStore
Definition Response.hpp:121
bool _sendBodyFile(int fd)
Definition Response.cpp:450
Response & operator=(const Response &other)
Definition Response.cpp:252
CGIManager * getCgiInstance() const
Definition Response.cpp:868
const ServerConf * _cachedConfig
Definition Response.hpp:135
int getCgiOutputFd() const
Returns the CGI output pipe fd for epoll registration.
Definition Response.cpp:870
bool sendSlice(int fd)
Sends a slice of the response to the client socket. To be called during a POLLOUT event.
Definition Response.cpp:409
size_t _writeBufferSize
Definition Response.hpp:120
void addHeader(const std::string &key, const std::string &value)
Adds a header to the response (e.g., "Content-Type", "text/html").
ResponseState getResponseState() const
Definition Response.cpp:866
int _postOutFd
Definition Response.hpp:136
void finalizeCgiResponse()
Finalizes the CGI response after all output has been read. Parses CGI output headers,...
Definition Response.cpp:930
std::string _lookupReasonPhrase(const std::string &code)
size_t _currentChunkSize
Definition Response.hpp:131
bool readCgiOutput()
Called by ServerManager when the CGI pipe is readable. Reads available data into _responseDataStore.
Definition Response.cpp:877
std::vector< std::string > _setCookies
Definition Response.hpp:124
size_t _postWritePos
Definition Response.hpp:137
void _serveFile(const std::string &path, const ServerConf &config)
Definition Response.cpp:830
bool _parseCgiHeaders(const std::string &headerBlock, std::string &contentType)
void _handleGet(const Request &req, const LocationConf &loc, const ServerConf &config)
Definition Response.cpp:540
const std::string & getResponsePhrase() const
Definition Response.cpp:865
std::string _version
Definition Response.hpp:117
std::string _headerBuffer
Definition Response.hpp:166
void _splitCgiOutput(const std::string &raw, std::string &headers, std::string &body)
BuildPhase _buildPhase
Definition Response.hpp:134
ResponseState _responseState
Definition Response.hpp:140
std::string _response_phrase
Definition Response.hpp:118
const std::string & getStatusCode() const
Definition Response.cpp:863
size_t _streamBufSent
Definition Response.hpp:129
void cgiTimeout(const ServerConf &config)
Called by ServerManager when the CGI process times out. Kills the process and builds a 504 error page...
Definition Response.cpp:910
bool _handlePost(Request &req, const LocationConf &loc, const ServerConf &config)
Definition Response.cpp:608
std::string _postFilename
Definition Response.hpp:138
bool _handleCGI(Request &req, const LocationConf &loc, const ServerConf &config)
Definition Response.cpp:713
void _handleDelete(const Request &req, const LocationConf &loc, const ServerConf &config)
Definition Response.cpp:773
bool _sendHeader(int fd)
Definition Response.cpp:418
void setStatusCode(const std::string &code)
bool _continuePostWrite(Request &req)
Definition Response.cpp:648
bool _sendBodyStatic(int fd)
Definition Response.cpp:442
size_t _fileSize
Definition Response.hpp:126