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

Manages a collection of HTTP headers. More...

#include <Headers.hpp>

Public Types

typedef std::map< std::string, std::string > HeaderMap
typedef HeaderMap::iterator iterator
typedef HeaderMap::const_iterator const_iterator

Public Member Functions

 Headers ()
 Constructs an empty Headers object.
bool has (std::string const &key) const
 Checks if a header with the given key exists.
Headersadd (std::string const &key, std::string const &value)
 Adds or overwrites a header key-value pair.
bool get (const std::string &key, std::string &value) const
 C++98-style getter. Retrieves a header value.
std::string get (const std::string &key) const
 C++11-style getter. Retrieves a header value.
size_t getContentLength () const
 A specialized accessor for the Content-Length header.
bool isContentChunked () const
 Checks if the 'Transfer-Encoding' header is set to 'chunked'.
Headersclear ()
 Clears all headers from the map.
Headerserase (std::string const &key)
 Removes a single header by its key.
std::string toString () const
 Serializes all headers into a single string. Each header is on its own line, formatted as "Key: Value\r\n".
HeaderMap const & getMap () const
 Returns a const reference to the internal header map. Useful for iterating over all headers (e.g., for CGI environment).
const_iterator find (std::string const &key) const
const_iterator begin () const
const_iterator end () const

Static Public Member Functions

static bool parse (std::string &, Headers &, bool strict=false)
 Parses a raw string of headers into a provided Headers reference. The string should contain multiple "Key: Value\r\n" lines.
static bool parse (std::istringstream &, Headers &, bool strict=false)
 Parses a raw string of headers into a provided Headers reference. The string should contain multiple "Key: Value\r\n" lines.
static size_t findHeaderEnd (const std::string &buffer, size_t &offset)
 Finds the end of the HTTP header section in a buffer. Compatible with \r
\r
,

, \r

, and
\r
.

Static Private Member Functions

static void normalizeKey (std::string &key)
 Internal: Normalizes a key to lowercase in-place.
static std::string normalizeKey (std::string const &key)
 Internal: Returns a lowercase version of a key.

Private Attributes

HeaderMap map_
 The internal storage for headers, mapping lowercase_key -> Original_Value.

Detailed Description

Manages a collection of HTTP headers.

This class stores HTTP headers in a case-insensitive manner (by normalizing keys to lowercase) and provides helpers for common header operations.

Member Typedef Documentation

◆ const_iterator

typedef HeaderMap::const_iterator http::Headers::const_iterator

◆ HeaderMap

typedef std::map<std::string, std::string> http::Headers::HeaderMap

◆ iterator

typedef HeaderMap::iterator http::Headers::iterator

Constructor & Destructor Documentation

◆ Headers()

http::Headers::Headers ( )

Constructs an empty Headers object.

Member Function Documentation

◆ add()

Headers & http::Headers::add ( std::string const & key,
std::string const & value )

Adds or overwrites a header key-value pair.

Parameters
keyThe header name.
valueThe header value.
Returns
A reference to this Headers object for chaining.

◆ begin()

Headers::const_iterator http::Headers::begin ( ) const

◆ clear()

Headers & http::Headers::clear ( )

Clears all headers from the map.

Returns
A reference to this Headers object for chaining.

◆ end()

Headers::const_iterator http::Headers::end ( ) const

◆ erase()

Headers & http::Headers::erase ( std::string const & key)

Removes a single header by its key.

Parameters
keyThe header name to remove (case-insensitive).
Returns
A reference to this Headers object for chaining.

◆ find()

Headers::const_iterator http::Headers::find ( std::string const & key) const

◆ findHeaderEnd()

size_t http::Headers::findHeaderEnd ( const std::string & buffer,
size_t & offset )
static

Finds the end of the HTTP header section in a buffer. Compatible with \r
\r
,

, \r

, and
\r
.

Parameters
bufferThe input buffer to search.
[out]offsetThe length of the delimiter found (2, 3, or 4).
Returns
The starting position of the delimiter, or std::string::npos if not found.

◆ get() [1/2]

std::string http::Headers::get ( const std::string & key) const

C++11-style getter. Retrieves a header value.

Parameters
keyThe header name (case-insensitive).
Returns
The header value as a string. Returns an empty string if not found.

◆ get() [2/2]

bool http::Headers::get ( const std::string & key,
std::string & value ) const

C++98-style getter. Retrieves a header value.

Parameters
keyThe header name (case-insensitive).
[out]valueThe string to be filled with the header's value.
Returns
True if the key was found, false otherwise.

◆ getContentLength()

size_t http::Headers::getContentLength ( ) const

A specialized accessor for the Content-Length header.

Returns
The value of Content-Length as a number, or 0 if not present or invalid.

◆ getMap()

Headers::HeaderMap const & http::Headers::getMap ( ) const

Returns a const reference to the internal header map. Useful for iterating over all headers (e.g., for CGI environment).

Returns
Const reference to the internal map of headers.

◆ has()

bool http::Headers::has ( std::string const & key) const

Checks if a header with the given key exists.

Parameters
keyThe header name (case-insensitive).
Returns
True if the header is present, false otherwise.

◆ isContentChunked()

bool http::Headers::isContentChunked ( ) const

Checks if the 'Transfer-Encoding' header is set to 'chunked'.

Returns
True if the transfer encoding is chunked, false otherwise.

◆ normalizeKey() [1/2]

void http::Headers::normalizeKey ( std::string & key)
staticprivate

Internal: Normalizes a key to lowercase in-place.

◆ normalizeKey() [2/2]

std::string http::Headers::normalizeKey ( std::string const & key)
staticprivate

Internal: Returns a lowercase version of a key.

◆ parse() [1/2]

bool http::Headers::parse ( std::istringstream & s,
Headers & res,
bool strict = false )
static

Parses a raw string of headers into a provided Headers reference. The string should contain multiple "Key: Value\r\n" lines.

Parameters
inputThe raw string containing headers.
[out]headersThe Headers object to be filled.
strictIf true, parsing is strict; otherwise, more lenient.
Returns
True if parsing succeeded, false otherwise.

◆ parse() [2/2]

bool http::Headers::parse ( std::string & buffer,
Headers & headers,
bool strict = false )
static

Parses a raw string of headers into a provided Headers reference. The string should contain multiple "Key: Value\r\n" lines.

Parameters
inputThe raw string containing headers.
[out]headersThe Headers object to be filled.
strictIf true, parsing is strict; otherwise, more lenient.
Returns
True if parsing succeeded, false otherwise.

◆ toString()

std::string http::Headers::toString ( ) const

Serializes all headers into a single string. Each header is on its own line, formatted as "Key: Value\r\n".

Returns
A string containing all headers for an HTTP response.

Member Data Documentation

◆ map_

HeaderMap http::Headers::map_
private

The internal storage for headers, mapping lowercase_key -> Original_Value.


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