summaryrefslogtreecommitdiff
path: root/js/headers.ts
diff options
context:
space:
mode:
Diffstat (limited to 'js/headers.ts')
-rw-r--r--js/headers.ts16
1 files changed, 13 insertions, 3 deletions
diff --git a/js/headers.ts b/js/headers.ts
index 7cc38181f..92077a03f 100644
--- a/js/headers.ts
+++ b/js/headers.ts
@@ -31,10 +31,11 @@ class HeadersBase {
// https://github.com/bitinn/node-fetch/blob/master/src/headers.js
// Copyright (c) 2016 David Frank. MIT License.
private _validateName(name: string) {
- if (invalidTokenRegex.test(name)) {
+ if (invalidTokenRegex.test(name) || name === "") {
throw new TypeError(`${name} is not a legal HTTP header name`);
}
}
+
private _validateValue(value: string) {
if (invalidHeaderCharRegex.test(value)) {
throw new TypeError(`${value} is not a legal HTTP header value`);
@@ -51,8 +52,17 @@ class HeadersBase {
} else {
this[headerMap] = new Map();
if (Array.isArray(init)) {
- for (const [rawName, rawValue] of init) {
- const [name, value] = this._normalizeParams(rawName, rawValue);
+ for (const tuple of init) {
+ // If header does not contain exactly two items,
+ // then throw a TypeError.
+ // ref: https://fetch.spec.whatwg.org/#concept-headers-fill
+ if (tuple.length !== 2) {
+ // tslint:disable:max-line-length
+ // prettier-ignore
+ throw new TypeError("Failed to construct 'Headers'; Each header pair must be an iterable [name, value] tuple");
+ }
+
+ const [name, value] = this._normalizeParams(tuple[0], tuple[1]);
this._validateName(name);
this._validateValue(value);
const existingValue = this[headerMap].get(name);