2015-03-24 21:40:11 +00:00
|
|
|
/**
|
2015-06-05 22:23:30 +00:00
|
|
|
* Copyright (c) 2015-present, Facebook, Inc.
|
|
|
|
* All rights reserved.
|
|
|
|
*
|
|
|
|
* This source code is licensed under the BSD-style license found in the
|
|
|
|
* LICENSE file in the root directory of this source tree. An additional grant
|
|
|
|
* of patent rights can be found in the PATENTS file in the same directory.
|
2015-03-24 21:40:11 +00:00
|
|
|
*
|
|
|
|
* @providesModule XMLHttpRequestBase
|
2015-06-05 22:23:30 +00:00
|
|
|
* @flow
|
2015-03-24 21:40:11 +00:00
|
|
|
*/
|
|
|
|
'use strict';
|
|
|
|
|
2015-11-17 14:28:44 +00:00
|
|
|
var RCTNetworking = require('RCTNetworking');
|
|
|
|
var RCTDeviceEventEmitter = require('RCTDeviceEventEmitter');
|
2016-04-11 12:53:41 +00:00
|
|
|
const invariant = require('fbjs/lib/invariant');
|
|
|
|
const utf8 = require('utf8');
|
|
|
|
const warning = require('fbjs/lib/warning');
|
|
|
|
|
|
|
|
type ResponseType = '' | 'arraybuffer' | 'blob' | 'document' | 'json' | 'text';
|
|
|
|
type Response = ?Object | string;
|
2015-11-17 14:28:44 +00:00
|
|
|
|
2016-02-17 07:58:49 +00:00
|
|
|
const UNSENT = 0;
|
|
|
|
const OPENED = 1;
|
|
|
|
const HEADERS_RECEIVED = 2;
|
|
|
|
const LOADING = 3;
|
|
|
|
const DONE = 4;
|
|
|
|
|
2016-04-11 12:53:41 +00:00
|
|
|
const SUPPORTED_RESPONSE_TYPES = {
|
|
|
|
arraybuffer: typeof global.ArrayBuffer === 'function',
|
|
|
|
blob: typeof global.Blob === 'function',
|
|
|
|
document: false,
|
|
|
|
json: true,
|
|
|
|
text: true,
|
|
|
|
'': true,
|
|
|
|
};
|
|
|
|
|
2015-03-24 21:40:11 +00:00
|
|
|
/**
|
|
|
|
* Shared base for platform-specific XMLHttpRequest implementations.
|
|
|
|
*/
|
|
|
|
class XMLHttpRequestBase {
|
|
|
|
|
2016-02-17 07:58:49 +00:00
|
|
|
static UNSENT: number;
|
|
|
|
static OPENED: number;
|
|
|
|
static HEADERS_RECEIVED: number;
|
|
|
|
static LOADING: number;
|
|
|
|
static DONE: number;
|
|
|
|
|
2015-03-24 21:40:11 +00:00
|
|
|
UNSENT: number;
|
|
|
|
OPENED: number;
|
|
|
|
HEADERS_RECEIVED: number;
|
|
|
|
LOADING: number;
|
|
|
|
DONE: number;
|
|
|
|
|
|
|
|
onreadystatechange: ?Function;
|
|
|
|
onload: ?Function;
|
|
|
|
upload: any;
|
|
|
|
readyState: number;
|
|
|
|
responseHeaders: ?Object;
|
2016-04-11 12:53:41 +00:00
|
|
|
responseText: string;
|
2015-05-22 23:21:58 +00:00
|
|
|
status: number;
|
2016-01-18 15:47:04 +00:00
|
|
|
timeout: number;
|
2016-01-07 18:25:56 +00:00
|
|
|
responseURL: ?string;
|
2015-03-24 21:40:11 +00:00
|
|
|
|
2015-11-17 14:28:44 +00:00
|
|
|
upload: ?{
|
|
|
|
onprogress?: (event: Object) => void;
|
|
|
|
};
|
|
|
|
|
|
|
|
_requestId: ?number;
|
|
|
|
_subscriptions: [any];
|
|
|
|
|
2015-03-24 21:40:11 +00:00
|
|
|
_aborted: boolean;
|
2016-04-11 12:53:41 +00:00
|
|
|
_cachedResponse: Response;
|
|
|
|
_hasError: boolean;
|
|
|
|
_headers: Object;
|
2015-06-05 22:23:30 +00:00
|
|
|
_lowerCaseResponseHeaders: Object;
|
2016-04-11 12:53:41 +00:00
|
|
|
_method: ?string;
|
|
|
|
_response: string | ?Object;
|
|
|
|
_responseType: ResponseType;
|
|
|
|
_sent: boolean;
|
|
|
|
_url: ?string;
|
2015-03-24 21:40:11 +00:00
|
|
|
|
|
|
|
constructor() {
|
2016-02-17 07:58:49 +00:00
|
|
|
this.UNSENT = UNSENT;
|
|
|
|
this.OPENED = OPENED;
|
|
|
|
this.HEADERS_RECEIVED = HEADERS_RECEIVED;
|
|
|
|
this.LOADING = LOADING;
|
|
|
|
this.DONE = DONE;
|
2015-03-24 21:40:11 +00:00
|
|
|
|
2015-06-05 22:23:30 +00:00
|
|
|
this.onreadystatechange = null;
|
|
|
|
this.onload = null;
|
|
|
|
this.upload = undefined; /* Upload not supported yet */
|
2016-01-18 15:47:04 +00:00
|
|
|
this.timeout = 0;
|
2015-06-05 22:23:30 +00:00
|
|
|
|
|
|
|
this._reset();
|
|
|
|
this._method = null;
|
|
|
|
this._url = null;
|
|
|
|
this._aborted = false;
|
|
|
|
}
|
|
|
|
|
2016-04-11 12:53:41 +00:00
|
|
|
_reset(): void {
|
2015-03-24 21:40:11 +00:00
|
|
|
this.readyState = this.UNSENT;
|
|
|
|
this.responseHeaders = undefined;
|
2015-06-05 22:23:30 +00:00
|
|
|
this.responseText = '';
|
2015-05-22 23:21:58 +00:00
|
|
|
this.status = 0;
|
2016-01-07 18:25:56 +00:00
|
|
|
delete this.responseURL;
|
2015-03-24 21:40:11 +00:00
|
|
|
|
2015-11-17 14:28:44 +00:00
|
|
|
this._requestId = null;
|
|
|
|
|
2016-04-11 12:53:41 +00:00
|
|
|
this._cachedResponse = undefined;
|
|
|
|
this._hasError = false;
|
2015-03-24 21:40:11 +00:00
|
|
|
this._headers = {};
|
2016-04-11 12:53:41 +00:00
|
|
|
this._responseType = '';
|
2015-03-24 21:40:11 +00:00
|
|
|
this._sent = false;
|
2015-06-05 22:23:30 +00:00
|
|
|
this._lowerCaseResponseHeaders = {};
|
2015-11-17 14:28:44 +00:00
|
|
|
|
|
|
|
this._clearSubscriptions();
|
|
|
|
}
|
|
|
|
|
2016-04-11 12:53:41 +00:00
|
|
|
// $FlowIssue #10784535
|
|
|
|
get responseType(): ResponseType {
|
|
|
|
return this._responseType;
|
|
|
|
}
|
|
|
|
|
|
|
|
// $FlowIssue #10784535
|
|
|
|
set responseType(responseType: ResponseType): void {
|
|
|
|
if (this.readyState > HEADERS_RECEIVED) {
|
|
|
|
throw new Error(
|
|
|
|
"Failed to set the 'responseType' property on 'XMLHttpRequest': The " +
|
|
|
|
"response type cannot be set if the object's state is LOADING or DONE"
|
|
|
|
);
|
|
|
|
}
|
|
|
|
if (!SUPPORTED_RESPONSE_TYPES.hasOwnProperty(responseType)) {
|
|
|
|
warning(
|
|
|
|
`The provided value '${responseType}' is not a valid 'responseType'.`);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
// redboxes early, e.g. for 'arraybuffer' on ios 7
|
|
|
|
invariant(
|
|
|
|
SUPPORTED_RESPONSE_TYPES[responseType] || responseType === 'document',
|
|
|
|
`The provided value '${responseType}' is unsupported in this environment.`
|
|
|
|
);
|
|
|
|
this._responseType = responseType;
|
|
|
|
}
|
|
|
|
|
|
|
|
// $FlowIssue #10784535
|
|
|
|
get response(): Response {
|
|
|
|
const {responseType} = this;
|
|
|
|
if (responseType === '' || responseType === 'text') {
|
|
|
|
return this.readyState < LOADING || this._hasError
|
|
|
|
? ''
|
|
|
|
: this.responseText;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (this.readyState !== DONE) {
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (this._cachedResponse !== undefined) {
|
|
|
|
return this._cachedResponse;
|
|
|
|
}
|
|
|
|
|
|
|
|
switch (this.responseType) {
|
|
|
|
case 'document':
|
|
|
|
this._cachedResponse = null;
|
|
|
|
break;
|
|
|
|
|
|
|
|
case 'arraybuffer':
|
|
|
|
this._cachedResponse = toArrayBuffer(
|
|
|
|
this.responseText, this.getResponseHeader('content-type') || '');
|
|
|
|
break;
|
|
|
|
|
|
|
|
case 'blob':
|
|
|
|
this._cachedResponse = new global.Blob(
|
|
|
|
[this.responseText],
|
|
|
|
{type: this.getResponseHeader('content-type') || ''}
|
|
|
|
);
|
|
|
|
break;
|
|
|
|
|
|
|
|
case 'json':
|
|
|
|
try {
|
|
|
|
this._cachedResponse = JSON.parse(this.responseText);
|
|
|
|
} catch (_) {
|
|
|
|
this._cachedResponse = null;
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
|
|
|
|
default:
|
|
|
|
this._cachedResponse = null;
|
|
|
|
}
|
|
|
|
|
|
|
|
return this._cachedResponse;
|
|
|
|
}
|
|
|
|
|
2015-11-17 14:28:44 +00:00
|
|
|
didCreateRequest(requestId: number): void {
|
|
|
|
this._requestId = requestId;
|
|
|
|
this._subscriptions.push(RCTDeviceEventEmitter.addListener(
|
|
|
|
'didSendNetworkData',
|
|
|
|
(args) => this._didUploadProgress.call(this, ...args)
|
|
|
|
));
|
|
|
|
this._subscriptions.push(RCTDeviceEventEmitter.addListener(
|
|
|
|
'didReceiveNetworkResponse',
|
|
|
|
(args) => this._didReceiveResponse.call(this, ...args)
|
|
|
|
));
|
|
|
|
this._subscriptions.push(RCTDeviceEventEmitter.addListener(
|
|
|
|
'didReceiveNetworkData',
|
|
|
|
(args) => this._didReceiveData.call(this, ...args)
|
|
|
|
));
|
|
|
|
this._subscriptions.push(RCTDeviceEventEmitter.addListener(
|
|
|
|
'didCompleteNetworkResponse',
|
|
|
|
(args) => this._didCompleteResponse.call(this, ...args)
|
|
|
|
));
|
|
|
|
}
|
|
|
|
|
|
|
|
_didUploadProgress(requestId: number, progress: number, total: number): void {
|
|
|
|
if (requestId === this._requestId && this.upload && this.upload.onprogress) {
|
|
|
|
var event = {
|
|
|
|
lengthComputable: true,
|
|
|
|
loaded: progress,
|
|
|
|
total,
|
|
|
|
};
|
|
|
|
this.upload.onprogress(event);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-01-07 18:25:56 +00:00
|
|
|
_didReceiveResponse(requestId: number, status: number, responseHeaders: ?Object, responseURL: ?string): void {
|
2015-11-17 14:28:44 +00:00
|
|
|
if (requestId === this._requestId) {
|
|
|
|
this.status = status;
|
|
|
|
this.setResponseHeaders(responseHeaders);
|
|
|
|
this.setReadyState(this.HEADERS_RECEIVED);
|
2016-01-07 18:25:56 +00:00
|
|
|
if (responseURL || responseURL === '') {
|
|
|
|
this.responseURL = responseURL;
|
|
|
|
} else {
|
|
|
|
delete this.responseURL;
|
|
|
|
}
|
2015-11-17 14:28:44 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
_didReceiveData(requestId: number, responseText: string): void {
|
|
|
|
if (requestId === this._requestId) {
|
|
|
|
if (!this.responseText) {
|
|
|
|
this.responseText = responseText;
|
|
|
|
} else {
|
|
|
|
this.responseText += responseText;
|
|
|
|
}
|
2016-04-11 12:53:41 +00:00
|
|
|
this._cachedResponse = undefined; // force lazy recomputation
|
2015-11-17 14:28:44 +00:00
|
|
|
this.setReadyState(this.LOADING);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
_didCompleteResponse(requestId: number, error: string): void {
|
|
|
|
if (requestId === this._requestId) {
|
|
|
|
if (error) {
|
|
|
|
this.responseText = error;
|
2016-04-11 12:53:41 +00:00
|
|
|
this._hasError = true;
|
2015-11-17 14:28:44 +00:00
|
|
|
}
|
|
|
|
this._clearSubscriptions();
|
|
|
|
this._requestId = null;
|
|
|
|
this.setReadyState(this.DONE);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
_clearSubscriptions(): void {
|
|
|
|
(this._subscriptions || []).forEach(sub => {
|
|
|
|
sub.remove();
|
|
|
|
});
|
|
|
|
this._subscriptions = [];
|
2015-03-24 21:40:11 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
getAllResponseHeaders(): ?string {
|
2015-06-05 22:23:30 +00:00
|
|
|
if (!this.responseHeaders) {
|
|
|
|
// according to the spec, return null if no response has been received
|
|
|
|
return null;
|
2015-04-21 23:43:07 +00:00
|
|
|
}
|
2015-06-05 22:23:30 +00:00
|
|
|
var headers = this.responseHeaders || {};
|
|
|
|
return Object.keys(headers).map((headerName) => {
|
|
|
|
return headerName + ': ' + headers[headerName];
|
|
|
|
}).join('\n');
|
2015-03-24 21:40:11 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
getResponseHeader(header: string): ?string {
|
2015-06-05 22:23:30 +00:00
|
|
|
var value = this._lowerCaseResponseHeaders[header.toLowerCase()];
|
|
|
|
return value !== undefined ? value : null;
|
2015-03-24 21:40:11 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
setRequestHeader(header: string, value: any): void {
|
2015-06-05 22:23:30 +00:00
|
|
|
if (this.readyState !== this.OPENED) {
|
|
|
|
throw new Error('Request has not been opened');
|
|
|
|
}
|
2015-05-26 15:12:59 +00:00
|
|
|
this._headers[header.toLowerCase()] = value;
|
2015-03-24 21:40:11 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
open(method: string, url: string, async: ?boolean): void {
|
2015-06-05 22:23:30 +00:00
|
|
|
/* Other optional arguments are not supported yet */
|
2015-03-24 21:40:11 +00:00
|
|
|
if (this.readyState !== this.UNSENT) {
|
|
|
|
throw new Error('Cannot open, already sending');
|
|
|
|
}
|
|
|
|
if (async !== undefined && !async) {
|
|
|
|
// async is default
|
|
|
|
throw new Error('Synchronous http requests are not supported');
|
|
|
|
}
|
2016-01-07 12:00:15 +00:00
|
|
|
if (!url) {
|
|
|
|
throw new Error('Cannot load an empty url');
|
|
|
|
}
|
2015-06-05 22:23:30 +00:00
|
|
|
this._reset();
|
2016-04-13 13:47:29 +00:00
|
|
|
this._method = method.toUpperCase();
|
2015-03-24 21:40:11 +00:00
|
|
|
this._url = url;
|
|
|
|
this._aborted = false;
|
2015-06-05 22:23:30 +00:00
|
|
|
this.setReadyState(this.OPENED);
|
2015-03-24 21:40:11 +00:00
|
|
|
}
|
|
|
|
|
2016-01-18 15:47:04 +00:00
|
|
|
sendImpl(method: ?string, url: ?string, headers: Object, data: any, timeout: number): void {
|
2015-03-24 21:40:11 +00:00
|
|
|
throw new Error('Subclass must define sendImpl method');
|
|
|
|
}
|
|
|
|
|
|
|
|
send(data: any): void {
|
|
|
|
if (this.readyState !== this.OPENED) {
|
|
|
|
throw new Error('Request has not been opened');
|
|
|
|
}
|
|
|
|
if (this._sent) {
|
|
|
|
throw new Error('Request has already been sent');
|
|
|
|
}
|
|
|
|
this._sent = true;
|
2016-01-18 15:47:04 +00:00
|
|
|
this.sendImpl(this._method, this._url, this._headers, data, this.timeout);
|
2015-03-24 21:40:11 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
abort(): void {
|
2015-06-05 22:23:30 +00:00
|
|
|
this._aborted = true;
|
2015-11-17 14:28:44 +00:00
|
|
|
if (this._requestId) {
|
|
|
|
RCTNetworking.abortRequest(this._requestId);
|
|
|
|
}
|
2015-03-24 21:40:11 +00:00
|
|
|
// only call onreadystatechange if there is something to abort,
|
|
|
|
// below logic is per spec
|
|
|
|
if (!(this.readyState === this.UNSENT ||
|
|
|
|
(this.readyState === this.OPENED && !this._sent) ||
|
|
|
|
this.readyState === this.DONE)) {
|
2015-06-05 22:23:30 +00:00
|
|
|
this._reset();
|
|
|
|
this.setReadyState(this.DONE);
|
2015-03-24 21:40:11 +00:00
|
|
|
}
|
2015-06-05 22:23:30 +00:00
|
|
|
// Reset again after, in case modified in handler
|
|
|
|
this._reset();
|
2015-03-24 21:40:11 +00:00
|
|
|
}
|
|
|
|
|
2015-06-05 22:23:30 +00:00
|
|
|
setResponseHeaders(responseHeaders: ?Object): void {
|
|
|
|
this.responseHeaders = responseHeaders || null;
|
|
|
|
var headers = responseHeaders || {};
|
|
|
|
this._lowerCaseResponseHeaders =
|
|
|
|
Object.keys(headers).reduce((lcaseHeaders, headerName) => {
|
|
|
|
lcaseHeaders[headerName.toLowerCase()] = headers[headerName];
|
2015-07-07 21:57:05 +00:00
|
|
|
return lcaseHeaders;
|
2015-06-05 22:23:30 +00:00
|
|
|
}, {});
|
2015-03-24 21:40:11 +00:00
|
|
|
}
|
|
|
|
|
2015-06-05 22:23:30 +00:00
|
|
|
setReadyState(newState: number): void {
|
2015-03-24 21:40:11 +00:00
|
|
|
this.readyState = newState;
|
|
|
|
// TODO: workaround flow bug with nullable function checks
|
|
|
|
var onreadystatechange = this.onreadystatechange;
|
|
|
|
if (onreadystatechange) {
|
|
|
|
// We should send an event to handler, but since we don't process that
|
|
|
|
// event anywhere, let's leave it empty
|
2016-01-18 16:54:14 +00:00
|
|
|
onreadystatechange.call(this, null);
|
2015-03-24 21:40:11 +00:00
|
|
|
}
|
2015-06-05 22:23:30 +00:00
|
|
|
if (newState === this.DONE && !this._aborted) {
|
|
|
|
this._sendLoad();
|
|
|
|
}
|
2015-03-24 21:40:11 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
_sendLoad(): void {
|
|
|
|
// TODO: workaround flow bug with nullable function checks
|
|
|
|
var onload = this.onload;
|
|
|
|
if (onload) {
|
|
|
|
// We should send an event to handler, but since we don't process that
|
|
|
|
// event anywhere, let's leave it empty
|
|
|
|
onload(null);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-02-17 07:58:49 +00:00
|
|
|
XMLHttpRequestBase.UNSENT = UNSENT;
|
|
|
|
XMLHttpRequestBase.OPENED = OPENED;
|
|
|
|
XMLHttpRequestBase.HEADERS_RECEIVED = HEADERS_RECEIVED;
|
|
|
|
XMLHttpRequestBase.LOADING = LOADING;
|
|
|
|
XMLHttpRequestBase.DONE = DONE;
|
|
|
|
|
2016-04-11 12:53:41 +00:00
|
|
|
function toArrayBuffer(text: string, contentType: string): ArrayBuffer {
|
|
|
|
const {length} = text;
|
|
|
|
if (length === 0) {
|
|
|
|
return new ArrayBuffer(0);
|
|
|
|
}
|
|
|
|
|
|
|
|
const charsetMatch = contentType.match(/;\s*charset=([^;]*)/i);
|
|
|
|
const charset = charsetMatch ? charsetMatch[1].trim() : 'utf-8';
|
|
|
|
|
|
|
|
if (/^utf-?8$/i.test(charset)) {
|
|
|
|
return utf8.encode(text);
|
|
|
|
} else { //TODO: utf16 / ucs2 / utf32
|
|
|
|
const array = new Uint8Array(length);
|
|
|
|
for (let i = 0; i < length; i++) {
|
|
|
|
array[i] = text.charCodeAt(i); // Uint8Array automatically masks with 0xff
|
|
|
|
}
|
|
|
|
return array.buffer;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-03-24 21:40:11 +00:00
|
|
|
module.exports = XMLHttpRequestBase;
|