|
Webserv
|
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. | |
| Headers & | add (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'. | |
| Headers & | clear () |
| Clears all headers from the map. | |
| Headers & | erase (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. | |
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.
| typedef HeaderMap::const_iterator http::Headers::const_iterator |
| typedef std::map<std::string, std::string> http::Headers::HeaderMap |
| typedef HeaderMap::iterator http::Headers::iterator |
| http::Headers::Headers | ( | ) |
Constructs an empty Headers object.
| Headers & http::Headers::add | ( | std::string const & | key, |
| std::string const & | value ) |
Adds or overwrites a header key-value pair.
| key | The header name. |
| value | The header value. |
| Headers::const_iterator http::Headers::begin | ( | ) | const |
| Headers & http::Headers::clear | ( | ) |
Clears all headers from the map.
| Headers::const_iterator http::Headers::end | ( | ) | const |
| Headers & http::Headers::erase | ( | std::string const & | key | ) |
Removes a single header by its key.
| key | The header name to remove (case-insensitive). |
| Headers::const_iterator http::Headers::find | ( | std::string const & | key | ) | const |
|
static |
Finds the end of the HTTP header section in a buffer. Compatible with \r
\r
,
, \r
, and
\r
.
| buffer | The input buffer to search. | |
| [out] | offset | The length of the delimiter found (2, 3, or 4). |
| std::string http::Headers::get | ( | const std::string & | key | ) | const |
C++11-style getter. Retrieves a header value.
| key | The header name (case-insensitive). |
| bool http::Headers::get | ( | const std::string & | key, |
| std::string & | value ) const |
C++98-style getter. Retrieves a header value.
| key | The header name (case-insensitive). | |
| [out] | value | The string to be filled with the header's value. |
| size_t http::Headers::getContentLength | ( | ) | const |
A specialized accessor for the Content-Length header.
| 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).
| bool http::Headers::has | ( | std::string const & | key | ) | const |
Checks if a header with the given key exists.
| key | The header name (case-insensitive). |
| bool http::Headers::isContentChunked | ( | ) | const |
Checks if the 'Transfer-Encoding' header is set to 'chunked'.
|
staticprivate |
Internal: Normalizes a key to lowercase in-place.
|
staticprivate |
Internal: Returns a lowercase version of a key.
|
static |
Parses a raw string of headers into a provided Headers reference. The string should contain multiple "Key: Value\r\n" lines.
| input | The raw string containing headers. | |
| [out] | headers | The Headers object to be filled. |
| strict | If true, parsing is strict; otherwise, more lenient. |
|
static |
Parses a raw string of headers into a provided Headers reference. The string should contain multiple "Key: Value\r\n" lines.
| input | The raw string containing headers. | |
| [out] | headers | The Headers object to be filled. |
| strict | If true, parsing is strict; otherwise, more lenient. |
| 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".
|
private |
The internal storage for headers, mapping lowercase_key -> Original_Value.