Thoth
A functional, expressive, asynchronous C++26 webdev library
Loading...
Searching...
No Matches
Headers.hpp
Go to the documentation of this file.
1#pragma once
2#include <chrono>
4#include <optional>
5#include <format>
6#include <vector>
7#include <ranges>
8
11
14
15namespace Thoth::Http::NHeaders {
16 template<bool IsConst, Serializable ...T>
17 struct ListProxy;
18 template<bool IsConst, Serializable ...T>
19 struct ValueProxy;
20}
21
22
23namespace Thoth::Http {
24
25 enum class VersionEnum : uint8_t;
26
27
29 struct Headers {
32
35
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
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
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
87
90
91
95
98
103
106
111
112
115
119 std::optional<HeaderValue*> Get(HeaderKeyRef key);
120
124 [[nodiscard]] std::optional<const HeaderValue*> Get(HeaderKeyRef key) const;
125
126
130
134
138
139
144
149
154
159
164
169
174
179
184
189
194
196
197
198
201 [[nodiscard]] CIterType begin() const;
202 [[nodiscard]] CIterType end() const;
203
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
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>
Definition _base.hpp:5
std::string_view HeaderKeyRef
Definition _base.hpp:7
std::pair< HeaderKeyRef, HeaderValueRef > HeaderPairRef
Definition _base.hpp:15
std::string HeaderKey
Definition _base.hpp:6
std::vector< HeaderPair > MapType
Definition _base.hpp:16
std::string HeaderValue
Definition _base.hpp:9
std::pair< HeaderKey, HeaderValue > HeaderPair
Definition _base.hpp:14
std::string_view HeaderValueRef
Definition _base.hpp:10
Definition Client.hpp:12
std::expected< T, StatusCodeEnum > WebResult
Definition StatusCodeEnum.hpp:108
VersionEnum
Definition Request.hpp:11
This class stores the headers from HTTP.
Definition Headers.hpp:29
bool Empty() const
NHeaders::ListProxy< false, NHeaders::Upgrade > Upgrade()
Used to signal a protocol change (e.g., "websocket").
NHeaders::ValueProxy< false, std::chrono::utc_clock::time_point > Date()
Date and time at which the message was originated.
NHeaders::ValueProxy< false, uint64_t > ContentLength()
The size of the entity-body in bytes.
NHeaders::ListProxy< true, NHeaders::TransferEncodingEnum > TransferEncoding() const
List of compression applied to the entity.
bool Exists(HeaderKeyRef key, HeaderValueRef val) const
check if a key=val exists.
NHeaders::ListProxy< true, NHeaders::ContentEncodingEnum > ContentEncoding() const
List of encodings (compression) applied to the entity.
NHeaders::ListProxy< false, NHeaders::MimeType > Accept()
Accept-Encoding header (gzip, br, etc).
bool SetIfNull(HeaderKeyRef key, HeaderValueRef val)
same as SetIfNull(HeaderPairRef p).
NHeaders::HeaderKey HeaderKey
Definition Headers.hpp:30
Headers(const MapType &initAs)
Create with an existing vector.
NHeaders::ListProxy< true, std::string > Via() const
Path taken by the request/response through proxies (free std::string).
void Clear()
Clear all keys.
std::optional< HeaderValue * > Get(HeaderKeyRef key)
Get the reference of a key but don't create if it not exists.
NHeaders::HeaderKeyRef HeaderKeyRef
Definition Headers.hpp:31
decltype(MapType{}.rbegin()) RIterType
Definition Headers.hpp:44
NHeaders::ValueProxy< true, std::chrono::utc_clock::time_point > Date() const
Date and time at which the message was originated.
static Headers DefaultHeaders()
static WebResult< Headers > Parse(R &headers, size_t maxHeadersLength=1<< 16)
Tries to parse the headers from the raw TCP std::string.
NHeaders::ListProxy< true, std::string > ContentLanguage() const
Natural languages for the intended audience (e.g., "en-US").
bool Remove(HeaderKeyRef key, HeaderValueRef val)
same as Remove(HeaderPairRef p).
CIterType begin() const
NHeaders::HeaderPairRef HeaderPairRef
Definition Headers.hpp:37
bool SetIfNull(HeaderPairRef p)
If key not exists, set it to value.
NHeaders::ListProxy< false, std::string > ContentLanguage()
Natural languages for the intended audience (e.g., "en-US").
decltype(MapType{}.begin()) IterType
Definition Headers.hpp:42
NHeaders::ValueProxy< true, std::string > ContentLocation() const
The specific location for the entity-body.
NHeaders::ValueProxy< true, uint64_t > ContentLength() const
The size of the entity-body in bytes.
bool Remove(HeaderPairRef p)
Remove a value with the specified key.
NHeaders::ListProxy< false, NHeaders::TransferEncodingEnum > TransferEncoding()
List of compression applied to the entity.
CIterType end() const
decltype(MapType{}.cbegin()) CIterType
Definition Headers.hpp:43
NHeaders::ListProxy< true, NHeaders::AcceptEncodingEnum > AcceptEncoding() const
bool Exists(HeaderKeyRef key) const
check if a key exists.
NHeaders::HeaderValueRef HeaderValueRef
Definition Headers.hpp:34
NHeaders::HeaderPair HeaderPair
Definition Headers.hpp:36
NHeaders::ValueProxy< false, std::string > ContentLocation()
The specific location for the entity-body.
NHeaders::ListProxy< true, NHeaders::Upgrade > Upgrade() const
Used to signal a protocol change (e.g., "websocket").
CRIterType rbegin() const
NHeaders::ListProxy< true, NHeaders::MimeType > Accept() const
NHeaders::ListProxy< false, std::string > Connection()
Options for the current connection.
NHeaders::ValueProxy< true, NHeaders::MimeType > ContentType() const
Defines the media type of the resource (MIME).
std::optional< const HeaderValue * > Get(HeaderKeyRef key) const
Get the reference of a key but don't create if it not exists.
NHeaders::ListProxy< false, std::string > Trailer()
Indicates header fields present in the trailer of a chunked message.
NHeaders::ListProxy< false, NHeaders::AcceptEncodingEnum > AcceptEncoding()
Accept-Encoding header (gzip, br, etc).
void Set(HeaderKeyRef key, HeaderValueRef val)
same as Add(HeaderPairRef p).
void Add(HeaderPairRef p)
Add a value with the specified key. Append if already exists.
NHeaders::MapType MapType
Definition Headers.hpp:38
bool operator==(const Headers &other) const
size_t Size() const
decltype(MapType{}.crbegin()) CRIterType
Definition Headers.hpp:45
HeaderValue & operator[](HeaderKeyRef key)
NHeaders::ListProxy< true, std::string > Connection() const
Options for the current connection.
NHeaders::ValueProxy< false, NHeaders::MimeType > ContentType()
Defines the media type of the resource (MIME).
NHeaders::ListProxy< false, std::string > Via()
Path taken by the request/response through proxies (free std::string).
NHeaders::ListProxy< true, std::string > Trailer() const
Indicates header fields present in the trailer of a chunked message.
void Add(HeaderKeyRef key, HeaderValueRef val)
same as Add(HeaderPairRef p).
Headers(std::initializer_list< HeaderPair > init)
bool Exists(HeaderPairRef p) const
check if a key exists.
NHeaders::ListProxy< false, NHeaders::ContentEncodingEnum > ContentEncoding()
List of encodings (compression) applied to the entity.
void Set(HeaderPairRef p)
Add a value with the specified key. Replace if already exists.
NHeaders::HeaderValue HeaderValue
Definition Headers.hpp:33
CRIterType rend() const
Definition ListProxy.hpp:14
Definition ValueProxy.hpp:10