LibHTTP+LibWeb+RequestServer: Move Fetch's HTTP header infra to LibHTTP

The end goal here is for LibHTTP to be the home of our RFC 9111 (HTTP
caching) implementation. We currently have one implementation in LibWeb
for our in-memory cache and another in RequestServer for our disk cache.

The implementations both largely revolve around interacting with HTTP
headers. But in LibWeb, we are using Fetch's header infra, and in RS we
are using are home-grown header infra from LibHTTP.

So to give these a common denominator, this patch replaces the LibHTTP
implementation with Fetch's infra. Our existing LibHTTP implementation
was not particularly compliant with any spec, so this at least gives us
a standards-based common implementation.

This migration also required moving a handful of other Fetch AOs over
to LibHTTP. (It turns out these AOs were all from the Fetch/Infra/HTTP
folder, so perhaps it makes sense for LibHTTP to be the implementation
of that entire set of facilities.)
This commit is contained in:
Timothy Flynn 2025-11-26 14:13:23 -05:00 committed by Jelle Raaijmakers
parent 3dce6766a3
commit 9375660b64
Notes: github-actions[bot] 2025-11-27 13:58:52 +00:00
78 changed files with 935 additions and 1017 deletions

View file

@ -1,6 +1,10 @@
set(SOURCES set(SOURCES
Header.cpp
HeaderList.cpp
HTTP.cpp
HttpRequest.cpp HttpRequest.cpp
Method.cpp
) )
ladybird_lib(LibHTTP http) ladybird_lib(LibHTTP http)
target_link_libraries(LibHTTP PRIVATE LibCompress LibCore LibTLS LibURL) target_link_libraries(LibHTTP PRIVATE LibCompress LibCore LibIPC LibRegex LibTextCodec LibTLS LibURL)

View file

@ -8,7 +8,10 @@
namespace HTTP { namespace HTTP {
class HeaderList;
class HttpRequest; class HttpRequest;
class HttpResponse; class HttpResponse;
struct Header;
} }

View file

@ -0,0 +1,77 @@
/*
* Copyright (c) 2022, Luke Wilde <lukew@serenityos.org>
*
* SPDX-License-Identifier: BSD-2-Clause
*/
#include <AK/GenericLexer.h>
#include <AK/StringBuilder.h>
#include <LibHTTP/HTTP.h>
namespace HTTP {
// https://fetch.spec.whatwg.org/#collect-an-http-quoted-string
String collect_an_http_quoted_string(GenericLexer& lexer, HttpQuotedStringExtractValue extract_value)
{
// 1. Let positionStart be position.
auto position_start = lexer.tell();
// 2. Let value be the empty string.
StringBuilder value;
// 3. Assert: the code point at position within input is U+0022 (").
VERIFY(lexer.peek() == '"');
// 4. Advance position by 1.
lexer.ignore(1);
// 5. While true:
while (true) {
// 1. Append the result of collecting a sequence of code points that are not U+0022 (") or U+005C (\) from
// input, given position, to value.
auto value_part = lexer.consume_until([](char ch) {
return ch == '"' || ch == '\\';
});
value.append(value_part);
// 2. If position is past the end of input, then break.
if (lexer.is_eof())
break;
// 3. Let quoteOrBackslash be the code point at position within input.
// 4. Advance position by 1.
char quote_or_backslash = lexer.consume();
// 5. If quoteOrBackslash is U+005C (\), then:
if (quote_or_backslash == '\\') {
// 1. If position is past the end of input, then append U+005C (\) to value and break.
if (lexer.is_eof()) {
value.append('\\');
break;
}
// 2. Append the code point at position within input to value.
// 3. Advance position by 1.
value.append(lexer.consume());
}
// 6. Otherwise:
else {
// 1. Assert: quoteOrBackslash is U+0022 (").
VERIFY(quote_or_backslash == '"');
// 2. Break.
break;
}
}
// 6. If the extract-value flag is set, then return value.
if (extract_value == HttpQuotedStringExtractValue::Yes)
return MUST(value.to_string());
// 7. Return the code points from positionStart to position, inclusive, within input.
return MUST(String::from_utf8(lexer.input().substring_view(position_start, lexer.tell() - position_start)));
}
}

48
Libraries/LibHTTP/HTTP.h Normal file
View file

@ -0,0 +1,48 @@
/*
* Copyright (c) 2022-2023, Linus Groh <linusg@serenityos.org>
* Copyright (c) 2022, Luke Wilde <lukew@serenityos.org>
*
* SPDX-License-Identifier: BSD-2-Clause
*/
#pragma once
#include <AK/Array.h>
#include <AK/String.h>
#include <AK/StringView.h>
namespace HTTP {
// https://fetch.spec.whatwg.org/#http-tab-or-space
// An HTTP tab or space is U+0009 TAB or U+0020 SPACE.
constexpr inline auto 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.
constexpr inline auto HTTP_WHITESPACE = "\n\r\t "sv;
// https://fetch.spec.whatwg.org/#http-newline-byte
// An HTTP newline byte is 0x0A (LF) or 0x0D (CR).
constexpr inline Array HTTP_NEWLINE_BYTES { 0x0Au, 0x0Du };
// https://fetch.spec.whatwg.org/#http-tab-or-space-byte
// An HTTP tab or space byte is 0x09 (HT) or 0x20 (SP).
constexpr inline Array HTTP_TAB_OR_SPACE_BYTES { 0x09u, 0x20u };
constexpr bool is_http_newline(u32 code_point)
{
return code_point == 0x0Au || code_point == 0x0Du;
}
constexpr bool is_http_tab_or_space(u32 code_point)
{
return code_point == 0x09u || code_point == 0x20u;
}
enum class HttpQuotedStringExtractValue {
No,
Yes,
};
[[nodiscard]] String collect_an_http_quoted_string(GenericLexer& lexer, HttpQuotedStringExtractValue extract_value = HttpQuotedStringExtractValue::No);
}

View file

@ -1,35 +1,26 @@
/* /*
* Copyright (c) 2024, Andreas Kling <andreas@ladybird.org>
* Copyright (c) 2022-2023, Linus Groh <linusg@serenityos.org> * Copyright (c) 2022-2023, Linus Groh <linusg@serenityos.org>
* Copyright (c) 2022, Kenneth Myhra <kennethmyhra@serenityos.org>
* Copyright (c) 2022, Luke Wilde <lukew@serenityos.org>
* *
* SPDX-License-Identifier: BSD-2-Clause * SPDX-License-Identifier: BSD-2-Clause
*/ */
#include <AK/CharacterTypes.h>
#include <AK/Checked.h>
#include <AK/GenericLexer.h> #include <AK/GenericLexer.h>
#include <AK/QuickSort.h> #include <AK/QuickSort.h>
#include <AK/StringUtils.h> #include <LibHTTP/HTTP.h>
#include <LibJS/Runtime/VM.h> #include <LibHTTP/Header.h>
#include <LibHTTP/Method.h>
#include <LibIPC/Decoder.h>
#include <LibIPC/Encoder.h>
#include <LibRegex/Regex.h> #include <LibRegex/Regex.h>
#include <LibTextCodec/Decoder.h> #include <LibTextCodec/Decoder.h>
#include <LibTextCodec/Encoder.h> #include <LibTextCodec/Encoder.h>
#include <LibWeb/Fetch/Infrastructure/HTTP.h>
#include <LibWeb/Fetch/Infrastructure/HTTP/Headers.h>
#include <LibWeb/Fetch/Infrastructure/HTTP/Methods.h>
#include <LibWeb/Loader/ResourceLoader.h>
namespace Web::Fetch::Infrastructure { namespace HTTP {
GC_DEFINE_ALLOCATOR(HeaderList);
Header Header::isomorphic_encode(StringView name, StringView value) Header Header::isomorphic_encode(StringView name, StringView value)
{ {
return { return { TextCodec::isomorphic_encode(name), TextCodec::isomorphic_encode(value) };
.name = TextCodec::isomorphic_encode(name),
.value = TextCodec::isomorphic_encode(value),
};
} }
// https://fetch.spec.whatwg.org/#extract-header-values // https://fetch.spec.whatwg.org/#extract-header-values
@ -58,275 +49,6 @@ Optional<Vector<ByteString>> Header::extract_header_values() const
return Vector { value }; return Vector { value };
} }
GC::Ref<HeaderList> HeaderList::create(JS::VM& vm)
{
return vm.heap().allocate<HeaderList>();
}
// https://fetch.spec.whatwg.org/#header-list-contains
bool HeaderList::contains(StringView name) const
{
// A header list list contains a header name name if list contains a header whose name is a byte-case-insensitive
// match for name.
return any_of(*this, [&](auto const& header) {
return header.name.equals_ignoring_ascii_case(name);
});
}
// https://fetch.spec.whatwg.org/#concept-header-list-get
Optional<ByteString> HeaderList::get(StringView name) const
{
// To get a header name name from a header list list, run these steps:
// 1. If list does not contain name, then return null.
if (!contains(name))
return {};
// 2. Return the values of all headers in list whose name is a byte-case-insensitive match for name, separated from
// each other by 0x2C 0x20, in order.
StringBuilder builder;
bool first = true;
for (auto const& header : *this) {
if (!header.name.equals_ignoring_ascii_case(name))
continue;
if (!first)
builder.append(", "sv);
builder.append(header.value);
first = false;
}
return builder.to_byte_string();
}
// https://fetch.spec.whatwg.org/#concept-header-list-get-decode-split
Optional<Vector<String>> HeaderList::get_decode_and_split(StringView name) const
{
// To get, decode, and split a header name name from header list list, run these steps:
// 1. Let value be the result of getting name from list.
auto value = get(name);
// 2. If value is null, then return null.
if (!value.has_value())
return {};
// 3. Return the result of getting, decoding, and splitting value.
return get_decode_and_split_header_value(*value);
}
// https://fetch.spec.whatwg.org/#concept-header-list-append
void HeaderList::append(Header header)
{
// To append a header (name, value) to a header list list, run these steps:
// 1. If list contains name, then set name to the first such headers name.
// NOTE: This reuses the casing of the name of the header already in list, if any. If there are multiple matched
// headers their names will all be identical.
auto matching_header = first_matching([&](auto const& existing_header) {
return existing_header.name.equals_ignoring_ascii_case(header.name);
});
if (matching_header.has_value())
header.name = matching_header->name;
// 2. Append (name, value) to list.
Vector::append(move(header));
}
// https://fetch.spec.whatwg.org/#concept-header-list-delete
void HeaderList::delete_(StringView name)
{
// To delete a header name name from a header list list, remove all headers whose name is a byte-case-insensitive
// match for name from list.
remove_all_matching([&](auto const& header) {
return header.name.equals_ignoring_ascii_case(name);
});
}
// https://fetch.spec.whatwg.org/#concept-header-list-set
void HeaderList::set(Header header)
{
// To set a header (name, value) in a header list list, run these steps:
// 1. If list contains name, then set the value of the first such header to value and remove the others.
auto it = find_if([&](auto const& existing_header) {
return existing_header.name.equals_ignoring_ascii_case(header.name);
});
if (it != end()) {
it->value = move(header.value);
size_t i = 0;
remove_all_matching([&](auto const& existing_header) {
if (i++ <= it.index())
return false;
return existing_header.name.equals_ignoring_ascii_case(it->name);
});
}
// 2. Otherwise, append header (name, value) to list.
else {
append(move(header));
}
}
// https://fetch.spec.whatwg.org/#concept-header-list-combine
void HeaderList::combine(Header header)
{
// To combine a header (name, value) in a header list list, run these steps:
// 1. If list contains name, then set the value of the first such header to its value, followed by 0x2C 0x20,
// followed by value.
auto matching_header = first_matching([&](auto const& existing_header) {
return existing_header.name.equals_ignoring_ascii_case(header.name);
});
if (matching_header.has_value()) {
matching_header->value = ByteString::formatted("{}, {}", matching_header->value, header.value);
}
// 2. Otherwise, append (name, value) to list.
else {
append(move(header));
}
}
// https://fetch.spec.whatwg.org/#concept-header-list-sort-and-combine
Vector<Header> HeaderList::sort_and_combine() const
{
// To sort and combine a header list list, run these steps:
// 1. Let headers be an empty list of headers with the key being the name and value the value.
Vector<Header> headers;
// 2. Let names be the result of convert header names to a sorted-lowercase set with all the names of the headers
// in list.
Vector<ByteString> names_list;
names_list.ensure_capacity(size());
for (auto const& header : *this)
names_list.unchecked_append(header.name);
auto names = convert_header_names_to_a_sorted_lowercase_set(names_list);
// 3. For each name of names:
for (auto& name : names) {
// 1. If name is `set-cookie`, then:
if (name == "set-cookie"sv) {
// 1. Let values be a list of all values of headers in list whose name is a byte-case-insensitive match for
// name, in order.
// 2. For each value of values:
for (auto const& [header_name, value] : *this) {
if (header_name.equals_ignoring_ascii_case(name)) {
// 1. Append (name, value) to headers.
headers.append({ name, value });
}
}
}
// 2. Otherwise:
else {
// 1. Let value be the result of getting name from list.
auto value = get(name);
// 2. Assert: value is not null.
VERIFY(value.has_value());
// 3. Append (name, value) to headers.
headers.empend(move(name), value.release_value());
}
}
// 4. Return headers.
return headers;
}
// https://fetch.spec.whatwg.org/#extract-header-list-values
Variant<Empty, Vector<ByteString>, HeaderList::ExtractHeaderParseFailure> HeaderList::extract_header_list_values(StringView name) const
{
// 1. If list does not contain name, then return null.
if (!contains(name))
return {};
// FIXME: 2. If the ABNF for name allows a single header and list contains more than one, then return failure.
// NOTE: If different error handling is needed, extract the desired header first.
// 3. Let values be an empty list.
Vector<ByteString> values;
// 4. For each header header list contains whose name is name:
for (auto const& header : *this) {
if (!header.name.equals_ignoring_ascii_case(name))
continue;
// 1. Let extract be the result of extracting header values from header.
auto extract = header.extract_header_values();
// 2. If extract is failure, then return failure.
if (!extract.has_value())
return ExtractHeaderParseFailure {};
// 3. Append each value in extract, in order, to values.
values.extend(extract.release_value());
}
// 5. Return values.
return values;
}
// https://fetch.spec.whatwg.org/#header-list-extract-a-length
Variant<Empty, u64, HeaderList::ExtractLengthFailure> HeaderList::extract_length() const
{
// 1. Let values be the result of getting, decoding, and splitting `Content-Length` from headers.
auto values = get_decode_and_split("Content-Length"sv);
// 2. If values is null, then return null.
if (!values.has_value())
return {};
// 3. Let candidateValue be null.
Optional<String> candidate_value;
// 4. For each value of values:
for (auto const& value : *values) {
// 1. If candidateValue is null, then set candidateValue to value.
if (!candidate_value.has_value()) {
candidate_value = value;
}
// 2. Otherwise, if value is not candidateValue, return failure.
else if (candidate_value.value() != value) {
return ExtractLengthFailure {};
}
}
// 5. If candidateValue is the empty string or has a code point that is not an ASCII digit, then return null.
// 6. Return candidateValue, interpreted as decimal number.
// FIXME: This will return an empty Optional if it cannot fit into a u64, is this correct?
auto result = candidate_value->to_number<u64>(TrimWhitespace::No);
if (!result.has_value())
return {};
return *result;
}
// Non-standard
Vector<ByteString> HeaderList::unique_names() const
{
Vector<ByteString> header_names_set;
HashTable<StringView, CaseInsensitiveStringTraits> header_names_seen;
for (auto const& header : *this) {
if (header_names_seen.contains(header.name))
continue;
header_names_set.append(header.name);
header_names_seen.set(header.name);
}
return header_names_set;
}
// https://fetch.spec.whatwg.org/#header-name // https://fetch.spec.whatwg.org/#header-name
bool is_header_name(StringView header_name) bool is_header_name(StringView header_name)
{ {
@ -368,7 +90,6 @@ ByteString normalize_header_value(StringView potential_value)
// https://fetch.spec.whatwg.org/#forbidden-header-name // https://fetch.spec.whatwg.org/#forbidden-header-name
bool is_forbidden_request_header(Header const& header) bool is_forbidden_request_header(Header const& header)
{ {
// A header (name, value) is forbidden request-header if these steps return true:
auto const& [name, value] = header; auto const& [name, value] = header;
// 1. If name is a byte-case-insensitive match for one of: // 1. If name is a byte-case-insensitive match for one of:
@ -441,8 +162,6 @@ bool is_forbidden_response_header_name(StringView header_name)
// https://fetch.spec.whatwg.org/#header-value-get-decode-and-split // https://fetch.spec.whatwg.org/#header-value-get-decode-and-split
Vector<String> get_decode_and_split_header_value(StringView value) Vector<String> get_decode_and_split_header_value(StringView value)
{ {
// To get, decode, and split a header value value, run these steps:
// 1. Let input be the result of isomorphic decoding value. // 1. Let input be the result of isomorphic decoding value.
auto input = TextCodec::isomorphic_decode(value); auto input = TextCodec::isomorphic_decode(value);
@ -496,8 +215,6 @@ Vector<String> get_decode_and_split_header_value(StringView value)
// https://fetch.spec.whatwg.org/#convert-header-names-to-a-sorted-lowercase-set // https://fetch.spec.whatwg.org/#convert-header-names-to-a-sorted-lowercase-set
Vector<ByteString> convert_header_names_to_a_sorted_lowercase_set(ReadonlySpan<ByteString> header_names) Vector<ByteString> convert_header_names_to_a_sorted_lowercase_set(ReadonlySpan<ByteString> header_names)
{ {
// To convert header names to a sorted-lowercase set, given a list of names headerNames, run these steps:
// 1. Let headerNamesSet be a new ordered set. // 1. Let headerNamesSet be a new ordered set.
HashTable<StringView, CaseInsensitiveStringTraits> header_names_seen; HashTable<StringView, CaseInsensitiveStringTraits> header_names_seen;
Vector<ByteString> header_names_set; Vector<ByteString> header_names_set;
@ -530,10 +247,10 @@ ByteString build_content_range(u64 range_start, u64 range_end, u64 full_length)
} }
// https://fetch.spec.whatwg.org/#simple-range-header-value // https://fetch.spec.whatwg.org/#simple-range-header-value
Optional<RangeHeaderValue> parse_single_range_header_value(StringView const value, bool const allow_whitespace) Optional<RangeHeaderValue> parse_single_range_header_value(StringView value, bool allow_whitespace)
{ {
// 1. Let data be the isomorphic decoding of value. // 1. Let data be the isomorphic decoding of value.
auto const data = TextCodec::isomorphic_decode(value); auto data = TextCodec::isomorphic_decode(value);
// 2. If data does not start with "bytes", then return failure. // 2. If data does not start with "bytes", then return failure.
if (!data.starts_with_bytes("bytes"sv)) if (!data.starts_with_bytes("bytes"sv))
@ -559,7 +276,8 @@ Optional<RangeHeaderValue> parse_single_range_header_value(StringView const valu
// 8. Let rangeStart be the result of collecting a sequence of code points that are ASCII digits, from data given position. // 8. Let rangeStart be the result of collecting a sequence of code points that are ASCII digits, from data given position.
auto range_start = lexer.consume_while(is_ascii_digit); auto range_start = lexer.consume_while(is_ascii_digit);
// 9. Let rangeStartValue be rangeStart, interpreted as decimal number, if rangeStart is not the empty string; otherwise null. // 9. Let rangeStartValue be rangeStart, interpreted as decimal number, if rangeStart is not the empty string;
// otherwise null.
auto range_start_value = range_start.to_number<u64>(); auto range_start_value = range_start.to_number<u64>();
// 10. If allowWhitespace is true, collect a sequence of code points that are HTTP tab or space, from data given position. // 10. If allowWhitespace is true, collect a sequence of code points that are HTTP tab or space, from data given position.
@ -589,20 +307,33 @@ Optional<RangeHeaderValue> parse_single_range_header_value(StringView const valu
if (!range_end_value.has_value() && !range_start_value.has_value()) if (!range_end_value.has_value() && !range_start_value.has_value())
return {}; return {};
// 18. If rangeStartValue and rangeEndValue are numbers, and rangeStartValue is greater than rangeEndValue, then return failure. // 18. If rangeStartValue and rangeEndValue are numbers, and rangeStartValue is greater than rangeEndValue, then
// return failure.
if (range_start_value.has_value() && range_end_value.has_value() && *range_start_value > *range_end_value) if (range_start_value.has_value() && range_end_value.has_value() && *range_start_value > *range_end_value)
return {}; return {};
// 19. Return (rangeStartValue, rangeEndValue). // 19. Return (rangeStartValue, rangeEndValue).
return RangeHeaderValue { move(range_start_value), move(range_end_value) }; return RangeHeaderValue { range_start_value, range_end_value };
} }
// https://fetch.spec.whatwg.org/#default-user-agent-value }
ByteString const& default_user_agent_value()
namespace IPC {
template<>
ErrorOr<void> encode(Encoder& encoder, HTTP::Header const& header)
{ {
// A default `User-Agent` value is an implementation-defined header value for the `User-Agent` header. TRY(encoder.encode(header.name));
static auto user_agent = ResourceLoader::the().user_agent().to_byte_string(); TRY(encoder.encode(header.value));
return user_agent; return {};
}
template<>
ErrorOr<HTTP::Header> decode(Decoder& decoder)
{
auto name = TRY(decoder.decode<ByteString>());
auto value = TRY(decoder.decode<ByteString>());
return HTTP::Header { move(name), move(value) };
} }
} }

View file

@ -1,4 +1,5 @@
/* /*
* Copyright (c) 2022-2023, Linus Groh <linusg@serenityos.org>
* Copyright (c) 2024, Andreas Kling <andreas@ladybird.org> * Copyright (c) 2024, Andreas Kling <andreas@ladybird.org>
* *
* SPDX-License-Identifier: BSD-2-Clause * SPDX-License-Identifier: BSD-2-Clause
@ -7,34 +8,47 @@
#pragma once #pragma once
#include <AK/ByteString.h> #include <AK/ByteString.h>
#include <LibIPC/Decoder.h> #include <AK/StringView.h>
#include <LibIPC/Encoder.h> #include <LibIPC/Forward.h>
namespace HTTP { namespace HTTP {
// https://fetch.spec.whatwg.org/#concept-header
struct Header { struct Header {
[[nodiscard]] static Header isomorphic_encode(StringView, StringView);
Optional<Vector<ByteString>> extract_header_values() const;
ByteString name; ByteString name;
ByteString value; ByteString value;
}; };
[[nodiscard]] bool is_header_name(StringView);
[[nodiscard]] bool is_header_value(StringView);
[[nodiscard]] ByteString normalize_header_value(StringView);
[[nodiscard]] bool is_forbidden_request_header(Header const&);
[[nodiscard]] bool is_forbidden_response_header_name(StringView);
[[nodiscard]] Vector<String> get_decode_and_split_header_value(StringView value);
[[nodiscard]] Vector<ByteString> convert_header_names_to_a_sorted_lowercase_set(ReadonlySpan<ByteString> header_names);
struct RangeHeaderValue {
Optional<u64> start;
Optional<u64> end;
};
[[nodiscard]] ByteString build_content_range(u64 range_start, u64 range_end, u64 full_length);
Optional<RangeHeaderValue> parse_single_range_header_value(StringView, bool allow_whitespace);
} }
namespace IPC { namespace IPC {
template<> template<>
inline ErrorOr<void> encode(Encoder& encoder, HTTP::Header const& header) ErrorOr<void> encode(Encoder& encoder, HTTP::Header const& header);
{
TRY(encoder.encode(header.name));
TRY(encoder.encode(header.value));
return {};
}
template<> template<>
inline ErrorOr<HTTP::Header> decode(Decoder& decoder) ErrorOr<HTTP::Header> decode(Decoder& decoder);
{
auto name = TRY(decoder.decode<ByteString>());
auto value = TRY(decoder.decode<ByteString>());
return HTTP::Header { move(name), move(value) };
}
} }

View file

@ -0,0 +1,276 @@
/*
* Copyright (c) 2022-2023, Linus Groh <linusg@serenityos.org>
* Copyright (c) 2022, Kenneth Myhra <kennethmyhra@serenityos.org>
* Copyright (c) 2022, Luke Wilde <lukew@serenityos.org>
*
* SPDX-License-Identifier: BSD-2-Clause
*/
#include <AK/StringUtils.h>
#include <LibHTTP/HeaderList.h>
namespace HTTP {
NonnullRefPtr<HeaderList> HeaderList::create(Vector<Header> headers)
{
return adopt_ref(*new HeaderList { move(headers) });
}
HeaderList::HeaderList(Vector<Header> headers)
: m_headers(move(headers))
{
}
// https://fetch.spec.whatwg.org/#header-list-contains
bool HeaderList::contains(StringView name) const
{
// A header list list contains a header name name if list contains a header whose name is a byte-case-insensitive
// match for name.
return any_of(m_headers, [&](auto const& header) {
return header.name.equals_ignoring_ascii_case(name);
});
}
// https://fetch.spec.whatwg.org/#concept-header-list-get
Optional<ByteString> HeaderList::get(StringView name) const
{
// 1. If list does not contain name, then return null.
if (!contains(name))
return {};
// 2. Return the values of all headers in list whose name is a byte-case-insensitive match for name, separated from
// each other by 0x2C 0x20, in order.
StringBuilder builder;
bool first = true;
for (auto const& header : *this) {
if (!header.name.equals_ignoring_ascii_case(name))
continue;
if (!first)
builder.append(", "sv);
builder.append(header.value);
first = false;
}
return builder.to_byte_string();
}
// https://fetch.spec.whatwg.org/#concept-header-list-get-decode-split
Optional<Vector<String>> HeaderList::get_decode_and_split(StringView name) const
{
// 1. Let value be the result of getting name from list.
auto value = get(name);
// 2. If value is null, then return null.
if (!value.has_value())
return {};
// 3. Return the result of getting, decoding, and splitting value.
return get_decode_and_split_header_value(*value);
}
// https://fetch.spec.whatwg.org/#concept-header-list-append
void HeaderList::append(Header header)
{
// 1. If list contains name, then set name to the first such headers name.
// NOTE: This reuses the casing of the name of the header already in list, if any. If there are multiple matched
// headers their names will all be identical.
auto matching_header = m_headers.first_matching([&](auto const& existing_header) {
return existing_header.name.equals_ignoring_ascii_case(header.name);
});
if (matching_header.has_value())
header.name = matching_header->name;
// 2. Append (name, value) to list.
m_headers.append(move(header));
}
// https://fetch.spec.whatwg.org/#concept-header-list-delete
void HeaderList::delete_(StringView name)
{
// To delete a header name name from a header list list, remove all headers whose name is a byte-case-insensitive
// match for name from list.
m_headers.remove_all_matching([&](auto const& header) {
return header.name.equals_ignoring_ascii_case(name);
});
}
// https://fetch.spec.whatwg.org/#concept-header-list-set
void HeaderList::set(Header header)
{
// 1. If list contains name, then set the value of the first such header to value and remove the others.
auto it = m_headers.find_if([&](auto const& existing_header) {
return existing_header.name.equals_ignoring_ascii_case(header.name);
});
if (it != m_headers.end()) {
it->value = move(header.value);
size_t i = 0;
m_headers.remove_all_matching([&](auto const& existing_header) {
if (i++ <= it.index())
return false;
return existing_header.name.equals_ignoring_ascii_case(it->name);
});
}
// 2. Otherwise, append header (name, value) to list.
else {
append(move(header));
}
}
// https://fetch.spec.whatwg.org/#concept-header-list-combine
void HeaderList::combine(Header header)
{
// 1. If list contains name, then set the value of the first such header to its value, followed by 0x2C 0x20,
// followed by value.
auto matching_header = m_headers.first_matching([&](auto const& existing_header) {
return existing_header.name.equals_ignoring_ascii_case(header.name);
});
if (matching_header.has_value()) {
matching_header->value = ByteString::formatted("{}, {}", matching_header->value, header.value);
}
// 2. Otherwise, append (name, value) to list.
else {
append(move(header));
}
}
// https://fetch.spec.whatwg.org/#concept-header-list-sort-and-combine
Vector<Header> HeaderList::sort_and_combine() const
{
// 1. Let headers be an empty list of headers with the key being the name and value the value.
Vector<Header> headers;
// 2. Let names be the result of convert header names to a sorted-lowercase set with all the names of the headers
// in list.
Vector<ByteString> names_list;
names_list.ensure_capacity(m_headers.size());
for (auto const& header : *this)
names_list.unchecked_append(header.name);
auto names = convert_header_names_to_a_sorted_lowercase_set(names_list);
// 3. For each name of names:
for (auto& name : names) {
// 1. If name is `set-cookie`, then:
if (name == "set-cookie"sv) {
// 1. Let values be a list of all values of headers in list whose name is a byte-case-insensitive match for
// name, in order.
// 2. For each value of values:
for (auto const& [header_name, value] : *this) {
if (header_name.equals_ignoring_ascii_case(name)) {
// 1. Append (name, value) to headers.
headers.empend(name, value);
}
}
}
// 2. Otherwise:
else {
// 1. Let value be the result of getting name from list.
auto value = get(name);
// 2. Assert: value is not null.
VERIFY(value.has_value());
// 3. Append (name, value) to headers.
headers.empend(move(name), value.release_value());
}
}
// 4. Return headers.
return headers;
}
// https://fetch.spec.whatwg.org/#extract-header-list-values
Variant<Empty, Vector<ByteString>, HeaderList::ExtractHeaderParseFailure> HeaderList::extract_header_list_values(StringView name) const
{
// 1. If list does not contain name, then return null.
if (!contains(name))
return Empty {};
// FIXME: 2. If the ABNF for name allows a single header and list contains more than one, then return failure.
// NOTE: If different error handling is needed, extract the desired header first.
// 3. Let values be an empty list.
Vector<ByteString> values;
// 4. For each header header list contains whose name is name:
for (auto const& header : m_headers) {
if (!header.name.equals_ignoring_ascii_case(name))
continue;
// 1. Let extract be the result of extracting header values from header.
auto extract = header.extract_header_values();
// 2. If extract is failure, then return failure.
if (!extract.has_value())
return ExtractHeaderParseFailure {};
// 3. Append each value in extract, in order, to values.
values.extend(extract.release_value());
}
// 5. Return values.
return values;
}
// https://fetch.spec.whatwg.org/#header-list-extract-a-length
Variant<Empty, u64, HeaderList::ExtractLengthFailure> HeaderList::extract_length() const
{
// 1. Let values be the result of getting, decoding, and splitting `Content-Length` from headers.
auto values = get_decode_and_split("Content-Length"sv);
// 2. If values is null, then return null.
if (!values.has_value())
return {};
// 3. Let candidateValue be null.
Optional<String> candidate_value;
// 4. For each value of values:
for (auto const& value : *values) {
// 1. If candidateValue is null, then set candidateValue to value.
if (!candidate_value.has_value()) {
candidate_value = value;
}
// 2. Otherwise, if value is not candidateValue, return failure.
else if (candidate_value.value() != value) {
return ExtractLengthFailure {};
}
}
// 5. If candidateValue is the empty string or has a code point that is not an ASCII digit, then return null.
// 6. Return candidateValue, interpreted as decimal number.
// FIXME: This will return an empty Optional if it cannot fit into a u64, is this correct?
auto result = candidate_value->to_number<u64>(TrimWhitespace::No);
if (!result.has_value())
return {};
return *result;
}
// Non-standard
Vector<ByteString> HeaderList::unique_names() const
{
HashTable<StringView, CaseInsensitiveStringTraits> header_names_seen;
Vector<ByteString> header_names;
for (auto const& header : m_headers) {
if (header_names_seen.contains(header.name))
continue;
header_names_seen.set(header.name);
header_names.append(header.name);
}
return header_names;
}
}

View file

@ -0,0 +1,61 @@
/*
* Copyright (c) 2022-2023, Linus Groh <linusg@serenityos.org>
* Copyright (c) 2024, Andreas Kling <andreas@ladybird.org>
*
* SPDX-License-Identifier: BSD-2-Clause
*/
#pragma once
#include <AK/ByteString.h>
#include <AK/HashTable.h>
#include <AK/NonnullRefPtr.h>
#include <AK/Optional.h>
#include <AK/RefCounted.h>
#include <AK/String.h>
#include <AK/Vector.h>
#include <LibHTTP/Header.h>
namespace HTTP {
// https://fetch.spec.whatwg.org/#concept-header-list
class HeaderList final : public RefCounted<HeaderList> {
public:
static NonnullRefPtr<HeaderList> create(Vector<Header> = {});
Vector<Header> const& headers() const { return m_headers; }
[[nodiscard]] bool is_empty() const { return m_headers.is_empty(); }
[[nodiscard]] auto begin() { return m_headers.begin(); }
[[nodiscard]] auto begin() const { return m_headers.begin(); }
[[nodiscard]] auto end() { return m_headers.end(); }
[[nodiscard]] auto end() const { return m_headers.end(); }
void clear() { m_headers.clear(); }
[[nodiscard]] bool contains(StringView) const;
Optional<ByteString> get(StringView) const;
Optional<Vector<String>> get_decode_and_split(StringView) const;
void append(Header);
void delete_(StringView name);
void set(Header);
void combine(Header);
[[nodiscard]] Vector<Header> sort_and_combine() const;
struct ExtractHeaderParseFailure { };
[[nodiscard]] Variant<Empty, Vector<ByteString>, ExtractHeaderParseFailure> extract_header_list_values(StringView) const;
struct ExtractLengthFailure { };
[[nodiscard]] Variant<Empty, u64, ExtractLengthFailure> extract_length() const;
[[nodiscard]] Vector<ByteString> unique_names() const;
private:
explicit HeaderList(Vector<Header>);
Vector<Header> m_headers;
};
}

View file

@ -1,84 +0,0 @@
/*
* Copyright (c) 2024, Andreas Kling <andreas@ladybird.org>
*
* SPDX-License-Identifier: BSD-2-Clause
*/
#pragma once
#include <LibHTTP/Header.h>
namespace HTTP {
class HeaderMap {
public:
HeaderMap() = default;
~HeaderMap() = default;
HeaderMap(Vector<Header> headers)
: m_headers(move(headers))
{
for (auto& header : m_headers)
m_map.set(header.name, header.value);
}
HeaderMap(HeaderMap const&) = default;
HeaderMap(HeaderMap&&) = default;
HeaderMap& operator=(HeaderMap const&) = default;
HeaderMap& operator=(HeaderMap&&) = default;
void set(ByteString name, ByteString value)
{
m_map.set(name, value);
m_headers.append({ move(name), move(value) });
}
void remove(StringView name)
{
if (m_map.remove(name)) {
m_headers.remove_all_matching([&](Header const& header) {
return header.name.equals_ignoring_ascii_case(name);
});
}
}
[[nodiscard]] bool contains(ByteString const& name) const
{
return m_map.contains(name);
}
[[nodiscard]] Optional<ByteString const&> get(ByteString const& name) const
{
return m_map.get(name);
}
[[nodiscard]] Vector<Header> const& headers() const
{
return m_headers;
}
private:
HashMap<ByteString, ByteString, CaseInsensitiveStringTraits> m_map;
Vector<Header> m_headers;
};
}
namespace IPC {
template<>
inline ErrorOr<void> encode(Encoder& encoder, HTTP::HeaderMap const& header_map)
{
TRY(encoder.encode(header_map.headers()));
return {};
}
template<>
inline ErrorOr<HTTP::HeaderMap> decode(Decoder& decoder)
{
auto headers = TRY(decoder.decode<Vector<HTTP::Header>>());
return HTTP::HeaderMap { move(headers) };
}
}

View file

@ -11,6 +11,11 @@
namespace HTTP { namespace HTTP {
HttpRequest::HttpRequest(NonnullRefPtr<HeaderList> headers)
: m_headers(move(headers))
{
}
StringView to_string_view(HttpRequest::Method method) StringView to_string_view(HttpRequest::Method method)
{ {
switch (method) { switch (method) {
@ -60,8 +65,8 @@ ErrorOr<ByteBuffer> HttpRequest::to_raw_request() const
TRY(builder.try_appendff(":{}", *m_url.port())); TRY(builder.try_appendff(":{}", *m_url.port()));
TRY(builder.try_append("\r\n"sv)); TRY(builder.try_append("\r\n"sv));
// Start headers. // Start headers.
bool has_content_length = m_headers.contains("Content-Length"sv); bool has_content_length = m_headers->contains("Content-Length"sv);
for (auto const& [name, value] : m_headers.headers()) { for (auto const& [name, value] : *m_headers) {
TRY(builder.try_append(name)); TRY(builder.try_append(name));
TRY(builder.try_append(": "sv)); TRY(builder.try_append(": "sv));
TRY(builder.try_append(value)); TRY(builder.try_append(value));
@ -113,7 +118,7 @@ ErrorOr<HttpRequest, HttpRequest::ParseError> HttpRequest::from_raw_request(Read
ByteString method; ByteString method;
ByteString resource; ByteString resource;
ByteString protocol; ByteString protocol;
HeaderMap headers; auto headers = HeaderList::create();
Header current_header; Header current_header;
ByteBuffer body; ByteBuffer body;
@ -180,7 +185,7 @@ ErrorOr<HttpRequest, HttpRequest::ParseError> HttpRequest::from_raw_request(Read
if (current_header.name.equals_ignoring_ascii_case("Content-Length"sv)) if (current_header.name.equals_ignoring_ascii_case("Content-Length"sv))
content_length = current_header.value.to_number<unsigned>(); content_length = current_header.value.to_number<unsigned>();
headers.set(move(current_header.name), move(current_header.value)); headers->append({ move(current_header.name), move(current_header.value) });
break; break;
} }
buffer.append(consume()); buffer.append(consume());
@ -207,7 +212,7 @@ ErrorOr<HttpRequest, HttpRequest::ParseError> HttpRequest::from_raw_request(Read
if (content_length.has_value() && content_length.value() != body.size()) if (content_length.has_value() && content_length.value() != body.size())
return ParseError::RequestIncomplete; return ParseError::RequestIncomplete;
HttpRequest request; HttpRequest request { move(headers) };
if (method == "GET") if (method == "GET")
request.m_method = Method::GET; request.m_method = Method::GET;
else if (method == "HEAD") else if (method == "HEAD")
@ -229,7 +234,6 @@ ErrorOr<HttpRequest, HttpRequest::ParseError> HttpRequest::from_raw_request(Read
else else
return ParseError::UnsupportedMethod; return ParseError::UnsupportedMethod;
request.m_headers = move(headers);
auto url_parts = resource.split_limit('?', 2, SplitBehavior::KeepEmpty); auto url_parts = resource.split_limit('?', 2, SplitBehavior::KeepEmpty);
auto url_part_to_string = [](ByteString const& url_part) -> ErrorOr<String, ParseError> { auto url_part_to_string = [](ByteString const& url_part) -> ErrorOr<String, ParseError> {
@ -258,11 +262,6 @@ ErrorOr<HttpRequest, HttpRequest::ParseError> HttpRequest::from_raw_request(Read
return request; return request;
} }
void HttpRequest::set_headers(HTTP::HeaderMap headers)
{
m_headers = move(headers);
}
Optional<Header> HttpRequest::get_http_basic_authentication_header(URL::URL const& url) Optional<Header> HttpRequest::get_http_basic_authentication_header(URL::URL const& url)
{ {
if (!url.includes_credentials()) if (!url.includes_credentials())

View file

@ -12,7 +12,7 @@
#include <AK/Noncopyable.h> #include <AK/Noncopyable.h>
#include <AK/Optional.h> #include <AK/Optional.h>
#include <LibCore/Forward.h> #include <LibCore/Forward.h>
#include <LibHTTP/HeaderMap.h> #include <LibHTTP/HeaderList.h>
#include <LibURL/URL.h> #include <LibURL/URL.h>
namespace HTTP { namespace HTTP {
@ -61,13 +61,13 @@ public:
ByteString password; ByteString password;
}; };
HttpRequest() = default; explicit HttpRequest(NonnullRefPtr<HeaderList>);
~HttpRequest() = default; ~HttpRequest() = default;
AK_MAKE_DEFAULT_MOVABLE(HttpRequest); AK_MAKE_DEFAULT_MOVABLE(HttpRequest);
ByteString const& resource() const { return m_resource; } ByteString const& resource() const { return m_resource; }
HeaderMap const& headers() const { return m_headers; } HeaderList const& headers() const { return m_headers; }
URL::URL const& url() const { return m_url; } URL::URL const& url() const { return m_url; }
void set_url(URL::URL const& url) { m_url = url; } void set_url(URL::URL const& url) { m_url = url; }
@ -81,8 +81,6 @@ public:
StringView method_name() const; StringView method_name() const;
ErrorOr<ByteBuffer> to_raw_request() const; ErrorOr<ByteBuffer> to_raw_request() const;
void set_headers(HeaderMap);
static ErrorOr<HttpRequest, HttpRequest::ParseError> from_raw_request(ReadonlyBytes); static ErrorOr<HttpRequest, HttpRequest::ParseError> from_raw_request(ReadonlyBytes);
static Optional<Header> get_http_basic_authentication_header(URL::URL const&); static Optional<Header> get_http_basic_authentication_header(URL::URL const&);
static Optional<BasicAuthenticationCredentials> parse_http_basic_authentication_header(ByteString const&); static Optional<BasicAuthenticationCredentials> parse_http_basic_authentication_header(ByteString const&);
@ -91,7 +89,7 @@ private:
URL::URL m_url; URL::URL m_url;
ByteString m_resource; ByteString m_resource;
Method m_method { GET }; Method m_method { GET };
HeaderMap m_headers; NonnullRefPtr<HeaderList> m_headers;
ByteBuffer m_body; ByteBuffer m_body;
}; };

View file

@ -5,10 +5,10 @@
* SPDX-License-Identifier: BSD-2-Clause * SPDX-License-Identifier: BSD-2-Clause
*/ */
#include <LibHTTP/Method.h>
#include <LibRegex/Regex.h> #include <LibRegex/Regex.h>
#include <LibWeb/Fetch/Infrastructure/HTTP/Methods.h>
namespace Web::Fetch::Infrastructure { namespace HTTP {
// https://fetch.spec.whatwg.org/#concept-method // https://fetch.spec.whatwg.org/#concept-method
bool is_method(StringView method) bool is_method(StringView method)

View file

@ -8,12 +8,11 @@
#include <AK/ByteString.h> #include <AK/ByteString.h>
#include <AK/StringView.h> #include <AK/StringView.h>
#include <LibWeb/Export.h>
namespace Web::Fetch::Infrastructure { namespace HTTP {
[[nodiscard]] bool is_method(StringView); [[nodiscard]] bool is_method(StringView);
[[nodiscard]] WEB_API bool is_cors_safelisted_method(StringView); [[nodiscard]] bool is_cors_safelisted_method(StringView);
[[nodiscard]] bool is_forbidden_method(StringView); [[nodiscard]] bool is_forbidden_method(StringView);
[[nodiscard]] ByteString normalize_method(StringView); [[nodiscard]] ByteString normalize_method(StringView);

View file

@ -12,4 +12,4 @@ set(GENERATED_SOURCES
) )
ladybird_lib(LibRequests requests) ladybird_lib(LibRequests requests)
target_link_libraries(LibRequests PRIVATE LibCore LibIPC) target_link_libraries(LibRequests PRIVATE LibCore LibHTTP LibIPC)

View file

@ -65,8 +65,8 @@ void Request::set_buffered_request_finished_callback(BufferedRequestFinished on_
m_internal_buffered_data = make<InternalBufferedData>(); m_internal_buffered_data = make<InternalBufferedData>();
on_headers_received = [this](auto& headers, auto response_code, auto const& reason_phrase) { on_headers_received = [this](auto headers, auto response_code, auto const& reason_phrase) {
m_internal_buffered_data->response_headers = headers; m_internal_buffered_data->response_headers = move(headers);
m_internal_buffered_data->response_code = move(response_code); m_internal_buffered_data->response_code = move(response_code);
m_internal_buffered_data->reason_phrase = reason_phrase; m_internal_buffered_data->reason_phrase = reason_phrase;
}; };
@ -108,10 +108,10 @@ void Request::did_finish(Badge<RequestClient>, u64 total_size, RequestTimingInfo
on_finish(total_size, timing_info, network_error); on_finish(total_size, timing_info, network_error);
} }
void Request::did_receive_headers(Badge<RequestClient>, HTTP::HeaderMap const& response_headers, Optional<u32> response_code, Optional<String> const& reason_phrase) void Request::did_receive_headers(Badge<RequestClient>, NonnullRefPtr<HTTP::HeaderList> response_headers, Optional<u32> response_code, Optional<String> const& reason_phrase)
{ {
if (on_headers_received) if (on_headers_received)
on_headers_received(response_headers, response_code, reason_phrase); on_headers_received(move(response_headers), response_code, reason_phrase);
} }
void Request::did_request_certificates(Badge<RequestClient>) void Request::did_request_certificates(Badge<RequestClient>)
@ -187,4 +187,9 @@ void Request::set_up_internal_stream_data(DataReceived on_data_available)
}; };
} }
Request::InternalBufferedData::InternalBufferedData()
: response_headers(HTTP::HeaderList::create())
{
}
} }

View file

@ -13,7 +13,7 @@
#include <AK/RefCounted.h> #include <AK/RefCounted.h>
#include <AK/WeakPtr.h> #include <AK/WeakPtr.h>
#include <LibCore/Notifier.h> #include <LibCore/Notifier.h>
#include <LibHTTP/HeaderMap.h> #include <LibHTTP/HeaderList.h>
#include <LibRequests/NetworkError.h> #include <LibRequests/NetworkError.h>
#include <LibRequests/RequestTimingInfo.h> #include <LibRequests/RequestTimingInfo.h>
@ -58,13 +58,13 @@ public:
int fd() const { return m_fd; } int fd() const { return m_fd; }
bool stop(); bool stop();
using BufferedRequestFinished = Function<void(u64 total_size, RequestTimingInfo const& timing_info, Optional<NetworkError> const& network_error, HTTP::HeaderMap const& response_headers, Optional<u32> response_code, Optional<String> reason_phrase, ReadonlyBytes payload)>; using BufferedRequestFinished = Function<void(u64 total_size, RequestTimingInfo const& timing_info, Optional<NetworkError> const& network_error, NonnullRefPtr<HTTP::HeaderList> response_headers, Optional<u32> response_code, Optional<String> reason_phrase, ReadonlyBytes payload)>;
// Configure the request such that the entirety of the response data is buffered. The callback receives that data and // Configure the request such that the entirety of the response data is buffered. The callback receives that data and
// the response headers all at once. Using this method is mutually exclusive with `set_unbuffered_data_received_callback`. // the response headers all at once. Using this method is mutually exclusive with `set_unbuffered_data_received_callback`.
void set_buffered_request_finished_callback(BufferedRequestFinished); void set_buffered_request_finished_callback(BufferedRequestFinished);
using HeadersReceived = Function<void(HTTP::HeaderMap const& response_headers, Optional<u32> response_code, Optional<String> const& reason_phrase)>; using HeadersReceived = Function<void(NonnullRefPtr<HTTP::HeaderList> response_headers, Optional<u32> response_code, Optional<String> const& reason_phrase)>;
using DataReceived = Function<void(ReadonlyBytes data)>; using DataReceived = Function<void(ReadonlyBytes data)>;
using RequestFinished = Function<void(u64 total_size, RequestTimingInfo const& timing_info, Optional<NetworkError> network_error)>; using RequestFinished = Function<void(u64 total_size, RequestTimingInfo const& timing_info, Optional<NetworkError> network_error)>;
@ -75,7 +75,7 @@ public:
Function<CertificateAndKey()> on_certificate_requested; Function<CertificateAndKey()> on_certificate_requested;
void did_finish(Badge<RequestClient>, u64 total_size, RequestTimingInfo const& timing_info, Optional<NetworkError> const& network_error); void did_finish(Badge<RequestClient>, u64 total_size, RequestTimingInfo const& timing_info, Optional<NetworkError> const& network_error);
void did_receive_headers(Badge<RequestClient>, HTTP::HeaderMap const& response_headers, Optional<u32> response_code, Optional<String> const& reason_phrase); void did_receive_headers(Badge<RequestClient>, NonnullRefPtr<HTTP::HeaderList> response_headers, Optional<u32> response_code, Optional<String> const& reason_phrase);
void did_request_certificates(Badge<RequestClient>); void did_request_certificates(Badge<RequestClient>);
RefPtr<Core::Notifier>& write_notifier(Badge<RequestClient>) { return m_write_notifier; } RefPtr<Core::Notifier>& write_notifier(Badge<RequestClient>) { return m_write_notifier; }
@ -102,8 +102,10 @@ private:
RequestFinished on_finish; RequestFinished on_finish;
struct InternalBufferedData { struct InternalBufferedData {
InternalBufferedData();
AllocatingMemoryStream payload_stream; AllocatingMemoryStream payload_stream;
HTTP::HeaderMap response_headers; NonnullRefPtr<HTTP::HeaderList> response_headers;
Optional<u32> response_code; Optional<u32> response_code;
Optional<String> reason_phrase; Optional<String> reason_phrase;
}; };

View file

@ -37,7 +37,7 @@ void RequestClient::ensure_connection(URL::URL const& url, ::RequestServer::Cach
async_ensure_connection(url, cache_level); async_ensure_connection(url, cache_level);
} }
RefPtr<Request> RequestClient::start_request(ByteString const& method, URL::URL const& url, HTTP::HeaderMap const& request_headers, ReadonlyBytes request_body, Core::ProxyData const& proxy_data) RefPtr<Request> RequestClient::start_request(ByteString const& method, URL::URL const& url, Optional<HTTP::HeaderList const&> request_headers, ReadonlyBytes request_body, Core::ProxyData const& proxy_data)
{ {
auto body_result = ByteBuffer::copy(request_body); auto body_result = ByteBuffer::copy(request_body);
if (body_result.is_error()) if (body_result.is_error())
@ -46,7 +46,9 @@ RefPtr<Request> RequestClient::start_request(ByteString const& method, URL::URL
static i32 s_next_request_id = 0; static i32 s_next_request_id = 0;
auto request_id = s_next_request_id++; auto request_id = s_next_request_id++;
IPCProxy::async_start_request(request_id, method, url, request_headers, body_result.release_value(), proxy_data); auto headers = request_headers.map([](auto const& headers) { return headers.headers().span(); }).value_or({});
IPCProxy::async_start_request(request_id, method, url, headers, body_result.release_value(), proxy_data);
auto request = Request::create_from_id({}, *this, request_id); auto request = Request::create_from_id({}, *this, request_id);
m_requests.set(request_id, request); m_requests.set(request_id, request);
return request; return request;
@ -105,14 +107,14 @@ void RequestClient::request_finished(i32 request_id, u64 total_size, RequestTimi
m_requests.remove(request_id); m_requests.remove(request_id);
} }
void RequestClient::headers_became_available(i32 request_id, HTTP::HeaderMap response_headers, Optional<u32> status_code, Optional<String> reason_phrase) void RequestClient::headers_became_available(i32 request_id, Vector<HTTP::Header> response_headers, Optional<u32> status_code, Optional<String> reason_phrase)
{ {
auto request = const_cast<Request*>(m_requests.get(request_id).value_or(nullptr)); auto request = const_cast<Request*>(m_requests.get(request_id).value_or(nullptr));
if (!request) { if (!request) {
warnln("Received headers for non-existent request {}", request_id); warnln("Received headers for non-existent request {}", request_id);
return; return;
} }
request->did_receive_headers({}, response_headers, status_code, reason_phrase); request->did_receive_headers({}, HTTP::HeaderList::create(move(response_headers)), status_code, reason_phrase);
} }
void RequestClient::certificate_requested(i32 request_id) void RequestClient::certificate_requested(i32 request_id)
@ -122,10 +124,10 @@ void RequestClient::certificate_requested(i32 request_id)
} }
} }
RefPtr<WebSocket> RequestClient::websocket_connect(URL::URL const& url, ByteString const& origin, Vector<ByteString> const& protocols, Vector<ByteString> const& extensions, HTTP::HeaderMap const& request_headers) RefPtr<WebSocket> RequestClient::websocket_connect(URL::URL const& url, ByteString const& origin, Vector<ByteString> const& protocols, Vector<ByteString> const& extensions, HTTP::HeaderList const& request_headers)
{ {
auto websocket_id = m_next_websocket_id++; auto websocket_id = m_next_websocket_id++;
IPCProxy::async_websocket_connect(websocket_id, url, origin, protocols, extensions, request_headers); IPCProxy::async_websocket_connect(websocket_id, url, origin, protocols, extensions, request_headers.headers());
auto connection = WebSocket::create_from_id({}, *this, websocket_id); auto connection = WebSocket::create_from_id({}, *this, websocket_id);
m_websockets.set(websocket_id, connection); m_websockets.set(websocket_id, connection);
return connection; return connection;

View file

@ -7,7 +7,7 @@
#pragma once #pragma once
#include <AK/HashMap.h> #include <AK/HashMap.h>
#include <LibHTTP/HeaderMap.h> #include <LibHTTP/HeaderList.h>
#include <LibIPC/ConnectionToServer.h> #include <LibIPC/ConnectionToServer.h>
#include <LibRequests/CacheSizes.h> #include <LibRequests/CacheSizes.h>
#include <LibRequests/RequestTimingInfo.h> #include <LibRequests/RequestTimingInfo.h>
@ -31,9 +31,9 @@ public:
explicit RequestClient(NonnullOwnPtr<IPC::Transport>); explicit RequestClient(NonnullOwnPtr<IPC::Transport>);
virtual ~RequestClient() override; virtual ~RequestClient() override;
RefPtr<Request> start_request(ByteString const& method, URL::URL const&, HTTP::HeaderMap const& request_headers = {}, ReadonlyBytes request_body = {}, Core::ProxyData const& = {}); RefPtr<Request> start_request(ByteString const& method, URL::URL const&, Optional<HTTP::HeaderList const&> request_headers = {}, ReadonlyBytes request_body = {}, Core::ProxyData const& = {});
RefPtr<WebSocket> websocket_connect(URL::URL const&, ByteString const& origin = {}, Vector<ByteString> const& protocols = {}, Vector<ByteString> const& extensions = {}, HTTP::HeaderMap const& request_headers = {}); RefPtr<WebSocket> websocket_connect(URL::URL const&, ByteString const& origin, Vector<ByteString> const& protocols, Vector<ByteString> const& extensions, HTTP::HeaderList const& request_headers);
void ensure_connection(URL::URL const&, ::RequestServer::CacheLevel); void ensure_connection(URL::URL const&, ::RequestServer::CacheLevel);
@ -50,7 +50,7 @@ private:
virtual void request_started(i32, IPC::File) override; virtual void request_started(i32, IPC::File) override;
virtual void request_finished(i32, u64, RequestTimingInfo, Optional<NetworkError>) override; virtual void request_finished(i32, u64, RequestTimingInfo, Optional<NetworkError>) override;
virtual void certificate_requested(i32) override; virtual void certificate_requested(i32) override;
virtual void headers_became_available(i32, HTTP::HeaderMap, Optional<u32>, Optional<String>) override; virtual void headers_became_available(i32, Vector<HTTP::Header>, Optional<u32>, Optional<String>) override;
virtual void websocket_connected(i64 websocket_id) override; virtual void websocket_connected(i64 websocket_id) override;
virtual void websocket_received(i64 websocket_id, bool, ByteBuffer) override; virtual void websocket_received(i64 websocket_id, bool, ByteBuffer) override;

View file

@ -380,8 +380,6 @@ set(SOURCES
Fetch/Infrastructure/HTTP.cpp Fetch/Infrastructure/HTTP.cpp
Fetch/Infrastructure/HTTP/Bodies.cpp Fetch/Infrastructure/HTTP/Bodies.cpp
Fetch/Infrastructure/HTTP/CORS.cpp Fetch/Infrastructure/HTTP/CORS.cpp
Fetch/Infrastructure/HTTP/Headers.cpp
Fetch/Infrastructure/HTTP/Methods.cpp
Fetch/Infrastructure/HTTP/MIME.cpp Fetch/Infrastructure/HTTP/MIME.cpp
Fetch/Infrastructure/HTTP/Requests.cpp Fetch/Infrastructure/HTTP/Requests.cpp
Fetch/Infrastructure/HTTP/Responses.cpp Fetch/Infrastructure/HTTP/Responses.cpp

View file

@ -12,7 +12,6 @@
#include <LibWeb/ContentSecurityPolicy/Policy.h> #include <LibWeb/ContentSecurityPolicy/Policy.h>
#include <LibWeb/ContentSecurityPolicy/PolicyList.h> #include <LibWeb/ContentSecurityPolicy/PolicyList.h>
#include <LibWeb/ContentSecurityPolicy/SerializedPolicy.h> #include <LibWeb/ContentSecurityPolicy/SerializedPolicy.h>
#include <LibWeb/Fetch/Infrastructure/HTTP/Headers.h>
#include <LibWeb/Fetch/Infrastructure/HTTP/Responses.h> #include <LibWeb/Fetch/Infrastructure/HTTP/Responses.h>
#include <LibWeb/Infra/CharacterTypes.h> #include <LibWeb/Infra/CharacterTypes.h>

View file

@ -447,9 +447,9 @@ void Violation::report_a_violation(JS::Realm& realm)
// header list // header list
// A header list containing a single header whose name is "Content-Type", and value is // A header list containing a single header whose name is "Content-Type", and value is
// "application/csp-report" // "application/csp-report"
auto header_list = Fetch::Infrastructure::HeaderList::create(vm); auto header_list = HTTP::HeaderList::create();
header_list->append({ "Content-Type"sv, "application/csp-report"sv }); header_list->append({ "Content-Type"sv, "application/csp-report"sv });
request->set_header_list(header_list); request->set_header_list(move(header_list));
// body // body
// The result of executing § 5.3 Obtain the deprecated serialization of violation on // The result of executing § 5.3 Obtain the deprecated serialization of violation on

View file

@ -7,6 +7,8 @@
#include <AK/GenericLexer.h> #include <AK/GenericLexer.h>
#include <AK/TypeCasts.h> #include <AK/TypeCasts.h>
#include <LibHTTP/HTTP.h>
#include <LibHTTP/HeaderList.h>
#include <LibJS/Runtime/ArrayBuffer.h> #include <LibJS/Runtime/ArrayBuffer.h>
#include <LibJS/Runtime/Completion.h> #include <LibJS/Runtime/Completion.h>
#include <LibJS/Runtime/Error.h> #include <LibJS/Runtime/Error.h>
@ -19,7 +21,6 @@
#include <LibWeb/Fetch/Body.h> #include <LibWeb/Fetch/Body.h>
#include <LibWeb/Fetch/Infrastructure/HTTP.h> #include <LibWeb/Fetch/Infrastructure/HTTP.h>
#include <LibWeb/Fetch/Infrastructure/HTTP/Bodies.h> #include <LibWeb/Fetch/Infrastructure/HTTP/Bodies.h>
#include <LibWeb/Fetch/Infrastructure/HTTP/Headers.h>
#include <LibWeb/FileAPI/Blob.h> #include <LibWeb/FileAPI/Blob.h>
#include <LibWeb/FileAPI/File.h> #include <LibWeb/FileAPI/File.h>
#include <LibWeb/HTML/Scripting/TemporaryExecutionContext.h> #include <LibWeb/HTML/Scripting/TemporaryExecutionContext.h>
@ -301,10 +302,10 @@ static MultipartParsingErrorOr<MultiPartFormDataHeader> parse_multipart_form_dat
auto header_name = lexer.consume_until(is_any_of("\n\r:"sv)); auto header_name = lexer.consume_until(is_any_of("\n\r:"sv));
// 3. Remove any HTTP tab or space bytes from the start or end of header name. // 3. Remove any HTTP tab or space bytes from the start or end of header name.
header_name = header_name.trim(Infrastructure::HTTP_TAB_OR_SPACE, TrimMode::Both); header_name = header_name.trim(HTTP::HTTP_TAB_OR_SPACE, TrimMode::Both);
// 4. If header name does not match the field-name token production, return failure. // 4. If header name does not match the field-name token production, return failure.
if (!Infrastructure::is_header_name(header_name.bytes())) if (!HTTP::is_header_name(header_name.bytes()))
return MultipartParsingError { MUST(String::formatted("Invalid header name {}", header_name)) }; return MultipartParsingError { MUST(String::formatted("Invalid header name {}", header_name)) };
// 5. If the byte at position is not 0x3A (:), return failure. // 5. If the byte at position is not 0x3A (:), return failure.
@ -313,7 +314,7 @@ static MultipartParsingErrorOr<MultiPartFormDataHeader> parse_multipart_form_dat
return MultipartParsingError { MUST(String::formatted("Expected : at position {}", lexer.tell())) }; return MultipartParsingError { MUST(String::formatted("Expected : at position {}", lexer.tell())) };
// 7. Collect a sequence of bytes that are HTTP tab or space bytes given position. (Do nothing with those bytes.) // 7. Collect a sequence of bytes that are HTTP tab or space bytes given position. (Do nothing with those bytes.)
lexer.ignore_while(Infrastructure::is_http_tab_or_space); lexer.ignore_while(HTTP::is_http_tab_or_space);
// 8. Byte-lowercase header name and switch on the result: // 8. Byte-lowercase header name and switch on the result:
// -> `content-disposition` // -> `content-disposition`
@ -346,10 +347,10 @@ static MultipartParsingErrorOr<MultiPartFormDataHeader> parse_multipart_form_dat
// -> `content-type` // -> `content-type`
else if (header_name.equals_ignoring_ascii_case("content-type"sv)) { else if (header_name.equals_ignoring_ascii_case("content-type"sv)) {
// 1. Let header value be the result of collecting a sequence of bytes that are not 0x0A (LF) or 0x0D (CR), given position. // 1. Let header value be the result of collecting a sequence of bytes that are not 0x0A (LF) or 0x0D (CR), given position.
auto header_value = lexer.consume_until(Infrastructure::is_http_newline); auto header_value = lexer.consume_until(HTTP::is_http_newline);
// 2. Remove any HTTP tab or space bytes from the end of header value. // 2. Remove any HTTP tab or space bytes from the end of header value.
header_value = header_value.trim(Infrastructure::HTTP_TAB_OR_SPACE, TrimMode::Right); header_value = header_value.trim(HTTP::HTTP_TAB_OR_SPACE, TrimMode::Right);
// 3. Set contentType to the isomorphic decoding of header value. // 3. Set contentType to the isomorphic decoding of header value.
header.content_type = TextCodec::isomorphic_decode(header_value); header.content_type = TextCodec::isomorphic_decode(header_value);
@ -357,7 +358,7 @@ static MultipartParsingErrorOr<MultiPartFormDataHeader> parse_multipart_form_dat
// -> Otherwise // -> Otherwise
else { else {
// 1. Collect a sequence of bytes that are not 0x0A (LF) or 0x0D (CR), given position. (Do nothing with those bytes.) // 1. Collect a sequence of bytes that are not 0x0A (LF) or 0x0D (CR), given position. (Do nothing with those bytes.)
lexer.ignore_until(Infrastructure::is_http_newline); lexer.ignore_until(HTTP::is_http_newline);
} }
// 9. If position does not point to a sequence of bytes starting with 0x0D 0x0A (CR LF), return failure. Otherwise, advance position by 2 (past the newline). // 9. If position does not point to a sequence of bytes starting with 0x0D 0x0A (CR LF), return failure. Otherwise, advance position by 2 (past the newline).

View file

@ -12,6 +12,7 @@
#include <AK/Base64.h> #include <AK/Base64.h>
#include <AK/Debug.h> #include <AK/Debug.h>
#include <AK/ScopeGuard.h> #include <AK/ScopeGuard.h>
#include <LibHTTP/Method.h>
#include <LibJS/Runtime/Completion.h> #include <LibJS/Runtime/Completion.h>
#include <LibRequests/RequestTimingInfo.h> #include <LibRequests/RequestTimingInfo.h>
#include <LibTextCodec/Encoder.h> #include <LibTextCodec/Encoder.h>
@ -33,9 +34,7 @@
#include <LibWeb/Fetch/Infrastructure/FetchRecord.h> #include <LibWeb/Fetch/Infrastructure/FetchRecord.h>
#include <LibWeb/Fetch/Infrastructure/FetchTimingInfo.h> #include <LibWeb/Fetch/Infrastructure/FetchTimingInfo.h>
#include <LibWeb/Fetch/Infrastructure/HTTP/CORS.h> #include <LibWeb/Fetch/Infrastructure/HTTP/CORS.h>
#include <LibWeb/Fetch/Infrastructure/HTTP/Headers.h>
#include <LibWeb/Fetch/Infrastructure/HTTP/MIME.h> #include <LibWeb/Fetch/Infrastructure/HTTP/MIME.h>
#include <LibWeb/Fetch/Infrastructure/HTTP/Methods.h>
#include <LibWeb/Fetch/Infrastructure/HTTP/Requests.h> #include <LibWeb/Fetch/Infrastructure/HTTP/Requests.h>
#include <LibWeb/Fetch/Infrastructure/HTTP/Responses.h> #include <LibWeb/Fetch/Infrastructure/HTTP/Responses.h>
#include <LibWeb/Fetch/Infrastructure/HTTP/Statuses.h> #include <LibWeb/Fetch/Infrastructure/HTTP/Statuses.h>
@ -213,7 +212,7 @@ GC::Ref<Infrastructure::FetchController> fetch(JS::Realm& realm, Infrastructure:
} }
// 4. Append (`Accept`, value) to requests header list. // 4. Append (`Accept`, value) to requests header list.
auto header = Infrastructure::Header::isomorphic_encode("Accept"sv, value); auto header = HTTP::Header::isomorphic_encode("Accept"sv, value);
request.header_list()->append(move(header)); request.header_list()->append(move(header));
} }
@ -223,7 +222,7 @@ GC::Ref<Infrastructure::FetchController> fetch(JS::Realm& realm, Infrastructure:
StringBuilder accept_language; StringBuilder accept_language;
accept_language.join(","sv, ResourceLoader::the().preferred_languages()); accept_language.join(","sv, ResourceLoader::the().preferred_languages());
auto header = Infrastructure::Header::isomorphic_encode("Accept-Language"sv, accept_language.string_view()); auto header = HTTP::Header::isomorphic_encode("Accept-Language"sv, accept_language.string_view());
request.header_list()->append(move(header)); request.header_list()->append(move(header));
} }
@ -438,7 +437,7 @@ GC::Ptr<PendingResponse> main_fetch(JS::Realm& realm, Infrastructure::FetchParam
if ( if (
request->use_cors_preflight() request->use_cors_preflight()
|| (request->unsafe_request() || (request->unsafe_request()
&& (!Infrastructure::is_cors_safelisted_method(request->method()) && (!HTTP::is_cors_safelisted_method(request->method())
|| !Infrastructure::get_cors_unsafe_header_names(request->header_list()).is_empty()))) { || !Infrastructure::get_cors_unsafe_header_names(request->header_list()).is_empty()))) {
// 1. Set requests response tainting to "cors". // 1. Set requests response tainting to "cors".
request->set_response_tainting(Infrastructure::Request::ResponseTainting::CORS); request->set_response_tainting(Infrastructure::Request::ResponseTainting::CORS);
@ -663,8 +662,7 @@ void fetch_response_handover(JS::Realm& realm, Infrastructure::FetchParams const
// The user agent may decide to expose `Server-Timing` headers to non-secure contexts requests as well. // The user agent may decide to expose `Server-Timing` headers to non-secure contexts requests as well.
auto client = fetch_params.request()->client(); auto client = fetch_params.request()->client();
if (!response.is_network_error() && client != nullptr && HTML::is_secure_context(*client)) { if (!response.is_network_error() && client != nullptr && HTML::is_secure_context(*client)) {
auto server_timing_headers = response.header_list()->get_decode_and_split("Server-Timing"sv); if (auto server_timing_headers = response.header_list()->get_decode_and_split("Server-Timing"sv); server_timing_headers.has_value())
if (server_timing_headers.has_value())
timing_info->set_server_timing_headers(server_timing_headers.release_value()); timing_info->set_server_timing_headers(server_timing_headers.release_value());
} }
@ -921,10 +919,10 @@ GC::Ref<PendingResponse> scheme_fetch(JS::Realm& realm, Infrastructure::FetchPar
response->set_body(body_with_type.body); response->set_body(body_with_type.body);
// 4. Set responses header list to « (`Content-Length`, serializedFullLength), (`Content-Type`, type) ». // 4. Set responses header list to « (`Content-Length`, serializedFullLength), (`Content-Type`, type) ».
auto content_length_header = Infrastructure::Header::isomorphic_encode("Content-Length"sv, serialized_full_length); auto content_length_header = HTTP::Header::isomorphic_encode("Content-Length"sv, serialized_full_length);
response->header_list()->append(move(content_length_header)); response->header_list()->append(move(content_length_header));
auto content_type_header = Infrastructure::Header::isomorphic_encode("Content-Type"sv, type); auto content_type_header = HTTP::Header::isomorphic_encode("Content-Type"sv, type);
response->header_list()->append(move(content_type_header)); response->header_list()->append(move(content_type_header));
} }
// 14. Otherwise: // 14. Otherwise:
@ -936,7 +934,7 @@ GC::Ref<PendingResponse> scheme_fetch(JS::Realm& realm, Infrastructure::FetchPar
auto const range_header = request->header_list()->get("Range"sv).value_or({}); auto const range_header = request->header_list()->get("Range"sv).value_or({});
// 3. Let rangeValue be the result of parsing a single range header value given rangeHeader and true. // 3. Let rangeValue be the result of parsing a single range header value given rangeHeader and true.
auto maybe_range_value = Infrastructure::parse_single_range_header_value(range_header, true); auto maybe_range_value = HTTP::parse_single_range_header_value(range_header, true);
// 4. If rangeValue is failure, then return a network error. // 4. If rangeValue is failure, then return a network error.
if (!maybe_range_value.has_value()) if (!maybe_range_value.has_value())
@ -979,7 +977,7 @@ GC::Ref<PendingResponse> scheme_fetch(JS::Realm& realm, Infrastructure::FetchPar
auto serialized_sliced_length = String::number(sliced_blob->size()); auto serialized_sliced_length = String::number(sliced_blob->size());
// 12. Let contentRange be the result of invoking build a content range given rangeStart, rangeEnd, and fullLength. // 12. Let contentRange be the result of invoking build a content range given rangeStart, rangeEnd, and fullLength.
auto content_range = Infrastructure::build_content_range(*range_start, *range_end, full_length); auto content_range = HTTP::build_content_range(*range_start, *range_end, full_length);
// 13. Set responses status to 206. // 13. Set responses status to 206.
response->set_status(206); response->set_status(206);
@ -990,15 +988,15 @@ GC::Ref<PendingResponse> scheme_fetch(JS::Realm& realm, Infrastructure::FetchPar
// 15. Set responses header list to « // 15. Set responses header list to «
// (`Content-Length`, serializedSlicedLength), // (`Content-Length`, serializedSlicedLength),
auto content_length_header = Infrastructure::Header::isomorphic_encode("Content-Length"sv, serialized_sliced_length); auto content_length_header = HTTP::Header::isomorphic_encode("Content-Length"sv, serialized_sliced_length);
response->header_list()->append(move(content_length_header)); response->header_list()->append(move(content_length_header));
// (`Content-Type`, type), // (`Content-Type`, type),
auto content_type_header = Infrastructure::Header::isomorphic_encode("Content-Type"sv, type); auto content_type_header = HTTP::Header::isomorphic_encode("Content-Type"sv, type);
response->header_list()->append(move(content_type_header)); response->header_list()->append(move(content_type_header));
// (`Content-Range`, contentRange) ». // (`Content-Range`, contentRange) ».
auto content_range_header = Infrastructure::Header::isomorphic_encode("Content-Range"sv, content_range); auto content_range_header = HTTP::Header::isomorphic_encode("Content-Range"sv, content_range);
response->header_list()->append(move(content_range_header)); response->header_list()->append(move(content_range_header));
} }
@ -1022,7 +1020,7 @@ GC::Ref<PendingResponse> scheme_fetch(JS::Realm& realm, Infrastructure::FetchPar
auto response = Infrastructure::Response::create(vm); auto response = Infrastructure::Response::create(vm);
response->set_status_message("OK"sv); response->set_status_message("OK"sv);
auto header = Infrastructure::Header::isomorphic_encode("Content-Type"sv, mime_type); auto header = HTTP::Header::isomorphic_encode("Content-Type"sv, mime_type);
response->header_list()->append(move(header)); response->header_list()->append(move(header));
response->set_body(Infrastructure::byte_sequence_as_body(realm, data_url_struct.value().body)); response->set_body(Infrastructure::byte_sequence_as_body(realm, data_url_struct.value().body));
@ -1137,7 +1135,7 @@ GC::Ref<PendingResponse> http_fetch(JS::Realm& realm, Infrastructure::FetchParam
// - There is no method cache entry match for requests method using request, and either requests // - There is no method cache entry match for requests method using request, and either requests
// method is not a CORS-safelisted method or requests use-CORS-preflight flag is set. // method is not a CORS-safelisted method or requests use-CORS-preflight flag is set.
// FIXME: We currently have no cache, so there will always be no method cache entry. // FIXME: We currently have no cache, so there will always be no method cache entry.
(!Infrastructure::is_cors_safelisted_method(request->method()) || request->use_cors_preflight()) (!HTTP::is_cors_safelisted_method(request->method()) || request->use_cors_preflight())
// - There is at least one item in the CORS-unsafe request-header names with requests header list for // - There is at least one item in the CORS-unsafe request-header names with requests header list for
// which there is no header-name cache entry match using request. // which there is no header-name cache entry match using request.
// FIXME: We currently have no cache, so there will always be no header-name cache entry. // FIXME: We currently have no cache, so there will always be no header-name cache entry.
@ -1418,7 +1416,7 @@ GC::Ptr<PendingResponse> http_redirect_fetch(JS::Realm& realm, Infrastructure::F
class CachePartition : public RefCounted<CachePartition> { class CachePartition : public RefCounted<CachePartition> {
public: public:
// https://httpwg.org/specs/rfc9111.html#constructing.responses.from.caches // https://httpwg.org/specs/rfc9111.html#constructing.responses.from.caches
GC::Ptr<Infrastructure::Response> select_response(JS::Realm& realm, URL::URL const& url, StringView method, Vector<Infrastructure::Header> const& headers, Vector<GC::Ptr<Infrastructure::Response>>& initial_set_of_stored_responses) const GC::Ptr<Infrastructure::Response> select_response(JS::Realm& realm, URL::URL const& url, StringView method, HTTP::HeaderList const& headers, Vector<GC::Ptr<Infrastructure::Response>>& initial_set_of_stored_responses) const
{ {
// When presented with a request, a cache MUST NOT reuse a stored response unless: // When presented with a request, a cache MUST NOT reuse a stored response unless:
@ -1569,7 +1567,7 @@ private:
} }
// https://httpwg.org/specs/rfc9111.html#update // https://httpwg.org/specs/rfc9111.html#update
void update_stored_header_fields(Infrastructure::Response const& response, Infrastructure::HeaderList& headers) void update_stored_header_fields(Infrastructure::Response const& response, HTTP::HeaderList& headers)
{ {
for (auto const& header : *response.header_list()) { for (auto const& header : *response.header_list()) {
if (!is_exempted_for_updating(header.name)) if (!is_exempted_for_updating(header.name))
@ -1583,7 +1581,7 @@ private:
} }
// https://httpwg.org/specs/rfc9111.html#storing.fields // https://httpwg.org/specs/rfc9111.html#storing.fields
void store_header_and_trailer_fields(Infrastructure::Response const& response, Web::Fetch::Infrastructure::HeaderList& headers) void store_header_and_trailer_fields(Infrastructure::Response const& response, HTTP::HeaderList& headers)
{ {
for (auto const& header : *response.header_list()) { for (auto const& header : *response.header_list()) {
if (!is_exempted_for_storage(header.name)) if (!is_exempted_for_storage(header.name))
@ -1922,7 +1920,7 @@ GC::Ref<PendingResponse> http_network_or_cache_fetch(JS::Realm& realm, Infrastru
// 2. If cookies is not the empty string, then append (`Cookie`, cookies) to httpRequests header list. // 2. If cookies is not the empty string, then append (`Cookie`, cookies) to httpRequests header list.
if (!cookies.is_empty()) { if (!cookies.is_empty()) {
auto header = Infrastructure::Header::isomorphic_encode("Cookie"sv, cookies); auto header = HTTP::Header::isomorphic_encode("Cookie"sv, cookies);
http_request->header_list()->append(move(header)); http_request->header_list()->append(move(header));
} }
} }
@ -1950,7 +1948,7 @@ GC::Ref<PendingResponse> http_network_or_cache_fetch(JS::Realm& realm, Infrastru
// 4. If authorizationValue is non-null, then append (`Authorization`, authorizationValue) to // 4. If authorizationValue is non-null, then append (`Authorization`, authorizationValue) to
// httpRequests header list. // httpRequests header list.
if (authorization_value.has_value()) { if (authorization_value.has_value()) {
auto header = Infrastructure::Header::isomorphic_encode("Authorization"sv, *authorization_value); auto header = HTTP::Header::isomorphic_encode("Authorization"sv, *authorization_value);
http_request->header_list()->append(move(header)); http_request->header_list()->append(move(header));
} }
} }
@ -2019,12 +2017,12 @@ GC::Ref<PendingResponse> http_network_or_cache_fetch(JS::Realm& realm, Infrastru
// 1. If storedResponses header list contains `ETag`, then append (`If-None-Match`, `ETag`'s value) to httpRequests header list. // 1. If storedResponses header list contains `ETag`, then append (`If-None-Match`, `ETag`'s value) to httpRequests header list.
if (auto etag = stored_response->header_list()->get("ETag"sv); etag.has_value()) { if (auto etag = stored_response->header_list()->get("ETag"sv); etag.has_value()) {
http_request->header_list()->append(Infrastructure::Header::isomorphic_encode("If-None-Match"sv, *etag)); http_request->header_list()->append(HTTP::Header::isomorphic_encode("If-None-Match"sv, *etag));
} }
// 2. If storedResponses header list contains `Last-Modified`, then append (`If-Modified-Since`, `Last-Modified`'s value) to httpRequests header list. // 2. If storedResponses header list contains `Last-Modified`, then append (`If-Modified-Since`, `Last-Modified`'s value) to httpRequests header list.
if (auto last_modified = stored_response->header_list()->get("Last-Modified"sv); last_modified.has_value()) { if (auto last_modified = stored_response->header_list()->get("Last-Modified"sv); last_modified.has_value()) {
http_request->header_list()->append(Infrastructure::Header::isomorphic_encode("If-Modified-Since"sv, *last_modified)); http_request->header_list()->append(HTTP::Header::isomorphic_encode("If-Modified-Since"sv, *last_modified));
} }
} }
// 3. Otherwise, set response to storedResponse and set responses cache state to "local". // 3. Otherwise, set response to storedResponse and set responses cache state to "local".
@ -2285,15 +2283,12 @@ GC::Ref<PendingResponse> nonstandard_resource_loader_file_or_http_network_fetch(
// NOTE: Using LoadRequest::create_for_url_on_page here will unconditionally add cookies as long as there's a page available. // NOTE: Using LoadRequest::create_for_url_on_page here will unconditionally add cookies as long as there's a page available.
// However, it is up to http_network_or_cache_fetch to determine if cookies should be added to the request. // However, it is up to http_network_or_cache_fetch to determine if cookies should be added to the request.
LoadRequest load_request; LoadRequest load_request { request->header_list() };
load_request.set_url(request->current_url()); load_request.set_url(request->current_url());
load_request.set_page(page); load_request.set_page(page);
load_request.set_method(request->method()); load_request.set_method(request->method());
load_request.set_store_set_cookie_headers(include_credentials == IncludeCredentials::Yes); load_request.set_store_set_cookie_headers(include_credentials == IncludeCredentials::Yes);
for (auto const& header : *request->header_list())
load_request.set_header(header.name, header.value);
if (auto const* body = request->body().get_pointer<GC::Ref<Infrastructure::Body>>()) { if (auto const* body = request->body().get_pointer<GC::Ref<Infrastructure::Body>>()) {
(*body)->source().visit( (*body)->source().visit(
[&](ByteBuffer const& byte_buffer) { [&](ByteBuffer const& byte_buffer) {
@ -2343,7 +2338,7 @@ GC::Ref<PendingResponse> nonstandard_resource_loader_file_or_http_network_fetch(
// 13. Set up stream with byte reading support with pullAlgorithm set to pullAlgorithm, cancelAlgorithm set to cancelAlgorithm. // 13. Set up stream with byte reading support with pullAlgorithm set to pullAlgorithm, cancelAlgorithm set to cancelAlgorithm.
stream->set_up_with_byte_reading_support(pull_algorithm, cancel_algorithm); stream->set_up_with_byte_reading_support(pull_algorithm, cancel_algorithm);
auto on_headers_received = GC::create_function(vm.heap(), [&vm, pending_response, stream, request](HTTP::HeaderMap const& response_headers, Optional<u32> status_code, Optional<String> const& reason_phrase) { auto on_headers_received = GC::create_function(vm.heap(), [&vm, pending_response, stream, request](HTTP::HeaderList const& response_headers, Optional<u32> status_code, Optional<String> const& reason_phrase) {
if (pending_response->is_resolved()) { if (pending_response->is_resolved()) {
// RequestServer will send us the response headers twice, the second time being for HTTP trailers. This // RequestServer will send us the response headers twice, the second time being for HTTP trailers. This
// fetch algorithm is not interested in trailers, so just drop them here. // fetch algorithm is not interested in trailers, so just drop them here.
@ -2430,7 +2425,7 @@ GC::Ref<PendingResponse> cors_preflight_fetch(JS::Realm& realm, Infrastructure::
preflight->header_list()->append({ "Accept"sv, "*/*"sv }); preflight->header_list()->append({ "Accept"sv, "*/*"sv });
// 3. Append (`Access-Control-Request-Method`, requests method) to preflights header list. // 3. Append (`Access-Control-Request-Method`, requests method) to preflights header list.
auto temp_header = Infrastructure::Header::isomorphic_encode("Access-Control-Request-Method"sv, request.method()); auto temp_header = HTTP::Header::isomorphic_encode("Access-Control-Request-Method"sv, request.method());
preflight->header_list()->append(move(temp_header)); preflight->header_list()->append(move(temp_header));
// 4. Let headers be the CORS-unsafe request-header names with requests header list. // 4. Let headers be the CORS-unsafe request-header names with requests header list.
@ -2470,11 +2465,11 @@ GC::Ref<PendingResponse> cors_preflight_fetch(JS::Realm& realm, Infrastructure::
auto header_names_or_failure = response->header_list()->extract_header_list_values("Access-Control-Allow-Headers"sv); auto header_names_or_failure = response->header_list()->extract_header_list_values("Access-Control-Allow-Headers"sv);
// 3. If either methods or headerNames is failure, return a network error. // 3. If either methods or headerNames is failure, return a network error.
if (methods_or_failure.has<Infrastructure::HeaderList::ExtractHeaderParseFailure>()) { if (methods_or_failure.has<HTTP::HeaderList::ExtractHeaderParseFailure>()) {
returned_pending_response->resolve(Infrastructure::Response::network_error(vm, "The Access-Control-Allow-Methods in the CORS-preflight response is syntactically invalid"_string)); returned_pending_response->resolve(Infrastructure::Response::network_error(vm, "The Access-Control-Allow-Methods in the CORS-preflight response is syntactically invalid"_string));
return; return;
} }
if (header_names_or_failure.has<Infrastructure::HeaderList::ExtractHeaderParseFailure>()) { if (header_names_or_failure.has<HTTP::HeaderList::ExtractHeaderParseFailure>()) {
returned_pending_response->resolve(Infrastructure::Response::network_error(vm, "The Access-Control-Allow-Headers in the CORS-preflight response is syntactically invalid"_string)); returned_pending_response->resolve(Infrastructure::Response::network_error(vm, "The Access-Control-Allow-Headers in the CORS-preflight response is syntactically invalid"_string));
return; return;
} }
@ -2496,7 +2491,7 @@ GC::Ref<PendingResponse> cors_preflight_fetch(JS::Realm& realm, Infrastructure::
// 5. If requests method is not in methods, requests method is not a CORS-safelisted method, and requests credentials mode // 5. If requests method is not in methods, requests method is not a CORS-safelisted method, and requests credentials mode
// is "include" or methods does not contain `*`, then return a network error. // is "include" or methods does not contain `*`, then return a network error.
if (!methods.contains_slow(request.method()) && !Infrastructure::is_cors_safelisted_method(request.method())) { if (!methods.contains_slow(request.method()) && !HTTP::is_cors_safelisted_method(request.method())) {
if (request.credentials_mode() == Infrastructure::Request::CredentialsMode::Include) { if (request.credentials_mode() == Infrastructure::Request::CredentialsMode::Include) {
returned_pending_response->resolve(Infrastructure::Response::network_error(vm, TRY_OR_IGNORE(String::formatted("Non-CORS-safelisted method '{}' not found in the CORS-preflight response's Access-Control-Allow-Methods header (the header may be missing). '*' is not allowed as the main request includes credentials.", request.method())))); returned_pending_response->resolve(Infrastructure::Response::network_error(vm, TRY_OR_IGNORE(String::formatted("Non-CORS-safelisted method '{}' not found in the CORS-preflight response's Access-Control-Allow-Methods header (the header may be missing). '*' is not allowed as the main request includes credentials.", request.method()))));
return; return;

View file

@ -19,10 +19,8 @@ GC_DEFINE_ALLOCATOR(Headers);
// https://fetch.spec.whatwg.org/#dom-headers // https://fetch.spec.whatwg.org/#dom-headers
WebIDL::ExceptionOr<GC::Ref<Headers>> Headers::construct_impl(JS::Realm& realm, Optional<HeadersInit> const& init) WebIDL::ExceptionOr<GC::Ref<Headers>> Headers::construct_impl(JS::Realm& realm, Optional<HeadersInit> const& init)
{ {
auto& vm = realm.vm();
// The new Headers(init) constructor steps are: // The new Headers(init) constructor steps are:
auto headers = realm.create<Headers>(realm, Infrastructure::HeaderList::create(vm)); auto headers = realm.create<Headers>(realm, HTTP::HeaderList::create());
// 1. Set thiss guard to "none". // 1. Set thiss guard to "none".
headers->m_guard = Guard::None; headers->m_guard = Guard::None;
@ -34,9 +32,9 @@ WebIDL::ExceptionOr<GC::Ref<Headers>> Headers::construct_impl(JS::Realm& realm,
return headers; return headers;
} }
Headers::Headers(JS::Realm& realm, GC::Ref<Infrastructure::HeaderList> header_list) Headers::Headers(JS::Realm& realm, NonnullRefPtr<HTTP::HeaderList> header_list)
: PlatformObject(realm) : PlatformObject(realm)
, m_header_list(header_list) , m_header_list(move(header_list))
{ {
} }
@ -48,17 +46,11 @@ void Headers::initialize(JS::Realm& realm)
Base::initialize(realm); Base::initialize(realm);
} }
void Headers::visit_edges(JS::Cell::Visitor& visitor)
{
Base::visit_edges(visitor);
visitor.visit(m_header_list);
}
// https://fetch.spec.whatwg.org/#dom-headers-append // https://fetch.spec.whatwg.org/#dom-headers-append
WebIDL::ExceptionOr<void> Headers::append(String const& name_string, String const& value_string) WebIDL::ExceptionOr<void> Headers::append(String const& name_string, String const& value_string)
{ {
// The append(name, value) method steps are to append (name, value) to this. // The append(name, value) method steps are to append (name, value) to this.
auto header = Infrastructure::Header::isomorphic_encode(name_string, value_string); auto header = HTTP::Header::isomorphic_encode(name_string, value_string);
TRY(append(move(header))); TRY(append(move(header)));
return {}; return {};
} }
@ -70,7 +62,7 @@ WebIDL::ExceptionOr<void> Headers::delete_(String const& name)
// 1. If validating (name, ``) for headers returns false, then return. // 1. If validating (name, ``) for headers returns false, then return.
// NOTE: Passing a dummy header value ought not to have any negative repercussions. // NOTE: Passing a dummy header value ought not to have any negative repercussions.
auto header = Infrastructure::Header::isomorphic_encode(name, ""sv); auto header = HTTP::Header::isomorphic_encode(name, ""sv);
if (!TRY(validate(header))) if (!TRY(validate(header)))
return {}; return {};
@ -98,7 +90,7 @@ WebIDL::ExceptionOr<Optional<String>> Headers::get(String const& name)
// The get(name) method steps are: // The get(name) method steps are:
// 1. If name is not a header name, then throw a TypeError. // 1. If name is not a header name, then throw a TypeError.
if (!Infrastructure::is_header_name(name)) if (!HTTP::is_header_name(name))
return WebIDL::SimpleException { WebIDL::SimpleExceptionType::TypeError, "Invalid header name"sv }; return WebIDL::SimpleException { WebIDL::SimpleExceptionType::TypeError, "Invalid header name"sv };
// 2. Return the result of getting name from thiss header list. // 2. Return the result of getting name from thiss header list.
@ -131,7 +123,7 @@ WebIDL::ExceptionOr<bool> Headers::has(String const& name)
// The has(name) method steps are: // The has(name) method steps are:
// 1. If name is not a header name, then throw a TypeError. // 1. If name is not a header name, then throw a TypeError.
if (!Infrastructure::is_header_name(name)) if (!HTTP::is_header_name(name))
return WebIDL::SimpleException { WebIDL::SimpleExceptionType::TypeError, "Invalid header name"sv }; return WebIDL::SimpleException { WebIDL::SimpleExceptionType::TypeError, "Invalid header name"sv };
// 2. Return true if thiss header list contains name; otherwise false. // 2. Return true if thiss header list contains name; otherwise false.
@ -144,9 +136,9 @@ WebIDL::ExceptionOr<void> Headers::set(String const& name, String const& value)
// The set(name, value) method steps are: // The set(name, value) method steps are:
// 1. Normalize value. // 1. Normalize value.
auto normalized_value = Infrastructure::normalize_header_value(value); auto normalized_value = HTTP::normalize_header_value(value);
auto header = Infrastructure::Header::isomorphic_encode(name, normalized_value); auto header = HTTP::Header::isomorphic_encode(name, normalized_value);
// 2. If validating (name, value) for headers returns false, then return. // 2. If validating (name, value) for headers returns false, then return.
if (!TRY(validate(header))) if (!TRY(validate(header)))
@ -201,15 +193,15 @@ JS::ThrowCompletionOr<void> Headers::for_each(ForEachCallback callback)
} }
// https://fetch.spec.whatwg.org/#headers-validate // https://fetch.spec.whatwg.org/#headers-validate
WebIDL::ExceptionOr<bool> Headers::validate(Infrastructure::Header const& header) const WebIDL::ExceptionOr<bool> Headers::validate(HTTP::Header const& header) const
{ {
// To validate a header (name, value) for a Headers object headers: // To validate a header (name, value) for a Headers object headers:
auto const& [name, value] = header; auto const& [name, value] = header;
// 1. If name is not a header name or value is not a header value, then throw a TypeError. // 1. If name is not a header name or value is not a header value, then throw a TypeError.
if (!Infrastructure::is_header_name(name)) if (!HTTP::is_header_name(name))
return WebIDL::SimpleException { WebIDL::SimpleExceptionType::TypeError, "Invalid header name"sv }; return WebIDL::SimpleException { WebIDL::SimpleExceptionType::TypeError, "Invalid header name"sv };
if (!Infrastructure::is_header_value(value)) if (!HTTP::is_header_value(value))
return WebIDL::SimpleException { WebIDL::SimpleExceptionType::TypeError, "Invalid header value"sv }; return WebIDL::SimpleException { WebIDL::SimpleExceptionType::TypeError, "Invalid header value"sv };
// 2. If headerss guard is "immutable", then throw a TypeError. // 2. If headerss guard is "immutable", then throw a TypeError.
@ -217,11 +209,11 @@ WebIDL::ExceptionOr<bool> Headers::validate(Infrastructure::Header const& header
return WebIDL::SimpleException { WebIDL::SimpleExceptionType::TypeError, "Headers object is immutable"sv }; return WebIDL::SimpleException { WebIDL::SimpleExceptionType::TypeError, "Headers object is immutable"sv };
// 3. If headerss guard is "request" and (name, value) is a forbidden request-header, then return false. // 3. If headerss guard is "request" and (name, value) is a forbidden request-header, then return false.
if (m_guard == Guard::Request && Infrastructure::is_forbidden_request_header(header)) if (m_guard == Guard::Request && HTTP::is_forbidden_request_header(header))
return false; return false;
// 4. If headerss guard is "response" and name is a forbidden response-header name, then return false. // 4. If headerss guard is "response" and name is a forbidden response-header name, then return false.
if (m_guard == Guard::Response && Infrastructure::is_forbidden_response_header_name(name)) if (m_guard == Guard::Response && HTTP::is_forbidden_response_header_name(name))
return false; return false;
// 5. Return true. // 5. Return true.
@ -229,13 +221,13 @@ WebIDL::ExceptionOr<bool> Headers::validate(Infrastructure::Header const& header
} }
// https://fetch.spec.whatwg.org/#concept-headers-append // https://fetch.spec.whatwg.org/#concept-headers-append
WebIDL::ExceptionOr<void> Headers::append(Infrastructure::Header header) WebIDL::ExceptionOr<void> Headers::append(HTTP::Header header)
{ {
// To append a header (name, value) to a Headers object headers, run these steps: // To append a header (name, value) to a Headers object headers, run these steps:
auto& [name, value] = header; auto& [name, value] = header;
// 1. Normalize value. // 1. Normalize value.
value = Infrastructure::normalize_header_value(value); value = HTTP::normalize_header_value(value);
// 2. If validating (name, value) for headers returns false, then return. // 2. If validating (name, value) for headers returns false, then return.
if (!TRY(validate(header))) if (!TRY(validate(header)))
@ -255,7 +247,7 @@ WebIDL::ExceptionOr<void> Headers::append(Infrastructure::Header header)
temporary_value = ByteString::formatted("{}, {}", *temporary_value, value); temporary_value = ByteString::formatted("{}, {}", *temporary_value, value);
} }
auto temporary_header = Infrastructure::Header { auto temporary_header = HTTP::Header {
.name = name, .name = name,
.value = temporary_value.release_value(), .value = temporary_value.release_value(),
}; };
@ -288,7 +280,7 @@ WebIDL::ExceptionOr<void> Headers::fill(HeadersInit const& object)
return WebIDL::SimpleException { WebIDL::SimpleExceptionType::TypeError, "Array must contain header key/value pair"sv }; return WebIDL::SimpleException { WebIDL::SimpleExceptionType::TypeError, "Array must contain header key/value pair"sv };
// 2. Append (header[0], header[1]) to headers. // 2. Append (header[0], header[1]) to headers.
auto header = Infrastructure::Header::isomorphic_encode(entry[0], entry[1]); auto header = HTTP::Header::isomorphic_encode(entry[0], entry[1]);
TRY(append(move(header))); TRY(append(move(header)));
} }
return {}; return {};
@ -296,7 +288,7 @@ WebIDL::ExceptionOr<void> Headers::fill(HeadersInit const& object)
// 2. Otherwise, object is a record, then for each key → value of object, append (key, value) to headers. // 2. Otherwise, object is a record, then for each key → value of object, append (key, value) to headers.
[&](OrderedHashMap<String, String> const& object) -> WebIDL::ExceptionOr<void> { [&](OrderedHashMap<String, String> const& object) -> WebIDL::ExceptionOr<void> {
for (auto const& entry : object) { for (auto const& entry : object) {
auto header = Infrastructure::Header::isomorphic_encode(entry.key, entry.value); auto header = HTTP::Header::isomorphic_encode(entry.key, entry.value);
TRY(append(move(header))); TRY(append(move(header)));
} }
return {}; return {};

View file

@ -11,9 +11,9 @@
#include <AK/Variant.h> #include <AK/Variant.h>
#include <AK/Vector.h> #include <AK/Vector.h>
#include <LibGC/Ptr.h> #include <LibGC/Ptr.h>
#include <LibHTTP/HeaderList.h>
#include <LibJS/Forward.h> #include <LibJS/Forward.h>
#include <LibWeb/Bindings/PlatformObject.h> #include <LibWeb/Bindings/PlatformObject.h>
#include <LibWeb/Fetch/Infrastructure/HTTP/Headers.h>
#include <LibWeb/WebIDL/ExceptionOr.h> #include <LibWeb/WebIDL/ExceptionOr.h>
namespace Web::Fetch { namespace Web::Fetch {
@ -38,14 +38,14 @@ public:
virtual ~Headers() override; virtual ~Headers() override;
[[nodiscard]] GC::Ref<Infrastructure::HeaderList> header_list() const { return m_header_list; } [[nodiscard]] NonnullRefPtr<HTTP::HeaderList> const& header_list() const { return m_header_list; }
void set_header_list(GC::Ref<Infrastructure::HeaderList> header_list) { m_header_list = header_list; } void set_header_list(NonnullRefPtr<HTTP::HeaderList> header_list) { m_header_list = move(header_list); }
[[nodiscard]] Guard guard() const { return m_guard; } [[nodiscard]] Guard guard() const { return m_guard; }
void set_guard(Guard guard) { m_guard = guard; } void set_guard(Guard guard) { m_guard = guard; }
WebIDL::ExceptionOr<void> fill(HeadersInit const&); WebIDL::ExceptionOr<void> fill(HeadersInit const&);
WebIDL::ExceptionOr<void> append(Infrastructure::Header); WebIDL::ExceptionOr<void> append(HTTP::Header);
// JS API functions // JS API functions
WebIDL::ExceptionOr<void> append(String const& name, String const& value); WebIDL::ExceptionOr<void> append(String const& name, String const& value);
@ -61,17 +61,16 @@ public:
private: private:
friend class HeadersIterator; friend class HeadersIterator;
Headers(JS::Realm&, GC::Ref<Infrastructure::HeaderList>); Headers(JS::Realm&, NonnullRefPtr<HTTP::HeaderList>);
virtual void initialize(JS::Realm&) override; virtual void initialize(JS::Realm&) override;
virtual void visit_edges(JS::Cell::Visitor&) override;
WebIDL::ExceptionOr<bool> validate(Infrastructure::Header const&) const; WebIDL::ExceptionOr<bool> validate(HTTP::Header const&) const;
void remove_privileged_no_cors_request_headers(); void remove_privileged_no_cors_request_headers();
// https://fetch.spec.whatwg.org/#concept-headers-header-list // https://fetch.spec.whatwg.org/#concept-headers-header-list
// A Headers object has an associated header list (a header list), which is initially empty. // A Headers object has an associated header list (a header list), which is initially empty.
GC::Ref<Infrastructure::HeaderList> m_header_list; NonnullRefPtr<HTTP::HeaderList> m_header_list;
// https://fetch.spec.whatwg.org/#concept-headers-guard // https://fetch.spec.whatwg.org/#concept-headers-guard
// A Headers object also has an associated guard, which is a headers guard. A headers guard is "immutable", "request", "request-no-cors", "response" or "none". // A Headers object also has an associated guard, which is a headers guard. A headers guard is "immutable", "request", "request-no-cors", "response" or "none".

View file

@ -4,75 +4,17 @@
* SPDX-License-Identifier: BSD-2-Clause * SPDX-License-Identifier: BSD-2-Clause
*/ */
#include <AK/GenericLexer.h>
#include <AK/String.h>
#include <AK/StringBuilder.h>
#include <LibWeb/Fetch/Infrastructure/HTTP.h> #include <LibWeb/Fetch/Infrastructure/HTTP.h>
#include <LibWeb/Loader/ResourceLoader.h>
namespace Web::Fetch::Infrastructure { namespace Web::Fetch::Infrastructure {
// https://fetch.spec.whatwg.org/#collect-an-http-quoted-string // https://fetch.spec.whatwg.org/#default-user-agent-value
String collect_an_http_quoted_string(GenericLexer& lexer, HttpQuotedStringExtractValue extract_value) ByteString const& default_user_agent_value()
{ {
// To collect an HTTP quoted string from a string input, given a position variable position and optionally an extract-value flag, run these steps: // A default `User-Agent` value is an implementation-defined header value for the `User-Agent` header.
// 1. Let positionStart be position. static auto user_agent = ResourceLoader::the().user_agent().to_byte_string();
auto position_start = lexer.tell(); return user_agent;
// 2. Let value be the empty string.
StringBuilder value;
// 3. Assert: the code point at position within input is U+0022 (").
VERIFY(lexer.peek() == '"');
// 4. Advance position by 1.
lexer.ignore(1);
// 5. While true:
while (true) {
// 1. Append the result of collecting a sequence of code points that are not U+0022 (") or U+005C (\) from input, given position, to value.
auto value_part = lexer.consume_until([](char ch) {
return ch == '"' || ch == '\\';
});
value.append(value_part);
// 2. If position is past the end of input, then break.
if (lexer.is_eof())
break;
// 3. Let quoteOrBackslash be the code point at position within input.
// 4. Advance position by 1.
char quote_or_backslash = lexer.consume();
// 5. If quoteOrBackslash is U+005C (\), then:
if (quote_or_backslash == '\\') {
// 1. If position is past the end of input, then append U+005C (\) to value and break.
if (lexer.is_eof()) {
value.append('\\');
break;
}
// 2. Append the code point at position within input to value.
// 3. Advance position by 1.
value.append(lexer.consume());
}
// 6. Otherwise:
else {
// 1. Assert: quoteOrBackslash is U+0022 (").
VERIFY(quote_or_backslash == '"');
// 2. Break.
break;
}
}
// 6. If the extract-value flag is set, then return value.
if (extract_value == HttpQuotedStringExtractValue::Yes)
return MUST(value.to_string());
// 7. Return the code points from positionStart to position, inclusive, within input.
return MUST(String::from_utf8(lexer.input().substring_view(position_start, lexer.tell() - position_start)));
} }
} }

View file

@ -8,52 +8,15 @@
#pragma once #pragma once
#include <AK/Forward.h> #include <AK/Forward.h>
#include <AK/StringView.h>
#include <LibWeb/Export.h>
namespace Web::Fetch::Infrastructure { namespace Web::Fetch::Infrastructure {
// https://fetch.spec.whatwg.org/#http-tab-or-space
// An HTTP tab or space is U+0009 TAB or U+0020 SPACE.
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;
// https://fetch.spec.whatwg.org/#http-newline-byte
// An HTTP newline byte is 0x0A (LF) or 0x0D (CR).
inline constexpr Array HTTP_NEWLINE_BYTES = {
0x0A, 0x0D
};
// https://fetch.spec.whatwg.org/#http-tab-or-space-byte
// An HTTP tab or space byte is 0x09 (HT) or 0x20 (SP).
inline constexpr Array HTTP_TAB_OR_SPACE_BYTES = {
0x09, 0x20
};
constexpr bool is_http_tab_or_space(u32 const code_point)
{
return code_point == 0x09 || code_point == 0x20;
}
constexpr bool is_http_newline(u32 const code_point)
{
return code_point == 0x0A || code_point == 0x0D;
}
enum class HttpQuotedStringExtractValue {
No,
Yes,
};
enum class RedirectTaint { enum class RedirectTaint {
SameOrigin, SameOrigin,
SameSite, SameSite,
CrossSite, CrossSite,
}; };
[[nodiscard]] WEB_API String collect_an_http_quoted_string(GenericLexer& lexer, HttpQuotedStringExtractValue extract_value = HttpQuotedStringExtractValue::No); [[nodiscard]] ByteString const& default_user_agent_value();
} }

View file

@ -8,15 +8,15 @@
#include <AK/Checked.h> #include <AK/Checked.h>
#include <LibHTTP/Header.h> #include <LibHTTP/Header.h>
#include <LibHTTP/HeaderList.h>
#include <LibTextCodec/Decoder.h> #include <LibTextCodec/Decoder.h>
#include <LibWeb/Fetch/Infrastructure/HTTP/CORS.h> #include <LibWeb/Fetch/Infrastructure/HTTP/CORS.h>
#include <LibWeb/Fetch/Infrastructure/HTTP/Headers.h>
#include <LibWeb/MimeSniff/MimeType.h> #include <LibWeb/MimeSniff/MimeType.h>
namespace Web::Fetch::Infrastructure { namespace Web::Fetch::Infrastructure {
// https://fetch.spec.whatwg.org/#cors-safelisted-request-header // https://fetch.spec.whatwg.org/#cors-safelisted-request-header
bool is_cors_safelisted_request_header(Header const& header) bool is_cors_safelisted_request_header(HTTP::Header const& header)
{ {
auto const& [name, value] = header; auto const& [name, value] = header;
@ -61,7 +61,7 @@ bool is_cors_safelisted_request_header(Header const& header)
// `range` // `range`
else if (name.equals_ignoring_ascii_case("range"sv)) { else if (name.equals_ignoring_ascii_case("range"sv)) {
// 1. Let rangeValue be the result of parsing a single range header value given value and false. // 1. Let rangeValue be the result of parsing a single range header value given value and false.
auto range_value = parse_single_range_header_value(value, false); auto range_value = HTTP::parse_single_range_header_value(value, false);
// 2. If rangeValue is failure, then return false. // 2. If rangeValue is failure, then return false.
if (!range_value.has_value()) if (!range_value.has_value())
@ -93,7 +93,7 @@ bool is_cors_unsafe_request_header_byte(u8 byte)
} }
// https://fetch.spec.whatwg.org/#cors-unsafe-request-header-names // https://fetch.spec.whatwg.org/#cors-unsafe-request-header-names
Vector<ByteString> get_cors_unsafe_header_names(HeaderList const& headers) Vector<ByteString> get_cors_unsafe_header_names(HTTP::HeaderList const& headers)
{ {
// 1. Let unsafeNames be a new list. // 1. Let unsafeNames be a new list.
Vector<ByteString> unsafe_names; Vector<ByteString> unsafe_names;
@ -124,7 +124,7 @@ Vector<ByteString> get_cors_unsafe_header_names(HeaderList const& headers)
unsafe_names.extend(move(potentially_unsafe_names)); unsafe_names.extend(move(potentially_unsafe_names));
// 6. Return the result of convert header names to a sorted-lowercase set with unsafeNames. // 6. Return the result of convert header names to a sorted-lowercase set with unsafeNames.
return convert_header_names_to_a_sorted_lowercase_set(unsafe_names.span()); return HTTP::convert_header_names_to_a_sorted_lowercase_set(unsafe_names.span());
} }
// https://fetch.spec.whatwg.org/#cors-non-wildcard-request-header-name // https://fetch.spec.whatwg.org/#cors-non-wildcard-request-header-name
@ -164,7 +164,7 @@ bool is_cors_safelisted_response_header_name(StringView header_name, ReadonlySpa
"Pragma"sv) "Pragma"sv)
|| any_of(list, [&](auto list_header_name) { || any_of(list, [&](auto list_header_name) {
return header_name.equals_ignoring_ascii_case(list_header_name) return header_name.equals_ignoring_ascii_case(list_header_name)
&& !is_forbidden_response_header_name(list_header_name); && !HTTP::is_forbidden_response_header_name(list_header_name);
}); });
} }
@ -184,7 +184,7 @@ bool is_no_cors_safelisted_request_header_name(StringView header_name)
} }
// https://fetch.spec.whatwg.org/#no-cors-safelisted-request-header // https://fetch.spec.whatwg.org/#no-cors-safelisted-request-header
bool is_no_cors_safelisted_request_header(Header const& header) bool is_no_cors_safelisted_request_header(HTTP::Header const& header)
{ {
// 1. If name is not a no-CORS-safelisted request-header name, then return false. // 1. If name is not a no-CORS-safelisted request-header name, then return false.
if (!is_no_cors_safelisted_request_header_name(header.name)) if (!is_no_cors_safelisted_request_header_name(header.name))

View file

@ -9,17 +9,17 @@
#include <AK/ByteString.h> #include <AK/ByteString.h>
#include <AK/StringView.h> #include <AK/StringView.h>
#include <AK/Vector.h> #include <AK/Vector.h>
#include <LibWeb/Forward.h> #include <LibHTTP/Forward.h>
namespace Web::Fetch::Infrastructure { namespace Web::Fetch::Infrastructure {
[[nodiscard]] bool is_cors_safelisted_request_header(Header const&); [[nodiscard]] bool is_cors_safelisted_request_header(HTTP::Header const&);
[[nodiscard]] bool is_cors_unsafe_request_header_byte(u8); [[nodiscard]] bool is_cors_unsafe_request_header_byte(u8);
[[nodiscard]] Vector<ByteString> get_cors_unsafe_header_names(HeaderList const&); [[nodiscard]] Vector<ByteString> get_cors_unsafe_header_names(HTTP::HeaderList const&);
[[nodiscard]] bool is_cors_non_wildcard_request_header_name(StringView); [[nodiscard]] bool is_cors_non_wildcard_request_header_name(StringView);
[[nodiscard]] bool is_privileged_no_cors_request_header_name(StringView); [[nodiscard]] bool is_privileged_no_cors_request_header_name(StringView);
[[nodiscard]] bool is_cors_safelisted_response_header_name(StringView, ReadonlySpan<StringView>); [[nodiscard]] bool is_cors_safelisted_response_header_name(StringView, ReadonlySpan<StringView>);
[[nodiscard]] bool is_no_cors_safelisted_request_header_name(StringView); [[nodiscard]] bool is_no_cors_safelisted_request_header_name(StringView);
[[nodiscard]] bool is_no_cors_safelisted_request_header(Header const&); [[nodiscard]] bool is_no_cors_safelisted_request_header(HTTP::Header const&);
} }

View file

@ -1,86 +0,0 @@
/*
* Copyright (c) 2022-2023, Linus Groh <linusg@serenityos.org>
*
* SPDX-License-Identifier: BSD-2-Clause
*/
#pragma once
#include <AK/ByteString.h>
#include <AK/Optional.h>
#include <AK/String.h>
#include <AK/Vector.h>
#include <LibGC/Heap.h>
#include <LibGC/Ptr.h>
#include <LibJS/Forward.h>
#include <LibJS/Heap/Cell.h>
#include <LibWeb/Export.h>
namespace Web::Fetch::Infrastructure {
// https://fetch.spec.whatwg.org/#concept-header
// A header is a tuple that consists of a name (a header name) and value (a header value).
struct WEB_API Header {
[[nodiscard]] static Header isomorphic_encode(StringView, StringView);
Optional<Vector<ByteString>> extract_header_values() const;
ByteString name;
ByteString value;
};
// https://fetch.spec.whatwg.org/#concept-header-list
// A header list is a list of zero or more headers. It is initially the empty list.
class WEB_API HeaderList final
: public JS::Cell
, public Vector<Header> {
GC_CELL(HeaderList, JS::Cell);
GC_DECLARE_ALLOCATOR(HeaderList);
public:
[[nodiscard]] static GC::Ref<HeaderList> create(JS::VM&);
using Vector::begin;
using Vector::clear;
using Vector::end;
using Vector::is_empty;
[[nodiscard]] bool contains(StringView) const;
[[nodiscard]] Optional<ByteString> get(StringView) const;
[[nodiscard]] Optional<Vector<String>> get_decode_and_split(StringView) const;
void append(Header);
void delete_(StringView name);
void set(Header);
void combine(Header);
[[nodiscard]] Vector<Header> sort_and_combine() const;
struct ExtractHeaderParseFailure { };
[[nodiscard]] Variant<Empty, Vector<ByteString>, ExtractHeaderParseFailure> extract_header_list_values(StringView) const;
struct ExtractLengthFailure { };
[[nodiscard]] Variant<Empty, u64, ExtractLengthFailure> extract_length() const;
[[nodiscard]] Vector<ByteString> unique_names() const;
};
struct RangeHeaderValue {
Optional<u64> start;
Optional<u64> end;
};
[[nodiscard]] bool is_header_name(StringView);
[[nodiscard]] bool is_header_value(StringView);
[[nodiscard]] ByteString normalize_header_value(StringView);
[[nodiscard]] bool is_forbidden_request_header(Header const&);
[[nodiscard]] bool is_forbidden_response_header_name(StringView);
[[nodiscard]] Vector<String> get_decode_and_split_header_value(StringView);
[[nodiscard]] Vector<ByteString> convert_header_names_to_a_sorted_lowercase_set(ReadonlySpan<ByteString>);
[[nodiscard]] WEB_API ByteString build_content_range(u64 range_start, u64 range_end, u64 full_length);
[[nodiscard]] WEB_API Optional<RangeHeaderValue> parse_single_range_header_value(StringView, bool);
[[nodiscard]] WEB_API ByteString const& default_user_agent_value();
}

View file

@ -6,14 +6,14 @@
* SPDX-License-Identifier: BSD-2-Clause * SPDX-License-Identifier: BSD-2-Clause
*/ */
#include <LibHTTP/HeaderList.h>
#include <LibTextCodec/Decoder.h> #include <LibTextCodec/Decoder.h>
#include <LibWeb/Fetch/Infrastructure/HTTP/Headers.h>
#include <LibWeb/Fetch/Infrastructure/HTTP/MIME.h> #include <LibWeb/Fetch/Infrastructure/HTTP/MIME.h>
namespace Web::Fetch::Infrastructure { namespace Web::Fetch::Infrastructure {
// https://fetch.spec.whatwg.org/#concept-header-extract-mime-type // https://fetch.spec.whatwg.org/#concept-header-extract-mime-type
Optional<MimeSniff::MimeType> extract_mime_type(HeaderList const& headers) Optional<MimeSniff::MimeType> extract_mime_type(HTTP::HeaderList const& headers)
{ {
// 1. Let charset be null. // 1. Let charset be null.
Optional<String> charset; Optional<String> charset;

View file

@ -7,12 +7,12 @@
#pragma once #pragma once
#include <AK/Optional.h> #include <AK/Optional.h>
#include <LibWeb/Forward.h> #include <LibHTTP/Forward.h>
#include <LibWeb/MimeSniff/MimeType.h> #include <LibWeb/MimeSniff/MimeType.h>
namespace Web::Fetch::Infrastructure { namespace Web::Fetch::Infrastructure {
Optional<MimeSniff::MimeType> extract_mime_type(HeaderList const&); Optional<MimeSniff::MimeType> extract_mime_type(HTTP::HeaderList const&);
StringView legacy_extract_an_encoding(Optional<MimeSniff::MimeType> const& mime_type, StringView fallback_encoding); StringView legacy_extract_an_encoding(Optional<MimeSniff::MimeType> const& mime_type, StringView fallback_encoding);
} }

View file

@ -20,15 +20,19 @@ namespace Web::Fetch::Infrastructure {
GC_DEFINE_ALLOCATOR(Request); GC_DEFINE_ALLOCATOR(Request);
Request::Request(GC::Ref<HeaderList> header_list) GC::Ref<Request> Request::create(JS::VM& vm)
: m_header_list(header_list) {
return vm.heap().allocate<Request>(HTTP::HeaderList::create());
}
Request::Request(NonnullRefPtr<HTTP::HeaderList> header_list)
: m_header_list(move(header_list))
{ {
} }
void Request::visit_edges(JS::Cell::Visitor& visitor) void Request::visit_edges(JS::Cell::Visitor& visitor)
{ {
Base::visit_edges(visitor); Base::visit_edges(visitor);
visitor.visit(m_header_list);
visitor.visit(m_client); visitor.visit(m_client);
m_body.visit( m_body.visit(
[&](GC::Ref<Body>& body) { visitor.visit(body); }, [&](GC::Ref<Body>& body) { visitor.visit(body); },
@ -44,11 +48,6 @@ void Request::visit_edges(JS::Cell::Visitor& visitor)
[](auto const&) {}); [](auto const&) {});
} }
GC::Ref<Request> Request::create(JS::VM& vm)
{
return vm.heap().allocate<Request>(HeaderList::create(vm));
}
// https://fetch.spec.whatwg.org/#concept-request-url // https://fetch.spec.whatwg.org/#concept-request-url
URL::URL& Request::url() URL::URL& Request::url()
{ {

View file

@ -16,6 +16,7 @@
#include <AK/Variant.h> #include <AK/Variant.h>
#include <AK/Vector.h> #include <AK/Vector.h>
#include <LibGC/Ptr.h> #include <LibGC/Ptr.h>
#include <LibHTTP/HeaderList.h>
#include <LibJS/Forward.h> #include <LibJS/Forward.h>
#include <LibJS/Heap/Cell.h> #include <LibJS/Heap/Cell.h>
#include <LibURL/Origin.h> #include <LibURL/Origin.h>
@ -23,7 +24,6 @@
#include <LibWeb/Export.h> #include <LibWeb/Export.h>
#include <LibWeb/Fetch/Infrastructure/HTTP.h> #include <LibWeb/Fetch/Infrastructure/HTTP.h>
#include <LibWeb/Fetch/Infrastructure/HTTP/Bodies.h> #include <LibWeb/Fetch/Infrastructure/HTTP/Bodies.h>
#include <LibWeb/Fetch/Infrastructure/HTTP/Headers.h>
namespace Web::Fetch::Infrastructure { namespace Web::Fetch::Infrastructure {
@ -178,8 +178,8 @@ public:
[[nodiscard]] bool local_urls_only() const { return m_local_urls_only; } [[nodiscard]] bool local_urls_only() const { return m_local_urls_only; }
void set_local_urls_only(bool local_urls_only) { m_local_urls_only = local_urls_only; } void set_local_urls_only(bool local_urls_only) { m_local_urls_only = local_urls_only; }
[[nodiscard]] GC::Ref<HeaderList> header_list() const { return m_header_list; } NonnullRefPtr<HTTP::HeaderList> const& header_list() const { return m_header_list; }
void set_header_list(GC::Ref<HeaderList> header_list) { m_header_list = header_list; } void set_header_list(NonnullRefPtr<HTTP::HeaderList> header_list) { m_header_list = move(header_list); }
[[nodiscard]] bool unsafe_request() const { return m_unsafe_request; } [[nodiscard]] bool unsafe_request() const { return m_unsafe_request; }
void set_unsafe_request(bool unsafe_request) { m_unsafe_request = unsafe_request; } void set_unsafe_request(bool unsafe_request) { m_unsafe_request = unsafe_request; }
@ -330,7 +330,7 @@ public:
} }
private: private:
explicit Request(GC::Ref<HeaderList>); explicit Request(NonnullRefPtr<HTTP::HeaderList>);
virtual void visit_edges(JS::Cell::Visitor&) override; virtual void visit_edges(JS::Cell::Visitor&) override;
@ -344,7 +344,7 @@ private:
// https://fetch.spec.whatwg.org/#concept-request-header-list // https://fetch.spec.whatwg.org/#concept-request-header-list
// A request has an associated header list (a header list). Unless stated otherwise it is empty. // A request has an associated header list (a header list). Unless stated otherwise it is empty.
GC::Ref<HeaderList> m_header_list; NonnullRefPtr<HTTP::HeaderList> m_header_list;
// https://fetch.spec.whatwg.org/#unsafe-request-flag // https://fetch.spec.whatwg.org/#unsafe-request-flag
// A request has an associated unsafe-request flag. Unless stated otherwise it is unset. // A request has an associated unsafe-request flag. Unless stated otherwise it is unset.

View file

@ -15,6 +15,7 @@
#include <LibWeb/Fetch/Infrastructure/HTTP/Bodies.h> #include <LibWeb/Fetch/Infrastructure/HTTP/Bodies.h>
#include <LibWeb/Fetch/Infrastructure/HTTP/CORS.h> #include <LibWeb/Fetch/Infrastructure/HTTP/CORS.h>
#include <LibWeb/Fetch/Infrastructure/HTTP/Responses.h> #include <LibWeb/Fetch/Infrastructure/HTTP/Responses.h>
#include <LibWeb/MimeSniff/MimeType.h>
namespace Web::Fetch::Infrastructure { namespace Web::Fetch::Infrastructure {
@ -24,8 +25,13 @@ GC_DEFINE_ALLOCATOR(CORSFilteredResponse);
GC_DEFINE_ALLOCATOR(OpaqueFilteredResponse); GC_DEFINE_ALLOCATOR(OpaqueFilteredResponse);
GC_DEFINE_ALLOCATOR(OpaqueRedirectFilteredResponse); GC_DEFINE_ALLOCATOR(OpaqueRedirectFilteredResponse);
Response::Response(GC::Ref<HeaderList> header_list) GC::Ref<Response> Response::create(JS::VM& vm)
: m_header_list(header_list) {
return vm.heap().allocate<Response>(HTTP::HeaderList::create());
}
Response::Response(NonnullRefPtr<HTTP::HeaderList> header_list)
: m_header_list(move(header_list))
, m_response_time(MonotonicTime::now()) , m_response_time(MonotonicTime::now())
{ {
} }
@ -33,15 +39,9 @@ Response::Response(GC::Ref<HeaderList> header_list)
void Response::visit_edges(JS::Cell::Visitor& visitor) void Response::visit_edges(JS::Cell::Visitor& visitor)
{ {
Base::visit_edges(visitor); Base::visit_edges(visitor);
visitor.visit(m_header_list);
visitor.visit(m_body); visitor.visit(m_body);
} }
GC::Ref<Response> Response::create(JS::VM& vm)
{
return vm.heap().allocate<Response>(HeaderList::create(vm));
}
// https://fetch.spec.whatwg.org/#ref-for-concept-network-error%E2%91%A3 // https://fetch.spec.whatwg.org/#ref-for-concept-network-error%E2%91%A3
// A network error is a response whose status is always 0, status message is always // A network error is a response whose status is always 0, status message is always
// the empty byte sequence, header list is always empty, and body is always null. // the empty byte sequence, header list is always empty, and body is always null.
@ -341,8 +341,8 @@ u64 Response::stale_while_revalidate_lifetime() const
// Non-standard // Non-standard
FilteredResponse::FilteredResponse(GC::Ref<Response> internal_response, GC::Ref<HeaderList> header_list) FilteredResponse::FilteredResponse(GC::Ref<Response> internal_response, NonnullRefPtr<HTTP::HeaderList> header_list)
: Response(header_list) : Response(move(header_list))
, m_internal_response(internal_response) , m_internal_response(internal_response)
{ {
} }
@ -361,27 +361,22 @@ GC::Ref<BasicFilteredResponse> BasicFilteredResponse::create(JS::VM& vm, GC::Ref
{ {
// A basic filtered response is a filtered response whose type is "basic" and header list excludes // A basic filtered response is a filtered response whose type is "basic" and header list excludes
// any headers in internal responses header list whose name is a forbidden response-header name. // any headers in internal responses header list whose name is a forbidden response-header name.
auto header_list = HeaderList::create(vm); auto header_list = HTTP::HeaderList::create();
for (auto const& header : *internal_response->header_list()) { for (auto const& header : *internal_response->header_list()) {
if (!is_forbidden_response_header_name(header.name)) if (!HTTP::is_forbidden_response_header_name(header.name))
header_list->append(header); header_list->append(header);
} }
return vm.heap().allocate<BasicFilteredResponse>(internal_response, header_list); return vm.heap().allocate<BasicFilteredResponse>(internal_response, move(header_list));
} }
BasicFilteredResponse::BasicFilteredResponse(GC::Ref<Response> internal_response, GC::Ref<HeaderList> header_list) BasicFilteredResponse::BasicFilteredResponse(GC::Ref<Response> internal_response, NonnullRefPtr<HTTP::HeaderList> header_list)
: FilteredResponse(internal_response, header_list) : FilteredResponse(internal_response, header_list)
, m_header_list(header_list) , m_header_list(move(header_list))
{ {
} }
void BasicFilteredResponse::visit_edges(JS::Cell::Visitor& visitor)
{
Base::visit_edges(visitor);
visitor.visit(m_header_list);
}
GC::Ref<CORSFilteredResponse> CORSFilteredResponse::create(JS::VM& vm, GC::Ref<Response> internal_response) GC::Ref<CORSFilteredResponse> CORSFilteredResponse::create(JS::VM& vm, GC::Ref<Response> internal_response)
{ {
// A CORS filtered response is a filtered response whose type is "cors" and header list excludes // A CORS filtered response is a filtered response whose type is "cors" and header list excludes
@ -393,7 +388,7 @@ GC::Ref<CORSFilteredResponse> CORSFilteredResponse::create(JS::VM& vm, GC::Ref<R
for (auto const& header_name : internal_response->cors_exposed_header_name_list()) for (auto const& header_name : internal_response->cors_exposed_header_name_list())
cors_exposed_header_name_list.unchecked_append(header_name); cors_exposed_header_name_list.unchecked_append(header_name);
auto header_list = HeaderList::create(vm); auto header_list = HTTP::HeaderList::create();
for (auto const& header : *internal_response->header_list()) { for (auto const& header : *internal_response->header_list()) {
if (is_cors_safelisted_response_header_name(header.name, cors_exposed_header_name_list)) if (is_cors_safelisted_response_header_name(header.name, cors_exposed_header_name_list))
header_list->append(header); header_list->append(header);
@ -402,54 +397,36 @@ GC::Ref<CORSFilteredResponse> CORSFilteredResponse::create(JS::VM& vm, GC::Ref<R
return vm.heap().allocate<CORSFilteredResponse>(internal_response, header_list); return vm.heap().allocate<CORSFilteredResponse>(internal_response, header_list);
} }
CORSFilteredResponse::CORSFilteredResponse(GC::Ref<Response> internal_response, GC::Ref<HeaderList> header_list) CORSFilteredResponse::CORSFilteredResponse(GC::Ref<Response> internal_response, NonnullRefPtr<HTTP::HeaderList> header_list)
: FilteredResponse(internal_response, header_list) : FilteredResponse(internal_response, header_list)
, m_header_list(header_list) , m_header_list(move(header_list))
{ {
} }
void CORSFilteredResponse::visit_edges(JS::Cell::Visitor& visitor)
{
Base::visit_edges(visitor);
visitor.visit(m_header_list);
}
GC::Ref<OpaqueFilteredResponse> OpaqueFilteredResponse::create(JS::VM& vm, GC::Ref<Response> internal_response) GC::Ref<OpaqueFilteredResponse> OpaqueFilteredResponse::create(JS::VM& vm, GC::Ref<Response> internal_response)
{ {
// An opaque filtered response is a filtered response whose type is "opaque", URL list is the empty list, // An opaque filtered response is a filtered response whose type is "opaque", URL list is the empty list,
// status is 0, status message is the empty byte sequence, header list is empty, and body is null. // status is 0, status message is the empty byte sequence, header list is empty, and body is null.
return vm.heap().allocate<OpaqueFilteredResponse>(internal_response, HeaderList::create(vm)); return vm.heap().allocate<OpaqueFilteredResponse>(internal_response, HTTP::HeaderList::create());
} }
OpaqueFilteredResponse::OpaqueFilteredResponse(GC::Ref<Response> internal_response, GC::Ref<HeaderList> header_list) OpaqueFilteredResponse::OpaqueFilteredResponse(GC::Ref<Response> internal_response, NonnullRefPtr<HTTP::HeaderList> header_list)
: FilteredResponse(internal_response, header_list) : FilteredResponse(internal_response, header_list)
, m_header_list(header_list) , m_header_list(move(header_list))
{ {
} }
void OpaqueFilteredResponse::visit_edges(JS::Cell::Visitor& visitor)
{
Base::visit_edges(visitor);
visitor.visit(m_header_list);
}
GC::Ref<OpaqueRedirectFilteredResponse> OpaqueRedirectFilteredResponse::create(JS::VM& vm, GC::Ref<Response> internal_response) GC::Ref<OpaqueRedirectFilteredResponse> OpaqueRedirectFilteredResponse::create(JS::VM& vm, GC::Ref<Response> internal_response)
{ {
// An opaque-redirect filtered response is a filtered response whose type is "opaqueredirect", // An opaque-redirect filtered response is a filtered response whose type is "opaqueredirect",
// status is 0, status message is the empty byte sequence, header list is empty, and body is null. // status is 0, status message is the empty byte sequence, header list is empty, and body is null.
return vm.heap().allocate<OpaqueRedirectFilteredResponse>(internal_response, HeaderList::create(vm)); return vm.heap().allocate<OpaqueRedirectFilteredResponse>(internal_response, HTTP::HeaderList::create());
} }
OpaqueRedirectFilteredResponse::OpaqueRedirectFilteredResponse(GC::Ref<Response> internal_response, GC::Ref<HeaderList> header_list) OpaqueRedirectFilteredResponse::OpaqueRedirectFilteredResponse(GC::Ref<Response> internal_response, NonnullRefPtr<HTTP::HeaderList> header_list)
: FilteredResponse(internal_response, header_list) : FilteredResponse(internal_response, header_list)
, m_header_list(header_list) , m_header_list(move(header_list))
{ {
} }
void OpaqueRedirectFilteredResponse::visit_edges(JS::Cell::Visitor& visitor)
{
Base::visit_edges(visitor);
visitor.visit(m_header_list);
}
} }

View file

@ -13,13 +13,13 @@
#include <AK/Time.h> #include <AK/Time.h>
#include <AK/Vector.h> #include <AK/Vector.h>
#include <LibGC/Ptr.h> #include <LibGC/Ptr.h>
#include <LibHTTP/HeaderList.h>
#include <LibJS/Forward.h> #include <LibJS/Forward.h>
#include <LibJS/Heap/Cell.h> #include <LibJS/Heap/Cell.h>
#include <LibURL/URL.h> #include <LibURL/URL.h>
#include <LibWeb/Export.h> #include <LibWeb/Export.h>
#include <LibWeb/Fetch/Infrastructure/HTTP.h> #include <LibWeb/Fetch/Infrastructure/HTTP.h>
#include <LibWeb/Fetch/Infrastructure/HTTP/Bodies.h> #include <LibWeb/Fetch/Infrastructure/HTTP/Bodies.h>
#include <LibWeb/Fetch/Infrastructure/HTTP/Headers.h>
#include <LibWeb/Fetch/Infrastructure/HTTP/Statuses.h> #include <LibWeb/Fetch/Infrastructure/HTTP/Statuses.h>
namespace Web::Fetch::Infrastructure { namespace Web::Fetch::Infrastructure {
@ -81,8 +81,8 @@ public:
[[nodiscard]] virtual ByteString const& status_message() const { return m_status_message; } [[nodiscard]] virtual ByteString const& status_message() const { return m_status_message; }
virtual void set_status_message(ByteString status_message) { m_status_message = move(status_message); } virtual void set_status_message(ByteString status_message) { m_status_message = move(status_message); }
[[nodiscard]] virtual GC::Ref<HeaderList> header_list() const { return m_header_list; } virtual NonnullRefPtr<HTTP::HeaderList> const& header_list() const { return m_header_list; }
virtual void set_header_list(GC::Ref<HeaderList> header_list) { m_header_list = header_list; } virtual void set_header_list(NonnullRefPtr<HTTP::HeaderList> header_list) { m_header_list = move(header_list); }
[[nodiscard]] virtual GC::Ptr<Body> body() const { return m_body; } [[nodiscard]] virtual GC::Ptr<Body> body() const { return m_body; }
virtual void set_body(GC::Ptr<Body> body) { m_body = body; } virtual void set_body(GC::Ptr<Body> body) { m_body = body; }
@ -130,7 +130,7 @@ public:
MonotonicTime response_time() const { return m_response_time; } MonotonicTime response_time() const { return m_response_time; }
protected: protected:
explicit Response(GC::Ref<HeaderList>); explicit Response(NonnullRefPtr<HTTP::HeaderList>);
virtual void visit_edges(JS::Cell::Visitor&) override; virtual void visit_edges(JS::Cell::Visitor&) override;
@ -157,7 +157,7 @@ private:
// https://fetch.spec.whatwg.org/#concept-response-header-list // https://fetch.spec.whatwg.org/#concept-response-header-list
// A response has an associated header list (a header list). Unless stated otherwise it is empty. // A response has an associated header list (a header list). Unless stated otherwise it is empty.
GC::Ref<HeaderList> m_header_list; NonnullRefPtr<HTTP::HeaderList> m_header_list;
// https://fetch.spec.whatwg.org/#concept-response-body // https://fetch.spec.whatwg.org/#concept-response-body
// A response has an associated body (null or a body). Unless stated otherwise it is null. // A response has an associated body (null or a body). Unless stated otherwise it is null.
@ -215,7 +215,7 @@ class FilteredResponse : public Response {
GC_CELL(FilteredResponse, Response); GC_CELL(FilteredResponse, Response);
public: public:
FilteredResponse(GC::Ref<Response>, GC::Ref<HeaderList>); FilteredResponse(GC::Ref<Response>, NonnullRefPtr<HTTP::HeaderList>);
virtual ~FilteredResponse() = 0; virtual ~FilteredResponse() = 0;
[[nodiscard]] virtual Type type() const override { return m_internal_response->type(); } [[nodiscard]] virtual Type type() const override { return m_internal_response->type(); }
@ -233,8 +233,8 @@ public:
[[nodiscard]] virtual ByteString const& status_message() const override { return m_internal_response->status_message(); } [[nodiscard]] virtual ByteString const& status_message() const override { return m_internal_response->status_message(); }
virtual void set_status_message(ByteString status_message) override { m_internal_response->set_status_message(move(status_message)); } virtual void set_status_message(ByteString status_message) override { m_internal_response->set_status_message(move(status_message)); }
[[nodiscard]] virtual GC::Ref<HeaderList> header_list() const override { return m_internal_response->header_list(); } virtual NonnullRefPtr<HTTP::HeaderList> const& header_list() const override { return m_internal_response->header_list(); }
virtual void set_header_list(GC::Ref<HeaderList> header_list) override { m_internal_response->set_header_list(header_list); } virtual void set_header_list(NonnullRefPtr<HTTP::HeaderList> header_list) override { m_internal_response->set_header_list(header_list); }
[[nodiscard]] virtual GC::Ptr<Body> body() const override { return m_internal_response->body(); } [[nodiscard]] virtual GC::Ptr<Body> body() const override { return m_internal_response->body(); }
virtual void set_body(GC::Ptr<Body> body) override { m_internal_response->set_body(body); } virtual void set_body(GC::Ptr<Body> body) override { m_internal_response->set_body(body); }
@ -276,14 +276,12 @@ public:
[[nodiscard]] static GC::Ref<BasicFilteredResponse> create(JS::VM&, GC::Ref<Response>); [[nodiscard]] static GC::Ref<BasicFilteredResponse> create(JS::VM&, GC::Ref<Response>);
[[nodiscard]] virtual Type type() const override { return Type::Basic; } [[nodiscard]] virtual Type type() const override { return Type::Basic; }
[[nodiscard]] virtual GC::Ref<HeaderList> header_list() const override { return m_header_list; } virtual NonnullRefPtr<HTTP::HeaderList> const& header_list() const override { return m_header_list; }
private: private:
BasicFilteredResponse(GC::Ref<Response>, GC::Ref<HeaderList>); BasicFilteredResponse(GC::Ref<Response>, NonnullRefPtr<HTTP::HeaderList>);
virtual void visit_edges(JS::Cell::Visitor&) override; NonnullRefPtr<HTTP::HeaderList> m_header_list;
GC::Ref<HeaderList> m_header_list;
}; };
// https://fetch.spec.whatwg.org/#concept-filtered-response-cors // https://fetch.spec.whatwg.org/#concept-filtered-response-cors
@ -295,14 +293,12 @@ public:
[[nodiscard]] static GC::Ref<CORSFilteredResponse> create(JS::VM&, GC::Ref<Response>); [[nodiscard]] static GC::Ref<CORSFilteredResponse> create(JS::VM&, GC::Ref<Response>);
[[nodiscard]] virtual Type type() const override { return Type::CORS; } [[nodiscard]] virtual Type type() const override { return Type::CORS; }
[[nodiscard]] virtual GC::Ref<HeaderList> header_list() const override { return m_header_list; } virtual NonnullRefPtr<HTTP::HeaderList> const& header_list() const override { return m_header_list; }
private: private:
CORSFilteredResponse(GC::Ref<Response>, GC::Ref<HeaderList>); CORSFilteredResponse(GC::Ref<Response>, NonnullRefPtr<HTTP::HeaderList>);
virtual void visit_edges(JS::Cell::Visitor&) override; NonnullRefPtr<HTTP::HeaderList> m_header_list;
GC::Ref<HeaderList> m_header_list;
}; };
// https://fetch.spec.whatwg.org/#concept-filtered-response-opaque // https://fetch.spec.whatwg.org/#concept-filtered-response-opaque
@ -318,17 +314,15 @@ public:
[[nodiscard]] virtual Vector<URL::URL>& url_list() override { return m_url_list; } [[nodiscard]] virtual Vector<URL::URL>& url_list() override { return m_url_list; }
[[nodiscard]] virtual Status status() const override { return 0; } [[nodiscard]] virtual Status status() const override { return 0; }
[[nodiscard]] virtual ByteString const& status_message() const override { return m_method; } [[nodiscard]] virtual ByteString const& status_message() const override { return m_method; }
[[nodiscard]] virtual GC::Ref<HeaderList> header_list() const override { return m_header_list; } virtual NonnullRefPtr<HTTP::HeaderList> const& header_list() const override { return m_header_list; }
[[nodiscard]] virtual GC::Ptr<Body> body() const override { return nullptr; } [[nodiscard]] virtual GC::Ptr<Body> body() const override { return nullptr; }
private: private:
OpaqueFilteredResponse(GC::Ref<Response>, GC::Ref<HeaderList>); OpaqueFilteredResponse(GC::Ref<Response>, NonnullRefPtr<HTTP::HeaderList>);
virtual void visit_edges(JS::Cell::Visitor&) override;
Vector<URL::URL> m_url_list; Vector<URL::URL> m_url_list;
ByteString const m_method; ByteString const m_method;
GC::Ref<HeaderList> m_header_list; NonnullRefPtr<HTTP::HeaderList> m_header_list;
}; };
// https://fetch.spec.whatwg.org/#concept-filtered-response-opaque-redirect // https://fetch.spec.whatwg.org/#concept-filtered-response-opaque-redirect
@ -342,16 +336,14 @@ public:
[[nodiscard]] virtual Type type() const override { return Type::OpaqueRedirect; } [[nodiscard]] virtual Type type() const override { return Type::OpaqueRedirect; }
[[nodiscard]] virtual Status status() const override { return 0; } [[nodiscard]] virtual Status status() const override { return 0; }
[[nodiscard]] virtual ByteString const& status_message() const override { return m_method; } [[nodiscard]] virtual ByteString const& status_message() const override { return m_method; }
[[nodiscard]] virtual GC::Ref<HeaderList> header_list() const override { return m_header_list; } virtual NonnullRefPtr<HTTP::HeaderList> const& header_list() const override { return m_header_list; }
[[nodiscard]] virtual GC::Ptr<Body> body() const override { return nullptr; } [[nodiscard]] virtual GC::Ptr<Body> body() const override { return nullptr; }
private: private:
OpaqueRedirectFilteredResponse(GC::Ref<Response>, GC::Ref<HeaderList>); OpaqueRedirectFilteredResponse(GC::Ref<Response>, NonnullRefPtr<HTTP::HeaderList>);
virtual void visit_edges(JS::Cell::Visitor&) override;
ByteString const m_method; ByteString const m_method;
GC::Ref<HeaderList> m_header_list; NonnullRefPtr<HTTP::HeaderList> m_header_list;
}; };
} }

View file

@ -4,7 +4,7 @@
* SPDX-License-Identifier: BSD-2-Clause * SPDX-License-Identifier: BSD-2-Clause
*/ */
#include <LibWeb/Fetch/Infrastructure/HTTP/Headers.h> #include <LibHTTP/HeaderList.h>
#include <LibWeb/Fetch/Infrastructure/HTTP/MIME.h> #include <LibWeb/Fetch/Infrastructure/HTTP/MIME.h>
#include <LibWeb/Fetch/Infrastructure/HTTP/Requests.h> #include <LibWeb/Fetch/Infrastructure/HTTP/Requests.h>
#include <LibWeb/Fetch/Infrastructure/HTTP/Responses.h> #include <LibWeb/Fetch/Infrastructure/HTTP/Responses.h>
@ -14,7 +14,7 @@
namespace Web::Fetch::Infrastructure { namespace Web::Fetch::Infrastructure {
// https://fetch.spec.whatwg.org/#determine-nosniff // https://fetch.spec.whatwg.org/#determine-nosniff
bool determine_nosniff(HeaderList const& list) bool determine_nosniff(HTTP::HeaderList const& list)
{ {
// 1. Let values be the result of getting, decoding, and splitting `X-Content-Type-Options` from list. // 1. Let values be the result of getting, decoding, and splitting `X-Content-Type-Options` from list.
auto values = list.get_decode_and_split("X-Content-Type-Options"sv.bytes()); auto values = list.get_decode_and_split("X-Content-Type-Options"sv.bytes());

View file

@ -7,13 +7,14 @@
#pragma once #pragma once
#include <AK/Forward.h> #include <AK/Forward.h>
#include <LibHTTP/Forward.h>
#include <LibWeb/Export.h> #include <LibWeb/Export.h>
#include <LibWeb/Fetch/Infrastructure/RequestOrResponseBlocking.h> #include <LibWeb/Fetch/Infrastructure/RequestOrResponseBlocking.h>
#include <LibWeb/Forward.h> #include <LibWeb/Forward.h>
namespace Web::Fetch::Infrastructure { namespace Web::Fetch::Infrastructure {
[[nodiscard]] bool determine_nosniff(HeaderList const&); [[nodiscard]] bool determine_nosniff(HTTP::HeaderList const&);
[[nodiscard]] WEB_API RequestOrResponseBlocking should_response_to_request_be_blocked_due_to_nosniff(Response const&, Request const&); [[nodiscard]] WEB_API RequestOrResponseBlocking should_response_to_request_be_blocked_due_to_nosniff(Response const&, Request const&);
} }

View file

@ -4,6 +4,7 @@
* SPDX-License-Identifier: BSD-2-Clause * SPDX-License-Identifier: BSD-2-Clause
*/ */
#include <LibHTTP/Method.h>
#include <LibJS/Runtime/Completion.h> #include <LibJS/Runtime/Completion.h>
#include <LibWeb/Bindings/Intrinsics.h> #include <LibWeb/Bindings/Intrinsics.h>
#include <LibWeb/Bindings/RequestPrototype.h> #include <LibWeb/Bindings/RequestPrototype.h>
@ -12,9 +13,7 @@
#include <LibWeb/Fetch/Enums.h> #include <LibWeb/Fetch/Enums.h>
#include <LibWeb/Fetch/Headers.h> #include <LibWeb/Fetch/Headers.h>
#include <LibWeb/Fetch/Infrastructure/HTTP/Bodies.h> #include <LibWeb/Fetch/Infrastructure/HTTP/Bodies.h>
#include <LibWeb/Fetch/Infrastructure/HTTP/Headers.h>
#include <LibWeb/Fetch/Infrastructure/HTTP/MIME.h> #include <LibWeb/Fetch/Infrastructure/HTTP/MIME.h>
#include <LibWeb/Fetch/Infrastructure/HTTP/Methods.h>
#include <LibWeb/Fetch/Infrastructure/HTTP/Requests.h> #include <LibWeb/Fetch/Infrastructure/HTTP/Requests.h>
#include <LibWeb/Fetch/Request.h> #include <LibWeb/Fetch/Request.h>
#include <LibWeb/HTML/Scripting/Environments.h> #include <LibWeb/HTML/Scripting/Environments.h>
@ -188,7 +187,7 @@ WebIDL::ExceptionOr<GC::Ref<Request>> Request::construct_impl(JS::Realm& realm,
// header list // header list
// A copy of requests header list. // A copy of requests header list.
auto header_list_copy = Infrastructure::HeaderList::create(vm); auto header_list_copy = HTTP::HeaderList::create();
for (auto& header : *input_request->header_list()) for (auto& header : *input_request->header_list())
header_list_copy->append(header); header_list_copy->append(header);
request->set_header_list(header_list_copy); request->set_header_list(header_list_copy);
@ -370,13 +369,13 @@ WebIDL::ExceptionOr<GC::Ref<Request>> Request::construct_impl(JS::Realm& realm,
auto method = *init.method; auto method = *init.method;
// 2. If method is not a method or method is a forbidden method, then throw a TypeError. // 2. If method is not a method or method is a forbidden method, then throw a TypeError.
if (!Infrastructure::is_method(method)) if (!HTTP::is_method(method))
return WebIDL::SimpleException { WebIDL::SimpleExceptionType::TypeError, "Method has invalid value"sv }; return WebIDL::SimpleException { WebIDL::SimpleExceptionType::TypeError, "Method has invalid value"sv };
if (Infrastructure::is_forbidden_method(method)) if (HTTP::is_forbidden_method(method))
return WebIDL::SimpleException { WebIDL::SimpleExceptionType::TypeError, "Method must not be one of CONNECT, TRACE, or TRACK"sv }; return WebIDL::SimpleException { WebIDL::SimpleExceptionType::TypeError, "Method must not be one of CONNECT, TRACE, or TRACK"sv };
// 3. Normalize method. // 3. Normalize method.
auto normalized_method = Infrastructure::normalize_method(method); auto normalized_method = HTTP::normalize_method(method);
// 4. Set requests method to method. // 4. Set requests method to method.
request->set_method(move(normalized_method)); request->set_method(move(normalized_method));
@ -410,7 +409,7 @@ WebIDL::ExceptionOr<GC::Ref<Request>> Request::construct_impl(JS::Realm& realm,
// 32. If thiss requests mode is "no-cors", then: // 32. If thiss requests mode is "no-cors", then:
if (request_object->request()->mode() == Infrastructure::Request::Mode::NoCORS) { if (request_object->request()->mode() == Infrastructure::Request::Mode::NoCORS) {
// 1. If thiss requests method is not a CORS-safelisted method, then throw a TypeError. // 1. If thiss requests method is not a CORS-safelisted method, then throw a TypeError.
if (!Infrastructure::is_cors_safelisted_method(request_object->request()->method())) if (!HTTP::is_cors_safelisted_method(request_object->request()->method()))
return WebIDL::SimpleException { WebIDL::SimpleExceptionType::TypeError, "Method must be one of GET, HEAD, or POST"sv }; return WebIDL::SimpleException { WebIDL::SimpleExceptionType::TypeError, "Method must be one of GET, HEAD, or POST"sv };
// 2. Set thiss headerss guard to "request-no-cors". // 2. Set thiss headerss guard to "request-no-cors".
@ -420,24 +419,28 @@ WebIDL::ExceptionOr<GC::Ref<Request>> Request::construct_impl(JS::Realm& realm,
// 33. If init is not empty, then: // 33. If init is not empty, then:
if (!init.is_empty()) { if (!init.is_empty()) {
// 1. Let headers be a copy of thiss headers and its associated header list. // 1. Let headers be a copy of thiss headers and its associated header list.
auto headers = Variant<HeadersInit, GC::Ref<Infrastructure::HeaderList>> { request_object->headers()->header_list() };
// 2. If init["headers"] exists, then set headers to init["headers"]. // 2. If init["headers"] exists, then set headers to init["headers"].
if (init.headers.has_value()) auto headers = [&]() -> Variant<HeadersInit, NonnullRefPtr<HTTP::HeaderList>> {
headers = *init.headers; if (init.headers.has_value())
return init.headers.value();
return HTTP::HeaderList::create(request_object->headers()->header_list()->headers());
}();
// 3. Empty thiss headerss header list. // 3. Empty thiss headerss header list.
request_object->headers()->header_list()->clear(); request_object->headers()->header_list()->clear();
// 4. If headers is a Headers object, then for each header of its header list, append header to thiss headers. TRY(headers.visit(
if (auto* header_list = headers.get_pointer<GC::Ref<Infrastructure::HeaderList>>()) { // 4. If headers is a Headers object, then for each header of its header list, append header to thiss headers.
for (auto& header : *header_list->ptr()) [&](NonnullRefPtr<HTTP::HeaderList> const& headers) -> WebIDL::ExceptionOr<void> {
TRY(request_object->headers()->append(Infrastructure::Header::isomorphic_encode(header.name, header.value))); for (auto const& header : *headers)
} TRY(request_object->headers()->append(HTTP::Header::isomorphic_encode(header.name, header.value)));
// 5. Otherwise, fill thiss headers with headers. return {};
else { },
TRY(request_object->headers()->fill(headers.get<HeadersInit>())); // 5. Otherwise, fill thiss headers with headers.
} [&](HeadersInit const& headers) -> WebIDL::ExceptionOr<void> {
TRY(request_object->headers()->fill(headers));
return {};
}));
} }
// 34. Let inputBody be inputs requests body if input is a Request object; otherwise null. // 34. Let inputBody be inputs requests body if input is a Request object; otherwise null.
@ -465,7 +468,7 @@ WebIDL::ExceptionOr<GC::Ref<Request>> Request::construct_impl(JS::Realm& realm,
// 4. If type is non-null and thiss headerss header list does not contain `Content-Type`, then append (`Content-Type`, type) to thiss headers. // 4. If type is non-null and thiss headerss header list does not contain `Content-Type`, then append (`Content-Type`, type) to thiss headers.
if (type.has_value() && !request_object->headers()->header_list()->contains("Content-Type"sv)) if (type.has_value() && !request_object->headers()->header_list()->contains("Content-Type"sv))
TRY(request_object->headers()->append(Infrastructure::Header::isomorphic_encode("Content-Type"sv, *type))); TRY(request_object->headers()->append(HTTP::Header::isomorphic_encode("Content-Type"sv, *type)));
} }
// 38. Let inputOrInitBody be initBody if it is non-null; otherwise inputBody. // 38. Let inputOrInitBody be initBody if it is non-null; otherwise inputBody.

View file

@ -200,7 +200,7 @@ WebIDL::ExceptionOr<GC::Ref<Response>> Response::redirect(JS::VM& vm, String con
auto value = parsed_url->serialize(); auto value = parsed_url->serialize();
// 7. Append (`Location`, value) to responseObjects responses header list. // 7. Append (`Location`, value) to responseObjects responses header list.
auto header = Infrastructure::Header::isomorphic_encode("Location"sv, value); auto header = HTTP::Header::isomorphic_encode("Location"sv, value);
response_object->response()->header_list()->append(move(header)); response_object->response()->header_list()->append(move(header));
// 8. Return responseObject. // 8. Return responseObject.

View file

@ -542,14 +542,12 @@ class FetchController;
class FetchParams; class FetchParams;
class FetchRecord; class FetchRecord;
class FetchTimingInfo; class FetchTimingInfo;
class HeaderList;
class IncrementalReadLoopReadRequest; class IncrementalReadLoopReadRequest;
class Request; class Request;
class Response; class Response;
struct BodyWithType; struct BodyWithType;
struct ConnectionTimingInfo; struct ConnectionTimingInfo;
struct Header;
} }

View file

@ -15,7 +15,6 @@
#include <LibWeb/Fetch/Fetching/Fetching.h> #include <LibWeb/Fetch/Fetching/Fetching.h>
#include <LibWeb/Fetch/Infrastructure/FetchAlgorithms.h> #include <LibWeb/Fetch/Infrastructure/FetchAlgorithms.h>
#include <LibWeb/Fetch/Infrastructure/FetchController.h> #include <LibWeb/Fetch/Infrastructure/FetchController.h>
#include <LibWeb/Fetch/Infrastructure/HTTP/Headers.h>
#include <LibWeb/Fetch/Infrastructure/HTTP/MIME.h> #include <LibWeb/Fetch/Infrastructure/HTTP/MIME.h>
#include <LibWeb/Fetch/Infrastructure/HTTP/Requests.h> #include <LibWeb/Fetch/Infrastructure/HTTP/Requests.h>
#include <LibWeb/Fetch/Infrastructure/HTTP/Responses.h> #include <LibWeb/Fetch/Infrastructure/HTTP/Responses.h>
@ -319,7 +318,7 @@ void EventSource::reestablish_the_connection()
if (!m_last_event_id.is_empty()) { if (!m_last_event_id.is_empty()) {
// 1. Let lastEventIDValue be the EventSource object's last event ID string, encoded as UTF-8. // 1. Let lastEventIDValue be the EventSource object's last event ID string, encoded as UTF-8.
// 2. Set (`Last-Event-ID`, lastEventIDValue) in request's header list. // 2. Set (`Last-Event-ID`, lastEventIDValue) in request's header list.
auto header = Fetch::Infrastructure::Header::isomorphic_encode("Last-Event-ID"sv, m_last_event_id); auto header = HTTP::Header::isomorphic_encode("Last-Event-ID"sv, m_last_event_id);
request->header_list()->set(move(header)); request->header_list()->set(move(header));
} }

View file

@ -1154,7 +1154,7 @@ static void create_navigation_params_by_fetching(GC::Ptr<SessionHistoryEntry> en
request_content_type = request_content_type_buffer.string_view(); request_content_type = request_content_type_buffer.string_view();
} }
auto header = Fetch::Infrastructure::Header::isomorphic_encode("Content-Type"sv, request_content_type); auto header = HTTP::Header::isomorphic_encode("Content-Type"sv, request_content_type);
request->header_list()->append(move(header)); request->header_list()->append(move(header));
} }

View file

@ -39,7 +39,7 @@ WebIDL::ExceptionOr<bool> NavigatorBeaconPartial::send_beacon(String const& url,
return WebIDL::SimpleException { WebIDL::SimpleExceptionType::TypeError, MUST(String::formatted("Beacon URL {} must be either http:// or https://.", url)) }; return WebIDL::SimpleException { WebIDL::SimpleExceptionType::TypeError, MUST(String::formatted("Beacon URL {} must be either http:// or https://.", url)) };
// 4. Let headerList be an empty list. // 4. Let headerList be an empty list.
auto header_list = Fetch::Infrastructure::HeaderList::create(vm); auto header_list = HTTP::HeaderList::create();
// 5. Let corsMode be "no-cors". // 5. Let corsMode be "no-cors".
auto cors_mode = Fetch::Infrastructure::Request::Mode::NoCORS; auto cors_mode = Fetch::Infrastructure::Request::Mode::NoCORS;
@ -62,7 +62,7 @@ WebIDL::ExceptionOr<bool> NavigatorBeaconPartial::send_beacon(String const& url,
cors_mode = Fetch::Infrastructure::Request::Mode::CORS; cors_mode = Fetch::Infrastructure::Request::Mode::CORS;
// If contentType value is a CORS-safelisted request-header value for the Content-Type header, set corsMode to "no-cors". // If contentType value is a CORS-safelisted request-header value for the Content-Type header, set corsMode to "no-cors".
auto content_type_header = Fetch::Infrastructure::Header::isomorphic_encode("Content-Type"sv, content_type.value()); auto content_type_header = HTTP::Header::isomorphic_encode("Content-Type"sv, content_type.value());
if (Fetch::Infrastructure::is_cors_safelisted_request_header(content_type_header)) if (Fetch::Infrastructure::is_cors_safelisted_request_header(content_type_header))
cors_mode = Fetch::Infrastructure::Request::Mode::NoCORS; cors_mode = Fetch::Infrastructure::Request::Mode::NoCORS;

View file

@ -16,7 +16,6 @@
#include <LibWeb/DOMURL/DOMURL.h> #include <LibWeb/DOMURL/DOMURL.h>
#include <LibWeb/Fetch/Fetching/Fetching.h> #include <LibWeb/Fetch/Fetching/Fetching.h>
#include <LibWeb/Fetch/Infrastructure/FetchAlgorithms.h> #include <LibWeb/Fetch/Infrastructure/FetchAlgorithms.h>
#include <LibWeb/Fetch/Infrastructure/HTTP/Headers.h>
#include <LibWeb/Fetch/Infrastructure/HTTP/MIME.h> #include <LibWeb/Fetch/Infrastructure/HTTP/MIME.h>
#include <LibWeb/Fetch/Infrastructure/HTTP/Requests.h> #include <LibWeb/Fetch/Infrastructure/HTTP/Requests.h>
#include <LibWeb/Fetch/Infrastructure/HTTP/Responses.h> #include <LibWeb/Fetch/Infrastructure/HTTP/Responses.h>

View file

@ -7,9 +7,9 @@
#pragma once #pragma once
#include <AK/ByteBuffer.h> #include <AK/ByteBuffer.h>
#include <AK/HashMap.h>
#include <AK/Time.h> #include <AK/Time.h>
#include <LibCore/ElapsedTimer.h> #include <LibCore/ElapsedTimer.h>
#include <LibHTTP/HeaderList.h>
#include <LibURL/URL.h> #include <LibURL/URL.h>
#include <LibWeb/Export.h> #include <LibWeb/Export.h>
#include <LibWeb/Forward.h> #include <LibWeb/Forward.h>
@ -19,7 +19,10 @@ namespace Web {
class WEB_API LoadRequest { class WEB_API LoadRequest {
public: public:
LoadRequest() = default; explicit LoadRequest(NonnullRefPtr<HTTP::HeaderList> headers)
: m_headers(move(headers))
{
}
Optional<URL::URL> const& url() const { return m_url; } Optional<URL::URL> const& url() const { return m_url; }
void set_url(Optional<URL::URL> url) { m_url = move(url); } void set_url(Optional<URL::URL> url) { m_url = move(url); }
@ -39,13 +42,12 @@ public:
GC::Ptr<Page> page() const { return m_page.ptr(); } GC::Ptr<Page> page() const { return m_page.ptr(); }
void set_page(Page& page) { m_page = page; } void set_page(Page& page) { m_page = page; }
void set_header(ByteString const& name, ByteString const& value) { m_headers.set(name, value); } HTTP::HeaderList const& headers() const { return m_headers; }
HashMap<ByteString, ByteString, CaseInsensitiveStringTraits> const& headers() const { return m_headers; }
private: private:
Optional<URL::URL> m_url; Optional<URL::URL> m_url;
ByteString m_method { "GET" }; ByteString m_method { "GET" };
HashMap<ByteString, ByteString, CaseInsensitiveStringTraits> m_headers; NonnullRefPtr<HTTP::HeaderList> m_headers;
ByteBuffer m_body; ByteBuffer m_body;
Core::ElapsedTimer m_load_timer; Core::ElapsedTimer m_load_timer;
GC::Root<Page> m_page; GC::Root<Page> m_page;

View file

@ -109,19 +109,20 @@ static void store_response_cookies(Page& page, URL::URL const& url, ByteString c
page.client().page_did_set_cookie(url, cookie.value(), Cookie::Source::Http); // FIXME: Determine cookie source correctly page.client().page_did_set_cookie(url, cookie.value(), Cookie::Source::Http); // FIXME: Determine cookie source correctly
} }
static HTTP::HeaderMap response_headers_for_file(StringView path, Optional<time_t> const& modified_time) static NonnullRefPtr<HTTP::HeaderList> response_headers_for_file(StringView path, Optional<time_t> const& modified_time)
{ {
// For file:// and resource:// URLs, we have to guess the MIME type, since there's no HTTP header to tell us what // For file:// and resource:// URLs, we have to guess the MIME type, since there's no HTTP header to tell us what
// it is. We insert a fake Content-Type header here, so that clients can use it to learn the MIME type. // it is. We insert a fake Content-Type header here, so that clients can use it to learn the MIME type.
auto mime_type = Core::guess_mime_type_based_on_filename(path); auto mime_type = Core::guess_mime_type_based_on_filename(path);
HTTP::HeaderMap response_headers; auto response_headers = HTTP::HeaderList::create({
response_headers.set("Access-Control-Allow-Origin"sv, "null"sv); { "Access-Control-Allow-Origin"sv, "null"sv },
response_headers.set("Content-Type"sv, mime_type); { "Content-Type"sv, mime_type },
});
if (modified_time.has_value()) { if (modified_time.has_value()) {
auto const datetime = AK::UnixDateTime::from_seconds_since_epoch(modified_time.value()); auto const datetime = AK::UnixDateTime::from_seconds_since_epoch(modified_time.value());
response_headers.set("Last-Modified"sv, datetime.to_byte_string("%a, %d %b %Y %H:%M:%S GMT"sv, AK::UnixDateTime::LocalTime::No)); response_headers->set({ "Last-Modified"sv, datetime.to_byte_string("%a, %d %b %Y %H:%M:%S GMT"sv, AK::UnixDateTime::LocalTime::No) });
} }
return response_headers; return response_headers;
@ -217,9 +218,10 @@ void ResourceLoader::handle_file_load_request(LoadRequest& request, FileHandler
return; return;
} }
FileLoadResult load_result; FileLoadResult load_result {
load_result.data = maybe_response.value().bytes(); .data = maybe_response.value().bytes(),
load_result.response_headers.set("Content-Type"sv, "text/html"sv); .response_headers = HTTP::HeaderList::create({ { "Content-Type"sv, "text/html"sv } }),
};
on_file(load_result); on_file(load_result);
return; return;
} }
@ -243,9 +245,10 @@ void ResourceLoader::handle_file_load_request(LoadRequest& request, FileHandler
return; return;
} }
FileLoadResult load_result; FileLoadResult load_result {
load_result.data = maybe_data.value().bytes(); .data = maybe_data.value().bytes(),
load_result.response_headers = response_headers_for_file(request.url()->file_path(), st_or_error.value().st_mtime); .response_headers = response_headers_for_file(request.url()->file_path(), st_or_error.value().st_mtime),
};
on_file(load_result); on_file(load_result);
}); });
@ -263,8 +266,9 @@ void ResourceLoader::handle_about_load_request(LoadRequest const& request, Callb
dbgln_if(SPAM_DEBUG, "Loading about: URL {}", url); dbgln_if(SPAM_DEBUG, "Loading about: URL {}", url);
HTTP::HeaderMap response_headers; auto response_headers = HTTP::HeaderList::create({
response_headers.set("Content-Type"sv, "text/html; charset=UTF-8"sv); { "Content-Type"sv, "text/html; charset=UTF-8"sv },
});
// FIXME: Implement timing info for about requests. // FIXME: Implement timing info for about requests.
Requests::RequestTimingInfo timing_info {}; Requests::RequestTimingInfo timing_info {};
@ -324,23 +328,22 @@ void ResourceLoader::handle_resource_load_request(LoadRequest const& request, Re
return; return;
} }
FileLoadResult load_result; FileLoadResult load_result {
load_result.data = maybe_response.value().bytes(); .data = maybe_response.value().bytes(),
load_result.response_headers.set("Content-Type"sv, "text/html"sv); .response_headers = HTTP::HeaderList::create({ { "Content-Type"sv, "text/html"sv } }),
};
on_resource(load_result); on_resource(load_result);
return; return;
} }
auto const& buffer = resource_value->data();
auto response_headers = response_headers_for_file(url.file_path(), resource_value->modified_time());
// FIXME: Implement timing info for resource requests. // FIXME: Implement timing info for resource requests.
Requests::RequestTimingInfo timing_info {}; Requests::RequestTimingInfo timing_info {};
FileLoadResult load_result; FileLoadResult load_result {
load_result.data = buffer; .data = resource_value->data(),
load_result.response_headers = move(response_headers); .response_headers = response_headers_for_file(url.file_path(), resource_value->modified_time()),
load_result.timing_info = timing_info; .timing_info = timing_info,
};
on_resource(load_result); on_resource(load_result);
} }
@ -357,7 +360,7 @@ void ResourceLoader::load(LoadRequest& request, GC::Root<OnHeadersReceived> on_h
} }
if (url.scheme() == "about"sv) { if (url.scheme() == "about"sv) {
handle_about_load_request(request, [on_headers_received, on_data_received, on_complete, request](ReadonlyBytes data, Requests::RequestTimingInfo const& timing_info, HTTP::HeaderMap const& response_headers) { handle_about_load_request(request, [on_headers_received, on_data_received, on_complete, request](ReadonlyBytes data, Requests::RequestTimingInfo const& timing_info, HTTP::HeaderList const& response_headers) {
log_success(request); log_success(request);
on_headers_received->function()(response_headers, {}, {}); on_headers_received->function()(response_headers, {}, {});
on_data_received->function()(data); on_data_received->function()(data);
@ -439,22 +442,13 @@ RefPtr<Requests::Request> ResourceLoader::start_network_request(LoadRequest cons
{ {
auto proxy = ProxyMappings::the().proxy_for_url(request.url().value()); auto proxy = ProxyMappings::the().proxy_for_url(request.url().value());
HTTP::HeaderMap headers;
for (auto const& it : request.headers()) {
headers.set(it.key, it.value);
}
if (!headers.contains("User-Agent"))
headers.set("User-Agent", m_user_agent.to_byte_string());
// FIXME: We could put this request in a queue until the client connection is re-established. // FIXME: We could put this request in a queue until the client connection is re-established.
if (!m_request_client) { if (!m_request_client) {
log_failure(request, "RequestServer is currently unavailable"sv); log_failure(request, "RequestServer is currently unavailable"sv);
return nullptr; return nullptr;
} }
auto protocol_request = m_request_client->start_request(request.method(), request.url().value(), headers, request.body(), proxy); auto protocol_request = m_request_client->start_request(request.method(), request.url().value(), request.headers(), request.body(), proxy);
if (!protocol_request) { if (!protocol_request) {
log_failure(request, "Failed to initiate load"sv); log_failure(request, "Failed to initiate load"sv);
return nullptr; return nullptr;
@ -472,7 +466,7 @@ RefPtr<Requests::Request> ResourceLoader::start_network_request(LoadRequest cons
return protocol_request; return protocol_request;
} }
void ResourceLoader::handle_network_response_headers(LoadRequest const& request, HTTP::HeaderMap const& response_headers) void ResourceLoader::handle_network_response_headers(LoadRequest const& request, HTTP::HeaderList const& response_headers)
{ {
if (!request.page()) if (!request.page())
return; return;
@ -481,7 +475,7 @@ void ResourceLoader::handle_network_response_headers(LoadRequest const& request,
// From https://fetch.spec.whatwg.org/#concept-http-network-fetch: // From https://fetch.spec.whatwg.org/#concept-http-network-fetch:
// 15. If includeCredentials is true, then the user agent should parse and store response // 15. If includeCredentials is true, then the user agent should parse and store response
// `Set-Cookie` headers given request and response. // `Set-Cookie` headers given request and response.
for (auto const& [header, value] : response_headers.headers()) { for (auto const& [header, value] : response_headers) {
if (header.equals_ignoring_ascii_case("Set-Cookie"sv)) { if (header.equals_ignoring_ascii_case("Set-Cookie"sv)) {
store_response_cookies(*request.page(), request.url().value(), value); store_response_cookies(*request.page(), request.url().value(), value);
} }

View file

@ -12,7 +12,7 @@
#include <AK/HashTable.h> #include <AK/HashTable.h>
#include <LibCore/EventReceiver.h> #include <LibCore/EventReceiver.h>
#include <LibGC/Function.h> #include <LibGC/Function.h>
#include <LibHTTP/HeaderMap.h> #include <LibHTTP/HeaderList.h>
#include <LibRequests/Forward.h> #include <LibRequests/Forward.h>
#include <LibRequests/RequestTimingInfo.h> #include <LibRequests/RequestTimingInfo.h>
#include <LibURL/URL.h> #include <LibURL/URL.h>
@ -30,7 +30,7 @@ public:
void set_client(NonnullRefPtr<Requests::RequestClient>); void set_client(NonnullRefPtr<Requests::RequestClient>);
using OnHeadersReceived = GC::Function<void(HTTP::HeaderMap const& response_headers, Optional<u32> status_code, Optional<String> const& reason_phrase)>; using OnHeadersReceived = GC::Function<void(HTTP::HeaderList const& response_headers, Optional<u32> status_code, Optional<String> const& reason_phrase)>;
using OnDataReceived = GC::Function<void(ReadonlyBytes data)>; using OnDataReceived = GC::Function<void(ReadonlyBytes data)>;
using OnComplete = GC::Function<void(bool success, Requests::RequestTimingInfo const& timing_info, Optional<StringView> error_message)>; using OnComplete = GC::Function<void(bool success, Requests::RequestTimingInfo const& timing_info, Optional<StringView> error_message)>;
@ -69,8 +69,8 @@ private:
struct FileLoadResult { struct FileLoadResult {
ReadonlyBytes data; ReadonlyBytes data;
HTTP::HeaderMap response_headers; NonnullRefPtr<HTTP::HeaderList> response_headers;
Requests::RequestTimingInfo timing_info; Requests::RequestTimingInfo timing_info {};
}; };
template<typename FileHandler, typename ErrorHandler> template<typename FileHandler, typename ErrorHandler>
void handle_file_load_request(LoadRequest& request, FileHandler on_file, ErrorHandler on_error); void handle_file_load_request(LoadRequest& request, FileHandler on_file, ErrorHandler on_error);
@ -80,7 +80,7 @@ private:
void handle_resource_load_request(LoadRequest const& request, ResourceHandler on_resource, ErrorHandler on_error); void handle_resource_load_request(LoadRequest const& request, ResourceHandler on_resource, ErrorHandler on_error);
RefPtr<Requests::Request> start_network_request(LoadRequest const&); RefPtr<Requests::Request> start_network_request(LoadRequest const&);
void handle_network_response_headers(LoadRequest const&, HTTP::HeaderMap const&); void handle_network_response_headers(LoadRequest const&, HTTP::HeaderList const&);
void finish_network_request(NonnullRefPtr<Requests::Request>); void finish_network_request(NonnullRefPtr<Requests::Request>);
int m_pending_loads { 0 }; int m_pending_loads { 0 };

View file

@ -12,6 +12,8 @@
#include <AK/String.h> #include <AK/String.h>
#include <AK/StringBuilder.h> #include <AK/StringBuilder.h>
#include <AK/Utf8View.h> #include <AK/Utf8View.h>
#include <LibHTTP/HTTP.h>
#include <LibHTTP/Header.h>
#include <LibWeb/Fetch/Infrastructure/HTTP.h> #include <LibWeb/Fetch/Infrastructure/HTTP.h>
#include <LibWeb/Infra/Strings.h> #include <LibWeb/Infra/Strings.h>
#include <LibWeb/MimeSniff/MimeType.h> #include <LibWeb/MimeSniff/MimeType.h>
@ -88,7 +90,7 @@ Optional<MimeType> MimeType::parse(StringView string)
return OptionalNone {}; return OptionalNone {};
// 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(Fetch::Infrastructure::HTTP_WHITESPACE, TrimMode::Both); auto trimmed_string = string.trim(HTTP::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);
@ -111,7 +113,7 @@ Optional<MimeType> MimeType::parse(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(Fetch::Infrastructure::HTTP_WHITESPACE, TrimMode::Right); subtype = subtype.trim(HTTP::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))
@ -126,7 +128,7 @@ Optional<MimeType> MimeType::parse(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(Fetch::Infrastructure::HTTP_WHITESPACE)); lexer.ignore_while(is_any_of(HTTP::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_view = lexer.consume_until([](char ch) { auto parameter_name_view = lexer.consume_until([](char ch) {
@ -157,7 +159,7 @@ Optional<MimeType> MimeType::parse(StringView string)
// 8. If the code point at position within input is U+0022 ("), then: // 8. If the code point at position within input is U+0022 ("), then:
if (lexer.peek() == '"') { if (lexer.peek() == '"') {
// 1. Set parameterValue to the result of collecting an HTTP quoted string from input, given position and the extract-value flag. // 1. Set parameterValue to the result of collecting an HTTP quoted string from input, given position and the extract-value flag.
parameter_value = Fetch::Infrastructure::collect_an_http_quoted_string(lexer, Fetch::Infrastructure::HttpQuotedStringExtractValue::Yes); parameter_value = HTTP::collect_an_http_quoted_string(lexer, HTTP::HttpQuotedStringExtractValue::Yes);
// 2. Collect a sequence of code points that are not U+003B (;) from input, given position. // 2. Collect a sequence of code points that are not U+003B (;) from input, given position.
lexer.ignore_until(';'); lexer.ignore_until(';');
@ -169,7 +171,7 @@ Optional<MimeType> MimeType::parse(StringView string)
parameter_value = String::from_utf8_without_validation(lexer.consume_until(';').bytes()); parameter_value = String::from_utf8_without_validation(lexer.consume_until(';').bytes());
// 2. Remove any trailing HTTP whitespace from parameterValue. // 2. Remove any trailing HTTP whitespace from parameterValue.
parameter_value = MUST(parameter_value.trim(Fetch::Infrastructure::HTTP_WHITESPACE, TrimMode::Right)); parameter_value = MUST(parameter_value.trim(HTTP::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

@ -217,7 +217,7 @@ static void update(JS::VM& vm, GC::Ref<Job> job)
// 1. Append `Service-Worker`/`script` to requests header list. // 1. Append `Service-Worker`/`script` to requests header list.
// Note: See https://w3c.github.io/ServiceWorker/#service-worker // Note: See https://w3c.github.io/ServiceWorker/#service-worker
request->header_list()->append(Fetch::Infrastructure::Header::isomorphic_encode("Service-Worker"sv, "script"sv)); request->header_list()->append(HTTP::Header::isomorphic_encode("Service-Worker"sv, "script"sv));
// 2. Set requests cache mode to "no-cache" if any of the following are true: // 2. Set requests cache mode to "no-cache" if any of the following are true:
// - registrations update via cache mode is not "all". // - registrations update via cache mode is not "all".
@ -273,7 +273,7 @@ static void update(JS::VM& vm, GC::Ref<Job> job)
// FIXME: CSP not implemented yet // FIXME: CSP not implemented yet
// 10. If serviceWorkerAllowed is failure, then: // 10. If serviceWorkerAllowed is failure, then:
if (service_worker_allowed.has<Fetch::Infrastructure::HeaderList::ExtractHeaderParseFailure>()) { if (service_worker_allowed.has<HTTP::HeaderList::ExtractHeaderParseFailure>()) {
// FIXME: Should we reject the job promise with a security error here? // FIXME: Should we reject the job promise with a security error here?
// 1. Asynchronously complete these steps with a network error. // 1. Asynchronously complete these steps with a network error.

View file

@ -181,7 +181,7 @@ Client::Client(NonnullOwnPtr<Core::BufferedTCPSocket> socket)
{ {
m_socket->on_ready_to_read = [this] { m_socket->on_ready_to_read = [this] {
if (auto result = on_ready_to_read(); result.is_error()) if (auto result = on_ready_to_read(); result.is_error())
handle_error({}, result.release_error()); handle_error(HTTP::HttpRequest { HTTP::HeaderList::create() }, result.release_error());
}; };
} }
@ -249,7 +249,7 @@ ErrorOr<JsonValue, Client::WrappedError> Client::read_body_as_json(HTTP::HttpReq
// FIXME: Check the Content-Type is actually application/json. // FIXME: Check the Content-Type is actually application/json.
size_t content_length = 0; size_t content_length = 0;
for (auto const& header : request.headers().headers()) { for (auto const& header : request.headers()) {
if (header.name.equals_ignoring_ascii_case("Content-Length"sv)) { if (header.name.equals_ignoring_ascii_case("Content-Length"sv)) {
content_length = header.value.to_number<size_t>(TrimWhitespace::Yes).value_or(0); content_length = header.value.to_number<size_t>(TrimWhitespace::Yes).value_or(0);
break; break;

View file

@ -17,6 +17,7 @@
#include <LibWeb/DOM/EventDispatcher.h> #include <LibWeb/DOM/EventDispatcher.h>
#include <LibWeb/DOM/IDLEventListener.h> #include <LibWeb/DOM/IDLEventListener.h>
#include <LibWeb/DOMURL/DOMURL.h> #include <LibWeb/DOMURL/DOMURL.h>
#include <LibWeb/Fetch/Infrastructure/HTTP.h>
#include <LibWeb/FileAPI/Blob.h> #include <LibWeb/FileAPI/Blob.h>
#include <LibWeb/HTML/CloseEvent.h> #include <LibWeb/HTML/CloseEvent.h>
#include <LibWeb/HTML/EventHandler.h> #include <LibWeb/HTML/EventHandler.h>
@ -198,7 +199,7 @@ ErrorOr<void> WebSocket::establish_web_socket_connection(URL::URL const& url_rec
for (auto const& protocol : protocols) for (auto const& protocol : protocols)
TRY(protocol_byte_strings.try_append(protocol.to_byte_string())); TRY(protocol_byte_strings.try_append(protocol.to_byte_string()));
HTTP::HeaderMap additional_headers; auto additional_headers = HTTP::HeaderList::create();
auto cookies = ([&] { auto cookies = ([&] {
auto& page = Bindings::principal_host_defined_page(HTML::principal_realm(realm())); auto& page = Bindings::principal_host_defined_page(HTML::principal_realm(realm()));
@ -206,10 +207,10 @@ ErrorOr<void> WebSocket::establish_web_socket_connection(URL::URL const& url_rec
})(); })();
if (!cookies.is_empty()) { if (!cookies.is_empty()) {
additional_headers.set("Cookie", cookies.to_byte_string()); additional_headers->append({ "Cookie"sv, cookies.to_byte_string() });
} }
additional_headers.set("User-Agent", ResourceLoader::the().user_agent().to_byte_string()); additional_headers->append({ "User-Agent"sv, Fetch::Infrastructure::default_user_agent_value() });
auto request_client = ResourceLoader::the().request_client(); auto request_client = ResourceLoader::the().request_client();

View file

@ -12,6 +12,7 @@
#include <AK/Debug.h> #include <AK/Debug.h>
#include <AK/GenericLexer.h> #include <AK/GenericLexer.h>
#include <AK/QuickSort.h> #include <AK/QuickSort.h>
#include <LibHTTP/Method.h>
#include <LibJS/Runtime/ArrayBuffer.h> #include <LibJS/Runtime/ArrayBuffer.h>
#include <LibJS/Runtime/Completion.h> #include <LibJS/Runtime/Completion.h>
#include <LibJS/Runtime/FunctionObject.h> #include <LibJS/Runtime/FunctionObject.h>
@ -32,7 +33,6 @@
#include <LibWeb/Fetch/Infrastructure/FetchController.h> #include <LibWeb/Fetch/Infrastructure/FetchController.h>
#include <LibWeb/Fetch/Infrastructure/HTTP.h> #include <LibWeb/Fetch/Infrastructure/HTTP.h>
#include <LibWeb/Fetch/Infrastructure/HTTP/MIME.h> #include <LibWeb/Fetch/Infrastructure/HTTP/MIME.h>
#include <LibWeb/Fetch/Infrastructure/HTTP/Methods.h>
#include <LibWeb/Fetch/Infrastructure/HTTP/Requests.h> #include <LibWeb/Fetch/Infrastructure/HTTP/Requests.h>
#include <LibWeb/Fetch/Infrastructure/HTTP/Responses.h> #include <LibWeb/Fetch/Infrastructure/HTTP/Responses.h>
#include <LibWeb/FileAPI/Blob.h> #include <LibWeb/FileAPI/Blob.h>
@ -62,16 +62,16 @@ GC_DEFINE_ALLOCATOR(XMLHttpRequest);
WebIDL::ExceptionOr<GC::Ref<XMLHttpRequest>> XMLHttpRequest::construct_impl(JS::Realm& realm) WebIDL::ExceptionOr<GC::Ref<XMLHttpRequest>> XMLHttpRequest::construct_impl(JS::Realm& realm)
{ {
auto upload_object = realm.create<XMLHttpRequestUpload>(realm); auto upload_object = realm.create<XMLHttpRequestUpload>(realm);
auto author_request_headers = Fetch::Infrastructure::HeaderList::create(realm.vm()); auto author_request_headers = HTTP::HeaderList::create();
auto response = Fetch::Infrastructure::Response::network_error(realm.vm(), "Not sent yet"_string); auto response = Fetch::Infrastructure::Response::network_error(realm.vm(), "Not sent yet"_string);
auto fetch_controller = Fetch::Infrastructure::FetchController::create(realm.vm()); auto fetch_controller = Fetch::Infrastructure::FetchController::create(realm.vm());
return realm.create<XMLHttpRequest>(realm, *upload_object, *author_request_headers, *response, *fetch_controller); return realm.create<XMLHttpRequest>(realm, *upload_object, move(author_request_headers), *response, *fetch_controller);
} }
XMLHttpRequest::XMLHttpRequest(JS::Realm& realm, XMLHttpRequestUpload& upload_object, Fetch::Infrastructure::HeaderList& author_request_headers, Fetch::Infrastructure::Response& response, Fetch::Infrastructure::FetchController& fetch_controller) XMLHttpRequest::XMLHttpRequest(JS::Realm& realm, XMLHttpRequestUpload& upload_object, NonnullRefPtr<HTTP::HeaderList> author_request_headers, Fetch::Infrastructure::Response& response, Fetch::Infrastructure::FetchController& fetch_controller)
: XMLHttpRequestEventTarget(realm) : XMLHttpRequestEventTarget(realm)
, m_upload_object(upload_object) , m_upload_object(upload_object)
, m_author_request_headers(author_request_headers) , m_author_request_headers(move(author_request_headers))
, m_response(response) , m_response(response)
, m_response_type(Bindings::XMLHttpRequestResponseType::Empty) , m_response_type(Bindings::XMLHttpRequestResponseType::Empty)
, m_fetch_controller(fetch_controller) , m_fetch_controller(fetch_controller)
@ -91,7 +91,6 @@ void XMLHttpRequest::visit_edges(Cell::Visitor& visitor)
{ {
Base::visit_edges(visitor); Base::visit_edges(visitor);
visitor.visit(m_upload_object); visitor.visit(m_upload_object);
visitor.visit(m_author_request_headers);
visitor.visit(m_request_body); visitor.visit(m_request_body);
visitor.visit(m_response); visitor.visit(m_response);
visitor.visit(m_fetch_controller); visitor.visit(m_fetch_controller);
@ -424,21 +423,21 @@ WebIDL::ExceptionOr<void> XMLHttpRequest::set_request_header(String const& name,
return WebIDL::InvalidStateError::create(realm, "XHR send() flag is already set"_utf16); return WebIDL::InvalidStateError::create(realm, "XHR send() flag is already set"_utf16);
// 3. Normalize value. // 3. Normalize value.
auto normalized_value = Fetch::Infrastructure::normalize_header_value(value); auto normalized_value = HTTP::normalize_header_value(value);
// 4. If name is not a header name or value is not a header value, then throw a "SyntaxError" DOMException. // 4. If name is not a header name or value is not a header value, then throw a "SyntaxError" DOMException.
if (!Fetch::Infrastructure::is_header_name(name)) if (!HTTP::is_header_name(name))
return WebIDL::SyntaxError::create(realm, "Header name contains invalid characters."_utf16); return WebIDL::SyntaxError::create(realm, "Header name contains invalid characters."_utf16);
if (!Fetch::Infrastructure::is_header_value(normalized_value)) if (!HTTP::is_header_value(normalized_value))
return WebIDL::SyntaxError::create(realm, "Header value contains invalid characters."_utf16); return WebIDL::SyntaxError::create(realm, "Header value contains invalid characters."_utf16);
auto header = Fetch::Infrastructure::Header { auto header = HTTP::Header {
.name = name.to_byte_string(), .name = name.to_byte_string(),
.value = move(normalized_value), .value = move(normalized_value),
}; };
// 5. If (name, value) is a forbidden request-header, then return. // 5. If (name, value) is a forbidden request-header, then return.
if (Fetch::Infrastructure::is_forbidden_request_header(header)) if (HTTP::is_forbidden_request_header(header))
return {}; return {};
// 6. Combine (name, value) in thiss author request headers. // 6. Combine (name, value) in thiss author request headers.
@ -464,15 +463,15 @@ WebIDL::ExceptionOr<void> XMLHttpRequest::open(String const& method, String cons
} }
// 2. If method is not a method, then throw a "SyntaxError" DOMException. // 2. If method is not a method, then throw a "SyntaxError" DOMException.
if (!Fetch::Infrastructure::is_method(method)) if (!HTTP::is_method(method))
return WebIDL::SyntaxError::create(realm(), "An invalid or illegal string was specified."_utf16); return WebIDL::SyntaxError::create(realm(), "An invalid or illegal string was specified."_utf16);
// 3. If method is a forbidden method, then throw a "SecurityError" DOMException. // 3. If method is a forbidden method, then throw a "SecurityError" DOMException.
if (Fetch::Infrastructure::is_forbidden_method(method)) if (HTTP::is_forbidden_method(method))
return WebIDL::SecurityError::create(realm(), "Forbidden method, must not be 'CONNECT', 'TRACE', or 'TRACK'"_utf16); return WebIDL::SecurityError::create(realm(), "Forbidden method, must not be 'CONNECT', 'TRACE', or 'TRACK'"_utf16);
// 4. Normalize method. // 4. Normalize method.
auto normalized_method = Fetch::Infrastructure::normalize_method(method); auto normalized_method = HTTP::normalize_method(method);
// 5. Let parsedURL be the result of parsing url with thiss relevant settings objects API base URL and thiss relevant settings objects API URL character encoding. // 5. Let parsedURL be the result of parsing url with thiss relevant settings objects API base URL and thiss relevant settings objects API URL character encoding.
auto& relevant_settings_object = HTML::relevant_settings_object(*this); auto& relevant_settings_object = HTML::relevant_settings_object(*this);
@ -604,7 +603,7 @@ WebIDL::ExceptionOr<void> XMLHttpRequest::send(Optional<DocumentOrXMLHttpRequest
auto new_content_type_serialized = content_type_record->serialized(); auto new_content_type_serialized = content_type_record->serialized();
// 3. Set (`Content-Type`, newContentTypeSerialized) in thiss author request headers. // 3. Set (`Content-Type`, newContentTypeSerialized) in thiss author request headers.
auto header = Fetch::Infrastructure::Header::isomorphic_encode("Content-Type"sv, new_content_type_serialized); auto header = HTTP::Header::isomorphic_encode("Content-Type"sv, new_content_type_serialized);
m_author_request_headers->set(move(header)); m_author_request_headers->set(move(header));
} }
} }
@ -629,7 +628,7 @@ WebIDL::ExceptionOr<void> XMLHttpRequest::send(Optional<DocumentOrXMLHttpRequest
} }
// 3. Otherwise, if extractedContentType is not null, set (`Content-Type`, extractedContentType) in thiss author request headers. // 3. Otherwise, if extractedContentType is not null, set (`Content-Type`, extractedContentType) in thiss author request headers.
else if (extracted_content_type.has_value()) { else if (extracted_content_type.has_value()) {
auto header = Fetch::Infrastructure::Header::isomorphic_encode("Content-Type"sv, extracted_content_type.value()); auto header = HTTP::Header::isomorphic_encode("Content-Type"sv, extracted_content_type.value());
m_author_request_headers->set(move(header)); m_author_request_headers->set(move(header));
} }
} }
@ -995,7 +994,7 @@ String XMLHttpRequest::get_all_response_headers() const
// 3. Let headers be the result of sorting initialHeaders in ascending order, with a being less than b if as name is legacy-uppercased-byte less than bs name. // 3. Let headers be the result of sorting initialHeaders in ascending order, with a being less than b if as name is legacy-uppercased-byte less than bs name.
// NOTE: Unfortunately, this is needed for compatibility with deployed content. // NOTE: Unfortunately, this is needed for compatibility with deployed content.
quick_sort(initial_headers, [](Fetch::Infrastructure::Header const& a, Fetch::Infrastructure::Header const& b) { quick_sort(initial_headers, [](HTTP::Header const& a, HTTP::Header const& b) {
return is_legacy_uppercased_byte_less_than(a.name, b.name); return is_legacy_uppercased_byte_less_than(a.name, b.name);
}); });

View file

@ -11,12 +11,12 @@
#include <AK/ByteBuffer.h> #include <AK/ByteBuffer.h>
#include <AK/RefCounted.h> #include <AK/RefCounted.h>
#include <AK/Weakable.h> #include <AK/Weakable.h>
#include <LibHTTP/HeaderList.h>
#include <LibURL/URL.h> #include <LibURL/URL.h>
#include <LibWeb/DOM/EventTarget.h> #include <LibWeb/DOM/EventTarget.h>
#include <LibWeb/DOMURL/URLSearchParams.h> #include <LibWeb/DOMURL/URLSearchParams.h>
#include <LibWeb/Fetch/BodyInit.h> #include <LibWeb/Fetch/BodyInit.h>
#include <LibWeb/Fetch/Infrastructure/HTTP/Bodies.h> #include <LibWeb/Fetch/Infrastructure/HTTP/Bodies.h>
#include <LibWeb/Fetch/Infrastructure/HTTP/Headers.h>
#include <LibWeb/Fetch/Infrastructure/HTTP/Statuses.h> #include <LibWeb/Fetch/Infrastructure/HTTP/Statuses.h>
#include <LibWeb/HTML/Window.h> #include <LibWeb/HTML/Window.h>
#include <LibWeb/MimeSniff/MimeType.h> #include <LibWeb/MimeSniff/MimeType.h>
@ -95,7 +95,7 @@ private:
WebIDL::ExceptionOr<void> handle_errors(); WebIDL::ExceptionOr<void> handle_errors();
JS::ThrowCompletionOr<void> request_error_steps(FlyString const& event_name, GC::Ptr<WebIDL::DOMException> exception = nullptr); JS::ThrowCompletionOr<void> request_error_steps(FlyString const& event_name, GC::Ptr<WebIDL::DOMException> exception = nullptr);
XMLHttpRequest(JS::Realm&, XMLHttpRequestUpload&, Fetch::Infrastructure::HeaderList&, Fetch::Infrastructure::Response&, Fetch::Infrastructure::FetchController&); XMLHttpRequest(JS::Realm&, XMLHttpRequestUpload&, NonnullRefPtr<HTTP::HeaderList>, Fetch::Infrastructure::Response&, Fetch::Infrastructure::FetchController&);
// https://xhr.spec.whatwg.org/#upload-object // https://xhr.spec.whatwg.org/#upload-object
// upload object // upload object
@ -135,7 +135,7 @@ private:
// https://xhr.spec.whatwg.org/#author-request-headers // https://xhr.spec.whatwg.org/#author-request-headers
// author request headers // author request headers
// A header list, initially empty. // A header list, initially empty.
GC::Ref<Fetch::Infrastructure::HeaderList> m_author_request_headers; NonnullRefPtr<HTTP::HeaderList> m_author_request_headers;
// https://xhr.spec.whatwg.org/#request-body // https://xhr.spec.whatwg.org/#request-body
// request body // request body

View file

@ -6,4 +6,4 @@ set(SOURCES
) )
ladybird_lib(LibWebSocket websocket) ladybird_lib(LibWebSocket websocket)
target_link_libraries(LibWebSocket PRIVATE LibCore LibCrypto LibTLS LibURL LibDNS) target_link_libraries(LibWebSocket PRIVATE LibCore LibCrypto LibHTTP LibTLS LibURL LibDNS)

View file

@ -10,6 +10,7 @@ namespace WebSocket {
ConnectionInfo::ConnectionInfo(URL::URL url) ConnectionInfo::ConnectionInfo(URL::URL url)
: m_url(move(url)) : m_url(move(url))
, m_headers(HTTP::HeaderList::create())
{ {
} }

View file

@ -8,7 +8,7 @@
#include <AK/Vector.h> #include <AK/Vector.h>
#include <LibDNS/Resolver.h> #include <LibDNS/Resolver.h>
#include <LibHTTP/HeaderMap.h> #include <LibHTTP/HeaderList.h>
#include <LibURL/URL.h> #include <LibURL/URL.h>
namespace WebSocket { namespace WebSocket {
@ -28,8 +28,8 @@ public:
Vector<ByteString> const& extensions() const { return m_extensions; } Vector<ByteString> const& extensions() const { return m_extensions; }
void set_extensions(Vector<ByteString> extensions) { m_extensions = move(extensions); } void set_extensions(Vector<ByteString> extensions) { m_extensions = move(extensions); }
HTTP::HeaderMap const& headers() const { return m_headers; } HTTP::HeaderList const& headers() const { return m_headers; }
void set_headers(HTTP::HeaderMap headers) { m_headers = move(headers); } void set_headers(NonnullRefPtr<HTTP::HeaderList> headers) { m_headers = move(headers); }
Optional<ByteString> const& root_certificates_path() const { return m_root_certificates_path; } Optional<ByteString> const& root_certificates_path() const { return m_root_certificates_path; }
void set_root_certificates_path(Optional<ByteString> root_certificates_path) { m_root_certificates_path = move(root_certificates_path); } void set_root_certificates_path(Optional<ByteString> root_certificates_path) { m_root_certificates_path = move(root_certificates_path); }
@ -48,7 +48,7 @@ private:
ByteString m_origin; ByteString m_origin;
Vector<ByteString> m_protocols {}; Vector<ByteString> m_protocols {};
Vector<ByteString> m_extensions {}; Vector<ByteString> m_extensions {};
HTTP::HeaderMap m_headers; NonnullRefPtr<HTTP::HeaderList> m_headers;
Optional<ByteString> m_root_certificates_path; Optional<ByteString> m_root_certificates_path;
RefPtr<DNS::LookupResult const> m_dns_result; RefPtr<DNS::LookupResult const> m_dns_result;
}; };

View file

@ -233,7 +233,7 @@ void WebSocket::send_client_handshake()
} }
// 12. Additional headers // 12. Additional headers
for (auto& header : m_connection.headers().headers()) { for (auto const& header : m_connection.headers()) {
builder.appendff("{}: {}\r\n", header.name, header.value); builder.appendff("{}: {}\r\n", header.name, header.value);
} }

View file

@ -72,7 +72,7 @@ void Autocomplete::query_autocomplete_engine(String query)
m_query = move(query); m_query = move(query);
m_request->set_buffered_request_finished_callback( m_request->set_buffered_request_finished_callback(
[this, engine = engine.release_value()](u64, Requests::RequestTimingInfo const&, Optional<Requests::NetworkError> const& network_error, HTTP::HeaderMap const& response_headers, Optional<u32> response_code, Optional<String> const& reason_phrase, ReadonlyBytes payload) { [this, engine = engine.release_value()](u64, Requests::RequestTimingInfo const&, Optional<Requests::NetworkError> const& network_error, HTTP::HeaderList const& response_headers, Optional<u32> response_code, Optional<String> const& reason_phrase, ReadonlyBytes payload) {
Core::deferred_invoke([this]() { m_request.clear(); }); Core::deferred_invoke([this]() { m_request.clear(); });
if (network_error.has_value()) { if (network_error.has_value()) {

View file

@ -69,7 +69,7 @@ set(GENERATED_SOURCES
) )
ladybird_lib(LibWebView webview EXPLICIT_SYMBOL_EXPORT) ladybird_lib(LibWebView webview EXPLICIT_SYMBOL_EXPORT)
target_link_libraries(LibWebView PRIVATE LibCore LibDatabase LibDevTools LibFileSystem LibGfx LibImageDecoderClient LibIPC LibRequests LibJS LibWeb LibUnicode LibURL LibSyntax LibTextCodec) target_link_libraries(LibWebView PRIVATE LibCore LibDatabase LibDevTools LibFileSystem LibGfx LibHTTP LibImageDecoderClient LibIPC LibRequests LibJS LibWeb LibUnicode LibURL LibSyntax LibTextCodec)
if (APPLE) if (APPLE)
target_link_libraries(LibWebView PRIVATE LibThreading) target_link_libraries(LibWebView PRIVATE LibThreading)

View file

@ -41,7 +41,7 @@ target_include_directories(requestserverservice PRIVATE ${CMAKE_CURRENT_BINARY_D
target_include_directories(requestserverservice PRIVATE ${LADYBIRD_SOURCE_DIR}/Services/) target_include_directories(requestserverservice PRIVATE ${LADYBIRD_SOURCE_DIR}/Services/)
target_link_libraries(RequestServer PRIVATE requestserverservice) target_link_libraries(RequestServer PRIVATE requestserverservice)
target_link_libraries(requestserverservice PUBLIC LibCore LibDatabase LibDNS LibCrypto LibFileSystem LibIPC LibMain LibRequests LibTLS LibWebSocket LibURL LibTextCodec LibThreading CURL::libcurl) target_link_libraries(requestserverservice PUBLIC LibCore LibDatabase LibDNS LibCrypto LibFileSystem LibHTTP LibIPC LibMain LibRequests LibTLS LibWebSocket LibURL LibTextCodec LibThreading CURL::libcurl)
target_link_libraries(requestserverservice PRIVATE OpenSSL::Crypto OpenSSL::SSL) target_link_libraries(requestserverservice PRIVATE OpenSSL::Crypto OpenSSL::SSL)
if (WIN32) if (WIN32)

View file

@ -117,7 +117,7 @@ CacheEntryWriter::CacheEntryWriter(DiskCache& disk_cache, CacheIndex& index, u64
{ {
} }
ErrorOr<void> CacheEntryWriter::write_status_and_reason(u32 status_code, Optional<String> reason_phrase, HTTP::HeaderMap const& response_headers) ErrorOr<void> CacheEntryWriter::write_status_and_reason(u32 status_code, Optional<String> reason_phrase, HTTP::HeaderList const& response_headers)
{ {
if (m_marked_for_deletion) { if (m_marked_for_deletion) {
close_and_destroy_cache_entry(); close_and_destroy_cache_entry();
@ -183,7 +183,7 @@ ErrorOr<void> CacheEntryWriter::write_data(ReadonlyBytes data)
return {}; return {};
} }
ErrorOr<void> CacheEntryWriter::flush(HTTP::HeaderMap response_headers) ErrorOr<void> CacheEntryWriter::flush(NonnullRefPtr<HTTP::HeaderList> response_headers)
{ {
ScopeGuard guard { [&]() { close_and_destroy_cache_entry(); } }; ScopeGuard guard { [&]() { close_and_destroy_cache_entry(); } };
@ -205,7 +205,7 @@ ErrorOr<void> CacheEntryWriter::flush(HTTP::HeaderMap response_headers)
return {}; return {};
} }
ErrorOr<NonnullOwnPtr<CacheEntryReader>> CacheEntryReader::create(DiskCache& disk_cache, CacheIndex& index, u64 cache_key, HTTP::HeaderMap response_headers, u64 data_size) ErrorOr<NonnullOwnPtr<CacheEntryReader>> CacheEntryReader::create(DiskCache& disk_cache, CacheIndex& index, u64 cache_key, NonnullRefPtr<HTTP::HeaderList> response_headers, u64 data_size)
{ {
auto path = path_for_cache_key(disk_cache.cache_directory(), cache_key); auto path = path_for_cache_key(disk_cache.cache_directory(), cache_key);
@ -253,7 +253,7 @@ ErrorOr<NonnullOwnPtr<CacheEntryReader>> CacheEntryReader::create(DiskCache& dis
return adopt_own(*new CacheEntryReader { disk_cache, index, cache_key, move(url), move(path), move(file), fd, cache_header, move(reason_phrase), move(response_headers), data_offset, data_size }); return adopt_own(*new CacheEntryReader { disk_cache, index, cache_key, move(url), move(path), move(file), fd, cache_header, move(reason_phrase), move(response_headers), data_offset, data_size });
} }
CacheEntryReader::CacheEntryReader(DiskCache& disk_cache, CacheIndex& index, u64 cache_key, String url, LexicalPath path, NonnullOwnPtr<Core::File> file, int fd, CacheHeader cache_header, Optional<String> reason_phrase, HTTP::HeaderMap response_headers, u64 data_offset, u64 data_size) CacheEntryReader::CacheEntryReader(DiskCache& disk_cache, CacheIndex& index, u64 cache_key, String url, LexicalPath path, NonnullOwnPtr<Core::File> file, int fd, CacheHeader cache_header, Optional<String> reason_phrase, NonnullRefPtr<HTTP::HeaderList> response_headers, u64 data_offset, u64 data_size)
: CacheEntry(disk_cache, index, cache_key, move(url), move(path), cache_header) : CacheEntry(disk_cache, index, cache_key, move(url), move(path), cache_header)
, m_file(move(file)) , m_file(move(file))
, m_fd(fd) , m_fd(fd)
@ -264,7 +264,7 @@ CacheEntryReader::CacheEntryReader(DiskCache& disk_cache, CacheIndex& index, u64
{ {
} }
void CacheEntryReader::revalidation_succeeded(HTTP::HeaderMap const& response_headers) void CacheEntryReader::revalidation_succeeded(HTTP::HeaderList const& response_headers)
{ {
dbgln("\033[34;1mCache revalidation succeeded for\033[0m {}", m_url); dbgln("\033[34;1mCache revalidation succeeded for\033[0m {}", m_url);

View file

@ -10,9 +10,10 @@
#include <AK/LexicalPath.h> #include <AK/LexicalPath.h>
#include <AK/Optional.h> #include <AK/Optional.h>
#include <AK/String.h> #include <AK/String.h>
#include <AK/Time.h>
#include <AK/Types.h> #include <AK/Types.h>
#include <LibCore/File.h> #include <LibCore/File.h>
#include <LibHTTP/HeaderMap.h> #include <LibHTTP/HeaderList.h>
#include <RequestServer/Cache/Version.h> #include <RequestServer/Cache/Version.h>
#include <RequestServer/Forward.h> #include <RequestServer/Forward.h>
@ -86,9 +87,9 @@ public:
static ErrorOr<NonnullOwnPtr<CacheEntryWriter>> create(DiskCache&, CacheIndex&, u64 cache_key, String url, UnixDateTime request_time, AK::Duration current_time_offset_for_testing); static ErrorOr<NonnullOwnPtr<CacheEntryWriter>> create(DiskCache&, CacheIndex&, u64 cache_key, String url, UnixDateTime request_time, AK::Duration current_time_offset_for_testing);
virtual ~CacheEntryWriter() override = default; virtual ~CacheEntryWriter() override = default;
ErrorOr<void> write_status_and_reason(u32 status_code, Optional<String> reason_phrase, HTTP::HeaderMap const&); ErrorOr<void> write_status_and_reason(u32 status_code, Optional<String> reason_phrase, HTTP::HeaderList const&);
ErrorOr<void> write_data(ReadonlyBytes); ErrorOr<void> write_data(ReadonlyBytes);
ErrorOr<void> flush(HTTP::HeaderMap); ErrorOr<void> flush(NonnullRefPtr<HTTP::HeaderList>);
private: private:
CacheEntryWriter(DiskCache&, CacheIndex&, u64 cache_key, String url, LexicalPath, NonnullOwnPtr<Core::OutputBufferedFile>, CacheHeader, UnixDateTime request_time, AK::Duration current_time_offset_for_testing); CacheEntryWriter(DiskCache&, CacheIndex&, u64 cache_key, String url, LexicalPath, NonnullOwnPtr<Core::OutputBufferedFile>, CacheHeader, UnixDateTime request_time, AK::Duration current_time_offset_for_testing);
@ -103,23 +104,24 @@ private:
class CacheEntryReader : public CacheEntry { class CacheEntryReader : public CacheEntry {
public: public:
static ErrorOr<NonnullOwnPtr<CacheEntryReader>> create(DiskCache&, CacheIndex&, u64 cache_key, HTTP::HeaderMap, u64 data_size); static ErrorOr<NonnullOwnPtr<CacheEntryReader>> create(DiskCache&, CacheIndex&, u64 cache_key, NonnullRefPtr<HTTP::HeaderList>, u64 data_size);
virtual ~CacheEntryReader() override = default; virtual ~CacheEntryReader() override = default;
bool must_revalidate() const { return m_must_revalidate; } bool must_revalidate() const { return m_must_revalidate; }
void set_must_revalidate() { m_must_revalidate = true; } void set_must_revalidate() { m_must_revalidate = true; }
void revalidation_succeeded(HTTP::HeaderMap const&); void revalidation_succeeded(HTTP::HeaderList const&);
void revalidation_failed(); void revalidation_failed();
void pipe_to(int pipe_fd, Function<void(u64 bytes_piped)> on_complete, Function<void(u64 bytes_piped)> on_error); void pipe_to(int pipe_fd, Function<void(u64 bytes_piped)> on_complete, Function<void(u64 bytes_piped)> on_error);
u32 status_code() const { return m_cache_header.status_code; } u32 status_code() const { return m_cache_header.status_code; }
Optional<String> const& reason_phrase() const { return m_reason_phrase; } Optional<String> const& reason_phrase() const { return m_reason_phrase; }
HTTP::HeaderMap const& response_headers() const { return m_response_headers; } HTTP::HeaderList& response_headers() { return m_response_headers; }
HTTP::HeaderList const& response_headers() const { return m_response_headers; }
private: private:
CacheEntryReader(DiskCache&, CacheIndex&, u64 cache_key, String url, LexicalPath, NonnullOwnPtr<Core::File>, int fd, CacheHeader, Optional<String> reason_phrase, HTTP::HeaderMap, u64 data_offset, u64 data_size); CacheEntryReader(DiskCache&, CacheIndex&, u64 cache_key, String url, LexicalPath, NonnullOwnPtr<Core::File>, int fd, CacheHeader, Optional<String> reason_phrase, NonnullRefPtr<HTTP::HeaderList>, u64 data_offset, u64 data_size);
void pipe_without_blocking(); void pipe_without_blocking();
void pipe_complete(); void pipe_complete();
@ -138,7 +140,7 @@ private:
u64 m_bytes_piped { 0 }; u64 m_bytes_piped { 0 };
Optional<String> m_reason_phrase; Optional<String> m_reason_phrase;
HTTP::HeaderMap m_response_headers; NonnullRefPtr<HTTP::HeaderList> m_response_headers;
bool m_must_revalidate { false }; bool m_must_revalidate { false };

View file

@ -13,11 +13,11 @@ namespace RequestServer {
static constexpr u32 CACHE_METADATA_KEY = 12389u; static constexpr u32 CACHE_METADATA_KEY = 12389u;
static ByteString serialize_headers(HTTP::HeaderMap const& headers) static ByteString serialize_headers(HTTP::HeaderList const& headers)
{ {
StringBuilder builder; StringBuilder builder;
for (auto const& header : headers.headers()) { for (auto const& header : headers) {
builder.append(header.name); builder.append(header.name);
builder.append(':'); builder.append(':');
builder.append(header.value); builder.append(header.value);
@ -27,9 +27,9 @@ static ByteString serialize_headers(HTTP::HeaderMap const& headers)
return builder.to_byte_string(); return builder.to_byte_string();
} }
static HTTP::HeaderMap deserialize_headers(StringView serialized_headers) static NonnullRefPtr<HTTP::HeaderList> deserialize_headers(StringView serialized_headers)
{ {
HTTP::HeaderMap headers; auto headers = HTTP::HeaderList::create();
serialized_headers.for_each_split_view('\n', SplitBehavior::Nothing, [&](StringView serialized_header) { serialized_headers.for_each_split_view('\n', SplitBehavior::Nothing, [&](StringView serialized_header) {
auto index = serialized_header.find(':'); auto index = serialized_header.find(':');
@ -41,7 +41,7 @@ static HTTP::HeaderMap deserialize_headers(StringView serialized_headers)
return; return;
auto value = serialized_header.substring_view(*index + 1).trim_whitespace(); auto value = serialized_header.substring_view(*index + 1).trim_whitespace();
headers.set(name, value); headers->append({ name, value });
}); });
return headers; return headers;
@ -110,15 +110,15 @@ CacheIndex::CacheIndex(Database::Database& database, Statements statements)
{ {
} }
void CacheIndex::create_entry(u64 cache_key, String url, HTTP::HeaderMap response_headers, u64 data_size, UnixDateTime request_time, UnixDateTime response_time) void CacheIndex::create_entry(u64 cache_key, String url, NonnullRefPtr<HTTP::HeaderList> response_headers, u64 data_size, UnixDateTime request_time, UnixDateTime response_time)
{ {
auto now = UnixDateTime::now(); auto now = UnixDateTime::now();
for (size_t i = 0; i < response_headers.headers().size();) { for (size_t i = 0; i < response_headers->headers().size();) {
auto const& header = response_headers.headers()[i]; auto const& header = response_headers->headers()[i];
if (is_header_exempted_from_storage(header.name)) if (is_header_exempted_from_storage(header.name))
response_headers.remove(header.name); response_headers->delete_(header.name);
else else
++i; ++i;
} }
@ -156,7 +156,7 @@ void CacheIndex::remove_entries_accessed_since(UnixDateTime since, Function<void
since); since);
} }
void CacheIndex::update_response_headers(u64 cache_key, HTTP::HeaderMap response_headers) void CacheIndex::update_response_headers(u64 cache_key, NonnullRefPtr<HTTP::HeaderList> response_headers)
{ {
auto entry = m_entries.get(cache_key); auto entry = m_entries.get(cache_key);
if (!entry.has_value()) if (!entry.has_value())

View file

@ -11,7 +11,7 @@
#include <AK/Time.h> #include <AK/Time.h>
#include <AK/Types.h> #include <AK/Types.h>
#include <LibDatabase/Database.h> #include <LibDatabase/Database.h>
#include <LibHTTP/HeaderMap.h> #include <LibHTTP/HeaderList.h>
#include <LibRequests/CacheSizes.h> #include <LibRequests/CacheSizes.h>
namespace RequestServer { namespace RequestServer {
@ -23,7 +23,7 @@ class CacheIndex {
u64 cache_key { 0 }; u64 cache_key { 0 };
String url; String url;
HTTP::HeaderMap response_headers; NonnullRefPtr<HTTP::HeaderList> response_headers;
u64 data_size { 0 }; u64 data_size { 0 };
UnixDateTime request_time; UnixDateTime request_time;
@ -34,13 +34,13 @@ class CacheIndex {
public: public:
static ErrorOr<CacheIndex> create(Database::Database&); static ErrorOr<CacheIndex> create(Database::Database&);
void create_entry(u64 cache_key, String url, HTTP::HeaderMap, u64 data_size, UnixDateTime request_time, UnixDateTime response_time); void create_entry(u64 cache_key, String url, NonnullRefPtr<HTTP::HeaderList>, u64 data_size, UnixDateTime request_time, UnixDateTime response_time);
void remove_entry(u64 cache_key); void remove_entry(u64 cache_key);
void remove_entries_accessed_since(UnixDateTime, Function<void(u64 cache_key)> on_entry_removed); void remove_entries_accessed_since(UnixDateTime, Function<void(u64 cache_key)> on_entry_removed);
Optional<Entry&> find_entry(u64 cache_key); Optional<Entry&> find_entry(u64 cache_key);
void update_response_headers(u64 cache_key, HTTP::HeaderMap); void update_response_headers(u64 cache_key, NonnullRefPtr<HTTP::HeaderList>);
void update_last_access_time(u64 cache_key); void update_last_access_time(u64 cache_key);
Requests::CacheSizes estimate_cache_size_accessed_since(UnixDateTime since) const; Requests::CacheSizes estimate_cache_size_accessed_since(UnixDateTime since) const;

View file

@ -12,6 +12,7 @@
#include <AK/StringView.h> #include <AK/StringView.h>
#include <AK/Time.h> #include <AK/Time.h>
#include <AK/Types.h> #include <AK/Types.h>
#include <AK/WeakPtr.h>
#include <LibDatabase/Database.h> #include <LibDatabase/Database.h>
#include <LibURL/Forward.h> #include <LibURL/Forward.h>
#include <RequestServer/Cache/CacheEntry.h> #include <RequestServer/Cache/CacheEntry.h>

View file

@ -111,7 +111,7 @@ static bool is_heuristically_cacheable_status(u32 status_code)
} }
// https://httpwg.org/specs/rfc9111.html#response.cacheability // https://httpwg.org/specs/rfc9111.html#response.cacheability
bool is_cacheable(u32 status_code, HTTP::HeaderMap const& headers) bool is_cacheable(u32 status_code, HTTP::HeaderList const& headers)
{ {
// A cache MUST NOT store a response to a request unless: // A cache MUST NOT store a response to a request unless:
@ -213,7 +213,7 @@ bool is_header_exempted_from_storage(StringView name)
} }
// https://httpwg.org/specs/rfc9111.html#heuristic.freshness // https://httpwg.org/specs/rfc9111.html#heuristic.freshness
static AK::Duration calculate_heuristic_freshness_lifetime(HTTP::HeaderMap const& headers, AK::Duration current_time_offset_for_testing) static AK::Duration calculate_heuristic_freshness_lifetime(HTTP::HeaderList const& headers, AK::Duration current_time_offset_for_testing)
{ {
// Since origin servers do not always provide explicit expiration times, a cache MAY assign a heuristic expiration // Since origin servers do not always provide explicit expiration times, a cache MAY assign a heuristic expiration
// time when an explicit time is not specified, employing algorithms that use other field values (such as the // time when an explicit time is not specified, employing algorithms that use other field values (such as the
@ -245,7 +245,7 @@ static AK::Duration calculate_heuristic_freshness_lifetime(HTTP::HeaderMap const
} }
// https://httpwg.org/specs/rfc9111.html#calculating.freshness.lifetime // https://httpwg.org/specs/rfc9111.html#calculating.freshness.lifetime
AK::Duration calculate_freshness_lifetime(u32 status_code, HTTP::HeaderMap const& headers, AK::Duration current_time_offset_for_testing) AK::Duration calculate_freshness_lifetime(u32 status_code, HTTP::HeaderList const& headers, AK::Duration current_time_offset_for_testing)
{ {
// A cache can calculate the freshness lifetime (denoted as freshness_lifetime) of a response by evaluating the // A cache can calculate the freshness lifetime (denoted as freshness_lifetime) of a response by evaluating the
// following rules and using the first match: // following rules and using the first match:
@ -296,7 +296,7 @@ AK::Duration calculate_freshness_lifetime(u32 status_code, HTTP::HeaderMap const
} }
// https://httpwg.org/specs/rfc9111.html#age.calculations // https://httpwg.org/specs/rfc9111.html#age.calculations
AK::Duration calculate_age(HTTP::HeaderMap const& headers, UnixDateTime request_time, UnixDateTime response_time, AK::Duration current_time_offset_for_testing) AK::Duration calculate_age(HTTP::HeaderList const& headers, UnixDateTime request_time, UnixDateTime response_time, AK::Duration current_time_offset_for_testing)
{ {
// The term "age_value" denotes the value of the Age header field (Section 5.1), in a form appropriate for arithmetic // The term "age_value" denotes the value of the Age header field (Section 5.1), in a form appropriate for arithmetic
// operation; or 0, if not available. // operation; or 0, if not available.
@ -328,7 +328,7 @@ AK::Duration calculate_age(HTTP::HeaderMap const& headers, UnixDateTime request_
return current_age; return current_age;
} }
CacheLifetimeStatus cache_lifetime_status(HTTP::HeaderMap const& headers, AK::Duration freshness_lifetime, AK::Duration current_age) CacheLifetimeStatus cache_lifetime_status(HTTP::HeaderList const& headers, AK::Duration freshness_lifetime, AK::Duration current_age)
{ {
auto revalidation_status = [&]() { auto revalidation_status = [&]() {
// In order to revalidate a cache entry, we must have one of these headers to attach to the revalidation request. // In order to revalidate a cache entry, we must have one of these headers to attach to the revalidation request.
@ -365,7 +365,7 @@ CacheLifetimeStatus cache_lifetime_status(HTTP::HeaderMap const& headers, AK::Du
} }
// https://httpwg.org/specs/rfc9111.html#validation.sent // https://httpwg.org/specs/rfc9111.html#validation.sent
RevalidationAttributes RevalidationAttributes::create(HTTP::HeaderMap const& headers) RevalidationAttributes RevalidationAttributes::create(HTTP::HeaderList const& headers)
{ {
RevalidationAttributes attributes; RevalidationAttributes attributes;
attributes.etag = headers.get("ETag"sv).map([](auto const& etag) { return etag; }); attributes.etag = headers.get("ETag"sv).map([](auto const& etag) { return etag; });
@ -375,7 +375,7 @@ RevalidationAttributes RevalidationAttributes::create(HTTP::HeaderMap const& hea
} }
// https://httpwg.org/specs/rfc9111.html#update // https://httpwg.org/specs/rfc9111.html#update
void update_header_fields(HTTP::HeaderMap& stored_headers, HTTP::HeaderMap const& updated_headers) void update_header_fields(HTTP::HeaderList& stored_headers, HTTP::HeaderList const& updated_headers)
{ {
// Caches are required to update a stored response's header fields from another (typically newer) response in // Caches are required to update a stored response's header fields from another (typically newer) response in
// several situations; for example, see Sections 3.4, 4.3.4, and 4.3.5. // several situations; for example, see Sections 3.4, 4.3.4, and 4.3.5.
@ -397,18 +397,18 @@ void update_header_fields(HTTP::HeaderMap& stored_headers, HTTP::HeaderMap const
return false; return false;
}; };
for (auto const& updated_header : updated_headers.headers()) { for (auto const& updated_header : updated_headers) {
if (!is_header_exempted_from_update(updated_header.name)) if (!is_header_exempted_from_update(updated_header.name))
stored_headers.remove(updated_header.name); stored_headers.delete_(updated_header.name);
} }
for (auto const& updated_header : updated_headers.headers()) { for (auto const& updated_header : updated_headers) {
if (!is_header_exempted_from_update(updated_header.name)) if (!is_header_exempted_from_update(updated_header.name))
stored_headers.set(updated_header.name, updated_header.value); stored_headers.append({ updated_header.name, updated_header.value });
} }
} }
AK::Duration compute_current_time_offset_for_testing(Optional<DiskCache&> disk_cache, HTTP::HeaderMap const& request_headers) AK::Duration compute_current_time_offset_for_testing(Optional<DiskCache&> disk_cache, HTTP::HeaderList const& request_headers)
{ {
if (disk_cache.has_value() && disk_cache->mode() == DiskCache::Mode::Testing) { if (disk_cache.has_value() && disk_cache->mode() == DiskCache::Mode::Testing) {
if (auto header = request_headers.get(TEST_CACHE_REQUEST_TIME_OFFSET); header.has_value()) { if (auto header = request_headers.get(TEST_CACHE_REQUEST_TIME_OFFSET); header.has_value()) {

View file

@ -10,7 +10,7 @@
#include <AK/StringView.h> #include <AK/StringView.h>
#include <AK/Time.h> #include <AK/Time.h>
#include <AK/Types.h> #include <AK/Types.h>
#include <LibHTTP/HeaderMap.h> #include <LibHTTP/HeaderList.h>
#include <LibURL/Forward.h> #include <LibURL/Forward.h>
#include <RequestServer/Forward.h> #include <RequestServer/Forward.h>
@ -25,28 +25,28 @@ u64 create_cache_key(StringView url, StringView method);
LexicalPath path_for_cache_key(LexicalPath const& cache_directory, u64 cache_key); LexicalPath path_for_cache_key(LexicalPath const& cache_directory, u64 cache_key);
bool is_cacheable(StringView method); bool is_cacheable(StringView method);
bool is_cacheable(u32 status_code, HTTP::HeaderMap const&); bool is_cacheable(u32 status_code, HTTP::HeaderList const&);
bool is_header_exempted_from_storage(StringView name); bool is_header_exempted_from_storage(StringView name);
AK::Duration calculate_freshness_lifetime(u32 status_code, HTTP::HeaderMap const&, AK::Duration current_time_offset_for_testing); AK::Duration calculate_freshness_lifetime(u32 status_code, HTTP::HeaderList const&, AK::Duration current_time_offset_for_testing);
AK::Duration calculate_age(HTTP::HeaderMap const&, UnixDateTime request_time, UnixDateTime response_time, AK::Duration current_time_offset_for_testing); AK::Duration calculate_age(HTTP::HeaderList const&, UnixDateTime request_time, UnixDateTime response_time, AK::Duration current_time_offset_for_testing);
enum class CacheLifetimeStatus { enum class CacheLifetimeStatus {
Fresh, Fresh,
Expired, Expired,
MustRevalidate, MustRevalidate,
}; };
CacheLifetimeStatus cache_lifetime_status(HTTP::HeaderMap const&, AK::Duration freshness_lifetime, AK::Duration current_age); CacheLifetimeStatus cache_lifetime_status(HTTP::HeaderList const&, AK::Duration freshness_lifetime, AK::Duration current_age);
struct RevalidationAttributes { struct RevalidationAttributes {
static RevalidationAttributes create(HTTP::HeaderMap const&); static RevalidationAttributes create(HTTP::HeaderList const&);
Optional<ByteString> etag; Optional<ByteString> etag;
Optional<UnixDateTime> last_modified; Optional<UnixDateTime> last_modified;
}; };
void update_header_fields(HTTP::HeaderMap&, HTTP::HeaderMap const&); void update_header_fields(HTTP::HeaderList&, HTTP::HeaderList const&);
AK::Duration compute_current_time_offset_for_testing(Optional<DiskCache&>, HTTP::HeaderMap const& request_headers); AK::Duration compute_current_time_offset_for_testing(Optional<DiskCache&>, HTTP::HeaderList const& request_headers);
} }

View file

@ -181,11 +181,11 @@ void ConnectionFromClient::set_use_system_dns()
m_resolver->dns.reset_connection(); m_resolver->dns.reset_connection();
} }
void ConnectionFromClient::start_request(i32 request_id, ByteString method, URL::URL url, HTTP::HeaderMap request_headers, ByteBuffer request_body, Core::ProxyData proxy_data) void ConnectionFromClient::start_request(i32 request_id, ByteString method, URL::URL url, Vector<HTTP::Header> request_headers, ByteBuffer request_body, Core::ProxyData proxy_data)
{ {
dbgln_if(REQUESTSERVER_DEBUG, "RequestServer: start_request({}, {})", request_id, url); dbgln_if(REQUESTSERVER_DEBUG, "RequestServer: start_request({}, {})", request_id, url);
auto request = Request::fetch(request_id, g_disk_cache, *this, m_curl_multi, m_resolver, move(url), move(method), move(request_headers), move(request_body), m_alt_svc_cache_path, proxy_data); auto request = Request::fetch(request_id, g_disk_cache, *this, m_curl_multi, m_resolver, move(url), move(method), HTTP::HeaderList::create(move(request_headers)), move(request_body), m_alt_svc_cache_path, proxy_data);
m_active_requests.set(request_id, move(request)); m_active_requests.set(request_id, move(request));
} }
@ -318,7 +318,7 @@ void ConnectionFromClient::remove_cache_entries_accessed_since(UnixDateTime sinc
g_disk_cache->remove_entries_accessed_since(since); g_disk_cache->remove_entries_accessed_since(since);
} }
void ConnectionFromClient::websocket_connect(i64 websocket_id, URL::URL url, ByteString origin, Vector<ByteString> protocols, Vector<ByteString> extensions, HTTP::HeaderMap additional_request_headers) void ConnectionFromClient::websocket_connect(i64 websocket_id, URL::URL url, ByteString origin, Vector<ByteString> protocols, Vector<ByteString> extensions, Vector<HTTP::Header> additional_request_headers)
{ {
auto host = url.serialized_host().to_byte_string(); auto host = url.serialized_host().to_byte_string();
@ -338,7 +338,7 @@ void ConnectionFromClient::websocket_connect(i64 websocket_id, URL::URL url, Byt
connection_info.set_origin(move(origin)); connection_info.set_origin(move(origin));
connection_info.set_protocols(move(protocols)); connection_info.set_protocols(move(protocols));
connection_info.set_extensions(move(extensions)); connection_info.set_extensions(move(extensions));
connection_info.set_headers(move(additional_request_headers)); connection_info.set_headers(HTTP::HeaderList::create(move(additional_request_headers)));
connection_info.set_dns_result(move(dns_result)); connection_info.set_dns_result(move(dns_result));
if (auto const& path = default_certificate_path(); !path.is_empty()) if (auto const& path = default_certificate_path(); !path.is_empty())

View file

@ -37,7 +37,7 @@ private:
virtual Messages::RequestServer::IsSupportedProtocolResponse is_supported_protocol(ByteString) override; virtual Messages::RequestServer::IsSupportedProtocolResponse is_supported_protocol(ByteString) override;
virtual void set_dns_server(ByteString host_or_address, u16 port, bool use_tls, bool validate_dnssec_locally) override; virtual void set_dns_server(ByteString host_or_address, u16 port, bool use_tls, bool validate_dnssec_locally) override;
virtual void set_use_system_dns() override; virtual void set_use_system_dns() override;
virtual void start_request(i32 request_id, ByteString, URL::URL, HTTP::HeaderMap, ByteBuffer, Core::ProxyData) override; virtual void start_request(i32 request_id, ByteString, URL::URL, Vector<HTTP::Header>, ByteBuffer, Core::ProxyData) override;
virtual Messages::RequestServer::StopRequestResponse stop_request(i32) override; virtual Messages::RequestServer::StopRequestResponse stop_request(i32) override;
virtual Messages::RequestServer::SetCertificateResponse set_certificate(i32, ByteString, ByteString) override; virtual Messages::RequestServer::SetCertificateResponse set_certificate(i32, ByteString, ByteString) override;
virtual void ensure_connection(URL::URL url, ::RequestServer::CacheLevel cache_level) override; virtual void ensure_connection(URL::URL url, ::RequestServer::CacheLevel cache_level) override;
@ -45,7 +45,7 @@ private:
virtual void estimate_cache_size_accessed_since(u64 cache_size_estimation_id, UnixDateTime since) override; virtual void estimate_cache_size_accessed_since(u64 cache_size_estimation_id, UnixDateTime since) override;
virtual void remove_cache_entries_accessed_since(UnixDateTime since) override; virtual void remove_cache_entries_accessed_since(UnixDateTime since) override;
virtual void websocket_connect(i64 websocket_id, URL::URL, ByteString, Vector<ByteString>, Vector<ByteString>, HTTP::HeaderMap) override; virtual void websocket_connect(i64 websocket_id, URL::URL, ByteString, Vector<ByteString>, Vector<ByteString>, Vector<HTTP::Header>) override;
virtual void websocket_send(i64 websocket_id, bool, ByteBuffer) override; virtual void websocket_send(i64 websocket_id, bool, ByteBuffer) override;
virtual void websocket_close(i64 websocket_id, u16, ByteString) override; virtual void websocket_close(i64 websocket_id, u16, ByteString) override;
virtual Messages::RequestServer::WebsocketSetCertificateResponse websocket_set_certificate(i64, ByteString, ByteString) override; virtual Messages::RequestServer::WebsocketSetCertificateResponse websocket_set_certificate(i64, ByteString, ByteString) override;

View file

@ -28,7 +28,7 @@ NonnullOwnPtr<Request> Request::fetch(
Resolver& resolver, Resolver& resolver,
URL::URL url, URL::URL url,
ByteString method, ByteString method,
HTTP::HeaderMap request_headers, NonnullRefPtr<HTTP::HeaderList> request_headers,
ByteBuffer request_body, ByteBuffer request_body,
ByteString alt_svc_cache_path, ByteString alt_svc_cache_path,
Core::ProxyData proxy_data) Core::ProxyData proxy_data)
@ -69,7 +69,7 @@ Request::Request(
Resolver& resolver, Resolver& resolver,
URL::URL url, URL::URL url,
ByteString method, ByteString method,
HTTP::HeaderMap request_headers, NonnullRefPtr<HTTP::HeaderList> request_headers,
ByteBuffer request_body, ByteBuffer request_body,
ByteString alt_svc_cache_path, ByteString alt_svc_cache_path,
Core::ProxyData proxy_data) Core::ProxyData proxy_data)
@ -85,6 +85,7 @@ Request::Request(
, m_request_body(move(request_body)) , m_request_body(move(request_body))
, m_alt_svc_cache_path(move(alt_svc_cache_path)) , m_alt_svc_cache_path(move(alt_svc_cache_path))
, m_proxy_data(proxy_data) , m_proxy_data(proxy_data)
, m_response_headers(HTTP::HeaderList::create())
, m_current_time_offset_for_testing(compute_current_time_offset_for_testing(m_disk_cache, m_request_headers)) , m_current_time_offset_for_testing(compute_current_time_offset_for_testing(m_disk_cache, m_request_headers))
{ {
m_request_start_time += m_current_time_offset_for_testing; m_request_start_time += m_current_time_offset_for_testing;
@ -102,6 +103,8 @@ Request::Request(
, m_curl_multi_handle(curl_multi) , m_curl_multi_handle(curl_multi)
, m_resolver(resolver) , m_resolver(resolver)
, m_url(move(url)) , m_url(move(url))
, m_request_headers(HTTP::HeaderList::create())
, m_response_headers(HTTP::HeaderList::create())
, m_current_time_offset_for_testing(compute_current_time_offset_for_testing(m_disk_cache, m_request_headers)) , m_current_time_offset_for_testing(compute_current_time_offset_for_testing(m_disk_cache, m_request_headers))
{ {
m_request_start_time += m_current_time_offset_for_testing; m_request_start_time += m_current_time_offset_for_testing;
@ -123,7 +126,7 @@ Request::~Request()
curl_slist_free_all(string_list); curl_slist_free_all(string_list);
if (m_cache_entry_writer.has_value()) if (m_cache_entry_writer.has_value())
(void)m_cache_entry_writer->flush(move(m_response_headers)); (void)m_cache_entry_writer->flush(m_response_headers);
} }
void Request::notify_request_unblocked(Badge<DiskCache>) void Request::notify_request_unblocked(Badge<DiskCache>)
@ -361,13 +364,13 @@ void Request::handle_fetch_state()
// CURLOPT_POSTFIELDS automatically sets the Content-Type header. Tell curl to remove it by setting a blank // CURLOPT_POSTFIELDS automatically sets the Content-Type header. Tell curl to remove it by setting a blank
// value if the headers passed in don't contain a content type. // value if the headers passed in don't contain a content type.
if (!m_request_headers.contains("Content-Type"sv)) if (!m_request_headers->contains("Content-Type"sv))
curl_headers = curl_slist_append(curl_headers, "Content-Type:"); curl_headers = curl_slist_append(curl_headers, "Content-Type:");
} else if (m_method == "HEAD"sv) { } else if (m_method == "HEAD"sv) {
set_option(CURLOPT_NOBODY, 1L); set_option(CURLOPT_NOBODY, 1L);
} }
for (auto const& header : m_request_headers.headers()) { for (auto const& header : *m_request_headers) {
if (header.value.is_empty()) { if (header.value.is_empty()) {
// curl will discard the header unless we pass the header name followed by a semicolon (i.e. we need to pass // curl will discard the header unless we pass the header name followed by a semicolon (i.e. we need to pass
// "Content-Type;" instead of "Content-Type: "). // "Content-Type;" instead of "Content-Type: ").
@ -437,7 +440,7 @@ void Request::handle_complete_state()
// a "close notify" alert. OpenSSL version 3.2 began treating this as an error, which curl translates to // a "close notify" alert. OpenSSL version 3.2 began treating this as an error, which curl translates to
// CURLE_RECV_ERROR in the absence of a Content-Length response header. The Python server used by WPT is one // CURLE_RECV_ERROR in the absence of a Content-Length response header. The Python server used by WPT is one
// such server. We ignore this error if we were actually able to download some response data. // such server. We ignore this error if we were actually able to download some response data.
if (m_curl_result_code == CURLE_RECV_ERROR && m_bytes_transferred_to_client != 0 && !m_response_headers.contains("Content-Length"sv)) if (m_curl_result_code == CURLE_RECV_ERROR && m_bytes_transferred_to_client != 0 && !m_response_headers->contains("Content-Length"sv))
m_curl_result_code = CURLE_OK; m_curl_result_code = CURLE_OK;
if (m_curl_result_code != CURLE_OK) { if (m_curl_result_code != CURLE_OK) {
@ -493,7 +496,7 @@ size_t Request::on_header_received(void* buffer, size_t size, size_t nmemb, void
if (auto colon_index = header_line.find(':'); colon_index.has_value()) { if (auto colon_index = header_line.find(':'); colon_index.has_value()) {
auto name = header_line.substring_view(0, *colon_index).trim_whitespace(); auto name = header_line.substring_view(0, *colon_index).trim_whitespace();
auto value = header_line.substring_view(*colon_index + 1).trim_whitespace(); auto value = header_line.substring_view(*colon_index + 1).trim_whitespace();
request.m_response_headers.set(name, value); request.m_response_headers->append({ name, value });
} }
return total_size; return total_size;
@ -576,18 +579,18 @@ void Request::transfer_headers_to_client_if_needed()
case CacheStatus::Unknown: case CacheStatus::Unknown:
break; break;
case CacheStatus::NotCached: case CacheStatus::NotCached:
m_response_headers.set(TEST_CACHE_STATUS_HEADER, "not-cached"sv); m_response_headers->set({ TEST_CACHE_STATUS_HEADER, "not-cached"sv });
break; break;
case CacheStatus::WrittenToCache: case CacheStatus::WrittenToCache:
m_response_headers.set(TEST_CACHE_STATUS_HEADER, "written-to-cache"sv); m_response_headers->set({ TEST_CACHE_STATUS_HEADER, "written-to-cache"sv });
break; break;
case CacheStatus::ReadFromCache: case CacheStatus::ReadFromCache:
m_response_headers.set(TEST_CACHE_STATUS_HEADER, "read-from-cache"sv); m_response_headers->set({ TEST_CACHE_STATUS_HEADER, "read-from-cache"sv });
break; break;
} }
} }
m_client.async_headers_became_available(m_request_id, m_response_headers, m_status_code, m_reason_phrase); m_client.async_headers_became_available(m_request_id, m_response_headers->headers(), m_status_code, m_reason_phrase);
} }
ErrorOr<void> Request::write_queued_bytes_without_blocking() ErrorOr<void> Request::write_queued_bytes_without_blocking()

View file

@ -14,7 +14,7 @@
#include <AK/Weakable.h> #include <AK/Weakable.h>
#include <LibCore/Proxy.h> #include <LibCore/Proxy.h>
#include <LibDNS/Resolver.h> #include <LibDNS/Resolver.h>
#include <LibHTTP/HeaderMap.h> #include <LibHTTP/HeaderList.h>
#include <LibRequests/NetworkError.h> #include <LibRequests/NetworkError.h>
#include <LibRequests/RequestTimingInfo.h> #include <LibRequests/RequestTimingInfo.h>
#include <LibURL/URL.h> #include <LibURL/URL.h>
@ -36,7 +36,7 @@ public:
Resolver& resolver, Resolver& resolver,
URL::URL url, URL::URL url,
ByteString method, ByteString method,
HTTP::HeaderMap request_headers, NonnullRefPtr<HTTP::HeaderList> request_headers,
ByteBuffer request_body, ByteBuffer request_body,
ByteString alt_svc_cache_path, ByteString alt_svc_cache_path,
Core::ProxyData proxy_data); Core::ProxyData proxy_data);
@ -53,7 +53,7 @@ public:
URL::URL const& url() const { return m_url; } URL::URL const& url() const { return m_url; }
ByteString const& method() const { return m_method; } ByteString const& method() const { return m_method; }
HTTP::HeaderMap const& request_headers() const { return m_request_headers; } HTTP::HeaderList const& request_headers() const { return m_request_headers; }
UnixDateTime request_start_time() const { return m_request_start_time; } UnixDateTime request_start_time() const { return m_request_start_time; }
AK::Duration current_time_offset_for_testing() const { return m_current_time_offset_for_testing; } AK::Duration current_time_offset_for_testing() const { return m_current_time_offset_for_testing; }
@ -92,7 +92,7 @@ private:
Resolver& resolver, Resolver& resolver,
URL::URL url, URL::URL url,
ByteString method, ByteString method,
HTTP::HeaderMap request_headers, NonnullRefPtr<HTTP::HeaderList> request_headers,
ByteBuffer request_body, ByteBuffer request_body,
ByteString alt_svc_cache_path, ByteString alt_svc_cache_path,
Core::ProxyData proxy_data); Core::ProxyData proxy_data);
@ -147,7 +147,7 @@ private:
ByteString m_method; ByteString m_method;
UnixDateTime m_request_start_time { UnixDateTime::now() }; UnixDateTime m_request_start_time { UnixDateTime::now() };
HTTP::HeaderMap m_request_headers; NonnullRefPtr<HTTP::HeaderList> m_request_headers;
ByteBuffer m_request_body; ByteBuffer m_request_body;
ByteString m_alt_svc_cache_path; ByteString m_alt_svc_cache_path;
@ -156,7 +156,7 @@ private:
u32 m_status_code { 0 }; u32 m_status_code { 0 };
Optional<String> m_reason_phrase; Optional<String> m_reason_phrase;
HTTP::HeaderMap m_response_headers; NonnullRefPtr<HTTP::HeaderList> m_response_headers;
bool m_sent_response_headers_to_client { false }; bool m_sent_response_headers_to_client { false };
AllocatingMemoryStream m_response_buffer; AllocatingMemoryStream m_response_buffer;

View file

@ -1,4 +1,4 @@
#include <LibHTTP/HeaderMap.h> #include <LibHTTP/Header.h>
#include <LibRequests/CacheSizes.h> #include <LibRequests/CacheSizes.h>
#include <LibRequests/NetworkError.h> #include <LibRequests/NetworkError.h>
#include <LibRequests/RequestTimingInfo.h> #include <LibRequests/RequestTimingInfo.h>
@ -8,7 +8,7 @@ endpoint RequestClient
{ {
request_started(i32 request_id, IPC::File fd) =| request_started(i32 request_id, IPC::File fd) =|
request_finished(i32 request_id, u64 total_size, Requests::RequestTimingInfo timing_info, Optional<Requests::NetworkError> network_error) =| request_finished(i32 request_id, u64 total_size, Requests::RequestTimingInfo timing_info, Optional<Requests::NetworkError> network_error) =|
headers_became_available(i32 request_id, HTTP::HeaderMap response_headers, Optional<u32> status_code, Optional<String> reason_phrase) =| headers_became_available(i32 request_id, Vector<HTTP::Header> response_headers, Optional<u32> status_code, Optional<String> reason_phrase) =|
// Websocket API // Websocket API
// FIXME: See if this can be merged with the regular APIs // FIXME: See if this can be merged with the regular APIs

View file

@ -1,5 +1,5 @@
#include <LibCore/Proxy.h> #include <LibCore/Proxy.h>
#include <LibHTTP/HeaderMap.h> #include <LibHTTP/Header.h>
#include <LibURL/URL.h> #include <LibURL/URL.h>
#include <RequestServer/CacheLevel.h> #include <RequestServer/CacheLevel.h>
@ -16,7 +16,7 @@ endpoint RequestServer
// Test if a specific protocol is supported, e.g "http" // Test if a specific protocol is supported, e.g "http"
is_supported_protocol(ByteString protocol) => (bool supported) is_supported_protocol(ByteString protocol) => (bool supported)
start_request(i32 request_id, ByteString method, URL::URL url, HTTP::HeaderMap request_headers, ByteBuffer request_body, Core::ProxyData proxy_data) =| start_request(i32 request_id, ByteString method, URL::URL url, Vector<HTTP::Header> request_headers, ByteBuffer request_body, Core::ProxyData proxy_data) =|
stop_request(i32 request_id) => (bool success) stop_request(i32 request_id) => (bool success)
set_certificate(i32 request_id, ByteString certificate, ByteString key) => (bool success) set_certificate(i32 request_id, ByteString certificate, ByteString key) => (bool success)
@ -26,7 +26,7 @@ endpoint RequestServer
remove_cache_entries_accessed_since(UnixDateTime since) =| remove_cache_entries_accessed_since(UnixDateTime since) =|
// Websocket Connection API // Websocket Connection API
websocket_connect(i64 websocket_id, URL::URL url, ByteString origin, Vector<ByteString> protocols, Vector<ByteString> extensions, HTTP::HeaderMap additional_request_headers) =| websocket_connect(i64 websocket_id, URL::URL url, ByteString origin, Vector<ByteString> protocols, Vector<ByteString> extensions, Vector<HTTP::Header> additional_request_headers) =|
websocket_send(i64 websocket_id, bool is_text, ByteBuffer data) =| websocket_send(i64 websocket_id, bool is_text, ByteBuffer data) =|
websocket_close(i64 websocket_id, u16 code, ByteString reason) =| websocket_close(i64 websocket_id, u16 code, ByteString reason) =|
websocket_set_certificate(i64 request_id, ByteString certificate, ByteString key) => (bool success) websocket_set_certificate(i64 request_id, ByteString certificate, ByteString key) => (bool success)

View file

@ -5,6 +5,7 @@ add_subdirectory(LibCrypto)
add_subdirectory(LibDiff) add_subdirectory(LibDiff)
add_subdirectory(LibDNS) add_subdirectory(LibDNS)
add_subdirectory(LibGC) add_subdirectory(LibGC)
add_subdirectory(LibHTTP)
add_subdirectory(LibJS) add_subdirectory(LibJS)
add_subdirectory(LibRegex) add_subdirectory(LibRegex)
add_subdirectory(LibTest) add_subdirectory(LibTest)

View file

@ -0,0 +1,7 @@
set(TEST_SOURCES
TestHTTPUtils.cpp
)
foreach(source IN LISTS TEST_SOURCES)
ladybird_test("${source}" LibWeb LIBS LibHTTP)
endforeach()

View file

@ -1,5 +1,5 @@
/* /*
* Copyright (c) 2024, Tim Flynn <trflynn89@serenityos.org> * Copyright (c) 2024-2025, Tim Flynn <trflynn89@ladybird.org>
* *
* SPDX-License-Identifier: BSD-2-Clause * SPDX-License-Identifier: BSD-2-Clause
*/ */
@ -8,7 +8,7 @@
#include <AK/GenericLexer.h> #include <AK/GenericLexer.h>
#include <AK/String.h> #include <AK/String.h>
#include <LibWeb/Fetch/Infrastructure/HTTP.h> #include <LibHTTP/HTTP.h>
TEST_CASE(collect_an_http_quoted_string) TEST_CASE(collect_an_http_quoted_string)
{ {
@ -16,14 +16,14 @@ TEST_CASE(collect_an_http_quoted_string)
auto test = "\"\""_string; auto test = "\"\""_string;
GenericLexer lexer { test }; GenericLexer lexer { test };
auto result = Web::Fetch::Infrastructure::collect_an_http_quoted_string(lexer); auto result = HTTP::collect_an_http_quoted_string(lexer);
EXPECT_EQ(result, "\"\""_string); EXPECT_EQ(result, "\"\""_string);
} }
{ {
auto test = "\"abc\""_string; auto test = "\"abc\""_string;
GenericLexer lexer { test }; GenericLexer lexer { test };
auto result = Web::Fetch::Infrastructure::collect_an_http_quoted_string(lexer); auto result = HTTP::collect_an_http_quoted_string(lexer);
EXPECT_EQ(result, "\"abc\""_string); EXPECT_EQ(result, "\"abc\""_string);
} }
{ {
@ -32,7 +32,7 @@ TEST_CASE(collect_an_http_quoted_string)
GenericLexer lexer { test }; GenericLexer lexer { test };
lexer.ignore(4); lexer.ignore(4);
auto result = Web::Fetch::Infrastructure::collect_an_http_quoted_string(lexer); auto result = HTTP::collect_an_http_quoted_string(lexer);
EXPECT_EQ(result, "\"abc\""_string); EXPECT_EQ(result, "\"abc\""_string);
} }
{ {
@ -41,7 +41,7 @@ TEST_CASE(collect_an_http_quoted_string)
GenericLexer lexer { test }; GenericLexer lexer { test };
lexer.ignore(4); lexer.ignore(4);
auto result = Web::Fetch::Infrastructure::collect_an_http_quoted_string(lexer); auto result = HTTP::collect_an_http_quoted_string(lexer);
EXPECT_EQ(result, "\"abc\""_string); EXPECT_EQ(result, "\"abc\""_string);
} }
{ {
@ -50,14 +50,14 @@ TEST_CASE(collect_an_http_quoted_string)
GenericLexer lexer { test }; GenericLexer lexer { test };
lexer.ignore(4); lexer.ignore(4);
auto result = Web::Fetch::Infrastructure::collect_an_http_quoted_string(lexer); auto result = HTTP::collect_an_http_quoted_string(lexer);
EXPECT_EQ(result, "\"abc\""_string); EXPECT_EQ(result, "\"abc\""_string);
} }
{ {
auto test = "\"abc\" bar"_string; auto test = "\"abc\" bar"_string;
GenericLexer lexer { test }; GenericLexer lexer { test };
auto result = Web::Fetch::Infrastructure::collect_an_http_quoted_string(lexer); auto result = HTTP::collect_an_http_quoted_string(lexer);
EXPECT_EQ(result, "\"abc\""_string); EXPECT_EQ(result, "\"abc\""_string);
} }
} }

View file

@ -4,7 +4,6 @@ set(TEST_SOURCES
TestCSSPixels.cpp TestCSSPixels.cpp
TestCSSSyntaxParser.cpp TestCSSSyntaxParser.cpp
TestCSSTokenStream.cpp TestCSSTokenStream.cpp
TestFetchInfrastructure.cpp
TestFetchURL.cpp TestFetchURL.cpp
TestHTMLTokenizer.cpp TestHTMLTokenizer.cpp
TestMicrosyntax.cpp TestMicrosyntax.cpp