2
0

network_utils.js 2.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374
  1. /**
  2. * @licstart The following is the entire license notice for the
  3. * Javascript code in this page
  4. *
  5. * Copyright 2017 Mozilla Foundation
  6. *
  7. * Licensed under the Apache License, Version 2.0 (the "License");
  8. * you may not use this file except in compliance with the License.
  9. * You may obtain a copy of the License at
  10. *
  11. * http://www.apache.org/licenses/LICENSE-2.0
  12. *
  13. * Unless required by applicable law or agreed to in writing, software
  14. * distributed under the License is distributed on an "AS IS" BASIS,
  15. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  16. * See the License for the specific language governing permissions and
  17. * limitations under the License.
  18. *
  19. * @licend The above is the entire license notice for the
  20. * Javascript code in this page
  21. */
  22. 'use strict';
  23. Object.defineProperty(exports, "__esModule", {
  24. value: true
  25. });
  26. exports.validateResponseStatus = exports.validateRangeRequestCapabilities = exports.createResponseStatusError = undefined;
  27. var _util = require('../shared/util');
  28. function validateRangeRequestCapabilities(_ref) {
  29. var getResponseHeader = _ref.getResponseHeader,
  30. isHttp = _ref.isHttp,
  31. rangeChunkSize = _ref.rangeChunkSize,
  32. disableRange = _ref.disableRange;
  33. (0, _util.assert)(rangeChunkSize > 0, 'Range chunk size must be larger than zero');
  34. var returnValues = {
  35. allowRangeRequests: false,
  36. suggestedLength: undefined
  37. };
  38. if (disableRange || !isHttp) {
  39. return returnValues;
  40. }
  41. if (getResponseHeader('Accept-Ranges') !== 'bytes') {
  42. return returnValues;
  43. }
  44. var contentEncoding = getResponseHeader('Content-Encoding') || 'identity';
  45. if (contentEncoding !== 'identity') {
  46. return returnValues;
  47. }
  48. var length = parseInt(getResponseHeader('Content-Length'), 10);
  49. if (!Number.isInteger(length)) {
  50. return returnValues;
  51. }
  52. returnValues.suggestedLength = length;
  53. if (length <= 2 * rangeChunkSize) {
  54. return returnValues;
  55. }
  56. returnValues.allowRangeRequests = true;
  57. return returnValues;
  58. }
  59. function createResponseStatusError(status, url) {
  60. if (status === 404 || status === 0 && /^file:/.test(url)) {
  61. return new _util.MissingPDFException('Missing PDF "' + url + '".');
  62. }
  63. return new _util.UnexpectedResponseException('Unexpected server response (' + status + ') while retrieving PDF "' + url + '".', status);
  64. }
  65. function validateResponseStatus(status) {
  66. return status === 200 || status === 206;
  67. }
  68. exports.createResponseStatusError = createResponseStatusError;
  69. exports.validateRangeRequestCapabilities = validateRangeRequestCapabilities;
  70. exports.validateResponseStatus = validateResponseStatus;