Ada 4.1.1
Fast spec-compliant URL parser
Loading...
Searching...
No Matches
url_search_params-inl.h
Go to the documentation of this file.
1
5#ifndef ADA_URL_SEARCH_PARAMS_INL_H
6#define ADA_URL_SEARCH_PARAMS_INL_H
7
9#include "ada/unicode.h"
11
12#include <algorithm>
13#include <cstddef>
14#include <cstdint>
15#include <iterator>
16#include <optional>
17#include <ranges>
18#include <string>
19#include <string_view>
20#include <vector>
21
22namespace ada {
23
24// Declared in implementation.h; used here as a DoS bound on untrusted query
25// strings (ada.h includes both headers).
26uint32_t get_max_input_length();
27
28// A default, empty url_search_params for use with empty iterators.
29template <typename T, ada::url_search_params_iter_type Type>
30url_search_params url_search_params_iter<T, Type>::EMPTY;
31
32inline void url_search_params::reset(std::string_view input) {
33 // input may point into params.
34 const std::string copy(input);
35 params.clear();
36 initialize(copy);
37}
38
39inline void url_search_params::initialize(std::string_view input) {
40 if (!input.empty() && input.front() == '?') {
41 input.remove_prefix(1);
42 }
43 if (input.empty()) {
44 return;
45 }
46 // Refuse overlong query strings (same process-wide cap as URL parsing).
47 if (input.size() > get_max_input_length()) {
48 return;
49 }
50
51 // count('&')+1 is an upper bound on the pairs the loop below keeps
52 // (empty segments are skipped). It is one character count, which is
53 // the reserve the parse path already paid for. An input of only '&'
54 // would reserve N+1 pairs and store nothing; skip that.
55 const size_t ampersands = size_t(std::count(input.begin(), input.end(), '&'));
56 if (ampersands == input.size()) {
57 return;
58 }
59 params.reserve(ampersands + 1);
60
61 auto process_key_value = [&](const std::string_view current) {
62 const auto equal = current.find('=');
63 if (equal == std::string_view::npos) {
64 params.emplace_back(unicode::form_urlencoded_decode(current), "");
65 } else {
66 params.emplace_back(
67 unicode::form_urlencoded_decode(current.substr(0, equal)),
68 unicode::form_urlencoded_decode(current.substr(equal + 1)));
69 }
70 };
71
72 while (!input.empty()) {
73 const auto ampersand_index = input.find('&');
74
75 if (ampersand_index == std::string_view::npos) {
76 if (!input.empty()) {
77 process_key_value(input);
78 }
79 break;
80 } else if (ampersand_index != 0) {
81 process_key_value(input.substr(0, ampersand_index));
82 }
83
84 input.remove_prefix(ampersand_index + 1);
85 }
86}
87
88inline void url_search_params::append(const std::string_view key,
89 const std::string_view value) {
90 params.emplace_back(key, value);
91}
92
93inline size_t url_search_params::size() const noexcept { return params.size(); }
94
95inline std::optional<std::string_view> url_search_params::get(
96 const std::string_view key) {
97 auto entry = std::ranges::find_if(
98 params, [&key](const auto& param) { return param.first == key; });
99
100 if (entry == params.end()) {
101 return std::nullopt;
102 }
103
104 return entry->second;
105}
106
107inline std::vector<std::string> url_search_params::get_all(
108 const std::string_view key) {
109 std::vector<std::string> out{};
110
111 for (auto& param : params) {
112 if (param.first == key) {
113 out.emplace_back(param.second);
114 }
115 }
116
117 return out;
118}
119
120inline bool url_search_params::has(const std::string_view key) noexcept {
121 auto entry = std::ranges::find_if(
122 params, [&key](const auto& param) { return param.first == key; });
123 return entry != params.end();
124}
125
126inline bool url_search_params::has(std::string_view key,
127 std::string_view value) noexcept {
128 auto entry = std::ranges::find_if(params, [&key, &value](const auto& param) {
129 return param.first == key && param.second == value;
130 });
131 return entry != params.end();
132}
133
134inline std::string url_search_params::to_string() const {
136 std::string out{};
137 for (size_t i = 0; i < params.size(); i++) {
138 auto key = ada::unicode::percent_encode(params[i].first, character_set);
139 auto value = ada::unicode::percent_encode(params[i].second, character_set);
140
141 // Performance optimization: Move this inside percent_encode.
142 std::ranges::replace(key, ' ', '+');
143 std::ranges::replace(value, ' ', '+');
144
145 if (i != 0) {
146 out += "&";
147 }
148 out.append(key);
149 out += "=";
150 out.append(value);
151 }
152 return out;
153}
154
155inline void url_search_params::set(const std::string_view key,
156 const std::string_view value) {
157 const auto find = [&key](const auto& param) { return param.first == key; };
158
159 auto it = std::ranges::find_if(params, find);
160
161 if (it == params.end()) {
162 params.emplace_back(key, value);
163 } else {
164 it->second = value;
165 params.erase(std::remove_if(std::next(it), params.end(), find),
166 params.end());
167 }
168}
169
170inline void url_search_params::remove(const std::string_view key) {
171 std::erase_if(params,
172 [&key](const auto& param) { return param.first == key; });
173}
174
175inline void url_search_params::remove(const std::string_view key,
176 const std::string_view value) {
177 std::erase_if(params, [&key, &value](const auto& param) {
178 return param.first == key && param.second == value;
179 });
180}
181
183 // Keys are expected to be valid UTF-8, but percent_decode can produce
184 // arbitrary byte sequences. Handle truncated/invalid sequences gracefully.
185 std::ranges::stable_sort(params, [](const key_value_pair& lhs,
186 const key_value_pair& rhs) {
187 size_t i = 0, j = 0;
188 uint32_t low_surrogate1 = 0, low_surrogate2 = 0;
189 while ((i < lhs.first.size() || low_surrogate1 != 0) &&
190 (j < rhs.first.size() || low_surrogate2 != 0)) {
191 uint32_t codePoint1 = 0, codePoint2 = 0;
192
193 if (low_surrogate1 != 0) {
194 codePoint1 = low_surrogate1;
195 low_surrogate1 = 0;
196 } else {
197 uint8_t c1 = uint8_t(lhs.first[i]);
198 if (c1 > 0x7F && c1 <= 0xDF && i + 1 < lhs.first.size()) {
199 codePoint1 = ((c1 & 0x1F) << 6) | (uint8_t(lhs.first[i + 1]) & 0x3F);
200 i += 2;
201 } else if (c1 > 0xDF && c1 <= 0xEF && i + 2 < lhs.first.size()) {
202 codePoint1 = ((c1 & 0x0F) << 12) |
203 ((uint8_t(lhs.first[i + 1]) & 0x3F) << 6) |
204 (uint8_t(lhs.first[i + 2]) & 0x3F);
205 i += 3;
206 } else if (c1 > 0xEF && c1 <= 0xF7 && i + 3 < lhs.first.size()) {
207 codePoint1 = ((c1 & 0x07) << 18) |
208 ((uint8_t(lhs.first[i + 1]) & 0x3F) << 12) |
209 ((uint8_t(lhs.first[i + 2]) & 0x3F) << 6) |
210 (uint8_t(lhs.first[i + 3]) & 0x3F);
211 i += 4;
212
213 codePoint1 -= 0x10000;
214 uint16_t high_surrogate = uint16_t(0xD800 + (codePoint1 >> 10));
215 low_surrogate1 = uint16_t(0xDC00 + (codePoint1 & 0x3FF));
216 codePoint1 = high_surrogate;
217 } else {
218 // ASCII (c1 <= 0x7F) or truncated/invalid UTF-8: treat as raw byte
219 codePoint1 = c1;
220 i++;
221 }
222 }
223
224 if (low_surrogate2 != 0) {
225 codePoint2 = low_surrogate2;
226 low_surrogate2 = 0;
227 } else {
228 uint8_t c2 = uint8_t(rhs.first[j]);
229 if (c2 > 0x7F && c2 <= 0xDF && j + 1 < rhs.first.size()) {
230 codePoint2 = ((c2 & 0x1F) << 6) | (uint8_t(rhs.first[j + 1]) & 0x3F);
231 j += 2;
232 } else if (c2 > 0xDF && c2 <= 0xEF && j + 2 < rhs.first.size()) {
233 codePoint2 = ((c2 & 0x0F) << 12) |
234 ((uint8_t(rhs.first[j + 1]) & 0x3F) << 6) |
235 (uint8_t(rhs.first[j + 2]) & 0x3F);
236 j += 3;
237 } else if (c2 > 0xEF && c2 <= 0xF7 && j + 3 < rhs.first.size()) {
238 codePoint2 = ((c2 & 0x07) << 18) |
239 ((uint8_t(rhs.first[j + 1]) & 0x3F) << 12) |
240 ((uint8_t(rhs.first[j + 2]) & 0x3F) << 6) |
241 (uint8_t(rhs.first[j + 3]) & 0x3F);
242 j += 4;
243 codePoint2 -= 0x10000;
244 uint16_t high_surrogate = uint16_t(0xD800 + (codePoint2 >> 10));
245 low_surrogate2 = uint16_t(0xDC00 + (codePoint2 & 0x3FF));
246 codePoint2 = high_surrogate;
247 } else {
248 // ASCII (c2 <= 0x7F) or truncated/invalid UTF-8: treat as raw byte
249 codePoint2 = c2;
250 j++;
251 }
252 }
253
254 if (codePoint1 != codePoint2) {
255 return (codePoint1 < codePoint2);
256 }
257 }
258 return (j < rhs.first.size() || low_surrogate2 != 0);
259 });
260}
261
265
272
279
280template <typename T, url_search_params_iter_type Type>
282 return pos < params.params.size();
283}
284
285template <>
286inline std::optional<std::string_view> url_search_params_keys_iter::next() {
287 if (!has_next()) {
288 return std::nullopt;
289 }
290 return params.params[pos++].first;
291}
292
293template <>
294inline std::optional<std::string_view> url_search_params_values_iter::next() {
295 if (!has_next()) {
296 return std::nullopt;
297 }
298 return params.params[pos++].second;
299}
300
301template <>
302inline std::optional<key_value_view_pair>
304 if (!has_next()) {
305 return std::nullopt;
306 }
307 return params.params[pos++];
308}
309
310} // namespace ada
311
312#endif // ADA_URL_SEARCH_PARAMS_INL_H
Definitions of the character sets used by unicode functions.
constexpr uint8_t WWW_FORM_URLENCODED_PERCENT_ENCODE[32]
std::string form_urlencoded_decode(std::string_view input)
Definition unicode.cpp:538
url_search_params_iter< key_value_view_pair, url_search_params_iter_type::ENTRIES > url_search_params_entries_iter
url_search_params_iter< std::string_view, url_search_params_iter_type::KEYS > url_search_params_keys_iter
uint32_t get_max_input_length()
url_search_params_iter< std::string_view, url_search_params_iter_type::VALUES > url_search_params_values_iter
Class for parsing and manipulating URL query strings.
void set(std::string_view key, std::string_view value)
std::vector< std::string > get_all(std::string_view key)
void remove(std::string_view key)
url_search_params_entries_iter get_entries()
std::string to_string() const
url_search_params_keys_iter get_keys()
size_t size() const noexcept
void append(std::string_view key, std::string_view value)
url_search_params_values_iter get_values()
std::optional< std::string_view > get(std::string_view key)
bool has(std::string_view key) noexcept
Definitions for all unicode specific functions.
Declaration for the URL Search Params.