Skip to content

Commit f435978

Browse files
committed
Editorial: share validation logic in the Headers class
Fixes #1543.
1 parent 9004f4e commit f435978

File tree

1 file changed

+34
-38
lines changed

1 file changed

+34
-38
lines changed

fetch.bs

Lines changed: 34 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -6404,23 +6404,39 @@ new Headers(meta2);
64046404

64056405
<hr>
64066406

6407-
<p>To <dfn export for=Headers id=concept-headers-append>append</dfn> a <a for=/>header</a>
6408-
(<var>name</var>, <var>value</var>) to a {{Headers}} object <var>headers</var>, run these steps:
6407+
<p>To <dfn for=Headers>validate</dfn> a <a for=/>header</a> (<var>name</var>, <var>value</var>) for
6408+
a {{Headers}} object <var>headers</var>:
64096409

64106410
<ol>
6411-
<li><p><a for="header value">Normalize</a> <var>value</var>.
6412-
64136411
<li><p>If <var>name</var> is not a <a for=/>header name</a> or <var>value</var> is not a
64146412
<a for=/>header value</a>, then <a>throw</a> a {{TypeError}}.
64156413

64166414
<li><p>If <var>headers</var>'s <a for=Headers>guard</a> is "<code>immutable</code>", then
64176415
<a>throw</a> a {{TypeError}}.
64186416

6419-
<li><p>Otherwise, if <var>headers</var>'s <a for=Headers>guard</a> is "<code>request</code>" and
6420-
(<var>name</var>, <var>value</var>) is a <a>forbidden request-header</a>, return.
6417+
<li><p>If <var>headers</var>'s <a for=Headers>guard</a> is "<code>request</code>" and
6418+
(<var>name</var>, <var>value</var>) is a <a>forbidden request-header</a>, then return false.
6419+
6420+
<li><p>If <var>headers</var>'s <a for=Headers>guard</a> is "<code>response</code>" and
6421+
<var>name</var> is a <a>forbidden response-header name</a>, then return false.
6422+
6423+
<li><p>Return true.
6424+
</ol>
6425+
6426+
<p class=note>Steps for "<code>request-no-cors</code>" are not shared as you cannot have a fake
6427+
value (for {{Headers/delete()}}) that always succeeds in <a>CORS-safelisted request-header</a>.
6428+
6429+
<p>To <dfn export for=Headers id=concept-headers-append>append</dfn> a <a for=/>header</a>
6430+
(<var>name</var>, <var>value</var>) to a {{Headers}} object <var>headers</var>, run these steps:
6431+
6432+
<ol>
6433+
<li><p><a for="header value">Normalize</a> <var>value</var>.
6434+
6435+
<li><p>If <a for=Headers>validating</a> (<var>name</var>, <var>value</var>) for <var>headers</var>
6436+
returns false, then return.
64216437

64226438
<li>
6423-
<p>Otherwise, if <var>headers</var>'s <a for=Headers>guard</a> is "<code>request-no-cors</code>":
6439+
<p>If <var>headers</var>'s <a for=Headers>guard</a> is "<code>request-no-cors</code>":
64246440

64256441
<ol>
64266442
<li><p>Let <var>temporaryValue</var> be the result of <a for="header list">getting</a>
@@ -6436,9 +6452,6 @@ new Headers(meta2);
64366452
<a>no-CORS-safelisted request-header</a>, then return.
64376453
</ol>
64386454

6439-
<li><p>Otherwise, if <var>headers</var>'s <a for=Headers>guard</a> is "<code>response</code>" and
6440-
<var>name</var> is a <a>forbidden response-header name</a>, return.
6441-
64426455
<li><p><a for="header list">Append</a> (<var>name</var>, <var>value</var>) to <var>headers</var>'s
64436456
<a for=Headers>header list</a>.
64446457

@@ -6498,27 +6511,19 @@ method steps are to <a for=Headers>append</a> (<var>name</var>, <var>value</var>
64986511
<p>The <dfn export for=Headers method><code>delete(<var>name</var>)</code></dfn> method steps are:
64996512

65006513
<ol>
6501-
<li><p>If <var>name</var> is not a <a for=/>header name</a>, then <a>throw</a> a {{TypeError}}.
6502-
6503-
<li><p>If <a>this</a>'s <a for=Headers>guard</a> is "<code>immutable</code>", then <a>throw</a> a
6504-
{{TypeError}}.
6505-
65066514
<li>
6507-
<p>Otherwise, if <a>this</a>'s <a for=Headers>guard</a> is "<code>request</code>" and
6508-
(<var>name</var>, ``) is a <a>forbidden request-header</a>, return.
6515+
<p>If <a for=Headers>validating</a> (<var>name</var>, ``) for <var>headers</var> returns false,
6516+
then return.
65096517

6510-
<p class=note>Passing a dummy <a>header value</a> to <a>forbidden request-header</a> ought not to
6511-
have any negative repercussions.
6518+
<p class=note>Passing a dummy <a>header value</a> ought not to have any negative repercussions.
65126519

6513-
<li><p>Otherwise, if <a>this</a>'s <a for=Headers>guard</a> is "<code>request-no-cors</code>",
6514-
<var>name</var> is not a <a>no-CORS-safelisted request-header name</a>, and <var>name</var> is not
6515-
a <a>privileged no-CORS request-header name</a>, return.
6516-
6517-
<li><p>Otherwise, if <a>this</a>'s <a for=Headers>guard</a> is "<code>response</code>" and
6518-
<var>name</var> is a <a>forbidden response-header name</a>, return.
6520+
<li><p>If <a>this</a>'s <a for=Headers>guard</a> is "<code>request-no-cors</code>", <var>name</var>
6521+
is not a <a>no-CORS-safelisted request-header name</a>, and <var>name</var> is not a
6522+
<a>privileged no-CORS request-header name</a>, then return.
65196523

65206524
<li><p>If <a>this</a>'s <a for=Headers>header list</a> <a for="header list">does not contain</a>
65216525
<var>name</var>, then return.
6526+
<!-- This is intentional to avoid hitting the last step. -->
65226527

65236528
<li><p><a for="header list">Delete</a> <var>name</var> from <a>this</a>'s
65246529
<a for=Headers>header list</a>.
@@ -6551,20 +6556,11 @@ method steps are:
65516556
<ol>
65526557
<li><p><a for="header value">Normalize</a> <var>value</var>.
65536558

6554-
<li><p>If <var>name</var> is not a <a for=/>header name</a> or <var>value</var> is not a
6555-
<a for=/>header value</a>, then <a>throw</a> a {{TypeError}}.
6556-
6557-
<li><p>If <a>this</a>'s <a for=Headers>guard</a> is "<code>immutable</code>", then <a>throw</a> a
6558-
{{TypeError}}.
6559-
6560-
<li><p>Otherwise, if <a>this</a>'s <a for=Headers>guard</a> is "<code>request</code>" and
6561-
(<var>name</var>, <var>value</var>) is a <a>forbidden request-header</a>, return.
6562-
6563-
<li><p>Otherwise, if <a>this</a>'s <a for=Headers>guard</a> is "<code>request-no-cors</code>" and
6564-
(<var>name</var>, <var>value</var>) is not a <a>no-CORS-safelisted request-header</a>, return.
6559+
<li><p>If <a for=Headers>validating</a> (<var>name</var>, <var>value</var>) for <var>headers</var>
6560+
returns false, then return.
65656561

6566-
<li><p>Otherwise, if <a>this</a>'s <a for=Headers>guard</a> is "<code>response</code>" and
6567-
<var>name</var> is a <a>forbidden response-header name</a>, return.
6562+
<li><p>If <a>this</a>'s <a for=Headers>guard</a> is "<code>request-no-cors</code>" and
6563+
(<var>name</var>, <var>value</var>) is not a <a>no-CORS-safelisted request-header</a>, then return.
65686564

65696565
<li><p><a for="header list">Set</a> (<var>name</var>, <var>value</var>) in <a>this</a>'s
65706566
<a for=Headers>header list</a>.

0 commit comments

Comments
 (0)