2022-07-19 00:19:24 +01:00
/*
2023-02-10 22:02:18 +00:00
* Copyright ( c ) 2022 - 2023 , Linus Groh < linusg @ serenityos . org >
2022-07-19 00:19:24 +01:00
*
* SPDX - License - Identifier : BSD - 2 - Clause
*/
2023-01-07 12:14:54 -05:00
# include <LibJS/Runtime/Completion.h>
2022-09-04 16:56:15 +02:00
# include <LibJS/Runtime/VM.h>
2024-04-27 12:09:58 +12:00
# include <LibWeb/Bindings/HeadersPrototype.h>
2022-09-25 18:08:29 -06:00
# include <LibWeb/Bindings/Intrinsics.h>
2022-07-19 00:19:24 +01:00
# include <LibWeb/Fetch/Headers.h>
namespace Web : : Fetch {
2024-11-15 04:01:23 +13:00
GC_DEFINE_ALLOCATOR ( Headers ) ;
2023-11-19 19:47:52 +01:00
2022-07-19 00:19:24 +01:00
// https://fetch.spec.whatwg.org/#dom-headers
2024-11-15 04:01:23 +13:00
WebIDL : : ExceptionOr < GC : : Ref < Headers > > Headers : : construct_impl ( JS : : Realm & realm , Optional < HeadersInit > const & init )
2022-07-19 00:19:24 +01:00
{
2022-10-30 01:52:07 +00:00
auto & vm = realm . vm ( ) ;
2022-07-19 00:19:24 +01:00
// The new Headers(init) constructor steps are:
2024-11-14 05:50:17 +13:00
auto headers = realm . create < Headers > ( realm , Infrastructure : : HeaderList : : create ( vm ) ) ;
2022-07-19 00:19:24 +01:00
// 1. Set this’ s guard to "none".
headers - > m_guard = Guard : : None ;
// 2. If init is given, then fill this with init.
if ( init . has_value ( ) )
TRY ( headers - > fill ( * init ) ) ;
2022-12-14 17:40:33 +00:00
return headers ;
2022-09-04 12:48:49 +02:00
}
2024-11-15 04:01:23 +13:00
Headers : : Headers ( JS : : Realm & realm , GC : : Ref < Infrastructure : : HeaderList > header_list )
2022-09-25 18:08:29 -06:00
: PlatformObject ( realm )
2022-10-30 01:52:07 +00:00
, m_header_list ( header_list )
2022-09-04 12:48:49 +02:00
{
2022-07-19 00:19:24 +01:00
}
2022-09-04 12:48:49 +02:00
Headers : : ~ Headers ( ) = default ;
2023-08-07 08:41:28 +02:00
void Headers : : initialize ( JS : : Realm & realm )
2023-01-10 06:28:20 -05:00
{
2023-08-07 08:41:28 +02:00
Base : : initialize ( realm ) ;
2024-03-16 13:13:08 +01:00
WEB_SET_PROTOTYPE_FOR_INTERFACE ( Headers ) ;
2023-01-10 06:28:20 -05:00
}
2022-10-30 01:52:07 +00:00
void Headers : : visit_edges ( JS : : Cell : : Visitor & visitor )
{
Base : : visit_edges ( visitor ) ;
visitor . visit ( m_header_list ) ;
}
2022-07-19 00:19:24 +01:00
// https://fetch.spec.whatwg.org/#dom-headers-append
2023-03-02 22:26:12 +00:00
WebIDL : : ExceptionOr < void > Headers : : append ( String const & name_string , String const & value_string )
2022-07-19 00:19:24 +01:00
{
// The append(name, value) method steps are to append (name, value) to this.
auto header = Infrastructure : : Header {
2024-04-27 10:28:36 -04:00
. name = MUST ( ByteBuffer : : copy ( name_string . bytes ( ) ) ) ,
. value = MUST ( ByteBuffer : : copy ( value_string . bytes ( ) ) ) ,
2022-07-19 00:19:24 +01:00
} ;
TRY ( append ( move ( header ) ) ) ;
return { } ;
}
// https://fetch.spec.whatwg.org/#dom-headers-delete
2023-03-02 22:26:12 +00:00
WebIDL : : ExceptionOr < void > Headers : : delete_ ( String const & name_string )
2022-07-19 00:19:24 +01:00
{
// The delete(name) method steps are:
auto name = name_string . bytes ( ) ;
2022-12-07 19:14:45 +00:00
// 1. If validating (name, ``) for headers returns false, then return.
// NOTE: Passing a dummy header value ought not to have any negative repercussions.
2024-04-26 13:24:20 -04:00
auto header = Infrastructure : : Header : : from_string_pair ( name , " " sv ) ;
2022-12-07 19:14:45 +00:00
if ( ! TRY ( validate ( header ) ) )
2022-07-19 00:19:24 +01:00
return { } ;
2022-12-07 19:14:45 +00:00
// 2. If this’ s guard is "request-no-cors", name is not a no-CORS-safelisted request-header name, and name is not a privileged no-CORS request-header name, then return.
2022-07-19 00:19:24 +01:00
if ( m_guard = = Guard : : RequestNoCORS & & ! Infrastructure : : is_no_cors_safelisted_request_header_name ( name ) & & ! Infrastructure : : is_privileged_no_cors_request_header_name ( name ) )
return { } ;
2022-12-07 19:14:45 +00:00
// 3. If this’ s header list does not contain name, then return.
2022-09-25 20:52:51 +01:00
if ( ! m_header_list - > contains ( name ) )
2022-07-19 00:19:24 +01:00
return { } ;
2022-12-07 19:14:45 +00:00
// 4. Delete name from this’ s header list.
2022-09-25 20:52:51 +01:00
m_header_list - > delete_ ( name ) ;
2022-07-19 00:19:24 +01:00
2022-12-07 19:14:45 +00:00
// 5. If this’ s guard is "request-no-cors", then remove privileged no-CORS request-headers from this.
2022-07-19 00:19:24 +01:00
if ( m_guard = = Guard : : RequestNoCORS )
2022-12-07 18:16:32 +00:00
remove_privileged_no_cors_request_headers ( ) ;
2022-07-19 00:19:24 +01:00
return { } ;
}
// https://fetch.spec.whatwg.org/#dom-headers-get
2023-03-02 22:26:12 +00:00
WebIDL : : ExceptionOr < Optional < String > > Headers : : get ( String const & name_string )
2022-07-19 00:19:24 +01:00
{
// The get(name) method steps are:
auto name = name_string . bytes ( ) ;
// 1. If name is not a header name, then throw a TypeError.
if ( ! Infrastructure : : is_header_name ( name ) )
2022-10-27 23:18:16 +01:00
return WebIDL : : SimpleException { WebIDL : : SimpleExceptionType : : TypeError , " Invalid header name " sv } ;
2022-07-19 00:19:24 +01:00
// 2. Return the result of getting name from this’ s header list.
2024-04-26 13:24:20 -04:00
auto byte_buffer = m_header_list - > get ( name ) ;
2024-04-27 10:28:36 -04:00
return byte_buffer . has_value ( ) ? MUST ( String : : from_utf8 ( * byte_buffer ) ) : Optional < String > { } ;
2022-07-19 00:19:24 +01:00
}
2023-02-10 22:02:18 +00:00
// https://fetch.spec.whatwg.org/#dom-headers-getsetcookie
2024-04-27 10:28:36 -04:00
Vector < String > Headers : : get_set_cookie ( )
2023-02-10 22:02:18 +00:00
{
// The getSetCookie() method steps are:
2023-03-02 22:26:12 +00:00
auto values = Vector < String > { } ;
2023-02-10 22:02:18 +00:00
// 1. If this’ s header list does not contain `Set-Cookie`, then return « ».
if ( ! m_header_list - > contains ( " Set-Cookie " sv . bytes ( ) ) )
return values ;
// 2. Return the values of all headers in this’ s header list whose name is a byte-case-insensitive match for
// `Set-Cookie`, in order.
for ( auto const & header : * m_header_list ) {
2023-03-10 08:48:54 +01:00
if ( StringView { header . name } . equals_ignoring_ascii_case ( " Set-Cookie " sv ) )
2024-04-27 10:28:36 -04:00
values . append ( MUST ( String : : from_utf8 ( header . value ) ) ) ;
2023-02-10 22:02:18 +00:00
}
return values ;
}
2022-07-19 00:19:24 +01:00
// https://fetch.spec.whatwg.org/#dom-headers-has
2023-03-02 22:26:12 +00:00
WebIDL : : ExceptionOr < bool > Headers : : has ( String const & name_string )
2022-07-19 00:19:24 +01:00
{
// The has(name) method steps are:
auto name = name_string . bytes ( ) ;
// 1. If name is not a header name, then throw a TypeError.
if ( ! Infrastructure : : is_header_name ( name ) )
2022-10-27 23:18:16 +01:00
return WebIDL : : SimpleException { WebIDL : : SimpleExceptionType : : TypeError , " Invalid header name " sv } ;
2022-07-19 00:19:24 +01:00
// 2. Return true if this’ s header list contains name; otherwise false.
2022-09-25 20:52:51 +01:00
return m_header_list - > contains ( name ) ;
2022-07-19 00:19:24 +01:00
}
// https://fetch.spec.whatwg.org/#dom-headers-set
2023-03-02 22:26:12 +00:00
WebIDL : : ExceptionOr < void > Headers : : set ( String const & name_string , String const & value_string )
2022-07-19 00:19:24 +01:00
{
2023-01-07 12:14:54 -05:00
// The set(name, value) method steps are:
2022-07-19 00:19:24 +01:00
auto name = name_string . bytes ( ) ;
auto value = value_string . bytes ( ) ;
// 1. Normalize value.
2024-04-26 13:24:20 -04:00
auto normalized_value = Infrastructure : : normalize_header_value ( value ) ;
2022-07-19 00:19:24 +01:00
auto header = Infrastructure : : Header {
2024-04-27 10:28:36 -04:00
. name = MUST ( ByteBuffer : : copy ( name ) ) ,
2022-07-19 00:19:24 +01:00
. value = move ( normalized_value ) ,
} ;
2022-12-07 19:14:45 +00:00
// 2. If validating (name, value) for headers returns false, then return.
if ( ! TRY ( validate ( header ) ) )
2022-07-19 00:19:24 +01:00
return { } ;
2022-12-07 19:14:45 +00:00
// 3. If this’ s guard is "request-no-cors" and (name, value) is not a no-CORS-safelisted request-header, then return.
2022-07-19 00:19:24 +01:00
if ( m_guard = = Guard : : RequestNoCORS & & ! Infrastructure : : is_no_cors_safelisted_request_header ( header ) )
return { } ;
2022-12-07 19:14:45 +00:00
// 4. Set (name, value) in this’ s header list.
2024-04-26 13:24:20 -04:00
m_header_list - > set ( move ( header ) ) ;
2022-07-19 00:19:24 +01:00
2022-12-07 19:14:45 +00:00
// 5. If this’ s guard is "request-no-cors", then remove privileged no-CORS request-headers from this.
2022-07-19 00:19:24 +01:00
if ( m_guard = = Guard : : RequestNoCORS )
2022-12-07 18:16:32 +00:00
remove_privileged_no_cors_request_headers ( ) ;
2022-07-19 00:19:24 +01:00
return { } ;
}
// https://webidl.spec.whatwg.org/#es-iterable, Step 4
JS : : ThrowCompletionOr < void > Headers : : for_each ( ForEachCallback callback )
{
// The value pairs to iterate over are the return value of running sort and combine with this’ s header list.
2024-04-26 13:24:20 -04:00
auto value_pairs_to_iterate_over = [ & ] ( ) {
return m_header_list - > sort_and_combine ( ) ;
2022-07-19 00:19:24 +01:00
} ;
// 1-5. Are done in the generated wrapper code.
// 6. Let pairs be idlObject’ s list of value pairs to iterate over.
2024-04-26 13:24:20 -04:00
auto pairs = value_pairs_to_iterate_over ( ) ;
2022-07-19 00:19:24 +01:00
// 7. Let i be 0.
size_t i = 0 ;
// 8. While i < pairs’ s size:
while ( i < pairs . size ( ) ) {
// 1. Let pair be pairs[i].
auto const & pair = pairs [ i ] ;
// 2. Invoke idlCallback with « pair’ s value, pair’ s key, idlObject » and with thisArg as the callback this value.
2024-04-27 10:28:36 -04:00
TRY ( callback ( MUST ( String : : from_utf8 ( pair . name ) ) , MUST ( String : : from_utf8 ( pair . value ) ) ) ) ;
2022-07-19 00:19:24 +01:00
// 3. Set pairs to idlObject’ s current list of value pairs to iterate over. (It might have changed.)
2024-04-26 13:24:20 -04:00
pairs = value_pairs_to_iterate_over ( ) ;
2022-07-19 00:19:24 +01:00
// 4. Set i to i + 1.
+ + i ;
}
return { } ;
}
2022-12-07 19:14:45 +00:00
// https://fetch.spec.whatwg.org/#headers-validate
WebIDL : : ExceptionOr < bool > Headers : : validate ( Infrastructure : : Header const & header ) const
2022-07-19 00:19:24 +01:00
{
2023-01-07 12:14:54 -05:00
// To validate a header (name, value) for a Headers object headers:
2022-12-07 19:14:45 +00:00
auto const & [ name , value ] = header ;
2022-07-19 00:19:24 +01:00
2022-12-07 19:14:45 +00:00
// 1. If name is not a header name or value is not a header value, then throw a TypeError.
2022-07-19 00:19:24 +01:00
if ( ! Infrastructure : : is_header_name ( name ) )
2022-10-27 23:18:16 +01:00
return WebIDL : : SimpleException { WebIDL : : SimpleExceptionType : : TypeError , " Invalid header name " sv } ;
2022-07-19 00:19:24 +01:00
if ( ! Infrastructure : : is_header_value ( value ) )
2022-10-27 23:18:16 +01:00
return WebIDL : : SimpleException { WebIDL : : SimpleExceptionType : : TypeError , " Invalid header value " sv } ;
2022-07-19 00:19:24 +01:00
2022-12-07 19:14:45 +00:00
// 2. If headers’ s guard is "immutable", then throw a TypeError.
2022-07-19 00:19:24 +01:00
if ( m_guard = = Guard : : Immutable )
2022-10-27 23:18:16 +01:00
return WebIDL : : SimpleException { WebIDL : : SimpleExceptionType : : TypeError , " Headers object is immutable " sv } ;
2022-07-19 00:19:24 +01:00
2022-12-07 19:14:45 +00:00
// 3. If headers’ s guard is "request" and (name, value) is a forbidden request-header, then return false.
2024-04-26 13:24:20 -04:00
if ( m_guard = = Guard : : Request & & Infrastructure : : is_forbidden_request_header ( header ) )
2022-12-07 19:14:45 +00:00
return false ;
// 4. If headers’ s guard is "response" and name is a forbidden response-header name, then return false.
if ( m_guard = = Guard : : Response & & Infrastructure : : is_forbidden_response_header_name ( name ) )
return false ;
// 5. Return true.
return true ;
}
// https://fetch.spec.whatwg.org/#concept-headers-append
WebIDL : : ExceptionOr < void > Headers : : append ( Infrastructure : : Header header )
{
2023-01-07 12:14:54 -05:00
// To append a header (name, value) to a Headers object headers, run these steps:
2022-12-07 19:14:45 +00:00
auto & [ name , value ] = header ;
// 1. Normalize value.
2024-04-26 13:24:20 -04:00
value = Infrastructure : : normalize_header_value ( value ) ;
2022-12-07 19:14:45 +00:00
// 2. If validating (name, value) for headers returns false, then return.
if ( ! TRY ( validate ( header ) ) )
2022-07-19 00:19:24 +01:00
return { } ;
2022-12-07 19:14:45 +00:00
// 3. If headers’ s guard is "request-no-cors":
2022-07-19 00:19:24 +01:00
if ( m_guard = = Guard : : RequestNoCORS ) {
// 1. Let temporaryValue be the result of getting name from headers’ s header list.
2024-04-26 13:24:20 -04:00
auto temporary_value = m_header_list - > get ( name ) ;
2022-07-19 00:19:24 +01:00
// 2. If temporaryValue is null, then set temporaryValue to value.
if ( ! temporary_value . has_value ( ) ) {
2024-04-27 10:28:36 -04:00
temporary_value = MUST ( ByteBuffer : : copy ( value ) ) ;
2022-07-19 00:19:24 +01:00
}
// 3. Otherwise, set temporaryValue to temporaryValue, followed by 0x2C 0x20, followed by value.
else {
2024-04-27 10:28:36 -04:00
temporary_value - > append ( 0x2c ) ;
temporary_value - > append ( 0x20 ) ;
temporary_value - > append ( value ) ;
2022-07-19 00:19:24 +01:00
}
auto temporary_header = Infrastructure : : Header {
2024-04-27 10:28:36 -04:00
. name = MUST ( ByteBuffer : : copy ( name ) ) ,
2022-07-19 00:19:24 +01:00
. value = temporary_value . release_value ( ) ,
} ;
2022-12-07 18:14:52 +00:00
// 4. If (name, temporaryValue) is not a no-CORS-safelisted request-header, then return.
2022-07-19 00:19:24 +01:00
if ( ! Infrastructure : : is_no_cors_safelisted_request_header ( temporary_header ) )
return { } ;
}
2022-12-07 19:14:45 +00:00
// 4. Append (name, value) to headers’ s header list.
2024-04-26 13:24:20 -04:00
m_header_list - > append ( move ( header ) ) ;
2022-07-19 00:19:24 +01:00
2022-12-07 19:14:45 +00:00
// 5. If headers’ s guard is "request-no-cors", then remove privileged no-CORS request-headers from headers.
2022-07-19 00:19:24 +01:00
if ( m_guard = = Guard : : RequestNoCORS )
2022-12-07 18:16:32 +00:00
remove_privileged_no_cors_request_headers ( ) ;
2022-07-19 00:19:24 +01:00
return { } ;
}
// https://fetch.spec.whatwg.org/#concept-headers-fill
2022-09-25 17:03:42 +01:00
WebIDL : : ExceptionOr < void > Headers : : fill ( HeadersInit const & object )
2022-07-19 00:19:24 +01:00
{
// To fill a Headers object headers with a given object object, run these steps:
return object . visit (
2022-12-07 18:29:17 +00:00
// 1. If object is a sequence, then for each header of object:
2023-03-02 22:26:12 +00:00
[ & ] ( Vector < Vector < String > > const & object ) - > WebIDL : : ExceptionOr < void > {
2022-07-19 00:19:24 +01:00
for ( auto const & entry : object ) {
2022-12-07 18:29:17 +00:00
// 1. If header's size is not 2, then throw a TypeError.
2022-07-19 00:19:24 +01:00
if ( entry . size ( ) ! = 2 )
2022-10-27 23:18:16 +01:00
return WebIDL : : SimpleException { WebIDL : : SimpleExceptionType : : TypeError , " Array must contain header key/value pair " sv } ;
2022-07-19 00:19:24 +01:00
2022-12-07 18:29:17 +00:00
// 2. Append (header[0], header[1]) to headers.
2024-04-26 13:24:20 -04:00
auto header = Infrastructure : : Header : : from_string_pair ( entry [ 0 ] , entry [ 1 ] ) ;
2022-07-19 00:19:24 +01:00
TRY ( append ( move ( header ) ) ) ;
}
return { } ;
} ,
2022-12-07 18:29:17 +00:00
// 2. Otherwise, object is a record, then for each key → value of object, append (key, value) to headers.
2023-03-02 22:26:12 +00:00
[ & ] ( OrderedHashMap < String , String > const & object ) - > WebIDL : : ExceptionOr < void > {
2022-07-19 00:19:24 +01:00
for ( auto const & entry : object ) {
2024-04-26 13:24:20 -04:00
auto header = Infrastructure : : Header : : from_string_pair ( entry . key , entry . value ) ;
2022-07-19 00:19:24 +01:00
TRY ( append ( move ( header ) ) ) ;
}
return { } ;
} ) ;
}
// https://fetch.spec.whatwg.org/#concept-headers-remove-privileged-no-cors-request-headers
2022-12-07 18:16:32 +00:00
void Headers : : remove_privileged_no_cors_request_headers ( )
2022-07-19 00:19:24 +01:00
{
2022-12-07 18:16:32 +00:00
// To remove privileged no-CORS request-headers from a Headers object (headers), run these steps:
2022-07-19 00:19:24 +01:00
static constexpr Array privileged_no_cors_request_header_names = {
" Range " sv ,
} ;
// 1. For each headerName of privileged no-CORS request-header names:
for ( auto const & header_name : privileged_no_cors_request_header_names ) {
// 1. Delete headerName from headers’ s header list.
2022-09-25 20:52:51 +01:00
m_header_list - > delete_ ( header_name . bytes ( ) ) ;
2022-07-19 00:19:24 +01:00
}
}
}