Files
shaka-player/lib/net/http_plugin_utils.js
T
Jacob Trimble 0154dbc4d4 Convert 'var' to 'let'/'const' (6 of 9).
This is part of a change to convert all usages of 'var' with either
'let' or 'const'.  This takes a conservative approach for 'const' where
it will only be used for aliases and storing the "original" values in
tests.

Change-Id: I475eba0a477d13cd9201c88ad44899d521ad8991
2018-02-20 11:28:02 -08:00

78 lines
2.1 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
* @param {shaka.net.NetworkingEngine.RequestType} requestType
* @return {!shakaExtern.Response}
*/
shaka.net.HttpPluginUtils.makeResponse =
function(headers, data, status, uri, responseURL, requestType) {
if (status >= 200 && status <= 299 && status != 202) {
// Most 2xx HTTP codes are success cases.
if (responseURL) {
uri = responseURL;
}
/** @type {shakaExtern.Response} */
let response = {
uri: uri,
data: data,
headers: headers,
fromCache: !!headers['x-shaka-from-cache']
};
return response;
} else {
let responseText = null;
try {
responseText = shaka.util.StringUtils.fromBytesAutoDetect(data);
} catch (exception) {}
shaka.log.debug('HTTP error text:', responseText);
let 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,
requestType);
}
};