mirror of
https://github.com/shaka-project/shaka-player.git
synced 2026-06-26 17:46:26 +03:00
b4ca4bf51c
This plugin is preferred over the XHR plugin, if available. This plugin requires AbortController, which is only present on Firefox 57 and Edge 16, so this will not be active on every platform. This also adds a simple mock for the Fetch API for use with Jasmine. Closes #829 Change-Id: Ifb79d29334fbfcfd175afe0706da5a3d5e452e2f
76 lines
2.0 KiB
JavaScript
76 lines
2.0 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.HttpPluginUtils');
|
|
|
|
goog.require('shaka.log');
|
|
goog.require('shaka.util.Error');
|
|
goog.require('shaka.util.StringUtils');
|
|
|
|
|
|
/**
|
|
* @namespace shaka.net.HttpPluginUtils
|
|
* @summary A set of http networking utility functions.
|
|
* @exportDoc
|
|
*/
|
|
|
|
|
|
/**
|
|
* @param {!Object<string,string>} headers
|
|
* @param {?ArrayBuffer} data
|
|
* @param {number} status
|
|
* @param {string} uri
|
|
* @param {string} responseURL
|
|
* @return {!shakaExtern.Response}
|
|
*/
|
|
shaka.net.HttpPluginUtils.makeResponse =
|
|
function(headers, data, status, uri, responseURL) {
|
|
if (status >= 200 && status <= 299 && status != 202) {
|
|
// Most 2xx HTTP codes are success cases.
|
|
if (responseURL) {
|
|
uri = responseURL;
|
|
}
|
|
/** @type {shakaExtern.Response} */
|
|
var response = {
|
|
uri: uri,
|
|
data: data,
|
|
headers: headers,
|
|
fromCache: !!headers['x-shaka-from-cache']
|
|
};
|
|
return response;
|
|
} else {
|
|
var responseText = null;
|
|
try {
|
|
responseText = shaka.util.StringUtils.fromBytesAutoDetect(data);
|
|
} catch (exception) {}
|
|
shaka.log.debug('HTTP error text:', responseText);
|
|
|
|
var severity = status == 401 || status == 403 ?
|
|
shaka.util.Error.Severity.CRITICAL :
|
|
shaka.util.Error.Severity.RECOVERABLE;
|
|
throw new shaka.util.Error(
|
|
severity,
|
|
shaka.util.Error.Category.NETWORK,
|
|
shaka.util.Error.Code.BAD_HTTP_STATUS,
|
|
uri,
|
|
status,
|
|
responseText,
|
|
headers);
|
|
}
|
|
};
|
|
|