userver: http::headers::HeaderMap Class Reference
Loading...
Searching...
No Matches
http::headers::HeaderMap Class Referencefinal

#include <userver/http/header_map.hpp>

Detailed Description

Container that maps case-insensitive header name into header value.

Allows storing up to 24576 name->value pairs, after that an attempt to insert a new pair will throw.

Has an anti-hashdos collisions resolution built-in, and the capacity limit might be lowered in case of an attack being detected.

Iterators/pointers invalidation loosely matches that of std::vector:

  • if an insertion took place, iterators/pointers are invalidated;
  • successful erase invalidates the iterator being erased and begin (it's implemented via swap and pop_back idiom, and begin is actually an rbegin of underlying vector).

Definition at line 38 of file header_map.hpp.

Classes

class  ConstIterator
 
class  Iterator
 
class  TooManyHeadersException
 The exception being thrown in case of capacity overflow. More...
 

Public Types

using iterator = Iterator
 
using const_iterator = ConstIterator
 
using key_type = std::string
 
using mapped_type = std::string
 

Public Member Functions

 HeaderMap ()
 Default constructor.
 
 HeaderMap (std::initializer_list< std::pair< std::string, std::string > > headers)
 
 HeaderMap (std::initializer_list< std::pair< PredefinedHeader, std::string > > headers)
 
 HeaderMap (std::size_t capacity)
 
template<typename InputIt >
 HeaderMap (InputIt first, InputIt last)
 
 ~HeaderMap ()
 Destructor.
 
 HeaderMap (const HeaderMap &other)
 Copy constructor.
 
 HeaderMap (HeaderMap &&other) noexcept
 Move constructor.
 
HeaderMapoperator= (const HeaderMap &other)
 Copy assignment operator.
 
HeaderMapoperator= (HeaderMap &&other) noexcept
 Move assignment operator.
 
void reserve (std::size_t capacity)
 Non-binding call to reserve capacity elements for internal storage.
 
std::size_t size () const noexcept
 Returns the amount of name-value-pairs being stored.
 
bool empty () const noexcept
 Return true if no name-value-pairs are being stored, false otherwise.
 
void clear ()
 
std::size_t count (std::string_view key) const noexcept
 Returns 1 if the key exists, 0 otherwise.
 
std::size_t count (const PredefinedHeader &key) const noexcept
 This is an overloaded member function, provided for convenience. It differs from the above function only in what argument(s) it accepts.
 
template<std::size_t Size>
std::size_t count (const char(&)[Size]) const noexcept
 
bool contains (std::string_view key) const noexcept
 Returns true if the key exists, false otherwise.
 
bool contains (const PredefinedHeader &key) const noexcept
 This is an overloaded member function, provided for convenience. It differs from the above function only in what argument(s) it accepts.
 
template<std::size_t Size>
bool contains (const char(&)[Size]) const noexcept
 
std::string & operator[] (std::string &&key)
 
std::string & operator[] (std::string_view key)
 
std::string & operator[] (const PredefinedHeader &key)
 This is an overloaded member function, provided for convenience. It differs from the above function only in what argument(s) it accepts.
 
template<std::size_t Size>
std::string & operator[] (const char(&)[Size])
 
Iterator find (std::string_view key) noexcept
 
ConstIterator find (std::string_view key) const noexcept
 This is an overloaded member function, provided for convenience. It differs from the above function only in what argument(s) it accepts.
 
Iterator find (const PredefinedHeader &key) noexcept
 
ConstIterator find (const PredefinedHeader &key) const noexcept
 This is an overloaded member function, provided for convenience. It differs from the above function only in what argument(s) it accepts.
 
template<std::size_t Size>
Iterator find (const char(&)[Size]) noexcept
 
template<std::size_t Size>
ConstIterator find (const char(&)[Size]) const noexcept
 
template<typename... Args>
void emplace (std::string_view key, Args &&... args)
 
template<typename... Args>
void emplace (std::string key, Args &&... args)
 
template<typename... Args>
void try_emplace (std::string key, Args &&... args)
 
template<typename InputIt >
void insert (InputIt first, InputIt last)
 
void insert (const std::pair< std::string, std::string > &kvp)
 
void insert (std::pair< std::string, std::string > &&kvp)
 
void insert_or_assign (std::string key, std::string value)
 
void insert_or_assign (const PredefinedHeader &key, std::string value)
 This is an overloaded member function, provided for convenience. It differs from the above function only in what argument(s) it accepts.
 
void InsertOrAppend (std::string key, std::string value)
 
void InsertOrAppend (const PredefinedHeader &key, std::string value)
 This is an overloaded member function, provided for convenience. It differs from the above function only in what argument(s) it accepts.
 
Iterator erase (Iterator it)
 
Iterator erase (ConstIterator it)
 
Iterator erase (std::string_view key)
 
Iterator erase (const PredefinedHeader &key)
 This is an overloaded member function, provided for convenience. It differs from the above function only in what argument(s) it accepts.
 
template<std::size_t Size>
Iterator erase (const char(&)[Size])
 
std::string & at (std::string_view key)
 
std::string & at (const PredefinedHeader &key)
 This is an overloaded member function, provided for convenience. It differs from the above function only in what argument(s) it accepts.
 
const std::string & at (std::string_view key) const
 
const std::string & at (const PredefinedHeader &key) const
 This is an overloaded member function, provided for convenience. It differs from the above function only in what argument(s) it accepts.
 
template<std::size_t Size>
std::string & at (const char(&)[Size])
 
template<std::size_t Size>
const std::string & at (const char(&)[Size]) const
 
Iterator begin () noexcept
 Returns an iterator to the first name-value-pair being stored.
 
ConstIterator begin () const noexcept
 Returns an iterator to the first name-value-pair being stored.
 
ConstIterator cbegin () const noexcept
 Returns an iterator to the first name-value-pair being stored.
 
Iterator end () noexcept
 Returns an iterator to the end (valid but not dereferenceable).
 
ConstIterator end () const noexcept
 Returns an iterator to the end (valid but not dereferenceable).
 
ConstIterator cend () const noexcept
 Returns an iterator to the end (valid but not dereferenceable).
 
bool operator== (const HeaderMap &other) const noexcept
 
void OutputInHttpFormat (HeadersString &buffer) const
 

Member Typedef Documentation

◆ const_iterator

◆ iterator

Definition at line 45 of file header_map.hpp.

◆ key_type

using http::headers::HeaderMap::key_type = std::string

Definition at line 48 of file header_map.hpp.

◆ mapped_type

using http::headers::HeaderMap::mapped_type = std::string

Definition at line 49 of file header_map.hpp.

Constructor & Destructor Documentation

◆ HeaderMap() [1/4]

http::headers::HeaderMap::HeaderMap ( std::initializer_list< std::pair< std::string, std::string > > headers)

Constructor from initializer list: HeaderMap({{"a", "b"}, {"c", "d"}}). Its unspecified which pair is inserted in case of names not being unique.

◆ HeaderMap() [2/4]

http::headers::HeaderMap::HeaderMap ( std::initializer_list< std::pair< PredefinedHeader, std::string > > headers)

Constructor from initializer list: HeaderMap({{"a", "b"}, {"c", "d"}}). Its unspecified which pair is inserted in case of names not being unique.

◆ HeaderMap() [3/4]

http::headers::HeaderMap::HeaderMap ( std::size_t capacity)

Constructor with capacity: preallocates capacity elements for internal storage.

◆ HeaderMap() [4/4]

template<typename InputIt >
http::headers::HeaderMap::HeaderMap ( InputIt first,
InputIt last )

Constructor from iterator pair: HeaderMap{key_value_pairs.begin(), key_value_pairs.end()}. Its unspecified which pair is inserted in case of names not being unique.

Definition at line 276 of file header_map.hpp.

Member Function Documentation

◆ at() [1/4]

template<std::size_t Size>
std::string & http::headers::HeaderMap::at ( const char(&)[Size])
inline

Definition at line 227 of file header_map.hpp.

◆ at() [2/4]

template<std::size_t Size>
const std::string & http::headers::HeaderMap::at ( const char(&)[Size]) const
inline

Definition at line 231 of file header_map.hpp.

◆ at() [3/4]

std::string & http::headers::HeaderMap::at ( std::string_view key)

If the key is present in container, returns reference to its header value, otherwise throws std::out_of_range.

◆ at() [4/4]

const std::string & http::headers::HeaderMap::at ( std::string_view key) const

If the key is present in container, returns reference to its header value, otherwise throws std::out_of_range.

◆ clear()

void http::headers::HeaderMap::clear ( )

Removes all the key-value-pairs being stored, doesn't shrink underlying storage.

◆ contains()

template<std::size_t Size>
bool http::headers::HeaderMap::contains ( const char(&)[Size]) const
inlinenoexcept

Definition at line 112 of file header_map.hpp.

◆ count()

template<std::size_t Size>
std::size_t http::headers::HeaderMap::count ( const char(&)[Size]) const
inlinenoexcept

Definition at line 102 of file header_map.hpp.

◆ emplace() [1/2]

template<typename... Args>
void http::headers::HeaderMap::emplace ( std::string key,
Args &&... args )
inline

If the key is already present in the map, does nothing. Otherwise inserts a pair of key and inplace constructed value.

Definition at line 162 of file header_map.hpp.

◆ emplace() [2/2]

template<typename... Args>
void http::headers::HeaderMap::emplace ( std::string_view key,
Args &&... args )
inline

If the key is already present in the map, does nothing. Otherwise inserts a pair of key and in-place constructed value.

Definition at line 155 of file header_map.hpp.

◆ erase() [1/4]

template<std::size_t Size>
HeaderMap::Iterator http::headers::HeaderMap::erase ( const char(&)[Size])

Definition at line 446 of file header_map.hpp.

◆ erase() [2/4]

Iterator http::headers::HeaderMap::erase ( ConstIterator it)

Erases the pair to which the iterator points, returns iterator following the last removed element. Iterators/pointers to erased value and begin are invalidated.

◆ erase() [3/4]

Iterator http::headers::HeaderMap::erase ( Iterator it)

Erases the pair to which the iterator points, returns iterator following the last removed element. Iterators/pointers to erased value and begin are invalidated.

◆ erase() [4/4]

Iterator http::headers::HeaderMap::erase ( std::string_view key)

If the key is present in container, behaves exactly like erase(Iterator). Otherwise does nothing and returns end().

◆ find() [1/4]

template<std::size_t Size>
HeaderMap::ConstIterator http::headers::HeaderMap::find ( const char(&)[Size]) const
noexcept

Definition at line 441 of file header_map.hpp.

◆ find() [2/4]

template<std::size_t Size>
HeaderMap::Iterator http::headers::HeaderMap::find ( const char(&)[Size])
noexcept

Definition at line 436 of file header_map.hpp.

◆ find() [3/4]

Iterator http::headers::HeaderMap::find ( const PredefinedHeader & key)
noexcept

If the key is present, returns iterator to its name-value pair, otherwise return end().

◆ find() [4/4]

Iterator http::headers::HeaderMap::find ( std::string_view key)
noexcept

If the key is present, returns iterator to its name-value pair, otherwise return end().

◆ insert() [1/3]

void http::headers::HeaderMap::insert ( const std::pair< std::string, std::string > & kvp)

If kvp.first is already present in the map, does nothing, otherwise inserts the pair into container.

◆ insert() [2/3]

template<typename InputIt >
void http::headers::HeaderMap::insert ( InputIt first,
InputIt last )

For every iterator it in [first, last) inserts *it. Its unspecified which pair is inserted in case of names not being unique.

Definition at line 281 of file header_map.hpp.

◆ insert() [3/3]

void http::headers::HeaderMap::insert ( std::pair< std::string, std::string > && kvp)

If kvp.first is already present in the map, does nothing, otherwise inserts the pair into container.

◆ insert_or_assign()

void http::headers::HeaderMap::insert_or_assign ( std::string key,
std::string value )

If key is already present in the map, changes corresponding header value to provided value, otherwise inserts the pair into container.

◆ InsertOrAppend()

void http::headers::HeaderMap::InsertOrAppend ( std::string key,
std::string value )

If key is already present in the map, appends ",{value}" to the corresponding header value, otherwise inserts the pair into container.

◆ operator==()

bool http::headers::HeaderMap::operator== ( const HeaderMap & other) const
noexcept

Returns true if other contains exactly the same set of name-value-pairs, false otherwise.

◆ operator[]() [1/3]

template<std::size_t Size>
std::string & http::headers::HeaderMap::operator[] ( const char(&)[Size])
inline

Definition at line 130 of file header_map.hpp.

◆ operator[]() [2/3]

std::string & http::headers::HeaderMap::operator[] ( std::string && key)

If the key is present, returns reference to it's header value, otherwise inserts a pair (key, "") and returns reference to newly inserted empty string. In an insertion took place, key is moved-out, otherwise key is left unchanged.

◆ operator[]() [3/3]

std::string & http::headers::HeaderMap::operator[] ( std::string_view key)

If the key is present, returns reference to it's header value, otherwise inserts a pair (key, "") and returns reference to newly inserted empty string.

◆ OutputInHttpFormat()

void http::headers::HeaderMap::OutputInHttpFormat ( HeadersString & buffer) const

Appends container content in http headers format to provided buffer, that is appends

header1: value1\r\n
header2: value2\r\n
...

resizing buffer as needed.

◆ try_emplace()

template<typename... Args>
void http::headers::HeaderMap::try_emplace ( std::string key,
Args &&... args )
inline

If the key is already present in the map, does nothing. Otherwise inserts a pair of key and inplace constructed value.

Definition at line 169 of file header_map.hpp.

Friends And Related Symbol Documentation

◆ TestsHelper

friend class TestsHelper
friend

Definition at line 264 of file header_map.hpp.


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