Thoth
A functional, expressive, asynchronous C++26 webdev library
Loading...
Searching...
No Matches
Thoth::Http::Headers Struct Reference

This class stores the headers from HTTP. More...

#include <Headers.hpp>

Inheritance diagram for Thoth::Http::Headers:
[legend]

Public Types

using HeaderKey = NHeaders::HeaderKey
 
using HeaderKeyRef = NHeaders::HeaderKeyRef
 
using HeaderValue = NHeaders::HeaderValue
 
using HeaderValueRef = NHeaders::HeaderValueRef
 
using HeaderPair = NHeaders::HeaderPair
 
using HeaderPairRef = NHeaders::HeaderPairRef
 
using MapType = NHeaders::MapType
 
using MapType = std::vector< HeaderPair >
 
using IterType = decltype(MapType{}.begin())
 
using CIterType = decltype(MapType{}.cbegin())
 
using RIterType = decltype(MapType{}.rbegin())
 
using CRIterType = decltype(MapType{}.crbegin())
 

Public Member Functions

 Headers ()
 
 Headers (const MapType &initAs)
 Create with an existing vector.
 
 Headers (std::initializer_list< HeaderPair > init)
 
bool Exists (HeaderKeyRef key) const
 check if a key exists.
 
bool Exists (HeaderPairRef p) const
 check if a key exists.
 
bool Exists (HeaderKeyRef key, HeaderValueRef val) const
 check if a key=val exists.
 
void Add (HeaderPairRef p)
 Add a value with the specified key. Append if already exists.
 
void Add (HeaderKeyRef key, HeaderValueRef val)
 same as Add(HeaderPairRef p).
 
void Set (HeaderPairRef p)
 Add a value with the specified key. Replace if already exists.
 
void Set (HeaderKeyRef key, HeaderValueRef val)
 same as Add(HeaderPairRef p).
 
bool Remove (HeaderPairRef p)
 Remove a value with the specified key.
 
bool Remove (HeaderKeyRef key, HeaderValueRef val)
 same as Remove(HeaderPairRef p).
 
bool SetIfNull (HeaderPairRef p)
 If key not exists, set it to value.
 
bool SetIfNull (HeaderKeyRef key, HeaderValueRef val)
 same as SetIfNull(HeaderPairRef p).
 
std::optional< HeaderValue * > Get (HeaderKeyRef key)
 Get the reference of a key but don't create if it not exists.
 
std::optional< const HeaderValue * > Get (HeaderKeyRef key) const
 Get the reference of a key but don't create if it not exists.
 
IterType begin ()
 
IterType end ()
 
CIterType begin () const
 
CIterType end () const
 
RIterType rbegin ()
 
RIterType rend ()
 
CRIterType rbegin () const
 
CRIterType rend () const
 
void Clear ()
 Clear all keys.
 
size_t Size () const
 
bool Empty () const
 
HeaderValueoperator[] (HeaderKeyRef key)
 
bool operator== (const Headers &other) const
 
Proxies

Convenient calls to some headers.

NHeaders::ListProxy< false, NHeaders::MimeTypeAccept ()
 Accept-Encoding header (gzip, br, etc).
 
NHeaders::ListProxy< true, NHeaders::MimeTypeAccept () const
 
NHeaders::ListProxy< false, NHeaders::AcceptEncodingEnumAcceptEncoding ()
 Accept-Encoding header (gzip, br, etc).
 
NHeaders::ListProxy< true, NHeaders::AcceptEncodingEnumAcceptEncoding () const
 
NHeaders::ValueProxy< false, NHeaders::MimeTypeContentType ()
 Defines the media type of the resource (MIME).
 
NHeaders::ValueProxy< true, NHeaders::MimeTypeContentType () const
 Defines the media type of the resource (MIME).
 
NHeaders::ValueProxy< false, uint64_t > ContentLength ()
 The size of the entity-body in bytes.
 
NHeaders::ValueProxy< true, uint64_t > ContentLength () const
 The size of the entity-body in bytes.
 
NHeaders::ListProxy< false, NHeaders::ContentEncodingEnumContentEncoding ()
 List of encodings (compression) applied to the entity.
 
NHeaders::ListProxy< true, NHeaders::ContentEncodingEnumContentEncoding () const
 List of encodings (compression) applied to the entity.
 
NHeaders::ListProxy< false, NHeaders::TransferEncodingEnumTransferEncoding ()
 List of compression applied to the entity.
 
NHeaders::ListProxy< true, NHeaders::TransferEncodingEnumTransferEncoding () const
 List of compression applied to the entity.
 
NHeaders::ListProxy< false, std::string > ContentLanguage ()
 Natural languages for the intended audience (e.g., "en-US").
 
NHeaders::ListProxy< true, std::string > ContentLanguage () const
 Natural languages for the intended audience (e.g., "en-US").
 
NHeaders::ValueProxy< false, std::string > ContentLocation ()
 The specific location for the entity-body.
 
NHeaders::ValueProxy< true, std::string > ContentLocation () const
 The specific location for the entity-body.
 
NHeaders::ValueProxy< false, std::chrono::utc_clock::time_point > Date ()
 Date and time at which the message was originated.
 
NHeaders::ValueProxy< true, std::chrono::utc_clock::time_point > Date () const
 Date and time at which the message was originated.
 
NHeaders::ListProxy< false, std::string > Connection ()
 Options for the current connection.
 
NHeaders::ListProxy< true, std::string > Connection () const
 Options for the current connection.
 
NHeaders::ListProxy< false, NHeaders::UpgradeUpgrade ()
 Used to signal a protocol change (e.g., "websocket").
 
NHeaders::ListProxy< true, NHeaders::UpgradeUpgrade () const
 Used to signal a protocol change (e.g., "websocket").
 
NHeaders::ListProxy< false, std::string > Trailer ()
 Indicates header fields present in the trailer of a chunked message.
 
NHeaders::ListProxy< true, std::string > Trailer () const
 Indicates header fields present in the trailer of a chunked message.
 
NHeaders::ListProxy< false, std::string > Via ()
 Path taken by the request/response through proxies (free std::string).
 
NHeaders::ListProxy< true, std::string > Via () const
 Path taken by the request/response through proxies (free std::string).
 

Static Public Member Functions

template<std::ranges::input_range R>
static WebResult< HeadersParse (R &headers, size_t maxHeadersLength=1<< 16)
 Tries to parse the headers from the raw TCP std::string.
 
static Headers DefaultHeaders ()
 

Friends

struct std::formatter< Headers >
 

Detailed Description

This class stores the headers from HTTP.

Member Typedef Documentation

◆ CIterType

using Thoth::Http::Headers::CIterType = decltype(MapType{}.cbegin())

◆ CRIterType

using Thoth::Http::Headers::CRIterType = decltype(MapType{}.crbegin())

◆ HeaderKey

◆ HeaderKeyRef

◆ HeaderPair

◆ HeaderPairRef

◆ HeaderValue

◆ HeaderValueRef

◆ IterType

◆ MapType [1/2]

◆ MapType [2/2]

◆ RIterType

Constructor & Destructor Documentation

◆ Headers() [1/3]

Thoth::Http::Headers::Headers ( )

◆ Headers() [2/3]

Thoth::Http::Headers::Headers ( const MapType initAs)
explicit

Create with an existing vector.

◆ Headers() [3/3]

Thoth::Http::Headers::Headers ( std::initializer_list< HeaderPair init)

Member Function Documentation

◆ Accept() [1/2]

NHeaders::ListProxy< false, NHeaders::MimeType > Thoth::Http::Headers::Accept ( )

Accept-Encoding header (gzip, br, etc).

◆ Accept() [2/2]

NHeaders::ListProxy< true, NHeaders::MimeType > Thoth::Http::Headers::Accept ( ) const

◆ AcceptEncoding() [1/2]

NHeaders::ListProxy< false, NHeaders::AcceptEncodingEnum > Thoth::Http::Headers::AcceptEncoding ( )

Accept-Encoding header (gzip, br, etc).

◆ AcceptEncoding() [2/2]

NHeaders::ListProxy< true, NHeaders::AcceptEncodingEnum > Thoth::Http::Headers::AcceptEncoding ( ) const

◆ Add() [1/2]

void Thoth::Http::Headers::Add ( HeaderKeyRef  key,
HeaderValueRef  val 
)

◆ Add() [2/2]

void Thoth::Http::Headers::Add ( HeaderPairRef  p)

Add a value with the specified key. Append if already exists.

Parameters
pA pair with the key and the value to be added.

◆ begin() [1/2]

IterType Thoth::Http::Headers::begin ( )

◆ begin() [2/2]

CIterType Thoth::Http::Headers::begin ( ) const

◆ Clear()

void Thoth::Http::Headers::Clear ( )

Clear all keys.

◆ Connection() [1/2]

NHeaders::ListProxy< false, std::string > Thoth::Http::Headers::Connection ( )

Options for the current connection.

◆ Connection() [2/2]

NHeaders::ListProxy< true, std::string > Thoth::Http::Headers::Connection ( ) const

Options for the current connection.

◆ ContentEncoding() [1/2]

NHeaders::ListProxy< false, NHeaders::ContentEncodingEnum > Thoth::Http::Headers::ContentEncoding ( )

List of encodings (compression) applied to the entity.

◆ ContentEncoding() [2/2]

NHeaders::ListProxy< true, NHeaders::ContentEncodingEnum > Thoth::Http::Headers::ContentEncoding ( ) const

List of encodings (compression) applied to the entity.

◆ ContentLanguage() [1/2]

NHeaders::ListProxy< false, std::string > Thoth::Http::Headers::ContentLanguage ( )

Natural languages for the intended audience (e.g., "en-US").

◆ ContentLanguage() [2/2]

NHeaders::ListProxy< true, std::string > Thoth::Http::Headers::ContentLanguage ( ) const

Natural languages for the intended audience (e.g., "en-US").

◆ ContentLength() [1/2]

NHeaders::ValueProxy< false, uint64_t > Thoth::Http::Headers::ContentLength ( )

The size of the entity-body in bytes.

◆ ContentLength() [2/2]

NHeaders::ValueProxy< true, uint64_t > Thoth::Http::Headers::ContentLength ( ) const

The size of the entity-body in bytes.

◆ ContentLocation() [1/2]

NHeaders::ValueProxy< false, std::string > Thoth::Http::Headers::ContentLocation ( )

The specific location for the entity-body.

◆ ContentLocation() [2/2]

NHeaders::ValueProxy< true, std::string > Thoth::Http::Headers::ContentLocation ( ) const

The specific location for the entity-body.

◆ ContentType() [1/2]

NHeaders::ValueProxy< false, NHeaders::MimeType > Thoth::Http::Headers::ContentType ( )

Defines the media type of the resource (MIME).

◆ ContentType() [2/2]

NHeaders::ValueProxy< true, NHeaders::MimeType > Thoth::Http::Headers::ContentType ( ) const

Defines the media type of the resource (MIME).

◆ Date() [1/2]

NHeaders::ValueProxy< false, std::chrono::utc_clock::time_point > Thoth::Http::Headers::Date ( )

Date and time at which the message was originated.

◆ Date() [2/2]

NHeaders::ValueProxy< true, std::chrono::utc_clock::time_point > Thoth::Http::Headers::Date ( ) const

Date and time at which the message was originated.

◆ DefaultHeaders()

static Headers Thoth::Http::Headers::DefaultHeaders ( )
static

◆ Empty()

bool Thoth::Http::Headers::Empty ( ) const
Returns
True if Size() is 0.

◆ end() [1/2]

IterType Thoth::Http::Headers::end ( )

◆ end() [2/2]

CIterType Thoth::Http::Headers::end ( ) const

◆ Exists() [1/3]

bool Thoth::Http::Headers::Exists ( HeaderKeyRef  key) const

check if a key exists.

Parameters
keyThe key to be checked.
Returns
True if the key exists, false otherwise.

◆ Exists() [2/3]

bool Thoth::Http::Headers::Exists ( HeaderKeyRef  key,
HeaderValueRef  val 
) const

check if a key=val exists.

Parameters
keyThe key to be checked.
valThe value to be checked.
Returns
True if the key-value pair exists, false otherwise.

◆ Exists() [3/3]

bool Thoth::Http::Headers::Exists ( HeaderPairRef  p) const

check if a key exists.

Parameters
pA pair with the key and value to be checked.
Returns
True if the key exists, false otherwise.

◆ Get() [1/2]

std::optional< HeaderValue * > Thoth::Http::Headers::Get ( HeaderKeyRef  key)

Get the reference of a key but don't create if it not exists.

Parameters
keyThe key.
Returns
HeaderValue* if the key exists, std::nullopt otherwise.

◆ Get() [2/2]

std::optional< const HeaderValue * > Thoth::Http::Headers::Get ( HeaderKeyRef  key) const

Get the reference of a key but don't create if it not exists.

Parameters
keyThe key.
Returns
const HeaderValue* if the key exists, std::nullopt otherwise.

◆ operator==()

bool Thoth::Http::Headers::operator== ( const Headers other) const
Returns
True if both headers match.

◆ operator[]()

HeaderValue & Thoth::Http::Headers::operator[] ( HeaderKeyRef  key)
Returns
The HeaderValue& associated with a key. Create if it not exists. STL containers has many problems so it must be HeaderKey.

◆ Parse()

template<std::ranges::input_range R>
static WebResult< Headers > Thoth::Http::Headers::Parse ( R &  headers,
size_t  maxHeadersLength = 1<< 16 
)
static

Tries to parse the headers from the raw TCP std::string.

Parameters
headersthe headers separated by "\r\n".
maxHeadersLengththe max length that the headers can achieve.
Returns
A Headers if the parse success, bad request StatusCodeEnum::BAD_REQUEST if the parse fails and content too large StatusCodeEnum::CONTENT_TOO_LARGE if the header is too long.

◆ rbegin() [1/2]

RIterType Thoth::Http::Headers::rbegin ( )

◆ rbegin() [2/2]

CRIterType Thoth::Http::Headers::rbegin ( ) const

◆ Remove() [1/2]

bool Thoth::Http::Headers::Remove ( HeaderKeyRef  key,
HeaderValueRef  val 
)

◆ Remove() [2/2]

bool Thoth::Http::Headers::Remove ( HeaderPairRef  p)

Remove a value with the specified key.

Parameters
pA pair with the key and the value to be removed.
Returns
True if the key exists, false otherwise.

◆ rend() [1/2]

RIterType Thoth::Http::Headers::rend ( )

◆ rend() [2/2]

CRIterType Thoth::Http::Headers::rend ( ) const

◆ Set() [1/2]

void Thoth::Http::Headers::Set ( HeaderKeyRef  key,
HeaderValueRef  val 
)

◆ Set() [2/2]

void Thoth::Http::Headers::Set ( HeaderPairRef  p)

Add a value with the specified key. Replace if already exists.

Parameters
pA pair with the key and the value to be added.

◆ SetIfNull() [1/2]

bool Thoth::Http::Headers::SetIfNull ( HeaderKeyRef  key,
HeaderValueRef  val 
)

◆ SetIfNull() [2/2]

bool Thoth::Http::Headers::SetIfNull ( HeaderPairRef  p)

If key not exists, set it to value.

Parameters
pA pair with the key and the value to be added.
Returns
True if the key not exists, false otherwise.

◆ Size()

size_t Thoth::Http::Headers::Size ( ) const
Returns
The count of keys.

◆ Trailer() [1/2]

NHeaders::ListProxy< false, std::string > Thoth::Http::Headers::Trailer ( )

Indicates header fields present in the trailer of a chunked message.

◆ Trailer() [2/2]

NHeaders::ListProxy< true, std::string > Thoth::Http::Headers::Trailer ( ) const

Indicates header fields present in the trailer of a chunked message.

◆ TransferEncoding() [1/2]

NHeaders::ListProxy< false, NHeaders::TransferEncodingEnum > Thoth::Http::Headers::TransferEncoding ( )

List of compression applied to the entity.

◆ TransferEncoding() [2/2]

NHeaders::ListProxy< true, NHeaders::TransferEncodingEnum > Thoth::Http::Headers::TransferEncoding ( ) const

List of compression applied to the entity.

◆ Upgrade() [1/2]

NHeaders::ListProxy< false, NHeaders::Upgrade > Thoth::Http::Headers::Upgrade ( )

Used to signal a protocol change (e.g., "websocket").

◆ Upgrade() [2/2]

NHeaders::ListProxy< true, NHeaders::Upgrade > Thoth::Http::Headers::Upgrade ( ) const

Used to signal a protocol change (e.g., "websocket").

◆ Via() [1/2]

NHeaders::ListProxy< false, std::string > Thoth::Http::Headers::Via ( )

Path taken by the request/response through proxies (free std::string).

◆ Via() [2/2]

NHeaders::ListProxy< true, std::string > Thoth::Http::Headers::Via ( ) const

Path taken by the request/response through proxies (free std::string).

Friends And Related Symbol Documentation

◆ std::formatter< Headers >

friend struct std::formatter< Headers >
friend

The documentation for this struct was generated from the following file: