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';
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Shared base for platform-specific XMLHttpRequest implementations.
|
|
|
|
*/
|
|
|
|
class XMLHttpRequestBase {
|
|
|
|
|
|
|
|
UNSENT: number;
|
|
|
|
OPENED: number;
|
|
|
|
HEADERS_RECEIVED: number;
|
|
|
|
LOADING: number;
|
|
|
|
DONE: number;
|
|
|
|
|
|
|
|
onreadystatechange: ?Function;
|
|
|
|
onload: ?Function;
|
|
|
|
upload: any;
|
|
|
|
readyState: number;
|
|
|
|
responseHeaders: ?Object;
|
|
|
|
responseText: ?string;
|
2015-05-22 23:21:58 +00:00
|
|
|
status: number;
|
2015-03-24 21:40:11 +00:00
|
|
|
|
|
|
|
_method: ?string;
|
|
|
|
_url: ?string;
|
|
|
|
_headers: Object;
|
|
|
|
_sent: boolean;
|
|
|
|
_aborted: boolean;
|
2015-06-05 22:23:30 +00:00
|
|
|
_lowerCaseResponseHeaders: Object;
|
2015-03-24 21:40:11 +00:00
|
|
|
|
|
|
|
constructor() {
|
|
|
|
this.UNSENT = 0;
|
|
|
|
this.OPENED = 1;
|
|
|
|
this.HEADERS_RECEIVED = 2;
|
|
|
|
this.LOADING = 3;
|
|
|
|
this.DONE = 4;
|
|
|
|
|
2015-06-05 22:23:30 +00:00
|
|
|
this.onreadystatechange = null;
|
|
|
|
this.onload = null;
|
|
|
|
this.upload = undefined; /* Upload not supported yet */
|
|
|
|
|
|
|
|
this._reset();
|
|
|
|
this._method = null;
|
|
|
|
this._url = null;
|
|
|
|
this._aborted = false;
|
|
|
|
}
|
|
|
|
|
|
|
|
_reset() {
|
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;
|
2015-03-24 21:40:11 +00:00
|
|
|
|
|
|
|
this._headers = {};
|
|
|
|
this._sent = false;
|
2015-06-05 22:23:30 +00:00
|
|
|
this._lowerCaseResponseHeaders = {};
|
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');
|
|
|
|
}
|
2015-06-05 22:23:30 +00:00
|
|
|
this._reset();
|
2015-03-24 21:40:11 +00:00
|
|
|
this._method = method;
|
|
|
|
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
|
|
|
}
|
|
|
|
|
|
|
|
sendImpl(method: ?string, url: ?string, headers: Object, data: any): void {
|
|
|
|
throw new Error('Subclass must define sendImpl method');
|
|
|
|
}
|
|
|
|
|
|
|
|
abortImpl(): void {
|
|
|
|
throw new Error('Subclass must define abortImpl 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;
|
|
|
|
this.sendImpl(this._method, this._url, this._headers, data);
|
|
|
|
}
|
|
|
|
|
|
|
|
abort(): void {
|
2015-06-05 22:23:30 +00:00
|
|
|
this._aborted = true;
|
2015-03-24 21:40:11 +00:00
|
|
|
this.abortImpl();
|
|
|
|
// 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-05-22 23:21:58 +00:00
|
|
|
callback(status: number, responseHeaders: ?Object, responseText: string): void {
|
2015-03-24 21:40:11 +00:00
|
|
|
if (this._aborted) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
this.status = status;
|
2015-06-19 10:30:09 +00:00
|
|
|
this.setResponseHeaders(responseHeaders || {});
|
2015-03-24 21:40:11 +00:00
|
|
|
this.responseText = responseText;
|
2015-06-05 22:23:30 +00:00
|
|
|
this.setReadyState(this.DONE);
|
|
|
|
}
|
|
|
|
|
|
|
|
setResponseHeaders(responseHeaders: ?Object): void {
|
|
|
|
this.responseHeaders = responseHeaders || null;
|
|
|
|
var headers = responseHeaders || {};
|
|
|
|
this._lowerCaseResponseHeaders =
|
|
|
|
Object.keys(headers).reduce((lcaseHeaders, headerName) => {
|
|
|
|
lcaseHeaders[headerName.toLowerCase()] = headers[headerName];
|
|
|
|
return headers;
|
|
|
|
}, {});
|
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
|
|
|
|
onreadystatechange(null);
|
|
|
|
}
|
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);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
module.exports = XMLHttpRequestBase;
|