|
Webserv
|
A data container for an HTTP response, managed via a fluent API. More...
#include <Response.hpp>
Public Member Functions | |
| Response () | |
| ~Response () | |
| Response & | clear () |
| Resets the response to a default state, ready for reuse. Cleans up any existing body and resets headers and status. | |
| IResponseBody * | body () const |
| Gets a pointer to the (read-only) response body. | |
| Response & | setNoBody (bool removeContentHeaders=true) |
| Sets the response to have no body. Cleans up any existing body. | |
| Response & | setBodyInMemory (std::string const &data, std::string const &contentType) |
| Sets the response body from an in-memory string. This takes ownership of the data (by copying) and sets Content-Type. | |
| Response & | setBodyFromFile (std::string const &fpath, std::string const &contentType) |
| Sets the response body to stream from a file. Creates a FileBody object to manage the file descriptor. | |
| Response & | setBodyFromCgi (int pipeFd, bool hasHeaderParsing) |
| Sets the response body to stream from a CGI pipe. | |
| Headers & | headers () |
| Gets a mutable reference to the response headers. | |
| Headers const & | headers () const |
| Gets a constant reference to the response headers. | |
| void | buildHeaders (std::vector< char > &buffer, bool addBodyLine=true) const |
| Appends the fully formatted start-line and headers to a buffer. | |
| Response & | status (HttpStatus statusCode) |
| Sets the response status and automatically syncs the reason phrase. | |
| Response & | status (HttpStatus statusCode, std::string const &customMessage) |
| Sets the response status and automatically syncs the reason phrase. | |
| HttpStatus | status () const |
| Gets the current status code. | |
| std::string const & | protocol () const |
| Gets the current protocol string (e.g., "HTTP/1.1"). | |
| std::string const & | reasonPhrase () const |
| Gets the current reason phrase (e.g., "Not Found"). | |
| std::string const & | customMessage () const |
| Gets the custom error message, if any. | |
Private Member Functions | |
| Response (Response const &) | |
| Response const & | operator= (Response const &) |
| void | cleanupBody_ () |
| Internal: safely deletes the current body object. | |
Static Private Member Functions | |
| static char const * | getReasonPhrase_ (HttpStatus status) |
| Internal: maps a Status enum to its standard string. | |
Private Attributes | |
| ResponseStartLine | startLine_ |
| Headers | headers_ |
| IResponseBody * | body_ |
| std::string | customError_ |
A data container for an HTTP response, managed via a fluent API.
This class encapsulates all components of an HTTP response. Its state is modified by handlers through a fluent interface (methods returning *this), which ensures resources are managed safely and state is kept consistent.
| http::Response::Response | ( | ) |
| http::Response::~Response | ( | ) |
|
private |
| IResponseBody * http::Response::body | ( | ) | const |
Gets a pointer to the (read-only) response body.
| void http::Response::buildHeaders | ( | std::vector< char > & | buffer, |
| bool | addBodyLine = true ) const |
Appends the fully formatted start-line and headers to a buffer.
| buffer | The send buffer to which headers will be appended. |
| addBodyLine | If true, an extra CRLF line is added after the headers |
|
private |
Internal: safely deletes the current body object.
| Response & http::Response::clear | ( | ) |
Resets the response to a default state, ready for reuse. Cleans up any existing body and resets headers and status.
| std::string const & http::Response::customMessage | ( | ) | const |
Gets the custom error message, if any.
|
staticprivate |
Internal: maps a Status enum to its standard string.
| Headers & http::Response::headers | ( | ) |
Gets a mutable reference to the response headers.
| Headers const & http::Response::headers | ( | ) | const |
Gets a constant reference to the response headers.
| std::string const & http::Response::protocol | ( | ) | const |
Gets the current protocol string (e.g., "HTTP/1.1").
| std::string const & http::Response::reasonPhrase | ( | ) | const |
Gets the current reason phrase (e.g., "Not Found").
| Response & http::Response::setBodyFromCgi | ( | int | pipeFd, |
| bool | hasHeaderParsing ) |
Sets the response body to stream from a CGI pipe.
| pipeFd | The file descriptor of the CGI script's output pipe. |
| hasHeaderParsing | If true, the response expects the pipe to contain HTTP headers that need parsing before the body content. |
| Response & http::Response::setBodyFromFile | ( | std::string const & | fpath, |
| std::string const & | contentType ) |
Sets the response body to stream from a file. Creates a FileBody object to manage the file descriptor.
| fpath | The file path to read from. |
| contentType | The MIME type of the file. |
| Response & http::Response::setBodyInMemory | ( | std::string const & | data, |
| std::string const & | contentType ) |
Sets the response body from an in-memory string. This takes ownership of the data (by copying) and sets Content-Type.
| data | The string data to use as the body. |
| contentType | The MIME type of the body (e.g., "application/json"). |
| Response & http::Response::setNoBody | ( | bool | removeContentHeaders = true | ) |
Sets the response to have no body. Cleans up any existing body.
| HttpStatus http::Response::status | ( | ) | const |
Gets the current status code.
| Response & http::Response::status | ( | HttpStatus | statusCode | ) |
Sets the response status and automatically syncs the reason phrase.
| statusCode | The HttpStatus enum value (e.g., NOT_FOUND). |
| Response & http::Response::status | ( | HttpStatus | statusCode, |
| std::string const & | customMessage ) |
Sets the response status and automatically syncs the reason phrase.
| statusCode | The HttpStatus enum value (e.g., NOT_FOUND). |
|
private |
|
private |
|
private |
|
private |