dymokomi/luce-browser-engine
Source of release 0.6.1
0.6.1 / src / web / external / lib_http / http.lucb
#==============================================================================================
#
# http - LibHTTP's HTTP code point classes and quoted-string collection
#
# DESCRIPTION:
# Port of Libraries/LibHTTP/HTTP.h (lines 32-76: is_http_newline, is_http_tab_or_space,
# is_http_token_code_point, HttpQuotedStringExtractValue) and HTTP.cpp (lines 14-75:
# collect_an_http_quoted_string). The constants of HTTP.h (HTTP_TAB_OR_SPACE,
# HTTP_WHITESPACE and the byte arrays) are in types_http. Region r13 ported the quoted
# string for MIME type parsing; region p2b the rest.
#
# Copyright (c) 2022-2023, Linus Groh <linusg@serenityos.org>
# Copyright (c) 2022, Luke Wilde <lukew@serenityos.org>
# SPDX-License-Identifier: BSD-2-Clause
#
#==============================================================================================
# mark: HTTP.h ==================================================================================
## Ported from Libraries/LibHTTP/HTTP.h:32 HTTP::is_http_newline.
pub func http_is_http_newline(code_point: u32) -> bool:
return code_point == 0x0a or code_point == 0x0d
## Ported from Libraries/LibHTTP/HTTP.h:37 HTTP::is_http_tab_or_space.
pub func http_is_http_tab_or_space(code_point: u32) -> bool:
return code_point == 0x09 or code_point == 0x20
## Ported from Libraries/LibHTTP/HTTP.h:42 HTTP::is_http_token_code_point.
pub func http_is_http_token_code_point(code_point: u32) -> bool:
# (Every HTTP token code point is ASCII; the comparisons below are on its byte.)
if code_point > 0x7f:
return false
let byte = (u8)code_point
if (byte >= '0' and byte <= '9') or (byte >= 'A' and byte <= 'Z') or (byte >= 'a' and byte <= 'z'):
return true
match byte:
'!', '#', '$', '%', '&', '\'', '*', '+', '-', '.', '^', '_', '`', '|', '~':
return true
_:
return false
## `is_http_tab_or_space` as a GenericLexer predicate (`lexer.consume_while(is_http_tab_or_space)`).
pub struct HttpIsHttpTabOrSpace: ak.CharPredicate:
pub var unused: u8
pub func matches(ch: u8) -> bool:
return http_is_http_tab_or_space(ch)
## HTTP::HttpQuotedStringExtractValue (Libraries/LibHTTP/HTTP.h:72).
pub enum HttpQuotedStringExtractValue as i32:
no = 0
yes = 1
# mark: HTTP.cpp ================================================================================
## The lambda `[](char ch) { return ch == '"' || ch == '\\'; }` of collect_an_http_quoted_string.
struct HttpIsQuoteOrBackslash: ak.CharPredicate:
var unused: u8
func matches(ch: u8) -> bool:
return ch == '"' or ch == '\\'
## Ported from Libraries/LibHTTP/HTTP.cpp:14 HTTP::collect_an_http_quoted_string.
# https://fetch.spec.whatwg.org/#collect-an-http-quoted-string
pub func http_collect_an_http_quoted_string(lexer: ak.GenericLexer[u8]*, extract_value: HttpQuotedStringExtractValue = HttpQuotedStringExtractValue.no) -> ak.String:
# 1. Let positionStart be position.
let position_start = ak.generic_lexer_tell(lexer)
# 2. Let value be the empty string.
var value = ak.string_builder_new()
# 3. Assert: the code point at position within input is U+0022 (").
assert(ak.generic_lexer_peek(lexer) == '"')
# 4. Advance position by 1.
ak.generic_lexer_ignore(lexer, 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.
let is_quote_or_backslash = HttpIsQuoteOrBackslash()
let value_part = ak.generic_lexer_consume_until_tt_predicate(lexer, &is_quote_or_backslash)
ak.string_builder_append_string_view(&value, value_part)
# 2. If position is past the end of input, then break.
if ak.generic_lexer_is_eof(lexer):
break
# 3. Let quoteOrBackslash be the code point at position within input.
# 4. Advance position by 1.
let quote_or_backslash = ak.generic_lexer_consume(lexer)
# 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 ak.generic_lexer_is_eof(lexer):
ak.string_builder_append_u8(&value, '\\')
break
# 2. Append the code point at position within input to value.
# 3. Advance position by 1.
ak.string_builder_append_u8(&value, ak.generic_lexer_consume(lexer))
# 6. Otherwise:
else:
# 1. Assert: quoteOrBackslash is U+0022 (").
assert(quote_or_backslash == '"')
# 2. Break.
break
# 6. If the extract-value flag is set, then return value.
if extract_value == HttpQuotedStringExtractValue.yes:
return ak.string_builder_to_string(&value) else trap("MUST: StringBuilder::to_string")
# 7. Return the code points from positionStart to position, inclusive, within input.
let input = ak.generic_lexer_input(lexer)
return ak.string_from_utf8(ak.string_view_substring_view_usize(&input, position_start, ak.generic_lexer_tell(lexer) - position_start)) else trap("MUST: String::from_utf8")