Sourcemeta Core 0.0.0
Loading...
Searching...
No Matches
http_baggage.h
1#ifndef SOURCEMETA_CORE_HTTP_BAGGAGE_H_
2#define SOURCEMETA_CORE_HTTP_BAGGAGE_H_
3
4#ifndef SOURCEMETA_CORE_HTTP_EXPORT
5#include <sourcemeta/core/http_export.h>
6#endif
7
8#include <sourcemeta/core/http_syntax.h>
9#include <sourcemeta/core/text.h>
10
11#include <cassert> // assert
12#include <concepts> // std::invocable
13#include <optional> // std::optional
14#include <string_view> // std::string_view
15
16namespace sourcemeta::core {
17
44SOURCEMETA_CORE_HTTP_EXPORT
45auto http_is_baggage_value(const std::string_view value) noexcept -> bool;
46
83SOURCEMETA_CORE_HTTP_EXPORT
84auto http_is_baggage(const std::string_view value) noexcept -> bool;
85
118template <typename Callback>
119 requires std::invocable<Callback, std::string_view, std::string_view,
120 std::string_view>
121inline auto http_parse_baggage(const std::string_view value, Callback callback)
122 -> bool {
123 if (!http_is_baggage(value)) {
124 return false;
125 }
126
127 auto rest{value};
128 while (true) {
129 const auto comma{rest.find(',')};
130 const auto member{trim(rest.substr(0, comma), http_is_ows)};
131 const auto semicolon{member.find(';')};
132 const auto pair{trim(member.substr(0, semicolon), http_is_ows)};
133 const auto equal{pair.find('=')};
134
135 // Every member holds an equals sign, which the check above has settled, so
136 // the position below is never the one that stands for not found
137 assert(equal != std::string_view::npos);
138 callback(trim(pair.substr(0, equal), http_is_ows),
139 trim(pair.substr(equal + 1), http_is_ows),
140 semicolon == std::string_view::npos
141 ? member.substr(member.size())
142 : member.substr(semicolon + 1));
143 if (comma == std::string_view::npos) {
144 return true;
145 }
146
147 rest.remove_prefix(comma + 1);
148 }
149}
150
182template <typename Callback>
183 requires std::invocable<Callback, std::string_view,
184 std::optional<std::string_view>>
185inline auto http_parse_baggage_properties(const std::string_view properties,
186 Callback callback) -> bool {
187 // The repetition that carries properties may run zero times, so a member
188 // without any hands over nothing, and nothing is a set of no entries rather
189 // than a malformed one
190 // https://www.w3.org/TR/baggage/#definition
191 if (properties.empty()) {
192 return true;
193 }
194
195 auto pending{properties};
196 while (true) {
197 const auto semicolon{pending.find(';')};
198 const auto entry{trim(pending.substr(0, semicolon), http_is_ows)};
199 const auto equal{entry.find('=')};
200 if (!http_is_token(trim(entry.substr(0, equal), http_is_ows)) ||
201 (equal != std::string_view::npos &&
202 !http_is_baggage_value(trim(entry.substr(equal + 1), http_is_ows)))) {
203 return false;
204 }
205
206 if (semicolon == std::string_view::npos) {
207 break;
208 }
209
210 pending.remove_prefix(semicolon + 1);
211 }
212
213 auto rest{properties};
214 while (true) {
215 const auto semicolon{rest.find(';')};
216 const auto entry{trim(rest.substr(0, semicolon), http_is_ows)};
217 const auto equal{entry.find('=')};
218 callback(trim(entry.substr(0, equal), http_is_ows),
219 equal == std::string_view::npos
220 ? std::optional<std::string_view>{}
221 : trim(entry.substr(equal + 1), http_is_ows));
222 if (semicolon == std::string_view::npos) {
223 return true;
224 }
225
226 rest.remove_prefix(semicolon + 1);
227 }
228}
229
230} // namespace sourcemeta::core
231
232#endif
SOURCEMETA_CORE_HTTP_EXPORT auto http_is_baggage(const std::string_view value) noexcept -> bool
auto http_is_ows(const char character) noexcept -> bool
Definition http_syntax.h:28
SOURCEMETA_CORE_HTTP_EXPORT auto http_is_baggage_value(const std::string_view value) noexcept -> bool
auto http_is_token(const std::string_view value) noexcept -> bool
Definition http_syntax.h:153
auto http_parse_baggage(const std::string_view value, Callback callback) -> bool
Definition http_baggage.h:121
auto http_parse_baggage_properties(const std::string_view properties, Callback callback) -> bool
Definition http_baggage.h:185
SOURCEMETA_CORE_TEXT_EXPORT auto trim(const std::string_view input) noexcept -> std::string_view