2022-07-18 23:19:24 +00:00
/*
2023-02-10 22:02:18 +00:00
* Copyright ( c ) 2022 - 2023 , Linus Groh < linusg @ serenityos . org >
2022-07-18 23:19:24 +00:00
*
* SPDX - License - Identifier : BSD - 2 - Clause
*/
# pragma once
# include <AK/HashMap.h>
2023-03-02 22:26:12 +00:00
# include <AK/String.h>
2022-07-18 23:19:24 +00:00
# include <AK/Variant.h>
# include <AK/Vector.h>
2022-10-30 01:52:07 +00:00
# include <LibJS/Forward.h>
2023-02-10 13:29:14 +00:00
# include <LibJS/Heap/GCPtr.h>
2022-09-04 10:48:49 +00:00
# include <LibWeb/Bindings/PlatformObject.h>
2022-07-18 23:19:24 +00:00
# include <LibWeb/Fetch/Infrastructure/HTTP/Headers.h>
2022-09-25 16:03:42 +00:00
# include <LibWeb/WebIDL/ExceptionOr.h>
2022-07-18 23:19:24 +00:00
namespace Web : : Fetch {
2023-03-02 22:26:12 +00:00
using HeadersInit = Variant < Vector < Vector < String > > , OrderedHashMap < String , String > > ;
2022-07-18 23:19:24 +00:00
// https://fetch.spec.whatwg.org/#headers-class
2022-09-04 10:48:49 +00:00
class Headers final : public Bindings : : PlatformObject {
WEB_PLATFORM_OBJECT ( Headers , Bindings : : PlatformObject ) ;
2023-11-19 18:47:52 +00:00
JS_DECLARE_ALLOCATOR ( Headers ) ;
2022-07-18 23:19:24 +00:00
2022-09-04 10:48:49 +00:00
public :
2022-07-18 23:19:24 +00:00
enum class Guard {
Immutable ,
Request ,
RequestNoCORS ,
Response ,
None ,
} ;
2022-09-26 00:08:29 +00:00
static WebIDL : : ExceptionOr < JS : : NonnullGCPtr < Headers > > construct_impl ( JS : : Realm & realm , Optional < HeadersInit > const & init ) ;
2022-07-18 23:19:24 +00:00
2022-09-04 10:48:49 +00:00
virtual ~ Headers ( ) override ;
2022-07-18 23:19:24 +00:00
2022-10-30 01:52:07 +00:00
[ [ nodiscard ] ] JS : : NonnullGCPtr < Infrastructure : : HeaderList > header_list ( ) const { return m_header_list ; }
void set_header_list ( JS : : NonnullGCPtr < Infrastructure : : HeaderList > header_list ) { m_header_list = header_list ; }
2022-09-25 13:02:47 +00:00
[ [ nodiscard ] ] Guard guard ( ) const { return m_guard ; }
void set_guard ( Guard guard ) { m_guard = guard ; }
2022-09-25 18:33:13 +00:00
WebIDL : : ExceptionOr < void > fill ( HeadersInit const & ) ;
2023-03-02 22:26:12 +00:00
WebIDL : : ExceptionOr < void > append ( Infrastructure : : Header ) ;
2022-09-25 18:33:13 +00:00
2022-09-25 13:02:47 +00:00
// JS API functions
2023-03-02 22:26:12 +00:00
WebIDL : : ExceptionOr < void > append ( String const & name , String const & value ) ;
WebIDL : : ExceptionOr < void > delete_ ( String const & name ) ;
WebIDL : : ExceptionOr < Optional < String > > get ( String const & name ) ;
2024-04-27 14:28:36 +00:00
[ [ nodiscard ] ] Vector < String > get_set_cookie ( ) ;
2023-03-02 22:26:12 +00:00
WebIDL : : ExceptionOr < bool > has ( String const & name ) ;
WebIDL : : ExceptionOr < void > set ( String const & name , String const & value ) ;
using ForEachCallback = Function < JS : : ThrowCompletionOr < void > ( String const & , String const & ) > ;
2022-07-18 23:19:24 +00:00
JS : : ThrowCompletionOr < void > for_each ( ForEachCallback ) ;
private :
friend class HeadersIterator ;
2022-10-30 01:52:07 +00:00
Headers ( JS : : Realm & , JS : : NonnullGCPtr < Infrastructure : : HeaderList > ) ;
2023-08-07 06:41:28 +00:00
virtual void initialize ( JS : : Realm & ) override ;
2022-10-30 01:52:07 +00:00
virtual void visit_edges ( JS : : Cell : : Visitor & ) override ;
2022-07-18 23:19:24 +00:00
2022-12-07 19:14:45 +00:00
WebIDL : : ExceptionOr < bool > validate ( Infrastructure : : Header const & ) const ;
2022-12-07 18:16:32 +00:00
void remove_privileged_no_cors_request_headers ( ) ;
2022-07-18 23:19:24 +00:00
// https://fetch.spec.whatwg.org/#concept-headers-header-list
// A Headers object has an associated header list (a header list), which is initially empty.
2022-10-30 01:52:07 +00:00
JS : : NonnullGCPtr < Infrastructure : : HeaderList > m_header_list ;
2022-07-18 23:19:24 +00:00
// https://fetch.spec.whatwg.org/#concept-headers-guard
// A Headers object also has an associated guard, which is a headers guard. A headers guard is "immutable", "request", "request-no-cors", "response" or "none".
Guard m_guard { Guard : : None } ;
} ;
}