123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495 |
- goog.provide('goog.labs.net.xhr');
- goog.provide('goog.labs.net.xhr.Error');
- goog.provide('goog.labs.net.xhr.HttpError');
- goog.provide('goog.labs.net.xhr.Options');
- goog.provide('goog.labs.net.xhr.PostData');
- goog.provide('goog.labs.net.xhr.ResponseType');
- goog.provide('goog.labs.net.xhr.TimeoutError');
- goog.require('goog.Promise');
- goog.require('goog.asserts');
- goog.require('goog.debug.Error');
- goog.require('goog.json');
- goog.require('goog.net.HttpStatus');
- goog.require('goog.net.XmlHttp');
- goog.require('goog.object');
- goog.require('goog.string');
- goog.require('goog.uri.utils');
- goog.require('goog.userAgent');
- goog.scope(function() {
- var userAgent = goog.userAgent;
- var xhr = goog.labs.net.xhr;
- var HttpStatus = goog.net.HttpStatus;
- xhr.Options;
- xhr.PostData;
- xhr.CONTENT_TYPE_HEADER = 'Content-Type';
- xhr.FORM_CONTENT_TYPE = 'application/x-www-form-urlencoded;charset=utf-8';
- xhr.ResponseType = {
- ARRAYBUFFER: 'arraybuffer',
- BLOB: 'blob',
- DOCUMENT: 'document',
- JSON: 'json',
- TEXT: 'text'
- };
- xhr.get = function(url, opt_options) {
- return xhr.send('GET', url, null, opt_options).then(function(request) {
- return request.responseText;
- });
- };
- xhr.post = function(url, data, opt_options) {
- return xhr.send('POST', url, data, opt_options).then(function(request) {
- return request.responseText;
- });
- };
- xhr.getJson = function(url, opt_options) {
- return xhr.send('GET', url, null, opt_options).then(function(request) {
- return xhr.parseJson_(request.responseText, opt_options);
- });
- };
- xhr.getBlob = function(url, opt_options) {
- goog.asserts.assert(
- 'Blob' in goog.global, 'getBlob is not supported in this browser.');
- var options = opt_options ? goog.object.clone(opt_options) : {};
- options.responseType = xhr.ResponseType.BLOB;
- return xhr.send('GET', url, null, options).then(function(request) {
- return (request.response);
- });
- };
- xhr.getBytes = function(url, opt_options) {
- goog.asserts.assert(
- !userAgent.IE || userAgent.isDocumentModeOrHigher(9),
- 'getBytes is not supported in this browser.');
- var options = opt_options ? goog.object.clone(opt_options) : {};
- options.responseType = xhr.ResponseType.ARRAYBUFFER;
- return xhr.send('GET', url, null, options).then(function(request) {
-
-
- if (request.response) {
- return new Uint8Array( (request.response));
- }
-
-
-
-
-
- if (goog.global['VBArray']) {
- return new goog.global['VBArray'](request['responseBody']).toArray();
- }
-
-
-
-
- throw new xhr.Error(
- 'getBytes is not supported in this browser.', url, request);
- });
- };
- xhr.postJson = function(url, data, opt_options) {
- return xhr.send('POST', url, data, opt_options).then(function(request) {
- return xhr.parseJson_(request.responseText, opt_options);
- });
- };
- xhr.send = function(method, url, data, opt_options) {
- return new goog.Promise(function(resolve, reject) {
- var options = opt_options || {};
- var timer;
- var request = options.xmlHttpFactory ?
- options.xmlHttpFactory.createInstance() :
- goog.net.XmlHttp();
- try {
- request.open(method, url, true);
- } catch (e) {
-
-
- reject(new xhr.Error('Error opening XHR: ' + e.message, url, request));
- }
-
- request.onreadystatechange = function() {
- if (request.readyState == goog.net.XmlHttp.ReadyState.COMPLETE) {
- goog.global.clearTimeout(timer);
-
-
- if (HttpStatus.isSuccess(request.status) ||
- request.status === 0 && !xhr.isEffectiveSchemeHttp_(url)) {
- resolve(request);
- } else {
- reject(new xhr.HttpError(request.status, url, request));
- }
- }
- };
- request.onerror = function() {
- reject(new xhr.Error('Network error', url, request));
- };
-
- var contentType;
- if (options.headers) {
- for (var key in options.headers) {
- var value = options.headers[key];
- if (goog.isDefAndNotNull(value)) {
- request.setRequestHeader(key, value);
- }
- }
- contentType = options.headers[xhr.CONTENT_TYPE_HEADER];
- }
-
-
- var dataIsFormData =
- (goog.global['FormData'] && (data instanceof goog.global['FormData']));
-
-
-
-
- if (method == 'POST' && contentType === undefined && !dataIsFormData) {
- request.setRequestHeader(xhr.CONTENT_TYPE_HEADER, xhr.FORM_CONTENT_TYPE);
- }
-
-
- if (options.withCredentials) {
- request.withCredentials = options.withCredentials;
- }
-
-
- if (options.responseType) {
- request.responseType = options.responseType;
- }
-
-
- if (options.mimeType) {
- request.overrideMimeType(options.mimeType);
- }
-
- if (options.timeoutMs > 0) {
- timer = goog.global.setTimeout(function() {
-
-
- request.onreadystatechange = goog.nullFunction;
- request.abort();
- reject(new xhr.TimeoutError(url, request));
- }, options.timeoutMs);
- }
-
- try {
- request.send(data);
- } catch (e) {
-
-
- request.onreadystatechange = goog.nullFunction;
- goog.global.clearTimeout(timer);
- reject(new xhr.Error('Error sending XHR: ' + e.message, url, request));
- }
- });
- };
- xhr.isEffectiveSchemeHttp_ = function(url) {
- var scheme = goog.uri.utils.getEffectiveScheme(url);
-
-
- return scheme == 'http' || scheme == 'https' || scheme == '';
- };
- xhr.parseJson_ = function(responseText, options) {
- var prefixStrippedResult = responseText;
- if (options && options.xssiPrefix) {
- prefixStrippedResult =
- xhr.stripXssiPrefix_(options.xssiPrefix, prefixStrippedResult);
- }
- return goog.json.parse(prefixStrippedResult);
- };
- xhr.stripXssiPrefix_ = function(prefix, string) {
- if (goog.string.startsWith(string, prefix)) {
- string = string.substring(prefix.length);
- }
- return string;
- };
- xhr.Error = function(message, url, request) {
- xhr.Error.base(this, 'constructor', message + ', url=' + url);
-
- this.url = url;
-
- this.xhr = request;
- };
- goog.inherits(xhr.Error, goog.debug.Error);
- xhr.Error.prototype.name = 'XhrError';
- xhr.HttpError = function(status, url, request) {
- xhr.HttpError.base(
- this, 'constructor', 'Request Failed, status=' + status, url, request);
-
- this.status = status;
- };
- goog.inherits(xhr.HttpError, xhr.Error);
- xhr.HttpError.prototype.name = 'XhrHttpError';
- xhr.TimeoutError = function(url, request) {
- xhr.TimeoutError.base(this, 'constructor', 'Request timed out', url, request);
- };
- goog.inherits(xhr.TimeoutError, xhr.Error);
- xhr.TimeoutError.prototype.name = 'XhrTimeoutError';
- });
|