angular-cn/packages/http/src/body.ts

105 lines
2.9 KiB
TypeScript
Raw Normal View History

/**
* @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
*/
import {stringToArrayBuffer} from './http_utils';
import {URLSearchParams} from './url_search_params';
/**
* HTTP request body used by both {@link Request} and {@link Response}
* https://fetch.spec.whatwg.org/#body
*/
export abstract class Body {
/**
* @internal
*/
protected _body: any;
/**
* Attempts to return body as parsed `JSON` object, or raises an exception.
*/
json(): any {
2016-10-19 16:42:39 -04:00
if (typeof this._body === 'string') {
return JSON.parse(<string>this._body);
}
if (this._body instanceof ArrayBuffer) {
2016-10-19 16:42:39 -04:00
return JSON.parse(this.text());
}
return this._body;
}
/**
* Returns the body as a string, presuming `toString()` can be called on the response body.
fix(http): introduce encodingHint for text() for better ArrayBuffer support Currently, if a Response has an ArrayBuffer body and text() is called, Angular attempts to convert the ArrayBuffer to a string. Doing this requires knowing the encoding of the bytes in the buffer, which is context that we don't have. Instead, we assume that the buffer is encoded in UTF-16, and attempt to process it that way. Unfortunately the approach chosen (interpret buffer as Uint16Array and create a Javascript string from each entry using String.fromCharCode) is incorrect as it does not handle UTF-16 surrogate pairs. What Angular actually implements, then, is UCS-2 decoding, which is equivalent to UTF-16 with characters restricted to the base plane. No standard way of decoding UTF-8 or UTF-16 exists in the browser today. APIs like TextDecoder are only supported in a few browsers, and although hacks like using the FileReader API with a Blob to force browsers to do content encoding detection and decoding exist, they're slow and not compatible with the synchronous text() API. Thus, this bug is fixed by introducing an encodingHint parameter to text(). The default value of this parameter is 'legacy', indicating that the existing broken behavior should be used - this prevents breaking existing apps. The only other possible value of the hint is 'iso-8859' which interprets each byte of the buffer with String.fromCharCode. UTF-8 and UTF-16 are not supported - it is up to the consumer to get the ArrayBuffer and decode it themselves. The parameter is a hint, as it's not always used (for example, if the conversion to text doesn't involve an ArrayBuffer source). Additionally, this leaves the door open for future implementations to perform more sophisticated encoding detection and ignore the user-provided value if it can be proven to be incorrect. Fixes #15932. PR Close #16420
2017-04-28 14:54:40 -04:00
*
* When decoding an `ArrayBuffer`, the optional `encodingHint` parameter determines how the
* bytes in the buffer will be interpreted. Valid values are:
*
* - `legacy` - incorrectly interpret the bytes as UTF-16 (technically, UCS-2). Only characters
* in the Basic Multilingual Plane are supported, surrogate pairs are not handled correctly.
* In addition, the endianness of the 16-bit octet pairs in the `ArrayBuffer` is not taken
* into consideration. This is the default behavior to avoid breaking apps, but should be
* considered deprecated.
*
* - `iso-8859` - interpret the bytes as ISO-8859 (which can be used for ASCII encoded text).
*/
fix(http): introduce encodingHint for text() for better ArrayBuffer support Currently, if a Response has an ArrayBuffer body and text() is called, Angular attempts to convert the ArrayBuffer to a string. Doing this requires knowing the encoding of the bytes in the buffer, which is context that we don't have. Instead, we assume that the buffer is encoded in UTF-16, and attempt to process it that way. Unfortunately the approach chosen (interpret buffer as Uint16Array and create a Javascript string from each entry using String.fromCharCode) is incorrect as it does not handle UTF-16 surrogate pairs. What Angular actually implements, then, is UCS-2 decoding, which is equivalent to UTF-16 with characters restricted to the base plane. No standard way of decoding UTF-8 or UTF-16 exists in the browser today. APIs like TextDecoder are only supported in a few browsers, and although hacks like using the FileReader API with a Blob to force browsers to do content encoding detection and decoding exist, they're slow and not compatible with the synchronous text() API. Thus, this bug is fixed by introducing an encodingHint parameter to text(). The default value of this parameter is 'legacy', indicating that the existing broken behavior should be used - this prevents breaking existing apps. The only other possible value of the hint is 'iso-8859' which interprets each byte of the buffer with String.fromCharCode. UTF-8 and UTF-16 are not supported - it is up to the consumer to get the ArrayBuffer and decode it themselves. The parameter is a hint, as it's not always used (for example, if the conversion to text doesn't involve an ArrayBuffer source). Additionally, this leaves the door open for future implementations to perform more sophisticated encoding detection and ignore the user-provided value if it can be proven to be incorrect. Fixes #15932. PR Close #16420
2017-04-28 14:54:40 -04:00
text(encodingHint: 'legacy'|'iso-8859' = 'legacy'): string {
if (this._body instanceof URLSearchParams) {
return this._body.toString();
}
if (this._body instanceof ArrayBuffer) {
fix(http): introduce encodingHint for text() for better ArrayBuffer support Currently, if a Response has an ArrayBuffer body and text() is called, Angular attempts to convert the ArrayBuffer to a string. Doing this requires knowing the encoding of the bytes in the buffer, which is context that we don't have. Instead, we assume that the buffer is encoded in UTF-16, and attempt to process it that way. Unfortunately the approach chosen (interpret buffer as Uint16Array and create a Javascript string from each entry using String.fromCharCode) is incorrect as it does not handle UTF-16 surrogate pairs. What Angular actually implements, then, is UCS-2 decoding, which is equivalent to UTF-16 with characters restricted to the base plane. No standard way of decoding UTF-8 or UTF-16 exists in the browser today. APIs like TextDecoder are only supported in a few browsers, and although hacks like using the FileReader API with a Blob to force browsers to do content encoding detection and decoding exist, they're slow and not compatible with the synchronous text() API. Thus, this bug is fixed by introducing an encodingHint parameter to text(). The default value of this parameter is 'legacy', indicating that the existing broken behavior should be used - this prevents breaking existing apps. The only other possible value of the hint is 'iso-8859' which interprets each byte of the buffer with String.fromCharCode. UTF-8 and UTF-16 are not supported - it is up to the consumer to get the ArrayBuffer and decode it themselves. The parameter is a hint, as it's not always used (for example, if the conversion to text doesn't involve an ArrayBuffer source). Additionally, this leaves the door open for future implementations to perform more sophisticated encoding detection and ignore the user-provided value if it can be proven to be incorrect. Fixes #15932. PR Close #16420
2017-04-28 14:54:40 -04:00
switch (encodingHint) {
case 'legacy':
return String.fromCharCode.apply(null, new Uint16Array(this._body as ArrayBuffer));
case 'iso-8859':
return String.fromCharCode.apply(null, new Uint8Array(this._body as ArrayBuffer));
default:
throw new Error(`Invalid value for encodingHint: ${encodingHint}`);
}
}
if (this._body == null) {
return '';
}
if (typeof this._body === 'object') {
2016-10-19 16:42:39 -04:00
return JSON.stringify(this._body, null, 2);
}
return this._body.toString();
}
/**
* Return the body as an ArrayBuffer
*/
arrayBuffer(): ArrayBuffer {
if (this._body instanceof ArrayBuffer) {
return <ArrayBuffer>this._body;
}
return stringToArrayBuffer(this.text());
}
/**
* Returns the request's body as a Blob, assuming that body exists.
*/
blob(): Blob {
if (this._body instanceof Blob) {
return <Blob>this._body;
}
if (this._body instanceof ArrayBuffer) {
return new Blob([this._body]);
}
throw new Error('The request body isn\'t either a blob or an array buffer');
}
}