2
0

network_utils.js 2.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071
  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);
  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. function createResponseStatusError(status, url) {
  54. if (status === 404 || status === 0 && /^file:/.test(url)) {
  55. return new _util.MissingPDFException('Missing PDF "' + url + '".');
  56. }
  57. return new _util.UnexpectedResponseException('Unexpected server response (' + status + ') while retrieving PDF "' + url + '".', status);
  58. }
  59. function validateResponseStatus(status, isHttp) {
  60. if (!isHttp) {
  61. return status === 0;
  62. }
  63. return status === 200 || status === 206;
  64. }
  65. exports.createResponseStatusError = createResponseStatusError;
  66. exports.validateRangeRequestCapabilities = validateRangeRequestCapabilities;
  67. exports.validateResponseStatus = validateResponseStatus;