network_utils.js 2.9 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788
  1. /**
  2. * @licstart The following is the entire license notice for the
  3. * JavaScript code in this page
  4. *
  5. * Copyright 2022 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.js");
  31. var _content_disposition = require("./content_disposition.js");
  32. var _display_utils = require("./display_utils.js");
  33. function validateRangeRequestCapabilities({
  34. getResponseHeader,
  35. isHttp,
  36. rangeChunkSize,
  37. disableRange
  38. }) {
  39. const returnValues = {
  40. allowRangeRequests: false,
  41. suggestedLength: undefined
  42. };
  43. const length = parseInt(getResponseHeader("Content-Length"), 10);
  44. if (!Number.isInteger(length)) {
  45. return returnValues;
  46. }
  47. returnValues.suggestedLength = length;
  48. if (length <= 2 * rangeChunkSize) {
  49. return returnValues;
  50. }
  51. if (disableRange || !isHttp) {
  52. return returnValues;
  53. }
  54. if (getResponseHeader("Accept-Ranges") !== "bytes") {
  55. return returnValues;
  56. }
  57. const contentEncoding = getResponseHeader("Content-Encoding") || "identity";
  58. if (contentEncoding !== "identity") {
  59. return returnValues;
  60. }
  61. returnValues.allowRangeRequests = true;
  62. return returnValues;
  63. }
  64. function extractFilenameFromHeader(getResponseHeader) {
  65. const contentDisposition = getResponseHeader("Content-Disposition");
  66. if (contentDisposition) {
  67. let filename = (0, _content_disposition.getFilenameFromContentDispositionHeader)(contentDisposition);
  68. if (filename.includes("%")) {
  69. try {
  70. filename = decodeURIComponent(filename);
  71. } catch (ex) {}
  72. }
  73. if ((0, _display_utils.isPdfFile)(filename)) {
  74. return filename;
  75. }
  76. }
  77. return null;
  78. }
  79. function createResponseStatusError(status, url) {
  80. if (status === 404 || status === 0 && url.startsWith("file:")) {
  81. return new _util.MissingPDFException('Missing PDF "' + url + '".');
  82. }
  83. return new _util.UnexpectedResponseException(`Unexpected server response (${status}) while retrieving PDF "${url}".`, status);
  84. }
  85. function validateResponseStatus(status) {
  86. return status === 200 || status === 206;
  87. }