dymokomi/luce-browser-engine
Source of release 0.6.0
0.6.0 / src / web / external / lib_http / method.lucb
#==============================================================================================
#
# method - HTTP methods: methods, CORS-safelisted and forbidden methods, normalization
#
# DESCRIPTION:
# Port of Libraries/LibHTTP/Method.cpp (lines 13-50) with Method.h.
#
# Copyright (c) 2022, Linus Groh <linusg@serenityos.org>
# Copyright (c) 2022, Kenneth Myhra <kennethmyhra@serenityos.org>
# SPDX-License-Identifier: BSD-2-Clause
#
#==============================================================================================
## Ported from Libraries/LibHTTP/Method.cpp:15 HTTP::is_method.
# https://fetch.spec.whatwg.org/#concept-method
pub func http_is_method(method: ak.StringView) -> bool:
# A method is a byte sequence that matches the method token production.
if ak.string_view_is_empty(&method):
return false
for byte in ak.string_view_bytes(&method):
if not http_is_http_token_code_point(byte):
return false
return true
## Ported from Libraries/LibHTTP/Method.cpp:22 HTTP::is_cors_safelisted_method.
# https://fetch.spec.whatwg.org/#cors-safelisted-method
pub func http_is_cors_safelisted_method(method: ak.StringView) -> bool:
# A CORS-safelisted method is a method that is `GET`, `HEAD`, or `POST`.
return ak.string_view_equals(&method, "GET") or ak.string_view_equals(&method, "HEAD") or ak.string_view_equals(&method, "POST")
## Ported from Libraries/LibHTTP/Method.cpp:29 HTTP::is_forbidden_method.
# https://fetch.spec.whatwg.org/#forbidden-method
pub func http_is_forbidden_method(method: ak.StringView) -> bool:
# A forbidden method is a method that is a byte-case-insensitive match for `CONNECT`, `TRACE`, or `TRACK`.
return ak.string_view_is_one_of_ignoring_ascii_case(&method, ["CONNECT", "TRACE", "TRACK"])
## Ported from Libraries/LibHTTP/Method.cpp:36 HTTP::normalize_method.
# https://fetch.spec.whatwg.org/#concept-method-normalize
pub func http_normalize_method(method: ak.StringView) -> ak.ByteString:
# To normalize a method, if it is a byte-case-insensitive match for `DELETE`, `GET`, `HEAD`, `OPTIONS`, `POST`,
# or `PUT`, byte-uppercase it.
# The C++ keeps the six as a static array of ByteStrings; each is made from its literal when returned.
let normalized_methods: str[6] = ["DELETE", "GET", "HEAD", "OPTIONS", "POST", "PUT"]
for normalized_method in normalized_methods:
if ak.string_view_equals_ignoring_ascii_case(&method, ak.sv(normalized_method)):
return ak.byte_string_from_str(normalized_method)
return ak.byte_string_from_string_view(method)