comparison CSP2/CSP2_env/env-d9b9114564458d9d-741b3de822f2aaca6c6caa4325c4afce/include/kj/compat/url.h @ 69:33d812a61356

planemo upload commit 2e9511a184a1ca667c7be0c6321a36dc4e3d116d
author jpayne
date Tue, 18 Mar 2025 17:55:14 -0400
parents
children
comparison
equal deleted inserted replaced
67:0e9998148a16 69:33d812a61356
1 // Copyright (c) 2017 Cloudflare, Inc. and contributors
2 // Licensed under the MIT License:
3 //
4 // Permission is hereby granted, free of charge, to any person obtaining a copy
5 // of this software and associated documentation files (the "Software"), to deal
6 // in the Software without restriction, including without limitation the rights
7 // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
8 // copies of the Software, and to permit persons to whom the Software is
9 // furnished to do so, subject to the following conditions:
10 //
11 // The above copyright notice and this permission notice shall be included in
12 // all copies or substantial portions of the Software.
13 //
14 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
15 // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
16 // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
17 // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
18 // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
19 // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
20 // THE SOFTWARE.
21
22 #pragma once
23
24 #include <kj/string.h>
25 #include <kj/vector.h>
26 #include <inttypes.h>
27
28 KJ_BEGIN_HEADER
29
30 namespace kj {
31
32 struct UrlOptions {
33 // A bag of options that you can pass to Url::parse()/tryParse() to customize the parser's
34 // behavior.
35 //
36 // A copy of this options struct will be stored in the parsed Url object, at which point it
37 // controls the behavior of the serializer in Url::toString().
38
39 bool percentDecode = true;
40 // True if URL components should be automatically percent-decoded during parsing, and
41 // percent-encoded during serialization.
42
43 bool allowEmpty = false;
44 // Whether or not to allow empty path and query components when parsing; otherwise, they are
45 // silently removed. In other words, setting this false causes consecutive slashes in the path or
46 // consecutive ampersands in the query to be collapsed into one, whereas if true then they
47 // produce empty components.
48 };
49
50 struct Url {
51 // Represents a URL (or, more accurately, a URI, but whatever).
52 //
53 // Can be parsed from a string and composed back into a string.
54
55 String scheme;
56 // E.g. "http", "https".
57
58 struct UserInfo {
59 String username;
60 Maybe<String> password;
61 };
62
63 Maybe<UserInfo> userInfo;
64 // Username / password.
65
66 String host;
67 // Hostname, including port if specified. We choose not to parse out the port because KJ's
68 // network address parsing functions already accept addresses containing port numbers, and
69 // because most web standards don't actually want to separate host and port.
70
71 Vector<String> path;
72 bool hasTrailingSlash = false;
73 // Path, split on '/' characters. Note that the individual components of `path` could contain
74 // '/' characters if they were percent-encoded in the original URL.
75 //
76 // No component of the path is allowed to be "", ".", nor ".."; if such components are present,
77 // toString() will throw. Note that parse() and parseRelative() automatically resolve such
78 // components.
79
80 struct QueryParam {
81 String name;
82 String value;
83 };
84 Vector<QueryParam> query;
85 // Query, e.g. from "?key=value&key2=value2". If a component of the query contains no '=' sign,
86 // it will be parsed as a key with a null value, and later serialized with no '=' sign if you call
87 // Url::toString().
88 //
89 // To distinguish between null-valued and empty-valued query parameters, we test whether
90 // QueryParam::value is an allocated or unallocated string. For example:
91 //
92 // QueryParam { kj::str("name"), nullptr } // Null-valued; will not have an '=' sign.
93 // QueryParam { kj::str("name"), kj::str("") } // Empty-valued; WILL have an '=' sign.
94
95 Maybe<String> fragment;
96 // The stuff after the '#' character (not including the '#' character itself), if present.
97
98 using Options = UrlOptions;
99 Options options;
100
101 // ---------------------------------------------------------------------------
102
103 Url() = default;
104 Url(Url&&) = default;
105 ~Url() noexcept(false);
106 Url& operator=(Url&&) = default;
107
108 inline Url(String&& scheme, Maybe<UserInfo>&& userInfo, String&& host, Vector<String>&& path,
109 bool hasTrailingSlash, Vector<QueryParam>&& query, Maybe<String>&& fragment,
110 UrlOptions options)
111 : scheme(kj::mv(scheme)), userInfo(kj::mv(userInfo)), host(kj::mv(host)), path(kj::mv(path)),
112 hasTrailingSlash(hasTrailingSlash), query(kj::mv(query)), fragment(kj::mv(fragment)),
113 options(options) {}
114 // This constructor makes brace initialization work in C++11 and C++20 -- but is technically not
115 // needed in C++14 nor C++17. Go figure.
116
117 Url clone() const;
118
119 enum Context {
120 REMOTE_HREF,
121 // A link to a remote resource. Requires an authority (hostname) section, hence this will
122 // reject things like "mailto:" and "data:". This is the default context.
123
124 HTTP_PROXY_REQUEST,
125 // The URL to place in the first line of an HTTP proxy request. This includes scheme, host,
126 // path, and query, but omits userInfo (which should be used to construct the Authorization
127 // header) and fragment (which should not be transmitted).
128
129 HTTP_REQUEST
130 // The path to place in the first line of a regular HTTP request. This includes only the path
131 // and query. Scheme, user, host, and fragment are omitted.
132
133 // TODO(someday): Add context(s) that supports things like "mailto:", "data:", "blob:". These
134 // don't have an authority section.
135 };
136
137 kj::String toString(Context context = REMOTE_HREF) const;
138 // Convert the URL to a string.
139
140 static Url parse(StringPtr text, Context context = REMOTE_HREF, Options options = {});
141 static Maybe<Url> tryParse(StringPtr text, Context context = REMOTE_HREF, Options options = {});
142 // Parse an absolute URL.
143
144 Url parseRelative(StringPtr relative) const;
145 Maybe<Url> tryParseRelative(StringPtr relative) const;
146 // Parse a relative URL string with this URL as the base.
147 };
148
149 } // namespace kj
150
151 KJ_END_HEADER