network_utils.js 1.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657
  1. /* Copyright 2017 Mozilla Foundation
  2. *
  3. * Licensed under the Apache License, Version 2.0 (the "License");
  4. * you may not use this file except in compliance with the License.
  5. * You may obtain a copy of the License at
  6. *
  7. * http://www.apache.org/licenses/LICENSE-2.0
  8. *
  9. * Unless required by applicable law or agreed to in writing, software
  10. * distributed under the License is distributed on an "AS IS" BASIS,
  11. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  12. * See the License for the specific language governing permissions and
  13. * limitations under the License.
  14. */
  15. 'use strict';
  16. Object.defineProperty(exports, "__esModule", {
  17. value: true
  18. });
  19. exports.validateRangeRequestCapabilities = undefined;
  20. var _util = require('../shared/util');
  21. function validateRangeRequestCapabilities(_ref) {
  22. var getResponseHeader = _ref.getResponseHeader,
  23. isHttp = _ref.isHttp,
  24. rangeChunkSize = _ref.rangeChunkSize,
  25. disableRange = _ref.disableRange;
  26. (0, _util.assert)(rangeChunkSize > 0);
  27. var returnValues = {
  28. allowRangeRequests: false,
  29. suggestedLength: undefined
  30. };
  31. if (disableRange || !isHttp) {
  32. return returnValues;
  33. }
  34. if (getResponseHeader('Accept-Ranges') !== 'bytes') {
  35. return returnValues;
  36. }
  37. var contentEncoding = getResponseHeader('Content-Encoding') || 'identity';
  38. if (contentEncoding !== 'identity') {
  39. return returnValues;
  40. }
  41. var length = getResponseHeader('Content-Length');
  42. length = parseInt(length, 10);
  43. if (!(0, _util.isInt)(length)) {
  44. return returnValues;
  45. }
  46. returnValues.suggestedLength = length;
  47. if (length <= 2 * rangeChunkSize) {
  48. return returnValues;
  49. }
  50. returnValues.allowRangeRequests = true;
  51. return returnValues;
  52. }
  53. exports.validateRangeRequestCapabilities = validateRangeRequestCapabilities;