Sourcemeta Core 0.0.0
Loading...
Searching...
No Matches
http_syntax.h
1#ifndef SOURCEMETA_CORE_HTTP_SYNTAX_H_
2#define SOURCEMETA_CORE_HTTP_SYNTAX_H_
3
4#ifndef SOURCEMETA_CORE_HTTP_EXPORT
5#include <sourcemeta/core/http_export.h>
6#endif
7
8#include <sourcemeta/core/text.h>
9
10#include <cstddef> // std::size_t
11#include <optional> // std::optional
12#include <string> // std::string
13#include <string_view> // std::string_view
14
15namespace sourcemeta::core {
16
28inline auto http_is_ows(const char character) noexcept -> bool {
29 return character == ' ' || character == '\t';
30}
31
43inline auto http_is_tchar(const char character) noexcept -> bool {
44 switch (character) {
45 case '!':
46 case '#':
47 case '$':
48 case '%':
49 case '&':
50 case '\'':
51 case '*':
52 case '+':
53 case '-':
54 case '.':
55 case '^':
56 case '_':
57 case '`':
58 case '|':
59 case '~':
60 return true;
61 default:
62 return is_alphanum(character);
63 }
64}
65
86inline auto http_is_field_octet(const char character) noexcept -> bool {
87 const auto byte{static_cast<unsigned char>(character)};
88 return byte == 0x09 || (byte >= 0x20 && byte != 0x7F);
89}
90
103inline auto http_is_b64token_char(const char character) noexcept -> bool {
104 return is_alphanum(character) || character == '-' || character == '.' ||
105 character == '_' || character == '~' || character == '+' ||
106 character == '/';
107}
108
121inline auto http_is_b64token(const std::string_view value) noexcept -> bool {
122 std::size_t position{0};
123 while (position < value.size() && http_is_b64token_char(value[position])) {
124 position += 1;
125 }
126
127 if (position == 0) {
128 return false;
129 }
130
131 while (position < value.size()) {
132 if (value[position] != '=') {
133 return false;
134 }
135
136 position += 1;
137 }
138
139 return true;
140}
141
153inline auto http_is_token(const std::string_view value) noexcept -> bool {
154 if (value.empty()) {
155 return false;
156 }
157
158 for (const auto character : value) {
159 if (!http_is_tchar(character)) {
160 return false;
161 }
162 }
163
164 return true;
165}
166
183inline auto http_encode_quoted_string(const std::string_view value,
184 std::string &sink) -> bool {
185 // RFC 9110 Section 5.6.4: quoted text and a quoted-pair admit what a field
186 // value admits, so a control character beyond the horizontal tab makes the
187 // value unencodable, carriage return and line feed among them
188 for (const auto character : value) {
189 if (!http_is_field_octet(character)) {
190 return false;
191 }
192 }
193
194 sink.push_back('"');
195 for (const auto character : value) {
196 if (character == '"' || character == '\\') {
197 sink.push_back('\\');
198 }
199
200 sink.push_back(character);
201 }
202
203 sink.push_back('"');
204 return true;
205}
206
219inline auto
220http_field_line_has_forbidden_byte(const std::string_view value) noexcept
221 -> bool {
222 // RFC 9110 Section 5.5: "a recipient of CR, LF, or NUL within a field value
223 // MUST either reject the message or replace each of those characters with SP
224 // before further processing or forwarding of that message", so a field
225 // carrying one is refused as a header-injection defense
226 for (const auto character : value) {
227 if (character == '\r' || character == '\n' || character == '\0') {
228 return true;
229 }
230 }
231
232 return false;
233}
234
245inline auto http_trim_leading_ows(std::string_view value) noexcept
246 -> std::string_view {
247 while (!value.empty() && http_is_ows(value.front())) {
248 value.remove_prefix(1);
249 }
250
251 return value;
252}
253
264inline auto http_trim_trailing_ows(std::string_view value) noexcept
265 -> std::string_view {
266 while (!value.empty() && http_is_ows(value.back())) {
267 value.remove_suffix(1);
268 }
269
270 return value;
271}
272
295SOURCEMETA_CORE_HTTP_EXPORT
296auto http_scan_quoted_string(const std::string_view input,
297 const std::size_t position, std::string &storage,
298 std::string_view &value)
299 -> std::optional<std::size_t>;
300
301} // namespace sourcemeta::core
302
303#endif
auto http_trim_leading_ows(std::string_view value) noexcept -> std::string_view
Definition http_syntax.h:245
auto http_trim_trailing_ows(std::string_view value) noexcept -> std::string_view
Definition http_syntax.h:264
auto http_is_field_octet(const char character) noexcept -> bool
Definition http_syntax.h:86
auto http_is_ows(const char character) noexcept -> bool
Definition http_syntax.h:28
auto http_is_token(const std::string_view value) noexcept -> bool
Definition http_syntax.h:153
auto http_is_b64token(const std::string_view value) noexcept -> bool
Definition http_syntax.h:121
auto http_field_line_has_forbidden_byte(const std::string_view value) noexcept -> bool
Definition http_syntax.h:220
auto http_is_b64token_char(const char character) noexcept -> bool
Definition http_syntax.h:103
SOURCEMETA_CORE_HTTP_EXPORT auto http_scan_quoted_string(const std::string_view input, const std::size_t position, std::string &storage, std::string_view &value) -> std::optional< std::size_t >
auto http_is_tchar(const char character) noexcept -> bool
Definition http_syntax.h:43
auto http_encode_quoted_string(const std::string_view value, std::string &sink) -> bool
Definition http_syntax.h:183
constexpr auto is_alphanum(const Character character) noexcept -> bool
Definition text.h:293