From 4e48298414efa69029246dd300a10c52c83db1a6 Mon Sep 17 00:00:00 2001 From: rmg-x Date: Mon, 18 Nov 2024 17:20:33 -0600 Subject: [PATCH] LibWeb/Fetch: Implement `build_content_range(start, end, full_length)` --- .../LibWeb/Fetch/Infrastructure/HTTP/Headers.cpp | 13 +++++++++++++ .../LibWeb/Fetch/Infrastructure/HTTP/Headers.h | 1 + 2 files changed, 14 insertions(+) diff --git a/Libraries/LibWeb/Fetch/Infrastructure/HTTP/Headers.cpp b/Libraries/LibWeb/Fetch/Infrastructure/HTTP/Headers.cpp index a3632494e3b..8f2be78b19f 100644 --- a/Libraries/LibWeb/Fetch/Infrastructure/HTTP/Headers.cpp +++ b/Libraries/LibWeb/Fetch/Infrastructure/HTTP/Headers.cpp @@ -832,6 +832,19 @@ Variant, ExtractHeaderParseFailure, Empty> extract_header_lis return values; } +// https://fetch.spec.whatwg.org/#build-a-content-range +ByteString build_content_range(u64 const& range_start, u64 const& range_end, u64 const& full_length) +{ + // 1. Let contentRange be `bytes `. + // 2. Append rangeStart, serialized and isomorphic encoded, to contentRange. + // 3. Append 0x2D (-) to contentRange. + // 4. Append rangeEnd, serialized and isomorphic encoded to contentRange. + // 5. Append 0x2F (/) to contentRange. + // 6. Append fullLength, serialized and isomorphic encoded to contentRange. + // 7. Return contentRange. + return ByteString::formatted("bytes {}-{}/{}", String::number(range_start), String::number(range_end), String::number(full_length)); +} + // https://fetch.spec.whatwg.org/#simple-range-header-value Optional parse_single_range_header_value(ReadonlyBytes const value, bool const allow_whitespace) { diff --git a/Libraries/LibWeb/Fetch/Infrastructure/HTTP/Headers.h b/Libraries/LibWeb/Fetch/Infrastructure/HTTP/Headers.h index d786005c318..f032aeb1fa2 100644 --- a/Libraries/LibWeb/Fetch/Infrastructure/HTTP/Headers.h +++ b/Libraries/LibWeb/Fetch/Infrastructure/HTTP/Headers.h @@ -93,6 +93,7 @@ struct ExtractHeaderParseFailure { [[nodiscard]] bool is_request_body_header_name(ReadonlyBytes); [[nodiscard]] Optional> extract_header_values(Header const&); [[nodiscard]] Variant, ExtractHeaderParseFailure, Empty> extract_header_list_values(ReadonlyBytes, HeaderList const&); +[[nodiscard]] ByteString build_content_range(u64 const& range_start, u64 const& range_end, u64 const& full_length); [[nodiscard]] Optional parse_single_range_header_value(ReadonlyBytes, bool); [[nodiscard]] ByteBuffer default_user_agent_value();