Skip to content

Commit db0ca7b

Browse files
Add network.setExtraHeaders command (#960)
This allows appending, or overriding, HTTP headers for all requests, or for all requests originating from a specific user context or a specific top-level traversable and its descendants. --------- Co-authored-by: Julian Descottes <[email protected]>
1 parent 21c604b commit db0ca7b

File tree

1 file changed

+173
-19
lines changed

1 file changed

+173
-19
lines changed

index.bs

Lines changed: 173 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -6332,7 +6332,8 @@ NetworkCommand = (
63326332
network.ProvideResponse //
63336333
network.RemoveDataCollector //
63346334
network.RemoveIntercept //
6335-
network.SetCacheBehavior
6335+
network.SetCacheBehavior //
6336+
network.SetExtraHeaders
63366337
)
63376338

63386339
</pre>
@@ -6366,6 +6367,15 @@ A [=remote end=] has a <dfn>navigable cache behavior map</dfn> which is a weak
63666367
map between [=/top-level traversables=] and strings representing cache
63676368
behavior. It is initially empty.
63686369

6370+
A [=BiDi session=] has a <dfn for=session>extra headers</dfn> which is a
6371+
[=struct=] with an [=struct/item=] named <dfn for="extra headers">default
6372+
headers</dfn>, which is a [=/header list=] (initially set to an empty
6373+
[=/header list=]), an [=struct/item=] named
6374+
<dfn for="extra headers">user context headers</dfn>, which is a weak map
6375+
between [=user contexts=] and [=/header lists=], and a [=struct/item=] named
6376+
<dfn for="extra headers">navigable headers</dfn>, which is a weak map
6377+
between [=navigables=] and [=/header lists=].
6378+
63696379
### Network Data Collection ### {#network-data-collection}
63706380

63716381
A <dfn for="network">data</dfn> is a [=/struct=] consisting of
@@ -6715,21 +6725,8 @@ To <dfn>update the response</dfn> given |session|, |command| and |command parame
67156725

67166726
1. If |command parameters| [=map/contains=] "<code>headers</code>":
67176727

6718-
1. Let |headers| be an empty [=/header list=].
6719-
6720-
1. For |header| in |command parameters|["<code>headers</code>"]:
6721-
6722-
1. Let |deserialized header| be [=deserialize header=] with |header|.
6723-
6724-
1. If |deserialized header|'s name does not match the [=field-name token=]
6725-
production, return [=error=] with [=error code=]
6726-
"<code>invalid argument</code>".
6727-
6728-
1. If |deserialized header|'s value does not match the [=header value=]
6729-
production, return [=error=] with [=error code=]
6730-
"<code>invalid argument</code>".
6731-
6732-
1. Append |deserialized header| to |headers|.
6728+
1. Let |headers| be the result of [=trying=] to [=create a headers list=] with
6729+
|command parameters|["<code>headers</code>"].
67336730

67346731
1. Set |response|'s [=response/header list=] to |headers|.
67356732

@@ -7226,6 +7223,28 @@ To <dfn>deserialize header</dfn> given |protocol header|:
72267223

72277224
</div>
72287225

7226+
<div algorithm>
7227+
To <dfn>create a headers list</dfn> given |protocol headers|:
7228+
1. Let |headers| be an empty [=/header list=].
7229+
7230+
1. For |header| in |protocol headers|:
7231+
7232+
1. Let |deserialized header| be [=deserialize header=] with |header|.
7233+
7234+
1. If |deserialized header|'s name does not match the [=field-name token=]
7235+
production, return [=error=] with [=error code=]
7236+
"<code>invalid argument</code>".
7237+
7238+
1. If |deserialized header|'s value does not match the [=header value=]
7239+
production, return [=error=] with [=error code=]
7240+
"<code>invalid argument</code>".
7241+
7242+
1. Append |deserialized header| to |headers|.
7243+
7244+
1. Return [=success=] with data |headers|
7245+
7246+
</div>
7247+
72297248
#### The network.Initiator Type #### {#type-network-Initiator}
72307249

72317250
{^Remote end definition^} and {^local end definition^}
@@ -8906,6 +8925,138 @@ The [=remote end steps=] given <var ignore>session</var> and |command parameters
89068925

89078926
</div>
89088927

8928+
#### The network.setExtraHeaders Command #### {#command-network-setExtraHeaders}
8929+
8930+
The <dfn export for=commands>network.setExtraHeaders</dfn> command allows
8931+
specifying headers that will extend, or overwrite, existing request headers.
8932+
8933+
<dl>
8934+
<dt>Command Type</dt>
8935+
<dd>
8936+
<pre class="cddl" data-cddl-module="remote-cddl">
8937+
network.SetExtraHeaders = (
8938+
method: "network.setExtraHeaders",
8939+
params: network.SetExtraHeadersParameters
8940+
)
8941+
8942+
network.SetExtraHeadersParameters = {
8943+
headers: [+network.Header]
8944+
? contexts: [+browsingContext.BrowsingContext]
8945+
? userContexts: [+browser.UserContext]
8946+
}
8947+
</pre>
8948+
</dd>
8949+
<dt>Return Type</dt>
8950+
<dd>
8951+
<code>
8952+
EmptyResult
8953+
</code>
8954+
</dd>
8955+
</dl>
8956+
8957+
<div algorithm>
8958+
To <dfn>update headers</dfn> given |request| and |headers|:
8959+
8960+
1. Let |request headers| be |request|'s [=request/header list=].
8961+
8962+
1. For each |header| in |headers|:
8963+
8964+
1. [=header list/Set=] |header| in |request headers|.
8965+
8966+
Note: This always overwrites the existing value, if any. In particular it
8967+
doesn't append cookies to an existing `Set-Cookie` header.
8968+
8969+
</div>
8970+
8971+
<div algorithm>
8972+
To <dfn>update request headers</dfn> given |session|, |request|, and |related
8973+
navigables|:
8974+
8975+
1. Assert: |related navigables|'s [=set/size=] is 0 or 1.
8976+
8977+
Note: That means this will not work for workers associated with multiple
8978+
navigables. In that case it's unclear in which order to override the headers.
8979+
8980+
1. [=Update headers=] with |request| and |session|'s
8981+
[=session/extra headers=]' [=extra headers/default headers=]
8982+
8983+
1. Let |user context headers| be |session|'s [=session/extra headers=]'
8984+
[=extra headers/user context headers=].
8985+
8986+
1. For |navigable| in |related navigables|:
8987+
8988+
1. Let |user context| be |navigable|'s [=associated user context=].
8989+
8990+
1. If |user context headers| [=map/contains=] |user context|
8991+
then [=update headers=] with |request| and
8992+
|user context headers|[|user context|]
8993+
8994+
1. Let |navigable headers| be |session|'s [=session/extra headers=]'
8995+
[=extra headers/navigable headers=].
8996+
8997+
1. For |navigable| in |related navigables|:
8998+
8999+
1. Let |top-level traversable| be |navigable|'s
9000+
[=navigable/top-level traversable=].
9001+
9002+
1. If |navigable headers| contains |top-level traversable|
9003+
[=update headers=] with |request| and |navigable headers|[|top-level traversable|].
9004+
9005+
</div>
9006+
9007+
<div algorithm="remote end steps for network.setExtraHeaders">
9008+
The [=remote end steps=] given |session| and |command parameters| are:
9009+
9010+
1. If |command parameters| [=map/contains=] "<code>userContexts</code>"
9011+
and |command parameters| [=map/contains=] "<code>contexts</code>",
9012+
return [=error=] with [=error code=] [=invalid argument=].
9013+
9014+
1. Let |headers| be the result of [=trying=] to [=create a headers list=] with
9015+
|command parameters|["<code>headers</code>"].
9016+
9017+
1. If |command parameters| [=map/contains=] "<code>userContexts</code>":
9018+
9019+
1. Let |user contexts| be an empty [=/list=].
9020+
9021+
1. For |user context id| in |command parameters|["<code>userContexts</code>"]:
9022+
9023+
1. Let |user context| be [=get user context=] with |user context id|.
9024+
9025+
1. If |user context| is null, return [=error=] with [=error code=] [=invalid argument=].
9026+
9027+
1. [=list/Append=] |user context| to |user contexts|.
9028+
9029+
1. Let |target| be |session|'s [=session/extra headers=]'
9030+
[=extra headers/user context headers=]
9031+
9032+
1. For |user context| in |user contexts|:
9033+
9034+
1. Set |target|[|user context|] to |headers|.
9035+
9036+
1. Return [=success=] with data null.
9037+
9038+
1. If |command parameters| [=map/contains=] "<code>contexts</code>":
9039+
9040+
1. Let |navigables| be the result of [=trying=] to
9041+
[=get valid top-level traversables by ids=]
9042+
with |command parameters|["<code>contexts</code>"].
9043+
9044+
1. Let |target| be |session|'s [=session/extra headers=]'
9045+
[=extra headers/navigable headers=]
9046+
9047+
1. For |navigable| in |navigables|:
9048+
9049+
1. Set |target|[|navigable|] to |headers|.
9050+
9051+
1. Return [=success=] with data null.
9052+
9053+
1. Set |session|'s [=session/extra headers=]'
9054+
[=extra headers/default headers=] to |headers|.
9055+
9056+
1. Return [=success=] with data null.
9057+
9058+
</div>
9059+
89099060
### Events ### {#module-network-event}
89109061

89119062
#### The network.authRequired Event #### {#event-network-authRequired}
@@ -9034,7 +9185,7 @@ request sent</dfn> steps given |request|:
90349185
1. If the [=request originates in user context=] steps with |request| and
90359186
|user context| return true:
90369187

9037-
1. For each |session| in [=active BiDI sessions=]
9188+
1. For each |session| in [=active BiDi sessions=]:
90389189

90399190
Note: |user context| can be in not more then one
90409191
[=user context to accept insecure certificates override map=].
@@ -9090,14 +9241,17 @@ request sent</dfn> steps given |request|:
90909241

90919242
1. Let |response status| be "<code>incomplete</code>".
90929243

9244+
1. For each |session| in [=active BiDi sessions=]:
9245+
9246+
1. [=Update request headers=] with |session|, |request| and
9247+
|related navigables|.
9248+
90939249
1. For each |session| in the [=set of sessions for which an event is enabled=]
90949250
given "<code>network.beforeRequestSent</code>" and |related navigables|:
90959251

90969252
1. Let |params| be the result of [=process a network event=] with |session|,
90979253
"<code>network.beforeRequestSent</code>", and |request|.
90989254

9099-
1. If |params| is null then continue.
9100-
91019255
1. Let |initiator| be the result of [=get the initiator=] with |request|.
91029256

91039257
1. If |initiator| is not [=map/is empty|empty=], set the <code>initiator</code> field

0 commit comments

Comments
 (0)