mirror of
https://github.com/shaka-project/shaka-player.git
synced 2026-06-14 15:56:38 +03:00
6d04f7f225
This change allows to execute request filter on every request attempt. It can be useful i.e. to update credentials when first request fails. New field `attempt` has been added to `shaka.extern.Request` which informs which attempt is currently ongoing. This is needed to not do the unnecessary work multiple times. HTTP 401 Unauthorized and 403 Forbidden are no longer treated as immediate critical errors, as tokens can be updated during retry process.
67 lines
1.8 KiB
JavaScript
67 lines
1.8 KiB
JavaScript
/*! @license
|
|
* Shaka Player
|
|
* Copyright 2016 Google LLC
|
|
* SPDX-License-Identifier: Apache-2.0
|
|
*/
|
|
|
|
goog.provide('shaka.net.HttpPluginUtils');
|
|
|
|
goog.require('goog.asserts');
|
|
goog.require('shaka.log');
|
|
goog.require('shaka.util.Error');
|
|
goog.require('shaka.util.StringUtils');
|
|
goog.requireType('shaka.net.NetworkingEngine');
|
|
|
|
|
|
/**
|
|
* @summary A set of http networking utility functions.
|
|
* @exportDoc
|
|
*/
|
|
shaka.net.HttpPluginUtils = class {
|
|
/**
|
|
* @param {!Object<string, string>} headers
|
|
* @param {BufferSource} data
|
|
* @param {number} status
|
|
* @param {string} uri
|
|
* @param {string} responseURL
|
|
* @param {shaka.extern.Request} request
|
|
* @param {shaka.net.NetworkingEngine.RequestType} requestType
|
|
* @return {!shaka.extern.Response}
|
|
*/
|
|
static makeResponse(headers, data, status, uri, responseURL, request,
|
|
requestType) {
|
|
goog.asserts.assert(data, 'Data should be non-null!');
|
|
|
|
if ((status >= 200 && status <= 299 && status != 202) || status == 304) {
|
|
// Most 2xx HTTP codes are success cases.
|
|
/** @type {shaka.extern.Response} */
|
|
const response = {
|
|
uri: responseURL || uri,
|
|
originalUri: uri,
|
|
data: data,
|
|
status: status,
|
|
headers: headers,
|
|
fromCache: !!headers['x-shaka-from-cache'],
|
|
originalRequest: request,
|
|
};
|
|
return response;
|
|
} else {
|
|
let responseText = null;
|
|
try {
|
|
responseText = shaka.util.StringUtils.fromBytesAutoDetect(data);
|
|
} catch (exception) {}
|
|
shaka.log.debug('HTTP error text:', responseText);
|
|
throw new shaka.util.Error(
|
|
shaka.util.Error.Severity.RECOVERABLE,
|
|
shaka.util.Error.Category.NETWORK,
|
|
shaka.util.Error.Code.BAD_HTTP_STATUS,
|
|
uri,
|
|
status,
|
|
responseText,
|
|
headers,
|
|
requestType,
|
|
responseURL || uri);
|
|
}
|
|
}
|
|
};
|