Webserv
Loading...
Searching...
No Matches
http::IResponseBody Class Referenceabstract

An abstract interface for all HTTP response body sources. More...

#include <ResponseBody.hpp>

Inheritance diagram for http::IResponseBody:
http::BodyFromCgi http::BodyInMemory http::FileBody

Public Member Functions

 IResponseBody ()
virtual ~IResponseBody ()
virtual ssize_t read (char *buffer, size_t size)=0
 Reads a chunk of the body into the provided buffer. This is a non-blocking operation.
virtual size_t size () const =0
 Returns the total size of the body in bytes, if known.
virtual bool isDone () const =0
 Checks if the body source has been fully read.
virtual int getEventSourceFd () const
 Retrieves the file descriptor for event-driven body sources.
virtual bool hasHeaderParsing () const
 Indicates if the data read from this source contains HTTP headers that need to be parsed by the server (e.g., CGI output).

Private Member Functions

 IResponseBody (IResponseBody const &)
IResponseBody const & operator= (IResponseBody const &)

Detailed Description

An abstract interface for all HTTP response body sources.

This class defines a "strategy" for sending a response body. It allows the Reactor to stream data from any source (memory, file, CGI pipe) by simply calling read(), without needing to know the underlying type.

Constructor & Destructor Documentation

◆ IResponseBody() [1/2]

http::IResponseBody::IResponseBody ( )
inline

◆ ~IResponseBody()

virtual http::IResponseBody::~IResponseBody ( )
inlinevirtual

◆ IResponseBody() [2/2]

http::IResponseBody::IResponseBody ( IResponseBody const & )
private

Member Function Documentation

◆ getEventSourceFd()

int http::IResponseBody::getEventSourceFd ( ) const
virtual

Retrieves the file descriptor for event-driven body sources.

This method is used by the Reactor to determine if the response body source requires monitoring via the event loop (epoll).

Returns
A non-negative file descriptor (fd >= 0) if the source is "active" (e.g., a CGI pipe) and must be watched for read events.
-1 if the source is "passive" (e.g., in-memory data or a standard file) and does not require epoll monitoring.

Reimplemented in http::BodyFromCgi.

◆ hasHeaderParsing()

bool http::IResponseBody::hasHeaderParsing ( ) const
virtual

Indicates if the data read from this source contains HTTP headers that need to be parsed by the server (e.g., CGI output).

Returns
true if the Reactor should buffer and parse headers before streaming.
false if the data is the raw body (default).

Reimplemented in http::BodyFromCgi.

◆ isDone()

virtual bool http::IResponseBody::isDone ( ) const
pure virtual

Checks if the body source has been fully read.

Returns
true if the source is depleted (EOF has been reached), false otherwise.

Implemented in http::BodyFromCgi, http::BodyInMemory, and http::FileBody.

◆ operator=()

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

◆ read()

virtual ssize_t http::IResponseBody::read ( char * buffer,
size_t size )
pure virtual

Reads a chunk of the body into the provided buffer. This is a non-blocking operation.

Parameters
bufferAn output buffer to write data into.
sizeThe maximum size of the buffer.
Returns
The number of bytes read ( > 0).
0 on End-of-File (EOF).
-1 on error. errno may be set (e.g., to EAGAIN/EWOULDBLOCK if the source is not ready).

Implemented in http::BodyFromCgi, http::BodyInMemory, and http::FileBody.

◆ size()

virtual size_t http::IResponseBody::size ( ) const
pure virtual

Returns the total size of the body in bytes, if known.

Returns
The total content length. Returns 0 or (size_t)-1 if the size is unknown (e.g., for a streaming CGI response).

Implemented in http::BodyFromCgi, http::BodyInMemory, and http::FileBody.


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