forked from angular/angular
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathheaders.ts
More file actions
185 lines (160 loc) · 4.92 KB
/
Copy pathheaders.ts
File metadata and controls
185 lines (160 loc) · 4.92 KB
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
/**
* @license
* Copyright Google Inc. All Rights Reserved.
*
* Use of this source code is governed by an MIT-style license that can be
* found in the LICENSE file at https://angular.io/license
*/
/**
* Polyfill for [Headers](https://developer.mozilla.org/en-US/docs/Web/API/Headers/Headers), as
* specified in the [Fetch Spec](https://fetch.spec.whatwg.org/#headers-class).
*
* The only known difference between this `Headers` implementation and the spec is the
* lack of an `entries` method.
*
* ### Example
*
* ```
* import {Headers} from '@angular/http';
*
* var firstHeaders = new Headers();
* firstHeaders.append('Content-Type', 'image/jpeg');
* console.log(firstHeaders.get('Content-Type')) //'image/jpeg'
*
* // Create headers from Plain Old JavaScript Object
* var secondHeaders = new Headers({
* 'X-My-Custom-Header': 'Angular'
* });
* console.log(secondHeaders.get('X-My-Custom-Header')); //'Angular'
*
* var thirdHeaders = new Headers(secondHeaders);
* console.log(thirdHeaders.get('X-My-Custom-Header')); //'Angular'
* ```
*
* @experimental
*/
export class Headers {
/** @internal header names are lower case */
_headers: Map<string, string[]> = new Map();
/** @internal map lower case names to actual names */
_normalizedNames: Map<string, string> = new Map();
// TODO(vicb): any -> string|string[]
constructor(headers?: Headers|{[name: string]: any}) {
if (!headers) {
return;
}
if (headers instanceof Headers) {
headers.forEach((values: string[], name: string) => {
values.forEach(value => this.append(name, value));
});
return;
}
Object.keys(headers).forEach((name: string) => {
const values: string[] = Array.isArray(headers[name]) ? headers[name] : [headers[name]];
this.delete(name);
values.forEach(value => this.append(name, value));
});
}
/**
* Returns a new Headers instance from the given DOMString of Response Headers
*/
static fromResponseHeaderString(headersString: string): Headers {
const headers = new Headers();
headersString.split('\n').forEach(line => {
const index = line.indexOf(':');
if (index > 0) {
const name = line.slice(0, index);
const value = line.slice(index + 1).trim();
headers.set(name, value);
}
});
return headers;
}
/**
* Appends a header to existing list of header values for a given header name.
*/
append(name: string, value: string): void {
const values = this.getAll(name);
if (values === null) {
this.set(name, value);
} else {
values.push(value);
}
}
/**
* Deletes all header values for the given name.
*/
delete (name: string): void {
const lcName = name.toLowerCase();
this._normalizedNames.delete(lcName);
this._headers.delete(lcName);
}
forEach(fn: (values: string[], name: string, headers: Map<string, string[]>) => void): void {
this._headers.forEach(
(values, lcName) => fn(values, this._normalizedNames.get(lcName), this._headers));
}
/**
* Returns first header that matches given name.
*/
get(name: string): string {
const values = this.getAll(name);
if (values === null) {
return null;
}
return values.length > 0 ? values[0] : null;
}
/**
* Checks for existence of header by given name.
*/
has(name: string): boolean { return this._headers.has(name.toLowerCase()); }
/**
* Returns the names of the headers
*/
keys(): string[] { return Array.from(this._normalizedNames.values()); }
/**
* Sets or overrides header value for given name.
*/
set(name: string, value: string|string[]): void {
if (Array.isArray(value)) {
if (value.length) {
this._headers.set(name.toLowerCase(), [value.join(',')]);
}
} else {
this._headers.set(name.toLowerCase(), [value]);
}
this.mayBeSetNormalizedName(name);
}
/**
* Returns values of all headers.
*/
values(): string[][] { return Array.from(this._headers.values()); }
/**
* Returns string of all headers.
*/
// TODO(vicb): returns {[name: string]: string[]}
toJSON(): {[name: string]: any} {
const serialized: {[name: string]: string[]} = {};
this._headers.forEach((values: string[], name: string) => {
const split: string[] = [];
values.forEach(v => split.push(...v.split(',')));
serialized[this._normalizedNames.get(name)] = split;
});
return serialized;
}
/**
* Returns list of header values for a given name.
*/
getAll(name: string): string[] {
return this.has(name) ? this._headers.get(name.toLowerCase()) : null;
}
/**
* This method is not implemented.
*/
entries() { throw new Error('"entries" method is not implemented on Headers class'); }
private mayBeSetNormalizedName(name: string): void {
const lcName = name.toLowerCase();
if (!this._normalizedNames.has(lcName)) {
this._normalizedNames.set(lcName, name);
}
}
}