2022-07-19 00:19:24 +01:00
/*
* Copyright ( c ) 2022 , Linus Groh < linusg @ serenityos . org >
*
* SPDX - License - Identifier : BSD - 2 - Clause
*/
# pragma once
2022-12-04 18:02:33 +00:00
# include <AK/DeprecatedString.h>
2022-07-19 00:19:24 +01:00
# include <AK/HashMap.h>
# include <AK/Variant.h>
# include <AK/Vector.h>
2022-10-30 01:52:07 +00:00
# include <LibJS/Forward.h>
2022-09-04 12:48:49 +02:00
# include <LibWeb/Bindings/PlatformObject.h>
2022-07-19 00:19:24 +01:00
# include <LibWeb/Fetch/Infrastructure/HTTP/Headers.h>
2022-09-25 17:03:42 +01:00
# include <LibWeb/WebIDL/ExceptionOr.h>
2022-07-19 00:19:24 +01:00
namespace Web : : Fetch {
2022-12-04 18:02:33 +00:00
using HeadersInit = Variant < Vector < Vector < DeprecatedString > > , OrderedHashMap < DeprecatedString , DeprecatedString > > ;
2022-07-19 00:19:24 +01:00
// https://fetch.spec.whatwg.org/#headers-class
2022-09-04 12:48:49 +02:00
class Headers final : public Bindings : : PlatformObject {
WEB_PLATFORM_OBJECT ( Headers , Bindings : : PlatformObject ) ;
2022-07-19 00:19:24 +01:00
2022-09-04 12:48:49 +02:00
public :
2022-07-19 00:19:24 +01:00
enum class Guard {
Immutable ,
Request ,
RequestNoCORS ,
Response ,
None ,
} ;
2022-09-25 18:08:29 -06:00
static WebIDL : : ExceptionOr < JS : : NonnullGCPtr < Headers > > construct_impl ( JS : : Realm & realm , Optional < HeadersInit > const & init ) ;
2022-07-19 00:19:24 +01:00
2022-09-04 12:48:49 +02:00
virtual ~ Headers ( ) override ;
2022-07-19 00:19:24 +01: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 14:02:47 +01:00
[ [ nodiscard ] ] Guard guard ( ) const { return m_guard ; }
void set_guard ( Guard guard ) { m_guard = guard ; }
2022-09-25 19:33:13 +01:00
WebIDL : : ExceptionOr < void > fill ( HeadersInit const & ) ;
2022-09-25 14:02:47 +01:00
// JS API functions
2022-09-25 17:03:42 +01:00
WebIDL : : ExceptionOr < void > append ( Infrastructure : : Header ) ;
2022-12-04 18:02:33 +00:00
WebIDL : : ExceptionOr < void > append ( DeprecatedString const & name , DeprecatedString const & value ) ;
WebIDL : : ExceptionOr < void > delete_ ( DeprecatedString const & name ) ;
WebIDL : : ExceptionOr < DeprecatedString > get ( DeprecatedString const & name ) ;
WebIDL : : ExceptionOr < bool > has ( DeprecatedString const & name ) ;
WebIDL : : ExceptionOr < void > set ( DeprecatedString const & name , DeprecatedString const & value ) ;
2022-07-19 00:19:24 +01:00
2022-12-04 18:02:33 +00:00
using ForEachCallback = Function < JS : : ThrowCompletionOr < void > ( DeprecatedString const & , DeprecatedString const & ) > ;
2022-07-19 00:19:24 +01: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 > ) ;
virtual void visit_edges ( JS : : Cell : : Visitor & ) override ;
2022-07-19 00:19:24 +01:00
void remove_privileged_no_cors_headers ( ) ;
// 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-19 00:19:24 +01: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 } ;
} ;
}