network_utils.js 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107
  1. /**
  2. * @licstart The following is the entire license notice for the
  3. * Javascript code in this page
  4. *
  5. * Copyright 2020 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. function validateRangeRequestCapabilities({
  33. getResponseHeader,
  34. isHttp,
  35. rangeChunkSize,
  36. disableRange
  37. }) {
  38. (0, _util.assert)(rangeChunkSize > 0, "Range chunk size must be larger than zero");
  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 (/\.pdf$/i.test(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. }