network_utils.js 3.0 KB

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