Ada 4.1.1
Fast spec-compliant URL parser
Loading...
Searching...
No Matches
url_pattern_init.h
Go to the documentation of this file.
1
5#ifndef ADA_URL_PATTERN_INIT_H
6#define ADA_URL_PATTERN_INIT_H
7
8#include "ada/expected.h"
9#include "ada/errors.h"
10
11#include <concepts>
12#include <cstdint>
13#include <iostream>
14#include <optional>
15#include <string>
16#include <string_view>
17
18#if ADA_TESTING
19#include <iostream>
20#endif // ADA_TESTING
21
22#if ADA_INCLUDE_URL_PATTERN
23namespace ada {
24
25// Important: C++20 allows us to use concept rather than `using` or `typedef
26// and allows functions with second argument, which is optional (using either
27// std::nullopt or a parameter with default value)
28template <typename F>
29concept url_pattern_encoding_callback = requires(F f, std::string_view sv) {
30 { f(sv) } -> std::same_as<tl::expected<std::string, errors>>;
31};
32
33// A structure providing matching patterns for individual components
34// of a URL. When a URLPattern is created, or when a URLPattern is
35// used to match or test against a URL, the input can be given as
36// either a string or a URLPatternInit struct. If a string is given,
37// it will be parsed to create a URLPatternInit. The URLPatternInit
38// API is defined as part of the URLPattern specification.
39// All provided strings must be valid UTF-8.
40struct url_pattern_init {
41 enum class process_type : uint8_t {
42 url,
43 pattern,
44 };
45
46 friend std::ostream& operator<<(std::ostream& os, process_type type) {
47 switch (type) {
48 case process_type::url:
49 return os << "url";
50 case process_type::pattern:
51 return os << "pattern";
52 default:
53 return os << "unknown";
54 }
55 }
56
57 // All strings must be valid UTF-8.
58 // @see https://urlpattern.spec.whatwg.org/#process-a-urlpatterninit
59 static tl::expected<url_pattern_init, errors> process(
60 const url_pattern_init& init, process_type type,
61 std::optional<std::string_view> protocol = std::nullopt,
62 std::optional<std::string_view> username = std::nullopt,
63 std::optional<std::string_view> password = std::nullopt,
64 std::optional<std::string_view> hostname = std::nullopt,
65 std::optional<std::string_view> port = std::nullopt,
66 std::optional<std::string_view> pathname = std::nullopt,
67 std::optional<std::string_view> search = std::nullopt,
68 std::optional<std::string_view> hash = std::nullopt);
69
70 // @see https://urlpattern.spec.whatwg.org/#process-protocol-for-init
71 static tl::expected<std::string, errors> process_protocol(
72 std::string_view value, process_type type);
73
74 // @see https://urlpattern.spec.whatwg.org/#process-username-for-init
75 static tl::expected<std::string, errors> process_username(
76 std::string_view value, process_type type);
77
78 // @see https://urlpattern.spec.whatwg.org/#process-password-for-init
79 static tl::expected<std::string, errors> process_password(
80 std::string_view value, process_type type);
81
82 // @see https://urlpattern.spec.whatwg.org/#process-hostname-for-init
83 static tl::expected<std::string, errors> process_hostname(
84 std::string_view value, process_type type);
85
86 // @see https://urlpattern.spec.whatwg.org/#process-port-for-init
87 static tl::expected<std::string, errors> process_port(
88 std::string_view port, std::string_view protocol, process_type type);
89
90 // @see https://urlpattern.spec.whatwg.org/#process-pathname-for-init
91 static tl::expected<std::string, errors> process_pathname(
92 std::string_view value, std::string_view protocol, process_type type);
93
94 // @see https://urlpattern.spec.whatwg.org/#process-search-for-init
95 static tl::expected<std::string, errors> process_search(
96 std::string_view value, process_type type);
97
98 // @see https://urlpattern.spec.whatwg.org/#process-hash-for-init
99 static tl::expected<std::string, errors> process_hash(std::string_view value,
100 process_type type);
101
102#if ADA_TESTING
103 friend void PrintTo(const url_pattern_init& init, std::ostream* os) {
104 *os << "protocol: '" << init.protocol.value_or("undefined") << "', ";
105 *os << "username: '" << init.username.value_or("undefined") << "', ";
106 *os << "password: '" << init.password.value_or("undefined") << "', ";
107 *os << "hostname: '" << init.hostname.value_or("undefined") << "', ";
108 *os << "port: '" << init.port.value_or("undefined") << "', ";
109 *os << "pathname: '" << init.pathname.value_or("undefined") << "', ";
110 *os << "search: '" << init.search.value_or("undefined") << "', ";
111 *os << "hash: '" << init.hash.value_or("undefined") << "', ";
112 *os << "base_url: '" << init.base_url.value_or("undefined") << "', ";
113 }
114#endif // ADA_TESTING
115
116 bool operator==(const url_pattern_init&) const;
117 // If present, must be valid UTF-8.
118 std::optional<std::string> protocol{};
119 // If present, must be valid UTF-8.
120 std::optional<std::string> username{};
121 // If present, must be valid UTF-8.
122 std::optional<std::string> password{};
123 // If present, must be valid UTF-8.
124 std::optional<std::string> hostname{};
125 // If present, must be valid UTF-8.
126 std::optional<std::string> port{};
127 // If present, must be valid UTF-8.
128 std::optional<std::string> pathname{};
129 // If present, must be valid UTF-8.
130 std::optional<std::string> search{};
131 // If present, must be valid UTF-8.
132 std::optional<std::string> hash{};
133 // If present, must be valid UTF-8.
134 std::optional<std::string> base_url{};
135};
136} // namespace ada
137#endif // ADA_INCLUDE_URL_PATTERN
138#endif // ADA_URL_PATTERN_INIT_H
Error type definitions for URL parsing.
std::ostream & operator<<(std::ostream &out, const ada::url &u)
Definition url-inl.h:43