Files
shaka-player/lib/util/networking.js
T
Aaron Vaage 25710b6c33 Refactor Segment Request Creation To Be Shared
In four different places in our code base, we did the same
work to create a segment request. The bulk of the work was
based on setting-up the range header.

This CL creates a common utility that will create the request
for us while keeping networking engine as ignorant to segments
as it was before.

Issue #1788

Change-Id: Ie3dc2a99a34b3925fecbfdaae7b1e178a0b7a3b6
2019-03-12 21:18:27 +00:00

57 lines
1.7 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.util.Networking');
/**
* A collection of shared utilities that bridge the gap between our networking
* code and the other parts of our code base. This is to allow
* |shaka.net.NetworkingEngine| to remain general.
*
* @final
*/
shaka.util.Networking = class {
/**
* Create a request message for a segment. Providing |start| and |end|
* will set the byte range. A non-zero start must be provided for |end| to
* be used.
*
* @param {!Array.<string>} uris
* @param {?number} start
* @param {?number} end
* @param {shaka.extern.RetryParameters} retryParameters
* @return {shaka.extern.Request}
*/
static createSegmentRequest(uris, start, end, retryParameters) {
const request = shaka.net.NetworkingEngine.makeRequest(
uris, retryParameters);
// Set the Range header. Note that some web servers don't accept Range
// headers, so don't set one if it's not strictly required.
if (start != null) {
if (end) {
request.headers['Range'] = 'bytes=' + start + '-' + end;
} else {
request.headers['Range'] = 'bytes=' + start + '-';
}
}
return request;
}
};