Sourcemeta Core 0.0.0
Loading...
Searching...
No Matches
http.h
1#ifndef SOURCEMETA_CORE_HTTP_H_
2#define SOURCEMETA_CORE_HTTP_H_
3
4#ifndef SOURCEMETA_CORE_HTTP_EXPORT
5#include <sourcemeta/core/http_export.h>
6#endif
7
8// NOLINTBEGIN(misc-include-cleaner)
9#include <sourcemeta/core/http_aws_sigv4.h>
10#include <sourcemeta/core/http_error.h>
11#include <sourcemeta/core/http_message.h>
12#include <sourcemeta/core/http_method.h>
13#include <sourcemeta/core/http_problem.h>
14#include <sourcemeta/core/http_status.h>
15#include <sourcemeta/core/http_syntax.h>
16#include <sourcemeta/core/http_system.h>
17// NOLINTEND(misc-include-cleaner)
18
19#include <chrono> // std::chrono::system_clock
20#include <cstddef> // std::size_t
21#include <cstdint> // std::uint8_t
22#include <initializer_list> // std::initializer_list
23#include <optional> // std::optional
24#include <span> // std::span
25#include <string> // std::string
26#include <string_view> // std::string_view
27#include <utility> // std::pair
28#include <vector> // std::vector
29
48
49namespace sourcemeta::core {
50
53enum class HTTPContentEncoding : std::uint8_t {
58};
59
74SOURCEMETA_CORE_HTTP_EXPORT
75auto http_match_accept(const std::string_view accept_header,
76 std::initializer_list<std::string_view> candidates)
77 -> std::string_view;
78
93SOURCEMETA_CORE_HTTP_EXPORT
95 const std::string_view accept_header,
96 std::initializer_list<std::string_view> media_types) noexcept -> bool;
97
111SOURCEMETA_CORE_HTTP_EXPORT
112auto http_content_type_matches(const std::string_view content_type_header,
113 const std::string_view media_type) noexcept
114 -> bool;
115
121 std::string_view type{};
123 std::string_view subtype{};
127 std::string_view suffix{};
129 std::string_view parameters{};
130};
131
150SOURCEMETA_CORE_HTTP_EXPORT
151auto http_parse_media_type(const std::string_view media_type)
152 -> std::optional<HTTPMediaType>;
153
175SOURCEMETA_CORE_HTTP_EXPORT
176auto http_match_media_range(const std::string_view media_type,
177 std::span<const std::string_view> ranges) noexcept
178 -> std::string_view;
179
196SOURCEMETA_CORE_HTTP_EXPORT
198 const std::string_view accept_language_header,
199 std::initializer_list<std::string_view> candidates) -> std::string_view;
200
214SOURCEMETA_CORE_HTTP_EXPORT
216 const std::string_view accept_encoding_header,
217 const HTTPContentEncoding server_preference) noexcept
218 -> std::optional<HTTPContentEncoding>;
219
234SOURCEMETA_CORE_HTTP_EXPORT
235auto http_from_date(const std::string_view value) noexcept
236 -> std::optional<std::chrono::system_clock::time_point>;
237
254SOURCEMETA_CORE_HTTP_EXPORT
255auto http_cache_control_max_age(const std::string_view cache_control) noexcept
256 -> std::optional<std::chrono::seconds>;
257
262struct HTTPLink {
264 std::string_view target;
266 std::string_view rel;
268 std::span<const std::pair<std::string_view, std::string_view>> parameters{};
269};
270
284SOURCEMETA_CORE_HTTP_EXPORT
285auto http_format_link(const HTTPLink &link, std::string &out) -> void;
286
299SOURCEMETA_CORE_HTTP_EXPORT
300auto http_format_link(const HTTPLink &link) -> std::string;
301
319SOURCEMETA_CORE_HTTP_EXPORT
320auto http_format_links(std::span<const HTTPLink> links, std::string &out)
321 -> void;
322
337SOURCEMETA_CORE_HTTP_EXPORT
338auto http_format_links(std::span<const HTTPLink> links) -> std::string;
339
344enum class HTTPCacheVisibility : std::uint8_t {
352};
353
363 std::optional<HTTPCacheVisibility> visibility{};
365 std::optional<std::chrono::seconds> max_age{};
368 std::optional<std::chrono::seconds> shared_max_age{};
370 bool no_store{false};
373 bool no_cache{false};
376 bool must_revalidate{false};
378 bool proxy_revalidate{false};
381 bool must_understand{false};
383 bool no_transform{false};
385 bool immutable{false};
388 std::span<const std::string_view> private_fields{};
391 std::span<const std::string_view> no_cache_fields{};
392};
393
401SOURCEMETA_CORE_HTTP_EXPORT
402auto http_cache_control_valid(const HTTPCacheControl &directives) -> bool;
403
427SOURCEMETA_CORE_HTTP_EXPORT
429 std::string &out) -> bool;
430
443SOURCEMETA_CORE_HTTP_EXPORT
445 -> std::optional<std::string>;
446
456 std::string_view scheme{};
458 std::optional<std::string_view> token68{};
460 std::span<const std::pair<std::string_view, std::string_view>> parameters{};
461};
462
487SOURCEMETA_CORE_HTTP_EXPORT
488auto http_challenge_valid(const HTTPChallenge &challenge) -> bool;
489
516SOURCEMETA_CORE_HTTP_EXPORT
517auto http_serialize_challenge(const HTTPChallenge &challenge, std::string &out)
518 -> bool;
519
523SOURCEMETA_CORE_HTTP_EXPORT
525 -> std::optional<std::string>;
526
532SOURCEMETA_CORE_HTTP_EXPORT
533auto http_serialize_challenges(std::span<const HTTPChallenge> challenges,
534 std::string &out) -> bool;
535
555SOURCEMETA_CORE_HTTP_EXPORT
556auto http_serialize_challenges(std::span<const HTTPChallenge> challenges)
557 -> std::optional<std::string>;
558
566 std::string scheme;
568 std::optional<std::string> token68;
570 std::vector<std::pair<std::string, std::string>> parameters;
571};
572
602SOURCEMETA_CORE_HTTP_EXPORT
603auto http_parse_challenges(const std::string_view input,
604 std::vector<HTTPParsedChallenge> &challenges)
605 -> bool;
606
609enum class HTTPCookieSameSite : std::uint8_t {
616};
617
626 std::string_view name{};
628 std::string_view value{};
630 std::optional<std::string_view> path{};
632 std::optional<std::string_view> domain{};
634 std::optional<std::chrono::seconds> max_age{};
636 bool http_only{false};
638 bool secure{false};
640 std::optional<HTTPCookieSameSite> same_site{};
641};
642
649inline constexpr std::size_t HTTP_COOKIE_MAXIMUM_NAME_VALUE_LENGTH{4096};
650
655inline constexpr std::size_t HTTP_COOKIE_MAXIMUM_ATTRIBUTE_VALUE_LENGTH{1024};
656
676SOURCEMETA_CORE_HTTP_EXPORT
677auto http_cookie_valid(const HTTPCookie &cookie) -> bool;
678
695SOURCEMETA_CORE_HTTP_EXPORT
696auto http_serialize_cookie(const HTTPCookie &cookie, std::string &out) -> bool;
697
710SOURCEMETA_CORE_HTTP_EXPORT
712 -> std::optional<std::string>;
713
730SOURCEMETA_CORE_HTTP_EXPORT
732
746SOURCEMETA_CORE_HTTP_EXPORT
748 const std::string_view header_value,
749 std::initializer_list<std::string_view> tokens) noexcept -> bool;
750
775SOURCEMETA_CORE_HTTP_EXPORT
776auto http_format_vary(std::span<const std::string_view> field_names,
777 std::string &out) -> bool;
778
796SOURCEMETA_CORE_HTTP_EXPORT
797auto http_format_vary(std::span<const std::string_view> field_names)
798 -> std::optional<std::string>;
799
814SOURCEMETA_CORE_HTTP_EXPORT
815auto http_parse_bearer(const std::string_view authorization) noexcept
816 -> std::string_view;
817
818} // namespace sourcemeta::core
819
820#endif
std::string_view rel
The link relation type.
Definition http.h:266
std::vector< std::pair< std::string, std::string > > parameters
The authentication parameters, in the order they appeared.
Definition http.h:570
std::optional< std::string_view > token68
The credential the challenge carries in place of parameters.
Definition http.h:458
bool no_store
Whether no cache may store any part of the exchange (RFC 9111 §5.2.2.5).
Definition http.h:370
std::span< const std::string_view > private_fields
Definition http.h:388
bool proxy_revalidate
The same for a shared cache alone (RFC 9111 §5.2.2.8).
Definition http.h:378
bool secure
Whether the cookie is only sent over secure channels.
Definition http.h:638
std::optional< std::chrono::seconds > max_age
The cookie lifetime.
Definition http.h:634
std::span< const std::string_view > no_cache_fields
Definition http.h:391
std::string_view parameters
The parameters that follow the type, starting at the first ;.
Definition http.h:129
bool http_only
Whether the cookie is withheld from scripts.
Definition http.h:636
std::optional< std::string > token68
The credential the challenge carried in place of parameters.
Definition http.h:568
std::optional< std::string_view > domain
The host the cookie is scoped to.
Definition http.h:632
bool no_cache
Definition http.h:373
std::string scheme
The authentication scheme, as spelled in the field.
Definition http.h:566
std::optional< std::chrono::seconds > max_age
How long the response stays fresh (RFC 9111 §5.2.2.1).
Definition http.h:365
std::string_view name
The cookie name.
Definition http.h:626
bool must_revalidate
Definition http.h:376
std::span< const std::pair< std::string_view, std::string_view > > parameters
The authentication parameters.
Definition http.h:460
bool immutable
Whether the response will not be updated while fresh (RFC 8246).
Definition http.h:385
std::span< const std::pair< std::string_view, std::string_view > > parameters
The additional target attributes of the link.
Definition http.h:268
std::optional< HTTPCacheVisibility > visibility
Which caches may store the response.
Definition http.h:363
std::optional< HTTPCookieSameSite > same_site
The cross-site request policy for the cookie.
Definition http.h:640
std::string_view subtype
The subtype, such as geo+json.
Definition http.h:123
std::optional< std::chrono::seconds > shared_max_age
Definition http.h:368
bool must_understand
Definition http.h:381
std::optional< std::string_view > path
The path the cookie is scoped to.
Definition http.h:630
std::string_view target
The link target reference.
Definition http.h:264
std::string_view type
The type, such as application.
Definition http.h:121
std::string_view value
The cookie value.
Definition http.h:628
bool no_transform
Whether the payload must not be transformed (RFC 9111 §5.2.2.6).
Definition http.h:383
std::string_view suffix
Definition http.h:127
std::string_view scheme
The authentication scheme.
Definition http.h:456
HTTPCacheVisibility
Definition http.h:344
SOURCEMETA_CORE_HTTP_EXPORT auto http_match_accept_language(const std::string_view accept_language_header, std::initializer_list< std::string_view > candidates) -> std::string_view
SOURCEMETA_CORE_HTTP_EXPORT auto http_from_date(const std::string_view value) noexcept -> std::optional< std::chrono::system_clock::time_point >
SOURCEMETA_CORE_HTTP_EXPORT auto http_field_list_contains_any(const std::string_view header_value, std::initializer_list< std::string_view > tokens) noexcept -> bool
HTTPCookieSameSite
Definition http.h:609
SOURCEMETA_CORE_HTTP_EXPORT auto http_cache_control_valid(const HTTPCacheControl &directives) -> bool
SOURCEMETA_CORE_HTTP_EXPORT auto http_parse_media_type(const std::string_view media_type) -> std::optional< HTTPMediaType >
SOURCEMETA_CORE_HTTP_EXPORT auto http_parse_bearer(const std::string_view authorization) noexcept -> std::string_view
SOURCEMETA_CORE_HTTP_EXPORT auto http_serialize_challenge(const HTTPChallenge &challenge, std::string &out) -> bool
SOURCEMETA_CORE_HTTP_EXPORT auto http_match_accept(const std::string_view accept_header, std::initializer_list< std::string_view > candidates) -> std::string_view
SOURCEMETA_CORE_HTTP_EXPORT auto http_serialize_cookie(const HTTPCookie &cookie, std::string &out) -> bool
constexpr std::size_t HTTP_COOKIE_MAXIMUM_ATTRIBUTE_VALUE_LENGTH
Definition http.h:655
constexpr std::size_t HTTP_COOKIE_MAXIMUM_NAME_VALUE_LENGTH
Definition http.h:649
SOURCEMETA_CORE_HTTP_EXPORT auto http_content_type_matches(const std::string_view content_type_header, const std::string_view media_type) noexcept -> bool
SOURCEMETA_CORE_HTTP_EXPORT auto http_match_media_range(const std::string_view media_type, std::span< const std::string_view > ranges) noexcept -> std::string_view
SOURCEMETA_CORE_HTTP_EXPORT auto http_format_links(std::span< const HTTPLink > links, std::string &out) -> void
SOURCEMETA_CORE_HTTP_EXPORT auto http_challenge_valid(const HTTPChallenge &challenge) -> bool
SOURCEMETA_CORE_HTTP_EXPORT auto http_expire_cookie(const HTTPCookie &cookie) -> HTTPCookie
SOURCEMETA_CORE_HTTP_EXPORT auto http_negotiate_encoding(const std::string_view accept_encoding_header, const HTTPContentEncoding server_preference) noexcept -> std::optional< HTTPContentEncoding >
SOURCEMETA_CORE_HTTP_EXPORT auto http_serialize_challenges(std::span< const HTTPChallenge > challenges, std::string &out) -> bool
SOURCEMETA_CORE_HTTP_EXPORT auto http_format_vary(std::span< const std::string_view > field_names, std::string &out) -> bool
SOURCEMETA_CORE_HTTP_EXPORT auto http_format_link(const HTTPLink &link, std::string &out) -> void
SOURCEMETA_CORE_HTTP_EXPORT auto http_parse_challenges(const std::string_view input, std::vector< HTTPParsedChallenge > &challenges) -> bool
SOURCEMETA_CORE_HTTP_EXPORT auto http_accept_includes_all(const std::string_view accept_header, std::initializer_list< std::string_view > media_types) noexcept -> bool
SOURCEMETA_CORE_HTTP_EXPORT auto http_cookie_valid(const HTTPCookie &cookie) -> bool
HTTPContentEncoding
Definition http.h:53
SOURCEMETA_CORE_HTTP_EXPORT auto http_serialize_cache_control(const HTTPCacheControl &directives, std::string &out) -> bool
SOURCEMETA_CORE_HTTP_EXPORT auto http_cache_control_max_age(const std::string_view cache_control) noexcept -> std::optional< std::chrono::seconds >
@ Strict
The cookie is withheld from every cross-site request.
Definition http.h:611
@ None
The cookie is sent on every cross-site request.
Definition http.h:615
@ Lax
The cookie is sent on top-level cross-site navigations only.
Definition http.h:613
@ Identity
The identity coding that applies no transformation.
Definition http.h:55
@ GZIP
The gzip coding per RFC 9110 §8.4.1.3.
Definition http.h:57
Definition http.h:454
Definition http.h:624
Definition http.h:119