| 
									
										
										
										
											2016-06-23 09:47:54 -07:00
										 |  |  | /** | 
					
						
							|  |  |  |  * @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
 | 
					
						
							|  |  |  |  */ | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-06-09 15:18:57 -07:00
										 |  |  | /** | 
					
						
							|  |  |  |  * Polyfill for [Headers](https://developer.mozilla.org/en-US/docs/Web/API/Headers/Headers), as
 | 
					
						
							| 
									
										
										
										
											2015-09-17 15:36:38 -07:00
										 |  |  |  * 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. | 
					
						
							|  |  |  |  * | 
					
						
							| 
									
										
										
										
											2016-10-03 15:27:56 -07:00
										 |  |  |  * ### Example | 
					
						
							| 
									
										
										
										
											2015-09-17 15:36:38 -07:00
										 |  |  |  * | 
					
						
							|  |  |  |  * ```
 | 
					
						
							| 
									
										
										
										
											2016-04-28 17:50:03 -07:00
										 |  |  |  * import {Headers} from '@angular/http'; | 
					
						
							| 
									
										
										
										
											2015-09-17 15:36:38 -07:00
										 |  |  |  * | 
					
						
							|  |  |  |  * 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'
 | 
					
						
							|  |  |  |  * ```
 | 
					
						
							| 
									
										
										
										
											2016-06-27 12:27:23 -07:00
										 |  |  |  * | 
					
						
							| 
									
										
										
										
											2018-06-01 22:09:01 +02:00
										 |  |  |  * @deprecated see https://angular.io/guide/http
 | 
					
						
							| 
									
										
										
										
											2015-06-09 15:18:57 -07:00
										 |  |  |  */ | 
					
						
							| 
									
										
										
										
											2015-04-28 23:07:55 -07:00
										 |  |  | export class Headers { | 
					
						
							| 
									
										
										
										
											2016-10-03 15:27:56 -07:00
										 |  |  |   /** @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[]
 | 
					
						
							| 
									
										
										
										
											2017-04-17 11:12:53 -07:00
										 |  |  |   constructor(headers?: Headers|{[name: string]: any}|null) { | 
					
						
							| 
									
										
										
										
											2016-10-03 15:27:56 -07:00
										 |  |  |     if (!headers) { | 
					
						
							| 
									
										
										
										
											2015-04-28 23:07:55 -07:00
										 |  |  |       return; | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-10-03 15:27:56 -07:00
										 |  |  |     if (headers instanceof Headers) { | 
					
						
							| 
									
										
										
										
											2016-11-04 13:26:38 -07:00
										 |  |  |       headers.forEach((values: string[], name: string) => { | 
					
						
							| 
									
										
										
										
											2016-10-05 17:05:50 -07:00
										 |  |  |         values.forEach(value => this.append(name, value)); | 
					
						
							| 
									
										
										
										
											2016-10-03 15:27:56 -07:00
										 |  |  |       }); | 
					
						
							| 
									
										
										
										
											2015-10-08 16:01:18 -07:00
										 |  |  |       return; | 
					
						
							| 
									
										
										
										
											2015-04-28 23:07:55 -07:00
										 |  |  |     } | 
					
						
							| 
									
										
										
										
											2015-10-08 16:01:18 -07:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-10-03 15:27:56 -07:00
										 |  |  |     Object.keys(headers).forEach((name: string) => { | 
					
						
							| 
									
										
										
										
											2016-10-05 17:05:50 -07:00
										 |  |  |       const values: string[] = Array.isArray(headers[name]) ? headers[name] : [headers[name]]; | 
					
						
							|  |  |  |       this.delete(name); | 
					
						
							|  |  |  |       values.forEach(value => this.append(name, value)); | 
					
						
							| 
									
										
										
										
											2016-02-01 17:05:50 -08:00
										 |  |  |     }); | 
					
						
							| 
									
										
										
										
											2015-04-28 23:07:55 -07:00
										 |  |  |   } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-11-19 17:51:00 -08:00
										 |  |  |   /** | 
					
						
							|  |  |  |    * Returns a new Headers instance from the given DOMString of Response Headers | 
					
						
							|  |  |  |    */ | 
					
						
							|  |  |  |   static fromResponseHeaderString(headersString: string): Headers { | 
					
						
							| 
									
										
										
										
											2016-10-03 15:27:56 -07:00
										 |  |  |     const headers = new Headers(); | 
					
						
							| 
									
										
										
										
											2016-07-29 13:19:49 +08:00
										 |  |  | 
 | 
					
						
							|  |  |  |     headersString.split('\n').forEach(line => { | 
					
						
							|  |  |  |       const index = line.indexOf(':'); | 
					
						
							|  |  |  |       if (index > 0) { | 
					
						
							| 
									
										
										
										
											2016-10-03 15:27:56 -07:00
										 |  |  |         const name = line.slice(0, index); | 
					
						
							|  |  |  |         const value = line.slice(index + 1).trim(); | 
					
						
							|  |  |  |         headers.set(name, value); | 
					
						
							| 
									
										
										
										
											2016-07-29 13:19:49 +08:00
										 |  |  |       } | 
					
						
							|  |  |  |     }); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     return headers; | 
					
						
							| 
									
										
										
										
											2015-11-19 17:51:00 -08:00
										 |  |  |   } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-06-24 00:27:07 -07:00
										 |  |  |   /** | 
					
						
							|  |  |  |    * Appends a header to existing list of header values for a given header name. | 
					
						
							|  |  |  |    */ | 
					
						
							| 
									
										
										
										
											2015-04-28 23:07:55 -07:00
										 |  |  |   append(name: string, value: string): void { | 
					
						
							| 
									
										
										
										
											2016-10-03 15:27:56 -07:00
										 |  |  |     const values = this.getAll(name); | 
					
						
							| 
									
										
										
										
											2016-10-05 16:36:42 -07:00
										 |  |  | 
 | 
					
						
							|  |  |  |     if (values === null) { | 
					
						
							|  |  |  |       this.set(name, value); | 
					
						
							|  |  |  |     } else { | 
					
						
							|  |  |  |       values.push(value); | 
					
						
							|  |  |  |     } | 
					
						
							| 
									
										
										
										
											2015-04-28 23:07:55 -07:00
										 |  |  |   } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-06-24 00:27:07 -07:00
										 |  |  |   /** | 
					
						
							|  |  |  |    * Deletes all header values for the given name. | 
					
						
							|  |  |  |    */ | 
					
						
							| 
									
										
										
										
											2016-10-03 15:27:56 -07:00
										 |  |  |   delete (name: string): void { | 
					
						
							|  |  |  |     const lcName = name.toLowerCase(); | 
					
						
							|  |  |  |     this._normalizedNames.delete(lcName); | 
					
						
							|  |  |  |     this._headers.delete(lcName); | 
					
						
							|  |  |  |   } | 
					
						
							| 
									
										
										
										
											2015-04-28 23:07:55 -07:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-04-17 11:12:53 -07:00
										 |  |  |   forEach(fn: (values: string[], name: string|undefined, headers: Map<string, string[]>) => void): | 
					
						
							|  |  |  |       void { | 
					
						
							| 
									
										
										
										
											2016-10-03 15:27:56 -07:00
										 |  |  |     this._headers.forEach( | 
					
						
							|  |  |  |         (values, lcName) => fn(values, this._normalizedNames.get(lcName), this._headers)); | 
					
						
							| 
									
										
										
										
											2015-09-17 15:36:38 -07:00
										 |  |  |   } | 
					
						
							| 
									
										
										
										
											2015-04-28 23:07:55 -07:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-06-24 00:27:07 -07:00
										 |  |  |   /** | 
					
						
							|  |  |  |    * Returns first header that matches given name. | 
					
						
							|  |  |  |    */ | 
					
						
							| 
									
										
										
										
											2017-04-17 11:12:53 -07:00
										 |  |  |   get(name: string): string|null { | 
					
						
							| 
									
										
										
										
											2016-10-03 15:27:56 -07:00
										 |  |  |     const values = this.getAll(name); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     if (values === null) { | 
					
						
							|  |  |  |       return null; | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     return values.length > 0 ? values[0] : null; | 
					
						
							|  |  |  |   } | 
					
						
							| 
									
										
										
										
											2015-04-28 23:07:55 -07:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-06-24 00:27:07 -07:00
										 |  |  |   /** | 
					
						
							| 
									
										
										
										
											2016-10-03 15:27:56 -07:00
										 |  |  |    * Checks for existence of header by given name. | 
					
						
							| 
									
										
										
										
											2015-06-24 00:27:07 -07:00
										 |  |  |    */ | 
					
						
							| 
									
										
										
										
											2016-10-03 15:27:56 -07:00
										 |  |  |   has(name: string): boolean { return this._headers.has(name.toLowerCase()); } | 
					
						
							| 
									
										
										
										
											2015-04-28 23:07:55 -07:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-06-24 00:27:07 -07:00
										 |  |  |   /** | 
					
						
							| 
									
										
										
										
											2016-10-03 15:27:56 -07:00
										 |  |  |    * Returns the names of the headers | 
					
						
							| 
									
										
										
										
											2015-06-24 00:27:07 -07:00
										 |  |  |    */ | 
					
						
							| 
									
										
										
										
											2016-11-03 16:58:27 -07:00
										 |  |  |   keys(): string[] { return Array.from(this._normalizedNames.values()); } | 
					
						
							| 
									
										
										
										
											2015-04-28 23:07:55 -07:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-06-24 00:27:07 -07:00
										 |  |  |   /** | 
					
						
							|  |  |  |    * Sets or overrides header value for given name. | 
					
						
							|  |  |  |    */ | 
					
						
							| 
									
										
										
										
											2016-10-03 15:27:56 -07:00
										 |  |  |   set(name: string, value: string|string[]): void { | 
					
						
							| 
									
										
										
										
											2016-10-05 17:05:50 -07:00
										 |  |  |     if (Array.isArray(value)) { | 
					
						
							|  |  |  |       if (value.length) { | 
					
						
							|  |  |  |         this._headers.set(name.toLowerCase(), [value.join(',')]); | 
					
						
							|  |  |  |       } | 
					
						
							|  |  |  |     } else { | 
					
						
							|  |  |  |       this._headers.set(name.toLowerCase(), [value]); | 
					
						
							|  |  |  |     } | 
					
						
							| 
									
										
										
										
											2016-10-03 15:27:56 -07:00
										 |  |  |     this.mayBeSetNormalizedName(name); | 
					
						
							| 
									
										
										
										
											2015-04-28 23:07:55 -07:00
										 |  |  |   } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-06-24 00:27:07 -07:00
										 |  |  |   /** | 
					
						
							|  |  |  |    * Returns values of all headers. | 
					
						
							|  |  |  |    */ | 
					
						
							| 
									
										
										
										
											2016-11-03 16:58:27 -07:00
										 |  |  |   values(): string[][] { return Array.from(this._headers.values()); } | 
					
						
							| 
									
										
										
										
											2015-04-28 23:07:55 -07:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-12-09 20:56:32 -08:00
										 |  |  |   /** | 
					
						
							|  |  |  |    * Returns string of all headers. | 
					
						
							|  |  |  |    */ | 
					
						
							| 
									
										
										
										
											2016-10-03 15:27:56 -07:00
										 |  |  |   // 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(','))); | 
					
						
							| 
									
										
										
										
											2017-04-17 11:12:53 -07:00
										 |  |  |       serialized[this._normalizedNames.get(name) !] = split; | 
					
						
							| 
									
										
										
										
											2016-01-26 23:24:50 -08:00
										 |  |  |     }); | 
					
						
							| 
									
										
										
										
											2016-10-03 15:27:56 -07:00
										 |  |  | 
 | 
					
						
							|  |  |  |     return serialized; | 
					
						
							| 
									
										
										
										
											2016-01-26 23:24:50 -08:00
										 |  |  |   } | 
					
						
							| 
									
										
										
										
											2015-12-09 20:56:32 -08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-06-24 00:27:07 -07:00
										 |  |  |   /** | 
					
						
							|  |  |  |    * Returns list of header values for a given name. | 
					
						
							|  |  |  |    */ | 
					
						
							| 
									
										
										
										
											2017-04-17 11:12:53 -07:00
										 |  |  |   getAll(name: string): string[]|null { | 
					
						
							|  |  |  |     return this.has(name) ? this._headers.get(name.toLowerCase()) || null : null; | 
					
						
							| 
									
										
										
										
											2015-06-19 12:14:12 -07:00
										 |  |  |   } | 
					
						
							| 
									
										
										
										
											2015-04-28 23:07:55 -07:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-06-24 00:27:07 -07:00
										 |  |  |   /** | 
					
						
							|  |  |  |    * This method is not implemented. | 
					
						
							|  |  |  |    */ | 
					
						
							| 
									
										
										
										
											2016-08-25 00:50:16 -07:00
										 |  |  |   entries() { throw new Error('"entries" method is not implemented on Headers class'); } | 
					
						
							| 
									
										
										
										
											2016-07-26 12:30:43 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-10-03 15:27:56 -07:00
										 |  |  |   private mayBeSetNormalizedName(name: string): void { | 
					
						
							|  |  |  |     const lcName = name.toLowerCase(); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     if (!this._normalizedNames.has(lcName)) { | 
					
						
							|  |  |  |       this._normalizedNames.set(lcName, name); | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  |   } | 
					
						
							| 
									
										
										
										
											2016-07-26 12:30:43 +08:00
										 |  |  | } |