Source: lib/net/http_plugin_utils.js

  1. /**
  2. * @license
  3. * Copyright 2016 Google Inc.
  4. *
  5. * Licensed under the Apache License, Version 2.0 (the "License");
  6. * you may not use this file except in compliance with the License.
  7. * You may obtain a copy of the License at
  8. *
  9. * http://www.apache.org/licenses/LICENSE-2.0
  10. *
  11. * Unless required by applicable law or agreed to in writing, software
  12. * distributed under the License is distributed on an "AS IS" BASIS,
  13. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  14. * See the License for the specific language governing permissions and
  15. * limitations under the License.
  16. */
  17. goog.provide('shaka.net.HttpPluginUtils');
  18. goog.require('shaka.log');
  19. goog.require('shaka.util.Error');
  20. goog.require('shaka.util.StringUtils');
  21. /**
  22. * @namespace shaka.net.HttpPluginUtils
  23. * @summary A set of http networking utility functions.
  24. * @exportDoc
  25. */
  26. /**
  27. * @param {!Object.<string,string>} headers
  28. * @param {?ArrayBuffer} data
  29. * @param {number} status
  30. * @param {string} uri
  31. * @param {string} responseURL
  32. * @param {shaka.net.NetworkingEngine.RequestType} requestType
  33. * @return {!shakaExtern.Response}
  34. */
  35. shaka.net.HttpPluginUtils.makeResponse =
  36. function(headers, data, status, uri, responseURL, requestType) {
  37. if (status >= 200 && status <= 299 && status != 202) {
  38. // Most 2xx HTTP codes are success cases.
  39. if (responseURL) {
  40. uri = responseURL;
  41. }
  42. /** @type {shakaExtern.Response} */
  43. let response = {
  44. uri: uri,
  45. data: data,
  46. headers: headers,
  47. fromCache: !!headers['x-shaka-from-cache']
  48. };
  49. return response;
  50. } else {
  51. let responseText = null;
  52. try {
  53. responseText = shaka.util.StringUtils.fromBytesAutoDetect(data);
  54. } catch (exception) {}
  55. shaka.log.debug('HTTP error text:', responseText);
  56. let severity = status == 401 || status == 403 ?
  57. shaka.util.Error.Severity.CRITICAL :
  58. shaka.util.Error.Severity.RECOVERABLE;
  59. throw new shaka.util.Error(
  60. severity,
  61. shaka.util.Error.Category.NETWORK,
  62. shaka.util.Error.Code.BAD_HTTP_STATUS,
  63. uri,
  64. status,
  65. responseText,
  66. headers,
  67. requestType);
  68. }
  69. };