Thoth
A functional, expressive, asynchronous C++26 webdev library
Theme:
Default
Round
Robot
Loading...
Searching...
No Matches
Headers.hpp
Go to the documentation of this file.
1
#pragma once
2
#include <chrono>
3
#include <
Thoth/Http/Response/StatusCodeEnum.hpp
>
4
#include <optional>
5
#include <format>
6
#include <vector>
7
#include <ranges>
8
9
#include <
Thoth/Http/Request/Url.hpp
>
10
#include <
Thoth/Http/NHeaders/_base.hpp
>
11
12
#include <
Thoth/Http/NHeaders/Headers/_pch.hpp
>
13
#include <
Thoth/Http/NHeaders/Proxy/_base.hpp
>
14
15
namespace
Thoth::Http::NHeaders
{
16
template
<
bool
IsConst, Serializable ...T>
17
struct
ListProxy;
18
template
<
bool
IsConst, Serializable ...T>
19
struct
ValueProxy;
20
}
21
22
23
namespace
Thoth::Http
{
24
25
enum class
VersionEnum
: uint8_t;
26
27
29
struct
Headers
{
30
using
HeaderKey
=
NHeaders::HeaderKey
;
31
using
HeaderKeyRef
=
NHeaders::HeaderKeyRef
;
32
33
using
HeaderValue
=
NHeaders::HeaderValue
;
34
using
HeaderValueRef
=
NHeaders::HeaderValueRef
;
35
36
using
HeaderPair
=
NHeaders::HeaderPair
;
37
using
HeaderPairRef
=
NHeaders::HeaderPairRef
;
38
using
MapType
=
NHeaders::MapType
;
39
40
using
MapType
= std::vector<HeaderPair>;
41
42
using
IterType
=
decltype
(
MapType
{}.begin());
43
using
CIterType
=
decltype
(
MapType
{}.cbegin());
44
using
RIterType
=
decltype
(
MapType
{}.rbegin());
45
using
CRIterType
=
decltype
(
MapType
{}.crbegin());
46
47
48
Headers
();
49
51
explicit
Headers
(
const
MapType
& initAs);
52
53
Headers
(std::initializer_list<HeaderPair> init);
54
55
61
template
<std::ranges::input_range R>
62
static
WebResult<Headers>
Parse
(R& headers,
size_t
maxHeadersLength = 1<<16);
63
64
65
static
Headers
DefaultHeaders
();
66
67
71
[[nodiscard]]
bool
Exists
(
HeaderKeyRef
key)
const
;
72
76
[[nodiscard]]
bool
Exists
(
HeaderPairRef
p)
const
;
77
82
[[nodiscard]]
bool
Exists
(
HeaderKeyRef
key,
HeaderValueRef
val)
const
;
83
86
void
Add
(
HeaderPairRef
p);
87
89
void
Add
(
HeaderKeyRef
key,
HeaderValueRef
val);
90
91
94
void
Set
(
HeaderPairRef
p);
95
97
void
Set
(
HeaderKeyRef
key,
HeaderValueRef
val);
98
102
bool
Remove
(
HeaderPairRef
p);
103
105
bool
Remove
(
HeaderKeyRef
key,
HeaderValueRef
val);
106
110
bool
SetIfNull
(
HeaderPairRef
p);
111
112
114
bool
SetIfNull
(
HeaderKeyRef
key,
HeaderValueRef
val);
115
119
std::optional<HeaderValue*>
Get
(
HeaderKeyRef
key);
120
124
[[nodiscard]] std::optional<const HeaderValue*>
Get
(
HeaderKeyRef
key)
const
;
125
126
130
132
NHeaders::ListProxy<false, NHeaders::MimeType>
Accept
();
133
NHeaders::ListProxy<true, NHeaders::MimeType>
Accept
()
const
;
134
136
NHeaders::ListProxy<false, NHeaders::AcceptEncodingEnum>
AcceptEncoding
();
137
NHeaders::ListProxy<true, NHeaders::AcceptEncodingEnum>
AcceptEncoding
()
const
;
138
139
141
NHeaders::ValueProxy<false, NHeaders::MimeType>
ContentType
();
143
[[nodiscard]]
NHeaders::ValueProxy<true, NHeaders::MimeType>
ContentType
()
const
;
144
146
NHeaders::ValueProxy<false, uint64_t>
ContentLength
();
148
[[nodiscard]]
NHeaders::ValueProxy<true, uint64_t>
ContentLength
()
const
;
149
151
NHeaders::ListProxy<false, NHeaders::ContentEncodingEnum>
ContentEncoding
();
153
[[nodiscard]]
NHeaders::ListProxy<true, NHeaders::ContentEncodingEnum>
ContentEncoding
()
const
;
154
156
NHeaders::ListProxy<false, NHeaders::TransferEncodingEnum>
TransferEncoding
();
158
[[nodiscard]]
NHeaders::ListProxy<true, NHeaders::TransferEncodingEnum>
TransferEncoding
()
const
;
159
161
NHeaders::ListProxy<false, std::string>
ContentLanguage
();
163
[[nodiscard]]
NHeaders::ListProxy<true, std::string>
ContentLanguage
()
const
;
164
166
NHeaders::ValueProxy<false, std::string>
ContentLocation
();
168
[[nodiscard]]
NHeaders::ValueProxy<true, std::string>
ContentLocation
()
const
;
169
171
NHeaders::ValueProxy<false, std::chrono::utc_clock::time_point>
Date
();
173
[[nodiscard]]
NHeaders::ValueProxy<true, std::chrono::utc_clock::time_point>
Date
()
const
;
174
176
NHeaders::ListProxy<false, std::string>
Connection
();
178
[[nodiscard]]
NHeaders::ListProxy<true, std::string>
Connection
()
const
;
179
181
NHeaders::ListProxy<false, NHeaders::Upgrade>
Upgrade
();
183
[[nodiscard]]
NHeaders::ListProxy<true, NHeaders::Upgrade>
Upgrade
()
const
;
184
186
NHeaders::ListProxy<false, std::string>
Trailer
();
188
[[nodiscard]]
NHeaders::ListProxy<true, std::string>
Trailer
()
const
;
189
191
NHeaders::ListProxy<false, std::string>
Via
();
193
[[nodiscard]]
NHeaders::ListProxy<true, std::string>
Via
()
const
;
194
196
197
198
199
IterType
begin
();
200
IterType
end
();
201
[[nodiscard]]
CIterType
begin
()
const
;
202
[[nodiscard]]
CIterType
end
()
const
;
203
204
RIterType
rbegin
();
205
RIterType
rend
();
206
[[nodiscard]]
CRIterType
rbegin
()
const
;
207
[[nodiscard]]
CRIterType
rend
()
const
;
208
209
210
212
void
Clear
();
213
215
[[nodiscard]]
size_t
Size
()
const
;
216
218
[[nodiscard]]
bool
Empty
()
const
;
219
220
221
224
HeaderValue
&
operator[]
(
HeaderKeyRef
key);
225
227
bool
operator==
(
const
Headers
& other)
const
;
228
private
:
229
MapType
_headers;
230
231
friend
struct
std::formatter<
Headers
>;
232
};
233
}
234
235
#include <Thoth/Http/NHeaders/Headers.tpp>
_pch.hpp
_base.hpp
_base.hpp
StatusCodeEnum.hpp
Url.hpp
Thoth::Http::NHeaders
Definition
_base.hpp:5
Thoth::Http::NHeaders::HeaderKeyRef
std::string_view HeaderKeyRef
Definition
_base.hpp:7
Thoth::Http::NHeaders::HeaderPairRef
std::pair< HeaderKeyRef, HeaderValueRef > HeaderPairRef
Definition
_base.hpp:15
Thoth::Http::NHeaders::HeaderKey
std::string HeaderKey
Definition
_base.hpp:6
Thoth::Http::NHeaders::MapType
std::vector< HeaderPair > MapType
Definition
_base.hpp:16
Thoth::Http::NHeaders::HeaderValue
std::string HeaderValue
Definition
_base.hpp:9
Thoth::Http::NHeaders::HeaderPair
std::pair< HeaderKey, HeaderValue > HeaderPair
Definition
_base.hpp:14
Thoth::Http::NHeaders::HeaderValueRef
std::string_view HeaderValueRef
Definition
_base.hpp:10
Thoth::Http
Definition
Client.hpp:12
Thoth::Http::WebResult
std::expected< T, StatusCodeEnum > WebResult
Definition
StatusCodeEnum.hpp:108
Thoth::Http::VersionEnum
VersionEnum
Definition
Request.hpp:11
Thoth::Http::Headers
This class stores the headers from HTTP.
Definition
Headers.hpp:29
Thoth::Http::Headers::Empty
bool Empty() const
Thoth::Http::Headers::Upgrade
NHeaders::ListProxy< false, NHeaders::Upgrade > Upgrade()
Used to signal a protocol change (e.g., "websocket").
Thoth::Http::Headers::Date
NHeaders::ValueProxy< false, std::chrono::utc_clock::time_point > Date()
Date and time at which the message was originated.
Thoth::Http::Headers::ContentLength
NHeaders::ValueProxy< false, uint64_t > ContentLength()
The size of the entity-body in bytes.
Thoth::Http::Headers::TransferEncoding
NHeaders::ListProxy< true, NHeaders::TransferEncodingEnum > TransferEncoding() const
List of compression applied to the entity.
Thoth::Http::Headers::Exists
bool Exists(HeaderKeyRef key, HeaderValueRef val) const
check if a key=val exists.
Thoth::Http::Headers::ContentEncoding
NHeaders::ListProxy< true, NHeaders::ContentEncodingEnum > ContentEncoding() const
List of encodings (compression) applied to the entity.
Thoth::Http::Headers::Accept
NHeaders::ListProxy< false, NHeaders::MimeType > Accept()
Accept-Encoding header (gzip, br, etc).
Thoth::Http::Headers::SetIfNull
bool SetIfNull(HeaderKeyRef key, HeaderValueRef val)
same as SetIfNull(HeaderPairRef p).
Thoth::Http::Headers::Headers
Headers()
Thoth::Http::Headers::HeaderKey
NHeaders::HeaderKey HeaderKey
Definition
Headers.hpp:30
Thoth::Http::Headers::Headers
Headers(const MapType &initAs)
Create with an existing vector.
Thoth::Http::Headers::Via
NHeaders::ListProxy< true, std::string > Via() const
Path taken by the request/response through proxies (free std::string).
Thoth::Http::Headers::Clear
void Clear()
Clear all keys.
Thoth::Http::Headers::Get
std::optional< HeaderValue * > Get(HeaderKeyRef key)
Get the reference of a key but don't create if it not exists.
Thoth::Http::Headers::HeaderKeyRef
NHeaders::HeaderKeyRef HeaderKeyRef
Definition
Headers.hpp:31
Thoth::Http::Headers::RIterType
decltype(MapType{}.rbegin()) RIterType
Definition
Headers.hpp:44
Thoth::Http::Headers::Date
NHeaders::ValueProxy< true, std::chrono::utc_clock::time_point > Date() const
Date and time at which the message was originated.
Thoth::Http::Headers::DefaultHeaders
static Headers DefaultHeaders()
Thoth::Http::Headers::Parse
static WebResult< Headers > Parse(R &headers, size_t maxHeadersLength=1<< 16)
Tries to parse the headers from the raw TCP std::string.
Thoth::Http::Headers::ContentLanguage
NHeaders::ListProxy< true, std::string > ContentLanguage() const
Natural languages for the intended audience (e.g., "en-US").
Thoth::Http::Headers::Remove
bool Remove(HeaderKeyRef key, HeaderValueRef val)
same as Remove(HeaderPairRef p).
Thoth::Http::Headers::begin
CIterType begin() const
Thoth::Http::Headers::HeaderPairRef
NHeaders::HeaderPairRef HeaderPairRef
Definition
Headers.hpp:37
Thoth::Http::Headers::rbegin
RIterType rbegin()
Thoth::Http::Headers::SetIfNull
bool SetIfNull(HeaderPairRef p)
If key not exists, set it to value.
Thoth::Http::Headers::ContentLanguage
NHeaders::ListProxy< false, std::string > ContentLanguage()
Natural languages for the intended audience (e.g., "en-US").
Thoth::Http::Headers::IterType
decltype(MapType{}.begin()) IterType
Definition
Headers.hpp:42
Thoth::Http::Headers::ContentLocation
NHeaders::ValueProxy< true, std::string > ContentLocation() const
The specific location for the entity-body.
Thoth::Http::Headers::ContentLength
NHeaders::ValueProxy< true, uint64_t > ContentLength() const
The size of the entity-body in bytes.
Thoth::Http::Headers::Remove
bool Remove(HeaderPairRef p)
Remove a value with the specified key.
Thoth::Http::Headers::TransferEncoding
NHeaders::ListProxy< false, NHeaders::TransferEncodingEnum > TransferEncoding()
List of compression applied to the entity.
Thoth::Http::Headers::end
CIterType end() const
Thoth::Http::Headers::end
IterType end()
Thoth::Http::Headers::CIterType
decltype(MapType{}.cbegin()) CIterType
Definition
Headers.hpp:43
Thoth::Http::Headers::AcceptEncoding
NHeaders::ListProxy< true, NHeaders::AcceptEncodingEnum > AcceptEncoding() const
Thoth::Http::Headers::Exists
bool Exists(HeaderKeyRef key) const
check if a key exists.
Thoth::Http::Headers::HeaderValueRef
NHeaders::HeaderValueRef HeaderValueRef
Definition
Headers.hpp:34
Thoth::Http::Headers::HeaderPair
NHeaders::HeaderPair HeaderPair
Definition
Headers.hpp:36
Thoth::Http::Headers::ContentLocation
NHeaders::ValueProxy< false, std::string > ContentLocation()
The specific location for the entity-body.
Thoth::Http::Headers::Upgrade
NHeaders::ListProxy< true, NHeaders::Upgrade > Upgrade() const
Used to signal a protocol change (e.g., "websocket").
Thoth::Http::Headers::rbegin
CRIterType rbegin() const
Thoth::Http::Headers::Accept
NHeaders::ListProxy< true, NHeaders::MimeType > Accept() const
Thoth::Http::Headers::Connection
NHeaders::ListProxy< false, std::string > Connection()
Options for the current connection.
Thoth::Http::Headers::ContentType
NHeaders::ValueProxy< true, NHeaders::MimeType > ContentType() const
Defines the media type of the resource (MIME).
Thoth::Http::Headers::begin
IterType begin()
Thoth::Http::Headers::Get
std::optional< const HeaderValue * > Get(HeaderKeyRef key) const
Get the reference of a key but don't create if it not exists.
Thoth::Http::Headers::Trailer
NHeaders::ListProxy< false, std::string > Trailer()
Indicates header fields present in the trailer of a chunked message.
Thoth::Http::Headers::AcceptEncoding
NHeaders::ListProxy< false, NHeaders::AcceptEncodingEnum > AcceptEncoding()
Accept-Encoding header (gzip, br, etc).
Thoth::Http::Headers::Set
void Set(HeaderKeyRef key, HeaderValueRef val)
same as Add(HeaderPairRef p).
Thoth::Http::Headers::Add
void Add(HeaderPairRef p)
Add a value with the specified key. Append if already exists.
Thoth::Http::Headers::MapType
NHeaders::MapType MapType
Definition
Headers.hpp:38
Thoth::Http::Headers::operator==
bool operator==(const Headers &other) const
Thoth::Http::Headers::Size
size_t Size() const
Thoth::Http::Headers::CRIterType
decltype(MapType{}.crbegin()) CRIterType
Definition
Headers.hpp:45
Thoth::Http::Headers::operator[]
HeaderValue & operator[](HeaderKeyRef key)
Thoth::Http::Headers::Connection
NHeaders::ListProxy< true, std::string > Connection() const
Options for the current connection.
Thoth::Http::Headers::ContentType
NHeaders::ValueProxy< false, NHeaders::MimeType > ContentType()
Defines the media type of the resource (MIME).
Thoth::Http::Headers::Via
NHeaders::ListProxy< false, std::string > Via()
Path taken by the request/response through proxies (free std::string).
Thoth::Http::Headers::Trailer
NHeaders::ListProxy< true, std::string > Trailer() const
Indicates header fields present in the trailer of a chunked message.
Thoth::Http::Headers::Add
void Add(HeaderKeyRef key, HeaderValueRef val)
same as Add(HeaderPairRef p).
Thoth::Http::Headers::Headers
Headers(std::initializer_list< HeaderPair > init)
Thoth::Http::Headers::Exists
bool Exists(HeaderPairRef p) const
check if a key exists.
Thoth::Http::Headers::ContentEncoding
NHeaders::ListProxy< false, NHeaders::ContentEncodingEnum > ContentEncoding()
List of encodings (compression) applied to the entity.
Thoth::Http::Headers::rend
RIterType rend()
Thoth::Http::Headers::Set
void Set(HeaderPairRef p)
Add a value with the specified key. Replace if already exists.
Thoth::Http::Headers::HeaderValue
NHeaders::HeaderValue HeaderValue
Definition
Headers.hpp:33
Thoth::Http::Headers::rend
CRIterType rend() const
Thoth::Http::NHeaders::ListProxy
Definition
ListProxy.hpp:14
Thoth::Http::NHeaders::ValueProxy
Definition
ValueProxy.hpp:10
include
Thoth
Http
NHeaders
Headers.hpp
Generated by
1.9.8