-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathHttpHeaders.js
284 lines (256 loc) · 8.47 KB
/
HttpHeaders.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
// Copyright 2022-2025 the Lactoserv Authors (Dan Bornstein et alia).
// SPDX-License-Identifier: Apache-2.0
import { Cookies } from '#x/Cookies';
import { HttpUtil } from '#x/HttpUtil';
/**
* Subclass of the standard global class `Headers`, with extra functionality
* found to be useful in practice.
*
* TODO: Make `Object.freeze()` actually work.
*/
export class HttpHeaders extends Headers {
/**
* Constructs an instance.
*
* @param {?Headers|Map|object} [other] Initial source of entries. This is
* allowed to be anything that implements a map-like iterator, or a plain
* object. If non-`null`, this is treated the same way as would be done in
* {@link #appendAll}.
*/
constructor(other = null) {
super();
// Note: We don't use the default constructor's ability to initialize from
// an argument because it does the wrong thing with non-`Headers` objects
// with array-valued names. (Specifically, it joins them with `,` and not
// `, `; that is, it doesn't include a space.)
if (other) {
this.appendAll(other);
}
}
/**
* Like {@link #setAll} (see which), except appends to headers if they already
* exist in the instance.
*
* @param {Headers|Map|object} other Source of entries to append. This is
* allowed to be anything that implements a map-like iterator, or a plain
* object.
*/
appendAll(other) {
const originallyHad = {};
for (const [name, value] of HttpHeaders.#entriesForOther(other)) {
if (typeof value === 'function') {
if (originallyHad[name] === undefined) {
// First time seeing this name.
originallyHad[name] = this.has(name);
}
if (!originallyHad[name]) {
this.append(name, value());
}
} else {
this.append(name, value);
}
}
}
/**
* Appends all of the given cookies as `Set-Cookie` headers.
*
* @param {Cookies} cookies Cookies to append.
*/
appendSetCookie(cookies) {
for (const cookie of cookies.responseHeaders()) {
this.append('set-cookie', cookie);
}
}
/**
* Deletes all content-related headers. These are (unsurprisingly) the ones
* whose names begin with `content-`.
*/
deleteContent() {
// Note: Can't safely delete in the middle of an iteration. Arguably a bug
// in `Headers`.
const toDelete = [];
for (const [name] of this) {
if (name.startsWith('content-')) {
toDelete.push(name);
}
}
for (const name of toDelete) {
this.delete(name);
}
}
/**
* Like the default `entries()` method, except alters names to be cased
* appropriately for the indicated HTTP version. In addition, most values
* returned are strings, but `Set-Cookie` values are always arrays of strings.
*
* This method is meant to make it easy to call `setHeader()` on an HTTP-ish
* response object.
*
* @param {string|number} httpVersion HTTP version string (e.g. `'1.1'` or
* `'2.0'`) or major version number (e.g. `2`).
* @yields {Array} Entry with appropriately-cased name.
*/
*entriesForVersion(httpVersion) {
const classicNaming = (typeof httpVersion === 'string')
? (httpVersion[0] <= '1')
: (httpVersion <= 1);
let gotSetCookie = false;
for (const [name, value] of this) {
const finalName = classicNaming ? HttpUtil.classicHeaderNameFrom(name) : name;
if (name === 'set-cookie') {
// When iterating, a `Headers` object will emit multiple entries with
// `set-cookie`. We use the first to trigger use of our special form and
// then ignore subsequent ones.
if (!gotSetCookie) {
gotSetCookie = true;
yield [finalName, this.getSetCookie()];
}
} else {
yield [finalName, value];
}
}
}
/**
* Extracts a subset of the headers. Names which aren't found are not listed
* in the result, and don't cause an error. Extracted values are always simple
* (pre-combined) strings, except for `Set-Cookies`, which is always an array.
*
* @param {...string} names Names of headers to extract.
* @returns {object} The extracted subset, as a mapping from the given `names`
* to their values.
*/
extract(...names) {
const result = {};
for (const n of names) {
const modName = HttpUtil.modernHeaderNameFrom(n);
if (modName === 'set-cookie') {
const cookies = this.getSetCookie();
if (cookies.length !== 0) {
result[n] = cookies;
}
} else {
const got = this.get(modName);
if (got !== null) {
result[n] = got;
}
}
}
return result;
}
/**
* Indicates whether this instance has all of the named headers.
*
* @param {...string} names Header names.
* @returns {boolean} `true` if this instance has all of the headers, or
* `false` if not.
*/
hasAll(...names) {
for (const name of names) {
if (!this.has(name)) {
return false;
}
}
return true;
}
/**
* Indicates whether this instance has any of the named headers.
*
* @param {...string} names Header names.
* @returns {boolean} `true` if this instance has any of the headers, or
* `false` if not.
*/
hasAny(...names) {
for (const name of names) {
if (this.has(name)) {
return true;
}
}
return false;
}
/**
* Sets headers on this instance for each of the entries in the given other
* instance. This is _almost_ like iterating over the value and calling
* `set()` on each, except that `Header` iterators can sometimes report the
* same header name multiple times (because of `set-cookie`), and this method
* takes care not to let that mess things up.
*
* Given a `Headers` object (including an instance of this subclass), all of
* the entries are appended.
*
* Given a plain object or `Map` (or, really, more generally any object that
* returns a map-like iterator), what is appended depends on the value:
*
* * Type `string`: The value is used directly.
* * Other non-compound values: The value is converted to a string and then
* used.
* * Functions: The entry is treated as an "underlay." If this instance does
* not have the header in question, the function is called to produce a
* value, and that value is then appended.
* * Arrays: The contents of arrays are processed recursively, per these
* rules.
* * Everything else is an error.
*
* **Note:** "Underlaying" (when given a function value) is based on the state
* of the instance _before_ this method starts running.
*
* @param {Headers|Map|object} other Source of entries to append. This is
* allowed to be anything that implements a map-like iterator, or a plain
* object.
*/
setAll(other) {
const originallyHad = {};
for (const [name, value] of HttpHeaders.#entriesForOther(other)) {
const firstTime = (originallyHad[name] === undefined);
if (firstTime) {
// First time seeing this name.
originallyHad[name] = this.has(name);
}
if (typeof value === 'function') {
if (!originallyHad[name]) {
this.append(name, value());
}
} else if (firstTime) {
this.set(name, value);
} else {
this.append(name, value);
}
}
}
//
// Static members
//
/**
* Helper for {@link #appendAll} and {@link #setAll}, which iterates
* (potentially recursively) over an `other` argument, as defined by those
* methods.
*
* @param {*} other The instance to iterate over.
* @yields {Array<string, *>} An entry, suitable for appending or setting.
*/
static *#entriesForOther(other) {
function* doOne(name, value) {
if ((typeof value === 'string') || (typeof value === 'function')) {
// The overwhelmingly most common cases.
yield [name, value];
} else if (typeof value === 'object') {
if (Array.isArray(value)) {
for (const v of value) {
yield* doOne(name, v);
}
} else if (value === null) {
yield [name, 'null'];
} else {
throw new Error(`Strange header value: ${value}`);
}
} else {
yield [name, `${value}`];
}
}
const iterateOver = other[Symbol.iterator]
? other // Use `other`'s defined iterator.
: Object.entries(other); // Treat `other` as a plain object.
for (const [name, value] of iterateOver) {
yield* doOne(name, value);
}
}
}