2
0

network_utils.js 2.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667
  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.validateResponseStatus = exports.validateRangeRequestCapabilities = exports.createResponseStatusError = 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, 'Range chunk size must be larger than zero');
  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 = parseInt(getResponseHeader('Content-Length'), 10);
  42. if (!Number.isInteger(length)) {
  43. return returnValues;
  44. }
  45. returnValues.suggestedLength = length;
  46. if (length <= 2 * rangeChunkSize) {
  47. return returnValues;
  48. }
  49. returnValues.allowRangeRequests = true;
  50. return returnValues;
  51. }
  52. function createResponseStatusError(status, url) {
  53. if (status === 404 || status === 0 && /^file:/.test(url)) {
  54. return new _util.MissingPDFException('Missing PDF "' + url + '".');
  55. }
  56. return new _util.UnexpectedResponseException('Unexpected server response (' + status + ') while retrieving PDF "' + url + '".', status);
  57. }
  58. function validateResponseStatus(status) {
  59. return status === 200 || status === 206;
  60. }
  61. exports.createResponseStatusError = createResponseStatusError;
  62. exports.validateRangeRequestCapabilities = validateRangeRequestCapabilities;
  63. exports.validateResponseStatus = validateResponseStatus;