2
0

network_utils.js 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100
  1. /**
  2. * @licstart The following is the entire license notice for the
  3. * Javascript code in this page
  4. *
  5. * Copyright 2019 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.createResponseStatusError = createResponseStatusError;
  27. exports.extractFilenameFromHeader = extractFilenameFromHeader;
  28. exports.validateRangeRequestCapabilities = validateRangeRequestCapabilities;
  29. exports.validateResponseStatus = validateResponseStatus;
  30. var _util = require("../shared/util");
  31. var _content_disposition = require("./content_disposition");
  32. function validateRangeRequestCapabilities(_ref) {
  33. var getResponseHeader = _ref.getResponseHeader,
  34. isHttp = _ref.isHttp,
  35. rangeChunkSize = _ref.rangeChunkSize,
  36. disableRange = _ref.disableRange;
  37. (0, _util.assert)(rangeChunkSize > 0, 'Range chunk size must be larger than zero');
  38. var returnValues = {
  39. allowRangeRequests: false,
  40. suggestedLength: undefined
  41. };
  42. var length = parseInt(getResponseHeader('Content-Length'), 10);
  43. if (!Number.isInteger(length)) {
  44. return returnValues;
  45. }
  46. returnValues.suggestedLength = length;
  47. if (length <= 2 * rangeChunkSize) {
  48. return returnValues;
  49. }
  50. if (disableRange || !isHttp) {
  51. return returnValues;
  52. }
  53. if (getResponseHeader('Accept-Ranges') !== 'bytes') {
  54. return returnValues;
  55. }
  56. var contentEncoding = getResponseHeader('Content-Encoding') || 'identity';
  57. if (contentEncoding !== 'identity') {
  58. return returnValues;
  59. }
  60. returnValues.allowRangeRequests = true;
  61. return returnValues;
  62. }
  63. function extractFilenameFromHeader(getResponseHeader) {
  64. var contentDisposition = getResponseHeader('Content-Disposition');
  65. if (contentDisposition) {
  66. var filename = (0, _content_disposition.getFilenameFromContentDispositionHeader)(contentDisposition);
  67. if (/\.pdf$/i.test(filename)) {
  68. return filename;
  69. }
  70. }
  71. return null;
  72. }
  73. function createResponseStatusError(status, url) {
  74. if (status === 404 || status === 0 && /^file:/.test(url)) {
  75. return new _util.MissingPDFException('Missing PDF "' + url + '".');
  76. }
  77. return new _util.UnexpectedResponseException('Unexpected server response (' + status + ') while retrieving PDF "' + url + '".', status);
  78. }
  79. function validateResponseStatus(status) {
  80. return status === 200 || status === 206;
  81. }