Webserv
Loading...
Searching...
No Matches
http::Response Class Reference

A data container for an HTTP response, managed via a fluent API. More...

#include <Response.hpp>

Public Member Functions

 Response ()
 ~Response ()
Responseclear ()
 Resets the response to a default state, ready for reuse. Cleans up any existing body and resets headers and status.
IResponseBodybody () const
 Gets a pointer to the (read-only) response body.
ResponsesetNoBody (bool removeContentHeaders=true)
 Sets the response to have no body. Cleans up any existing body.
ResponsesetBodyInMemory (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.
ResponsesetBodyFromFile (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.
ResponsesetBodyFromCgi (int pipeFd, bool hasHeaderParsing)
 Sets the response body to stream from a CGI pipe.
Headersheaders ()
 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.
Responsestatus (HttpStatus statusCode)
 Sets the response status and automatically syncs the reason phrase.
Responsestatus (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_
IResponseBodybody_
std::string customError_

Detailed Description

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.

Constructor & Destructor Documentation

◆ Response() [1/2]

http::Response::Response ( )

◆ ~Response()

http::Response::~Response ( )

◆ Response() [2/2]

http::Response::Response ( Response const & )
private

Member Function Documentation

◆ body()

IResponseBody * http::Response::body ( ) const

Gets a pointer to the (read-only) response body.

Returns
A const pointer to the response body, or NULL if no body is set.

◆ buildHeaders()

void http::Response::buildHeaders ( std::vector< char > & buffer,
bool addBodyLine = true ) const

Appends the fully formatted start-line and headers to a buffer.

Parameters
bufferThe send buffer to which headers will be appended.
addBodyLineIf true, an extra CRLF line is added after the headers

◆ cleanupBody_()

void http::Response::cleanupBody_ ( )
private

Internal: safely deletes the current body object.

◆ clear()

Response & http::Response::clear ( )

Resets the response to a default state, ready for reuse. Cleans up any existing body and resets headers and status.

Returns
A reference to this object for chaining.

◆ customMessage()

std::string const & http::Response::customMessage ( ) const

Gets the custom error message, if any.

◆ getReasonPhrase_()

char const * http::Response::getReasonPhrase_ ( HttpStatus status)
staticprivate

Internal: maps a Status enum to its standard string.

◆ headers() [1/2]

Headers & http::Response::headers ( )

Gets a mutable reference to the response headers.

◆ headers() [2/2]

Headers const & http::Response::headers ( ) const

Gets a constant reference to the response headers.

◆ operator=()

Response const & http::Response::operator= ( Response const & )
private

◆ protocol()

std::string const & http::Response::protocol ( ) const

Gets the current protocol string (e.g., "HTTP/1.1").

◆ reasonPhrase()

std::string const & http::Response::reasonPhrase ( ) const

Gets the current reason phrase (e.g., "Not Found").

◆ setBodyFromCgi()

Response & http::Response::setBodyFromCgi ( int pipeFd,
bool hasHeaderParsing )

Sets the response body to stream from a CGI pipe.

Parameters
pipeFdThe file descriptor of the CGI script's output pipe.
hasHeaderParsingIf true, the response expects the pipe to contain HTTP headers that need parsing before the body content.
Returns
A reference to this object for chaining.

◆ setBodyFromFile()

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.

Parameters
fpathThe file path to read from.
contentTypeThe MIME type of the file.
Returns
A reference to this object for chaining.

◆ setBodyInMemory()

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.

Parameters
dataThe string data to use as the body.
contentTypeThe MIME type of the body (e.g., "application/json").
Returns
A reference to this object for chaining.

◆ setNoBody()

Response & http::Response::setNoBody ( bool removeContentHeaders = true)

Sets the response to have no body. Cleans up any existing body.

Returns
A reference to this object for chaining.

◆ status() [1/3]

HttpStatus http::Response::status ( ) const

Gets the current status code.

◆ status() [2/3]

Response & http::Response::status ( HttpStatus statusCode)

Sets the response status and automatically syncs the reason phrase.

Parameters
statusCodeThe HttpStatus enum value (e.g., NOT_FOUND).
Returns
A reference to this object for chaining.

◆ status() [3/3]

Response & http::Response::status ( HttpStatus statusCode,
std::string const & customMessage )

Sets the response status and automatically syncs the reason phrase.

Parameters
statusCodeThe HttpStatus enum value (e.g., NOT_FOUND).
Returns
A reference to this object for chaining.

Member Data Documentation

◆ body_

IResponseBody* http::Response::body_
private

◆ customError_

std::string http::Response::customError_
private

◆ headers_

Headers http::Response::headers_
private

◆ startLine_

ResponseStartLine http::Response::startLine_
private

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