Source: lib/net/http_plugin_utils.js

  1. /*! @license
  2. * Shaka Player
  3. * Copyright 2016 Google LLC
  4. * SPDX-License-Identifier: Apache-2.0
  5. */
  6. goog.provide('shaka.net.HttpPluginUtils');
  7. goog.require('shaka.log');
  8. goog.require('shaka.util.Error');
  9. goog.require('shaka.util.StringUtils');
  10. goog.requireType('shaka.net.NetworkingEngine');
  11. /**
  12. * @summary A set of http networking utility functions.
  13. * @exportDoc
  14. */
  15. shaka.net.HttpPluginUtils = class {
  16. /**
  17. * @param {!Object.<string,string>} headers
  18. * @param {BufferSource} data
  19. * @param {number} status
  20. * @param {string} uri
  21. * @param {string} responseURL
  22. * @param {shaka.net.NetworkingEngine.RequestType} requestType
  23. * @return {!shaka.extern.Response}
  24. */
  25. static makeResponse(headers, data, status, uri, responseURL, requestType) {
  26. if (status >= 200 && status <= 299 && status != 202) {
  27. // Most 2xx HTTP codes are success cases.
  28. /** @type {shaka.extern.Response} */
  29. const response = {
  30. uri: responseURL || uri,
  31. originalUri: uri,
  32. data: data,
  33. headers: headers,
  34. fromCache: !!headers['x-shaka-from-cache'],
  35. };
  36. return response;
  37. } else {
  38. let responseText = null;
  39. try {
  40. responseText = shaka.util.StringUtils.fromBytesAutoDetect(data);
  41. } catch (exception) {}
  42. shaka.log.debug('HTTP error text:', responseText);
  43. const severity = status == 401 || status == 403 ?
  44. shaka.util.Error.Severity.CRITICAL :
  45. shaka.util.Error.Severity.RECOVERABLE;
  46. throw new shaka.util.Error(
  47. severity,
  48. shaka.util.Error.Category.NETWORK,
  49. shaka.util.Error.Code.BAD_HTTP_STATUS,
  50. uri,
  51. status,
  52. responseText,
  53. headers,
  54. requestType);
  55. }
  56. }
  57. };