Truly independent web browser
LibHTTP+LibWebCommon: Share the HTTP lexing helpers in Rust
Shannon Booth7ee02be8f964
diff --git a/Cargo.lock b/Cargo.lockindex ebe53d599a5..e7f9f9f789c 100644--- a/Cargo.lock+++ b/Cargo.lock@@ -1772,6 +1772,13 @@ dependencies = ["yuv",]+[[package]]+name = "libhttp_rust"+version = "0.1.0"+dependencies = [+ "ak",+]+[[package]]name = "libimagedecoders_rust"version = "0.1.0"@@ -2013,6 +2020,7 @@ dependencies = ["ak","cbindgen","ladybird_allocator",+ "libhttp_rust","liburl_rust","rustc-demangle",]diff --git a/Cargo.toml b/Cargo.tomlindex b07f4e32fdb..5839ac86e7a 100644--- a/Cargo.toml+++ b/Cargo.toml@@ -4,6 +4,7 @@ members = ["Libraries/LibCompositing/Rust","Libraries/LibCompress/Rust","Libraries/LibGfx/Rust",+ "Libraries/LibHTTP/Rust","Libraries/LibImageDecoders/Rust","Libraries/LibIPC/Rust","Libraries/LibJS/ABI",diff --git a/Libraries/LibHTTP/Rust/Cargo.toml b/Libraries/LibHTTP/Rust/Cargo.tomlnew file mode 100644index 00000000000..bd55aed7272--- /dev/null+++ b/Libraries/LibHTTP/Rust/Cargo.toml@@ -0,0 +1,13 @@+[package]+name = "libhttp_rust"+version = "0.1.0"+edition = "2024"++[lib]+crate-type = ["rlib"]++[dependencies]+ak = { path = "../../../AK/Rust" }++[lints]+workspace = truediff --git a/Libraries/LibHTTP/Rust/src/http.rs b/Libraries/LibHTTP/Rust/src/http.rsnew file mode 100644index 00000000000..19b66181b85--- /dev/null+++ b/Libraries/LibHTTP/Rust/src/http.rs@@ -0,0 +1,96 @@+/*+ * Copyright (c) 2022-2023, Linus Groh <[email protected]>+ * Copyright (c) 2022, Luke Wilde <[email protected]>+ * Copyright (c) 2026-present, the Ladybird developers.+ *+ * SPDX-License-Identifier: BSD-2-Clause+ */++use ak::GenericLexer;++// https://fetch.spec.whatwg.org/#http-tab-or-space+// An HTTP tab or space is U+0009 TAB or U+0020 SPACE.+pub fn is_http_tab_or_space(code_point: impl Into<u32>) -> bool {+ matches!(code_point.into(), 0x09 | 0x20)+}++// https://fetch.spec.whatwg.org/#http-whitespace+// HTTP whitespace is U+000A LF, U+000D CR, or an HTTP tab or space.+pub fn is_http_whitespace(code_point: impl Into<u32>) -> bool {+ let code_point = code_point.into();+ matches!(code_point, 0x0A | 0x0D) || is_http_tab_or_space(code_point)+}++// https://mimesniff.spec.whatwg.org/#http-token-code-point+// An HTTP token code point is U+0021 (!), U+0023 (#), U+0024 ($), U+0025 (%), U+0026 (&), U+0027 ('), U+002A (*),+// U+002B (+), U+002D (-), U+002E (.), U+005E (^), U+005F (_), U+0060 (`), U+007C (|), U+007E (~), or an ASCII alphanumeric.+pub fn is_http_token_code_point(code_point: impl Into<u32>) -> bool {+ char::from_u32(code_point.into()).is_some_and(|code_point| {+ matches!(+ code_point,+ '!' | '#' | '$' | '%' | '&' | '\'' | '*' | '+' | '-' | '.' | '^' | '_' | '`' | '|' | '~'+ ) || code_point.is_ascii_alphanumeric()+ })+}++pub struct HttpQuotedString<T> {+ pub value: Vec<T>,+ pub closed: bool,+}++// https://fetch.spec.whatwg.org/#collect-an-http-quoted-string+pub fn collect_an_http_quoted_string_extracting_the_value<T: Copy + PartialEq + From<u8>>(+ lexer: &mut GenericLexer<'_, T>,+) -> HttpQuotedString<T> {+ let quote = T::from(b'"');+ let backslash = T::from(b'\\');++ // 1. Let positionStart be position.+ // NB: Skipped, as every caller uses the extract-value flag.++ // 2. Let value be the empty string.+ let mut value = Vec::new();+ let mut closed = false;++ // 3. Assert: the code point at position within input is U+0022 (").+ // 4. Advance position by 1.+ assert!(lexer.consume_specific(quote));++ // 5. While true:+ loop {+ // 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.+ value.extend_from_slice(lexer.consume_while(|code_point| code_point != quote && code_point != backslash));++ // 2. If position is past the end of input, then break.+ // 3. Let quoteOrBackslash be the code point at position within input.+ // 4. Advance position by 1.+ let Some(quote_or_backslash) = lexer.consume() else {+ break;+ };++ // 5. If quoteOrBackslash is U+005C (\), then:+ if quote_or_backslash == backslash {+ // 1. If position is past the end of input, then append U+005C (\) to value and break.+ // 2. Append the code point at position within input to value.+ // 3. Advance position by 1.+ let Some(code_point) = lexer.consume() else {+ value.push(backslash);+ break;+ };+ value.push(code_point);+ }+ // 6. Otherwise:+ else {+ // 1. Assert: quoteOrBackslash is U+0022 (").+ assert!(quote_or_backslash == quote);++ // 2. Break.+ closed = true;+ break;+ }+ }++ // 6. If the extract-value flag is set, then return value.+ HttpQuotedString { value, closed }+}diff --git a/Libraries/LibHTTP/Rust/src/lib.rs b/Libraries/LibHTTP/Rust/src/lib.rsnew file mode 100644index 00000000000..7289bc0ee7d--- /dev/null+++ b/Libraries/LibHTTP/Rust/src/lib.rs@@ -0,0 +1,7 @@+/*+ * Copyright (c) 2026-present, the Ladybird developers.+ *+ * SPDX-License-Identifier: BSD-2-Clause+ */++pub mod http;diff --git a/Libraries/LibWebCommon/Rust/Cargo.toml b/Libraries/LibWebCommon/Rust/Cargo.tomlindex e34b92e57bc..9cdffcc2d99 100644--- a/Libraries/LibWebCommon/Rust/Cargo.toml+++ b/Libraries/LibWebCommon/Rust/Cargo.toml@@ -9,6 +9,7 @@ crate-type = ["rlib", "staticlib"][dependencies]ak = { path = "../../../AK/Rust" }ladybird_allocator = { path = "../../RustAllocator", optional = true }+libhttp_rust = { path = "../../LibHTTP/Rust" }liburl_rust = { path = "../../LibURL/Rust", default-features = false }rustc-demangle = "0.1"diff --git a/Libraries/LibWebCommon/Rust/src/mime_sniff/mime_type.rs b/Libraries/LibWebCommon/Rust/src/mime_sniff/mime_type.rsindex 667a81ed894..c3c2f21bd68 100644--- a/Libraries/LibWebCommon/Rust/src/mime_sniff/mime_type.rs+++ b/Libraries/LibWebCommon/Rust/src/mime_sniff/mime_type.rs@@ -8,6 +8,11 @@* SPDX-License-Identifier: BSD-2-Clause*/+use ak::GenericLexer;+use libhttp_rust::http::{+ collect_an_http_quoted_string_extracting_the_value, is_http_token_code_point, is_http_whitespace,+};+// https://mimesniff.spec.whatwg.org/#javascript-mime-type// A JavaScript MIME type is any MIME type whose essence is one of the following:const JAVASCRIPT_MIME_TYPE_ESSENCE_STRINGS: [&str; 16] = [@@ -29,22 +34,6 @@ const JAVASCRIPT_MIME_TYPE_ESSENCE_STRINGS: [&str; 16] = ["text/x-javascript",];-// https://fetch.spec.whatwg.org/#http-whitespace-// HTTP whitespace is U+000A LF, U+000D CR, or an HTTP tab or space.-fn is_http_whitespace(code_point: char) -> bool {- matches!(code_point, '\n' | '\r' | '\t' | ' ')-}--// https://mimesniff.spec.whatwg.org/#http-token-code-point-// An HTTP token code point is U+0021 (!), U+0023 (#), U+0024 ($), U+0025 (%), U+0026 (&), U+0027 ('), U+002A (*),-// U+002B (+), U+002D (-), U+002E (.), U+005E (^), U+005F (_), U+0060 (`), U+007C (|), U+007E (~), or an ASCII alphanumeric.-fn is_http_token_code_point(code_point: char) -> bool {- matches!(- code_point,- '!' | '#' | '$' | '%' | '&' | '\'' | '*' | '+' | '-' | '.' | '^' | '_' | '`' | '|' | '~'- ) || code_point.is_ascii_alphanumeric()-}-// https://mimesniff.spec.whatwg.org/#http-quoted-string-token-code-point// An HTTP quoted-string token code point is U+0009 TAB, a code point in the range U+0020 SPACE to U+007E (~), inclusive,// or a code point in the range U+0080 through U+00FF (ÿ), inclusive.@@ -64,80 +53,6 @@ fn trim_trailing_http_whitespace(code_points: &[char]) -> &[char] {&code_points[..end]}-// https://infra.spec.whatwg.org/#collect-a-sequence-of-code-points-fn collect_a_sequence_of_code_points<'a>(- input: &'a [char],- position: &mut usize,- condition: impl Fn(char) -> bool,-) -> &'a [char] {- let start = *position;- while *position < input.len() && condition(input[*position]) {- *position += 1;- }- &input[start..*position]-}--// https://fetch.spec.whatwg.org/#collect-an-http-quoted-string-fn collect_an_http_quoted_string_extracting_the_value(input: &[char], position: &mut usize) -> String {- // 1. Let positionStart be position.- // NB: Skipped, as MIME type parsing uses the extract-value flag.-- // 2. Let value be the empty string.- let mut value = String::new();-- // 3. Assert: the code point at position within input is U+0022 (").- assert_eq!(input[*position], '"');-- // 4. Advance position by 1.- *position += 1;-- // 5. While true:- loop {- // 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.- value.extend(collect_a_sequence_of_code_points(input, position, |code_point| {- code_point != '"' && code_point != '\\'- }));-- // 2. If position is past the end of input, then break.- if *position >= input.len() {- break;- }-- // 3. Let quoteOrBackslash be the code point at position within input.- let quote_or_backslash = input[*position];-- // 4. Advance position by 1.- *position += 1;-- // 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 *position >= input.len() {- value.push('\\');- break;- }-- // 2. Append the code point at position within input to value.- value.push(input[*position]);-- // 3. Advance position by 1.- *position += 1;- }- // 6. Otherwise:- else {- // 1. Assert: quoteOrBackslash is U+0022 (").- assert_eq!(quote_or_backslash, '"');-- // 2. Break.- break;- }- }-- // 6. If the extract-value flag is set, then return value.- value-}-/// The type and subtype of a MIME type, which say which groups of MIME types it belongs to.#[derive(Clone, Copy)]pub struct Essence<'a> {@@ -323,10 +238,10 @@ impl MimeType {let input = trim_trailing_http_whitespace(&input[start..]);// 2. Let position be a position variable for input, initially pointing at the start of input.- let mut position = 0;+ let mut lexer = GenericLexer::new(input);// 3. Let type be the result of collecting a sequence of code points that are not U+002F (/) from input, given position.- let type_ = collect_a_sequence_of_code_points(input, &mut position, |code_point| code_point != '/');+ let type_ = lexer.consume_while(|code_point| code_point != '/');// 4. If type is the empty string or does not solely contain HTTP token code points, then return failure.if type_.is_empty() || !type_.iter().copied().all(is_http_token_code_point) {@@ -334,15 +249,15 @@ impl MimeType {}// 5. If position is past the end of input, then return failure.- if position >= input.len() {+ if lexer.is_eof() {return None;}// 6. Advance position by 1. (This skips past U+002F (/).)- position += 1;+ lexer.ignore(1);// 7. Let subtype be the result of collecting a sequence of code points that are not U+003B (;) from input, given position.- let subtype = collect_a_sequence_of_code_points(input, &mut position, |code_point| code_point != ';');+ let subtype = lexer.consume_while(|code_point| code_point != ';');// 8. Remove any trailing HTTP whitespace from subtype.let subtype = trim_trailing_http_whitespace(subtype);@@ -360,52 +275,53 @@ impl MimeType {};// 11. While position is not past the end of input:- while position < input.len() {+ while !lexer.is_eof() {// 1. Advance position by 1. (This skips past U+003B (;).)- position += 1;+ lexer.ignore(1);// 2. Collect a sequence of code points that are HTTP whitespace from input given position.- collect_a_sequence_of_code_points(input, &mut position, is_http_whitespace);+ lexer.ignore_while(is_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.- let parameter_name = collect_a_sequence_of_code_points(input, &mut position, |code_point| {- code_point != ';' && code_point != '='- });+ let parameter_name = lexer.consume_while(|code_point| code_point != ';' && code_point != '=');// 4. Set parameterName to parameterName, in ASCII lowercase.let parameter_name: String = parameter_name.iter().map(char::to_ascii_lowercase).collect();// 5. If position is not past the end of input, then:- if position < input.len() {+ if !lexer.is_eof() {// 1. If the code point at position within input is U+003B (;), then continue.- if input[position] == ';' {+ if lexer.peek() == Some(';') {continue;}// 2. Advance position by 1. (This skips past U+003D (=).)- position += 1;+ lexer.ignore(1);}// 6. If position is past the end of input, then break.- if position >= input.len() {+ if lexer.is_eof() {break;}// 7. Let parameterValue be null.- let parameter_value;+ let parameter_value: String;// 8. If the code point at position within input is U+0022 ("), then:- if input[position] == '"' {+ if lexer.peek() == Some('"') {// 1. Set parameterValue to the result of collecting an HTTP quoted string from input, given position and the extract-value flag.- parameter_value = collect_an_http_quoted_string_extracting_the_value(input, &mut position);+ parameter_value = collect_an_http_quoted_string_extracting_the_value(&mut lexer)+ .value+ .into_iter()+ .collect();// 2. Collect a sequence of code points that are not U+003B (;) from input, given position.- collect_a_sequence_of_code_points(input, &mut position, |code_point| code_point != ';');+ lexer.ignore_until(';');}// 9. Otherwise:else {// 1. Set parameterValue to the result of collecting a sequence of code points that are not U+003B (;) from input, given position.- let value = collect_a_sequence_of_code_points(input, &mut position, |code_point| code_point != ';');+ let value = lexer.consume_while(|code_point| code_point != ';');// 2. Remove any trailing HTTP whitespace from parameterValue.let value = trim_trailing_http_whitespace(value);