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. status: status,
  34. headers: headers,
  35. fromCache: !!headers['x-shaka-from-cache'],
  36. };
  37. return response;
  38. } else {
  39. let responseText = null;
  40. try {
  41. responseText = shaka.util.StringUtils.fromBytesAutoDetect(data);
  42. } catch (exception) {}
  43. shaka.log.debug('HTTP error text:', responseText);
  44. const severity = status == 401 || status == 403 ?
  45. shaka.util.Error.Severity.CRITICAL :
  46. shaka.util.Error.Severity.RECOVERABLE;
  47. throw new shaka.util.Error(
  48. severity,
  49. shaka.util.Error.Category.NETWORK,
  50. shaka.util.Error.Code.BAD_HTTP_STATUS,
  51. uri,
  52. status,
  53. responseText,
  54. headers,
  55. requestType,
  56. responseURL || uri);
  57. }
  58. }
  59. };