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