Files
shaka-player/lib/net/http_plugin.js
T
Joey Parrish 2f55d2a3bd Use AbortableOperation in networking
This uses AbortableOperation in all networking, from the scheme
plugins all the way to the request interface.

This also updates all default scheme plugins, docs, and sample code.

Backward compatibility is provided for scheme plugins and the
request API in NetworkingEngine.  This compatibility will be
removed in v2.5.

Two cancelation-related tests have been disabled in
player_integration until the new abort interface has been adopted
in the manifest parsers.

Issue #829

Change-Id: I91c8e6efe97798d111e8ddca5655cddc1f6bcbf3
2018-01-29 19:23:47 +00:00

137 lines
4.2 KiB
JavaScript

/**
* @license
* Copyright 2016 Google Inc.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
goog.provide('shaka.net.HttpPlugin');
goog.require('goog.asserts');
goog.require('shaka.log');
goog.require('shaka.net.NetworkingEngine');
goog.require('shaka.util.AbortableOperation');
goog.require('shaka.util.Error');
goog.require('shaka.util.StringUtils');
/**
* @namespace
* @summary A networking plugin to handle http and https URIs via XHR.
* @param {string} uri
* @param {shakaExtern.Request} request
* @return {!shakaExtern.IAbortableOperation.<shakaExtern.Response>}
* @export
*/
shaka.net.HttpPlugin = function(uri, request) {
var xhr = new shaka.net.HttpPlugin.xhr_();
var promise = new Promise(function(resolve, reject) {
xhr.open(request.method, uri, true);
xhr.responseType = 'arraybuffer';
xhr.timeout = request.retryParameters.timeout;
xhr.withCredentials = request.allowCrossSiteCredentials;
xhr.onload = function(event) {
var target = event.target;
goog.asserts.assert(target, 'XHR onload has no target!');
// Since IE/Edge incorrectly return the header with a leading new line
// character ('\n'), we trim the header here.
var headers = target.getAllResponseHeaders().trim().split('\r\n').reduce(
function(all, part) {
/** @type {!Array.<string>} */
var header = part.split(': ');
all[header[0].toLowerCase()] = header.slice(1).join(': ');
return all;
},
{});
if (target.status >= 200 && target.status <= 299 &&
target.status != 202) {
// Most 2xx HTTP codes are success cases.
if (target.responseURL) {
uri = target.responseURL;
}
/** @type {shakaExtern.Response} */
var response = {
uri: uri,
data: target.response,
headers: headers,
fromCache: !!headers['x-shaka-from-cache']
};
resolve(response);
} else {
var responseText = null;
try {
responseText = shaka.util.StringUtils.fromBytesAutoDetect(
target.response);
} catch (exception) {}
shaka.log.debug('HTTP error text:', responseText);
var severity = target.status == 401 || target.status == 403 ?
shaka.util.Error.Severity.CRITICAL :
shaka.util.Error.Severity.RECOVERABLE;
reject(new shaka.util.Error(
severity,
shaka.util.Error.Category.NETWORK,
shaka.util.Error.Code.BAD_HTTP_STATUS,
uri,
target.status,
responseText,
headers));
}
};
xhr.onerror = function(event) {
reject(new shaka.util.Error(
shaka.util.Error.Severity.RECOVERABLE,
shaka.util.Error.Category.NETWORK,
shaka.util.Error.Code.HTTP_ERROR,
uri));
};
xhr.ontimeout = function(event) {
reject(new shaka.util.Error(
shaka.util.Error.Severity.RECOVERABLE,
shaka.util.Error.Category.NETWORK,
shaka.util.Error.Code.TIMEOUT,
uri));
};
for (var k in request.headers) {
xhr.setRequestHeader(k, request.headers[k]);
}
xhr.send(request.body);
});
return new shaka.util.AbortableOperation(
promise,
() => {
xhr.abort();
return Promise.resolve();
});
};
/**
* Overridden in unit tests, but compiled out in production.
*
* @const {function(new: XMLHttpRequest)}
* @private
*/
shaka.net.HttpPlugin.xhr_ = window.XMLHttpRequest;
shaka.net.NetworkingEngine.registerScheme('http', shaka.net.HttpPlugin,
shaka.net.NetworkingEngine.PluginPriority.FALLBACK);
shaka.net.NetworkingEngine.registerScheme('https', shaka.net.HttpPlugin,
shaka.net.NetworkingEngine.PluginPriority.FALLBACK);