Source: lib/util/networking.js

  1. /*! @license
  2. * Shaka Player
  3. * Copyright 2016 Google LLC
  4. * SPDX-License-Identifier: Apache-2.0
  5. */
  6. goog.provide('shaka.util.Networking');
  7. goog.require('shaka.net.NetworkingEngine');
  8. /**
  9. * A collection of shared utilities that bridge the gap between our networking
  10. * code and the other parts of our code base. This is to allow
  11. * |shaka.net.NetworkingEngine| to remain general.
  12. *
  13. * @final
  14. */
  15. shaka.util.Networking = class {
  16. /**
  17. * Create a request message for a segment. Providing |start| and |end|
  18. * will set the byte range. A non-zero start must be provided for |end| to
  19. * be used.
  20. *
  21. * @param {!Array.<string>} uris
  22. * @param {?number} start
  23. * @param {?number} end
  24. * @param {shaka.extern.RetryParameters} retryParameters
  25. * @param {?function(BufferSource):!Promise=} streamDataCallback
  26. * @return {shaka.extern.Request}
  27. */
  28. static createSegmentRequest(uris, start, end, retryParameters,
  29. streamDataCallback) {
  30. const request = shaka.net.NetworkingEngine.makeRequest(
  31. uris, retryParameters, streamDataCallback);
  32. if (start == 0 && end == null) {
  33. // This is a request for the entire segment. The Range header is not
  34. // required. Note that some web servers don't accept Range headers, so
  35. // don't set one if it's not strictly required.
  36. } else {
  37. if (end) {
  38. request.headers['Range'] = 'bytes=' + start + '-' + end;
  39. } else {
  40. request.headers['Range'] = 'bytes=' + start + '-';
  41. }
  42. }
  43. return request;
  44. }
  45. };