Skip to content

Commit

Permalink
Use response-Headers in the different IPDFStream implementations
Browse files Browse the repository at this point in the history
Given that the `Headers` functionality is now available in all browsers/environments that we support, [see MDN](https://developer.mozilla.org/en-US/docs/Web/API/Headers#browser_compatibility), we can utilize "proper" `Headers` in the helper functions that are used to parse the response.
  • Loading branch information
Snuffleupagus committed Sep 3, 2024
1 parent 082ad95 commit a5dcf82
Show file tree
Hide file tree
Showing 5 changed files with 150 additions and 201 deletions.
6 changes: 3 additions & 3 deletions src/display/fetch_stream.js
Original file line number Diff line number Diff line change
Expand Up @@ -127,11 +127,11 @@ class PDFFetchStreamReader {
this._reader = response.body.getReader();
this._headersCapability.resolve();

const getResponseHeader = name => response.headers.get(name);
const responseHeaders = response.headers;

const { allowRangeRequests, suggestedLength } =
validateRangeRequestCapabilities({
getResponseHeader,
responseHeaders,
isHttp: stream.isHttp,
rangeChunkSize: this._rangeChunkSize,
disableRange: this._disableRange,
Expand All @@ -141,7 +141,7 @@ class PDFFetchStreamReader {
// Setting right content length.
this._contentLength = suggestedLength || this._contentLength;

this._filename = extractFilenameFromHeader(getResponseHeader);
this._filename = extractFilenameFromHeader(responseHeaders);

// We need to stop reading when range is supported and streaming is
// disabled.
Expand Down
12 changes: 9 additions & 3 deletions src/display/network.js
Original file line number Diff line number Diff line change
Expand Up @@ -273,11 +273,17 @@ class PDFNetworkStreamFullRequestReader {
const fullRequestXhrId = this._fullRequestId;
const fullRequestXhr = this._manager.getRequestXhr(fullRequestXhrId);

const getResponseHeader = name => fullRequestXhr.getResponseHeader(name);
const responseHeaders = new Headers(
fullRequestXhr
.getAllResponseHeaders()
.trim()
.split("\r\n")
.map(x => x.split(": ", 2))
);

const { allowRangeRequests, suggestedLength } =
validateRangeRequestCapabilities({
getResponseHeader,
responseHeaders,
isHttp: this._manager.isHttp,
rangeChunkSize: this._rangeChunkSize,
disableRange: this._disableRange,
Expand All @@ -289,7 +295,7 @@ class PDFNetworkStreamFullRequestReader {
// Setting right content length.
this._contentLength = suggestedLength || this._contentLength;

this._filename = extractFilenameFromHeader(getResponseHeader);
this._filename = extractFilenameFromHeader(responseHeaders);

if (this._isRangeSupported) {
// NOTE: by cancelling the full request, and then issuing range
Expand Down
12 changes: 6 additions & 6 deletions src/display/network_utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ function createHeaders(isHttp, httpHeaders) {
}

function validateRangeRequestCapabilities({
getResponseHeader,
responseHeaders,
isHttp,
rangeChunkSize,
disableRange,
Expand All @@ -53,7 +53,7 @@ function validateRangeRequestCapabilities({
suggestedLength: undefined,
};

const length = parseInt(getResponseHeader("Content-Length"), 10);
const length = parseInt(responseHeaders.get("Content-Length"), 10);
if (!Number.isInteger(length)) {
return returnValues;
}
Expand All @@ -69,11 +69,11 @@ function validateRangeRequestCapabilities({
if (disableRange || !isHttp) {
return returnValues;
}
if (getResponseHeader("Accept-Ranges") !== "bytes") {
if (responseHeaders.get("Accept-Ranges") !== "bytes") {
return returnValues;
}

const contentEncoding = getResponseHeader("Content-Encoding") || "identity";
const contentEncoding = responseHeaders.get("Content-Encoding") || "identity";
if (contentEncoding !== "identity") {
return returnValues;
}
Expand All @@ -82,8 +82,8 @@ function validateRangeRequestCapabilities({
return returnValues;
}

function extractFilenameFromHeader(getResponseHeader) {
const contentDisposition = getResponseHeader("Content-Disposition");
function extractFilenameFromHeader(responseHeaders) {
const contentDisposition = responseHeaders.get("Content-Disposition");
if (contentDisposition) {
let filename = getFilenameFromContentDispositionHeader(contentDisposition);
if (filename.includes("%")) {
Expand Down
9 changes: 3 additions & 6 deletions src/display/node_stream.js
Original file line number Diff line number Diff line change
Expand Up @@ -305,14 +305,11 @@ class PDFNodeStreamFullReader extends BaseFullReader {
this._headersCapability.resolve();
this._setReadableStream(response);

// Make sure that headers name are in lower case, as mentioned
// here: https://nodejs.org/api/http.html#http_message_headers.
const getResponseHeader = name =>
this._readableStream.headers[name.toLowerCase()];
const responseHeaders = new Headers(this._readableStream.headers);

const { allowRangeRequests, suggestedLength } =
validateRangeRequestCapabilities({
getResponseHeader,
responseHeaders,
isHttp: stream.isHttp,
rangeChunkSize: this._rangeChunkSize,
disableRange: this._disableRange,
Expand All @@ -322,7 +319,7 @@ class PDFNodeStreamFullReader extends BaseFullReader {
// Setting right content length.
this._contentLength = suggestedLength || this._contentLength;

this._filename = extractFilenameFromHeader(getResponseHeader);
this._filename = extractFilenameFromHeader(responseHeaders);
};

this._request = createRequest(this._url, headers, handleResponse);
Expand Down
Loading

0 comments on commit a5dcf82

Please sign in to comment.