1
Fork 0
mirror of https://github.com/RGBCube/serenity synced 2025-07-25 14:47:44 +00:00

LibWeb: Add & use 'HTTP whitespace' from '2.2. HTTP' in the Fetch spec

We had two independent definitions of this already, both referring to
the Fetch spec.
This commit is contained in:
Linus Groh 2022-07-10 19:20:49 +02:00
parent df3e25f1bb
commit e3798886ed
4 changed files with 10 additions and 12 deletions

View file

@ -15,4 +15,8 @@ namespace Web::Fetch {
// An HTTP tab or space is U+0009 TAB or U+0020 SPACE. // An HTTP tab or space is U+0009 TAB or U+0020 SPACE.
inline constexpr StringView HTTP_TAB_OR_SPACE = "\t "sv; inline constexpr StringView HTTP_TAB_OR_SPACE = "\t "sv;
// https://fetch.spec.whatwg.org/#http-whitespace
// HTTP whitespace is U+000A LF, U+000D CR, or an HTTP tab or space.
inline constexpr StringView HTTP_WHITESPACE = "\n\r\t "sv;
} }

View file

@ -8,6 +8,7 @@
#include <AK/GenericLexer.h> #include <AK/GenericLexer.h>
#include <AK/StringBuilder.h> #include <AK/StringBuilder.h>
#include <LibWeb/Fetch/AbstractOperations.h> #include <LibWeb/Fetch/AbstractOperations.h>
#include <LibWeb/Fetch/Infrastructure/HTTP.h>
#include <LibWeb/MimeSniff/MimeType.h> #include <LibWeb/MimeSniff/MimeType.h>
namespace Web::MimeSniff { namespace Web::MimeSniff {
@ -53,13 +54,8 @@ static bool contains_only_http_token_code_points(StringView string)
// https://mimesniff.spec.whatwg.org/#parse-a-mime-type // https://mimesniff.spec.whatwg.org/#parse-a-mime-type
Optional<MimeType> MimeType::from_string(StringView string) Optional<MimeType> MimeType::from_string(StringView string)
{ {
// https://fetch.spec.whatwg.org/#http-whitespace
// HTTP whitespace is U+000A LF, U+000D CR, or an HTTP tab or space.
// An HTTP tab or space is U+0009 TAB or U+0020 SPACE.
constexpr auto http_whitespace = "\n\r\t "sv;
// 1. Remove any leading and trailing HTTP whitespace from input. // 1. Remove any leading and trailing HTTP whitespace from input.
auto trimmed_string = string.trim(http_whitespace, TrimMode::Both); auto trimmed_string = string.trim(Fetch::HTTP_WHITESPACE, TrimMode::Both);
// 2. Let position be a position variable for input, initially pointing at the start of input. // 2. Let position be a position variable for input, initially pointing at the start of input.
GenericLexer lexer(trimmed_string); GenericLexer lexer(trimmed_string);
@ -82,7 +78,7 @@ Optional<MimeType> MimeType::from_string(StringView string)
auto subtype = lexer.consume_until(';'); auto subtype = lexer.consume_until(';');
// 8. Remove any trailing HTTP whitespace from subtype. // 8. Remove any trailing HTTP whitespace from subtype.
subtype = subtype.trim(http_whitespace, TrimMode::Right); subtype = subtype.trim(Fetch::HTTP_WHITESPACE, TrimMode::Right);
// 9. If subtype is the empty string or does not solely contain HTTP token code points, then return failure. // 9. If subtype is the empty string or does not solely contain HTTP token code points, then return failure.
if (subtype.is_empty() || !contains_only_http_token_code_points(subtype)) if (subtype.is_empty() || !contains_only_http_token_code_points(subtype))
@ -97,7 +93,7 @@ Optional<MimeType> MimeType::from_string(StringView string)
lexer.ignore(1); lexer.ignore(1);
// 2. Collect a sequence of code points that are HTTP whitespace from input given position. // 2. Collect a sequence of code points that are HTTP whitespace from input given position.
lexer.ignore_while(is_any_of(http_whitespace)); lexer.ignore_while(is_any_of(Fetch::HTTP_WHITESPACE));
// 3. Let parameterName be the result of collecting a sequence of code points that are not U+003B (;) or U+003D (=) from input, given position. // 3. Let parameterName be the result of collecting a sequence of code points that are not U+003B (;) or U+003D (=) from input, given position.
auto parameter_name = lexer.consume_until([](char ch) { auto parameter_name = lexer.consume_until([](char ch) {
@ -144,7 +140,7 @@ Optional<MimeType> MimeType::from_string(StringView string)
parameter_value = lexer.consume_until(';'); parameter_value = lexer.consume_until(';');
// 2. Remove any trailing HTTP whitespace from parameterValue. // 2. Remove any trailing HTTP whitespace from parameterValue.
parameter_value = parameter_value.trim(http_whitespace, TrimMode::Right); parameter_value = parameter_value.trim(Fetch::HTTP_WHITESPACE, TrimMode::Right);
// 3. If parameterValue is the empty string, then continue. // 3. If parameterValue is the empty string, then continue.
if (parameter_value.is_empty()) if (parameter_value.is_empty())

View file

@ -413,7 +413,7 @@ static String normalize_method(String const& method)
// https://fetch.spec.whatwg.org/#concept-header-value-normalize // https://fetch.spec.whatwg.org/#concept-header-value-normalize
static String normalize_header_value(String const& header_value) static String normalize_header_value(String const& header_value)
{ {
return header_value.trim(StringView { http_whitespace_bytes }); return header_value.trim(Fetch::HTTP_WHITESPACE);
} }
// https://fetch.spec.whatwg.org/#header-value // https://fetch.spec.whatwg.org/#header-value

View file

@ -21,8 +21,6 @@
namespace Web::XHR { namespace Web::XHR {
static constexpr Array<u8, 4> http_whitespace_bytes = { '\t', '\n', '\r', ' ' };
using XMLHttpRequestBodyInit = Variant<NonnullRefPtr<URL::URLSearchParams>, String>; using XMLHttpRequestBodyInit = Variant<NonnullRefPtr<URL::URLSearchParams>, String>;
class XMLHttpRequest final class XMLHttpRequest final