2022-07-11 21:42:14 +01:00
|
|
|
/*
|
2023-03-02 23:26:35 +00:00
|
|
|
* Copyright (c) 2022-2023, Linus Groh <linusg@serenityos.org>
|
2022-07-11 21:42:14 +01:00
|
|
|
*
|
|
|
|
* SPDX-License-Identifier: BSD-2-Clause
|
|
|
|
*/
|
|
|
|
|
|
|
|
#pragma once
|
|
|
|
|
|
|
|
#include <AK/ByteBuffer.h>
|
2022-07-14 00:58:10 +01:00
|
|
|
#include <AK/Error.h>
|
2022-07-11 21:42:14 +01:00
|
|
|
#include <AK/Forward.h>
|
2022-07-14 00:58:10 +01:00
|
|
|
#include <AK/HashTable.h>
|
|
|
|
#include <AK/Optional.h>
|
2023-03-02 23:26:35 +00:00
|
|
|
#include <AK/String.h>
|
2022-07-14 00:58:10 +01:00
|
|
|
#include <AK/Vector.h>
|
2024-11-15 04:01:23 +13:00
|
|
|
#include <LibGC/Heap.h>
|
|
|
|
#include <LibGC/Ptr.h>
|
2022-10-30 01:52:07 +00:00
|
|
|
#include <LibJS/Forward.h>
|
2024-11-14 20:22:33 +13:00
|
|
|
#include <LibJS/Heap/Cell.h>
|
2022-07-20 17:47:29 +01:00
|
|
|
#include <LibWeb/MimeSniff/MimeType.h>
|
2022-07-11 21:42:14 +01:00
|
|
|
|
2022-07-17 23:52:02 +01:00
|
|
|
namespace Web::Fetch::Infrastructure {
|
2022-07-11 21:42:14 +01:00
|
|
|
|
|
|
|
// 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 Header {
|
|
|
|
ByteBuffer name;
|
|
|
|
ByteBuffer value;
|
2022-10-24 09:16:32 +01:00
|
|
|
|
2024-10-22 11:47:22 +02:00
|
|
|
[[nodiscard]] static Header copy(Header const&);
|
|
|
|
[[nodiscard]] static Header from_string_pair(StringView, StringView);
|
2024-12-12 10:26:41 -08:00
|
|
|
[[nodiscard]] static Header from_latin1_pair(StringView, StringView);
|
2022-07-11 21:42:14 +01:00
|
|
|
};
|
|
|
|
|
|
|
|
// 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.
|
2022-09-25 20:52:51 +01:00
|
|
|
class HeaderList final
|
2022-10-30 01:52:07 +00:00
|
|
|
: public JS::Cell
|
2024-05-21 10:38:10 -06:00
|
|
|
, public Vector<Header> {
|
2024-11-15 04:01:23 +13:00
|
|
|
GC_CELL(HeaderList, JS::Cell);
|
|
|
|
GC_DECLARE_ALLOCATOR(HeaderList);
|
2022-10-30 01:52:07 +00:00
|
|
|
|
2022-07-11 21:42:14 +01:00
|
|
|
public:
|
|
|
|
using Vector::begin;
|
2022-09-25 19:33:13 +01:00
|
|
|
using Vector::clear;
|
2022-07-11 21:42:14 +01:00
|
|
|
using Vector::end;
|
2023-05-25 17:19:19 +02:00
|
|
|
using Vector::is_empty;
|
2022-07-11 21:42:14 +01:00
|
|
|
|
2024-11-15 04:01:23 +13:00
|
|
|
[[nodiscard]] static GC::Ref<HeaderList> create(JS::VM&);
|
2022-10-30 01:52:07 +00:00
|
|
|
|
2022-07-11 21:42:14 +01:00
|
|
|
[[nodiscard]] bool contains(ReadonlyBytes) const;
|
2024-04-26 13:24:20 -04:00
|
|
|
[[nodiscard]] Optional<ByteBuffer> get(ReadonlyBytes) const;
|
|
|
|
[[nodiscard]] Optional<Vector<String>> get_decode_and_split(ReadonlyBytes) const;
|
|
|
|
void append(Header);
|
2022-07-11 21:42:14 +01:00
|
|
|
void delete_(ReadonlyBytes name);
|
2024-04-26 13:24:20 -04:00
|
|
|
void set(Header);
|
|
|
|
void combine(Header);
|
|
|
|
[[nodiscard]] Vector<Header> sort_and_combine() const;
|
2023-02-28 18:20:28 +00:00
|
|
|
|
2024-04-26 13:24:20 -04:00
|
|
|
struct ExtractLengthFailure { };
|
2023-02-28 18:20:28 +00:00
|
|
|
using ExtractLengthResult = Variant<u64, ExtractLengthFailure, Empty>;
|
|
|
|
|
2024-04-26 13:24:20 -04:00
|
|
|
[[nodiscard]] ExtractLengthResult extract_length() const;
|
2023-02-28 18:20:28 +00:00
|
|
|
|
2024-04-26 13:24:20 -04:00
|
|
|
[[nodiscard]] Optional<MimeSniff::MimeType> extract_mime_type() const;
|
2023-08-01 21:40:30 +12:00
|
|
|
|
2024-04-26 13:24:20 -04:00
|
|
|
[[nodiscard]] Vector<ByteBuffer> unique_names() const;
|
2022-07-11 21:42:14 +01:00
|
|
|
};
|
|
|
|
|
2022-10-15 00:39:40 +02:00
|
|
|
struct RangeHeaderValue {
|
|
|
|
Optional<u64> start;
|
|
|
|
Optional<u64> end;
|
|
|
|
};
|
|
|
|
|
2023-02-08 23:32:44 +00:00
|
|
|
struct ExtractHeaderParseFailure {
|
|
|
|
};
|
|
|
|
|
2023-05-10 16:26:51 -04:00
|
|
|
[[nodiscard]] StringView legacy_extract_an_encoding(Optional<MimeSniff::MimeType> const& mime_type, StringView fallback_encoding);
|
2024-04-26 13:24:20 -04:00
|
|
|
[[nodiscard]] Optional<Vector<String>> get_decode_and_split_header_value(ReadonlyBytes);
|
|
|
|
[[nodiscard]] OrderedHashTable<ByteBuffer> convert_header_names_to_a_sorted_lowercase_set(Span<ReadonlyBytes>);
|
2022-07-11 21:42:14 +01:00
|
|
|
[[nodiscard]] bool is_header_name(ReadonlyBytes);
|
|
|
|
[[nodiscard]] bool is_header_value(ReadonlyBytes);
|
2024-04-26 13:24:20 -04:00
|
|
|
[[nodiscard]] ByteBuffer normalize_header_value(ReadonlyBytes);
|
2022-07-11 21:42:14 +01:00
|
|
|
[[nodiscard]] bool is_cors_safelisted_request_header(Header const&);
|
|
|
|
[[nodiscard]] bool is_cors_unsafe_request_header_byte(u8);
|
2024-04-26 13:24:20 -04:00
|
|
|
[[nodiscard]] OrderedHashTable<ByteBuffer> get_cors_unsafe_header_names(HeaderList const&);
|
2022-07-11 21:42:14 +01:00
|
|
|
[[nodiscard]] bool is_cors_non_wildcard_request_header_name(ReadonlyBytes);
|
|
|
|
[[nodiscard]] bool is_privileged_no_cors_request_header_name(ReadonlyBytes);
|
|
|
|
[[nodiscard]] bool is_cors_safelisted_response_header_name(ReadonlyBytes, Span<ReadonlyBytes>);
|
|
|
|
[[nodiscard]] bool is_no_cors_safelisted_request_header_name(ReadonlyBytes);
|
|
|
|
[[nodiscard]] bool is_no_cors_safelisted_request_header(Header const&);
|
2024-04-26 13:24:20 -04:00
|
|
|
[[nodiscard]] bool is_forbidden_request_header(Header const&);
|
2022-07-11 21:42:14 +01:00
|
|
|
[[nodiscard]] bool is_forbidden_response_header_name(ReadonlyBytes);
|
|
|
|
[[nodiscard]] bool is_request_body_header_name(ReadonlyBytes);
|
2024-04-26 13:24:20 -04:00
|
|
|
[[nodiscard]] Optional<Vector<ByteBuffer>> extract_header_values(Header const&);
|
|
|
|
[[nodiscard]] Variant<Vector<ByteBuffer>, ExtractHeaderParseFailure, Empty> extract_header_list_values(ReadonlyBytes, HeaderList const&);
|
2024-11-18 17:20:33 -06:00
|
|
|
[[nodiscard]] ByteString build_content_range(u64 const& range_start, u64 const& range_end, u64 const& full_length);
|
2024-11-18 17:18:26 -06:00
|
|
|
[[nodiscard]] Optional<RangeHeaderValue> parse_single_range_header_value(ReadonlyBytes, bool);
|
2023-12-27 11:43:14 +01:00
|
|
|
[[nodiscard]] ByteBuffer default_user_agent_value();
|
2022-07-11 21:42:14 +01:00
|
|
|
|
|
|
|
}
|