Use response-Headers in the different IPDFStream implementations

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.
This commit is contained in:
Jonas Jenwald 2024-09-02 10:33:50 +02:00
parent 77c7ec6927
commit 840cc5e0d4
5 changed files with 153 additions and 201 deletions

View File

@ -127,11 +127,11 @@ class PDFFetchStreamReader {
this._reader = response.body.getReader(); this._reader = response.body.getReader();
this._headersCapability.resolve(); this._headersCapability.resolve();
const getResponseHeader = name => response.headers.get(name); const responseHeaders = response.headers;
const { allowRangeRequests, suggestedLength } = const { allowRangeRequests, suggestedLength } =
validateRangeRequestCapabilities({ validateRangeRequestCapabilities({
getResponseHeader, responseHeaders,
isHttp: stream.isHttp, isHttp: stream.isHttp,
rangeChunkSize: this._rangeChunkSize, rangeChunkSize: this._rangeChunkSize,
disableRange: this._disableRange, disableRange: this._disableRange,
@ -141,7 +141,7 @@ class PDFFetchStreamReader {
// Setting right content length. // Setting right content length.
this._contentLength = suggestedLength || this._contentLength; 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 // We need to stop reading when range is supported and streaming is
// disabled. // disabled.

View File

@ -273,11 +273,20 @@ class PDFNetworkStreamFullRequestReader {
const fullRequestXhrId = this._fullRequestId; const fullRequestXhrId = this._fullRequestId;
const fullRequestXhr = this._manager.getRequestXhr(fullRequestXhrId); const fullRequestXhr = this._manager.getRequestXhr(fullRequestXhrId);
const getResponseHeader = name => fullRequestXhr.getResponseHeader(name); const responseHeaders = new Headers(
fullRequestXhr
.getAllResponseHeaders()
.trim()
.split(/[\r\n]+/)
.map(x => {
const [key, ...val] = x.split(": ");
return [key, val.join(": ")];
})
);
const { allowRangeRequests, suggestedLength } = const { allowRangeRequests, suggestedLength } =
validateRangeRequestCapabilities({ validateRangeRequestCapabilities({
getResponseHeader, responseHeaders,
isHttp: this._manager.isHttp, isHttp: this._manager.isHttp,
rangeChunkSize: this._rangeChunkSize, rangeChunkSize: this._rangeChunkSize,
disableRange: this._disableRange, disableRange: this._disableRange,
@ -289,7 +298,7 @@ class PDFNetworkStreamFullRequestReader {
// Setting right content length. // Setting right content length.
this._contentLength = suggestedLength || this._contentLength; this._contentLength = suggestedLength || this._contentLength;
this._filename = extractFilenameFromHeader(getResponseHeader); this._filename = extractFilenameFromHeader(responseHeaders);
if (this._isRangeSupported) { if (this._isRangeSupported) {
// NOTE: by cancelling the full request, and then issuing range // NOTE: by cancelling the full request, and then issuing range

View File

@ -37,7 +37,7 @@ function createHeaders(isHttp, httpHeaders) {
} }
function validateRangeRequestCapabilities({ function validateRangeRequestCapabilities({
getResponseHeader, responseHeaders,
isHttp, isHttp,
rangeChunkSize, rangeChunkSize,
disableRange, disableRange,
@ -53,7 +53,7 @@ function validateRangeRequestCapabilities({
suggestedLength: undefined, suggestedLength: undefined,
}; };
const length = parseInt(getResponseHeader("Content-Length"), 10); const length = parseInt(responseHeaders.get("Content-Length"), 10);
if (!Number.isInteger(length)) { if (!Number.isInteger(length)) {
return returnValues; return returnValues;
} }
@ -69,11 +69,11 @@ function validateRangeRequestCapabilities({
if (disableRange || !isHttp) { if (disableRange || !isHttp) {
return returnValues; return returnValues;
} }
if (getResponseHeader("Accept-Ranges") !== "bytes") { if (responseHeaders.get("Accept-Ranges") !== "bytes") {
return returnValues; return returnValues;
} }
const contentEncoding = getResponseHeader("Content-Encoding") || "identity"; const contentEncoding = responseHeaders.get("Content-Encoding") || "identity";
if (contentEncoding !== "identity") { if (contentEncoding !== "identity") {
return returnValues; return returnValues;
} }
@ -82,8 +82,8 @@ function validateRangeRequestCapabilities({
return returnValues; return returnValues;
} }
function extractFilenameFromHeader(getResponseHeader) { function extractFilenameFromHeader(responseHeaders) {
const contentDisposition = getResponseHeader("Content-Disposition"); const contentDisposition = responseHeaders.get("Content-Disposition");
if (contentDisposition) { if (contentDisposition) {
let filename = getFilenameFromContentDispositionHeader(contentDisposition); let filename = getFilenameFromContentDispositionHeader(contentDisposition);
if (filename.includes("%")) { if (filename.includes("%")) {

View File

@ -305,14 +305,11 @@ class PDFNodeStreamFullReader extends BaseFullReader {
this._headersCapability.resolve(); this._headersCapability.resolve();
this._setReadableStream(response); this._setReadableStream(response);
// Make sure that headers name are in lower case, as mentioned const responseHeaders = new Headers(this._readableStream.headers);
// here: https://nodejs.org/api/http.html#http_message_headers.
const getResponseHeader = name =>
this._readableStream.headers[name.toLowerCase()];
const { allowRangeRequests, suggestedLength } = const { allowRangeRequests, suggestedLength } =
validateRangeRequestCapabilities({ validateRangeRequestCapabilities({
getResponseHeader, responseHeaders,
isHttp: stream.isHttp, isHttp: stream.isHttp,
rangeChunkSize: this._rangeChunkSize, rangeChunkSize: this._rangeChunkSize,
disableRange: this._disableRange, disableRange: this._disableRange,
@ -322,7 +319,7 @@ class PDFNodeStreamFullReader extends BaseFullReader {
// Setting right content length. // Setting right content length.
this._contentLength = suggestedLength || this._contentLength; this._contentLength = suggestedLength || this._contentLength;
this._filename = extractFilenameFromHeader(getResponseHeader); this._filename = extractFilenameFromHeader(responseHeaders);
}; };
this._request = createRequest(this._url, headers, handleResponse); this._request = createRequest(this._url, headers, handleResponse);

View File

@ -84,12 +84,9 @@ describe("network_utils", function () {
validateRangeRequestCapabilities({ validateRangeRequestCapabilities({
disableRange: true, disableRange: true,
isHttp: true, isHttp: true,
getResponseHeader: headerName => { responseHeaders: new Headers({
if (headerName === "Content-Length") { "Content-Length": 8,
return 8; }),
}
throw new Error(`Unexpected headerName: ${headerName}`);
},
rangeChunkSize: 64, rangeChunkSize: 64,
}) })
).toEqual({ ).toEqual({
@ -101,12 +98,9 @@ describe("network_utils", function () {
validateRangeRequestCapabilities({ validateRangeRequestCapabilities({
disableRange: false, disableRange: false,
isHttp: false, isHttp: false,
getResponseHeader: headerName => { responseHeaders: new Headers({
if (headerName === "Content-Length") { "Content-Length": 8,
return 8; }),
}
throw new Error(`Unexpected headerName: ${headerName}`);
},
rangeChunkSize: 64, rangeChunkSize: 64,
}) })
).toEqual({ ).toEqual({
@ -120,14 +114,10 @@ describe("network_utils", function () {
validateRangeRequestCapabilities({ validateRangeRequestCapabilities({
disableRange: false, disableRange: false,
isHttp: true, isHttp: true,
getResponseHeader: headerName => { responseHeaders: new Headers({
if (headerName === "Accept-Ranges") { "Accept-Ranges": "none",
return "none"; "Content-Length": 8,
} else if (headerName === "Content-Length") { }),
return 8;
}
throw new Error(`Unexpected headerName: ${headerName}`);
},
rangeChunkSize: 64, rangeChunkSize: 64,
}) })
).toEqual({ ).toEqual({
@ -141,16 +131,11 @@ describe("network_utils", function () {
validateRangeRequestCapabilities({ validateRangeRequestCapabilities({
disableRange: false, disableRange: false,
isHttp: true, isHttp: true,
getResponseHeader: headerName => { responseHeaders: new Headers({
if (headerName === "Accept-Ranges") { "Accept-Ranges": "bytes",
return "bytes"; "Content-Encoding": "gzip",
} else if (headerName === "Content-Encoding") { "Content-Length": 8,
return "gzip"; }),
} else if (headerName === "Content-Length") {
return 8;
}
throw new Error(`Unexpected headerName: ${headerName}`);
},
rangeChunkSize: 64, rangeChunkSize: 64,
}) })
).toEqual({ ).toEqual({
@ -164,16 +149,10 @@ describe("network_utils", function () {
validateRangeRequestCapabilities({ validateRangeRequestCapabilities({
disableRange: false, disableRange: false,
isHttp: true, isHttp: true,
getResponseHeader: headerName => { responseHeaders: new Headers({
if (headerName === "Accept-Ranges") { "Accept-Ranges": "bytes",
return "bytes"; "Content-Length": "eight",
} else if (headerName === "Content-Encoding") { }),
return null;
} else if (headerName === "Content-Length") {
return "eight";
}
throw new Error(`Unexpected headerName: ${headerName}`);
},
rangeChunkSize: 64, rangeChunkSize: 64,
}) })
).toEqual({ ).toEqual({
@ -187,16 +166,10 @@ describe("network_utils", function () {
validateRangeRequestCapabilities({ validateRangeRequestCapabilities({
disableRange: false, disableRange: false,
isHttp: true, isHttp: true,
getResponseHeader: headerName => { responseHeaders: new Headers({
if (headerName === "Accept-Ranges") { "Accept-Ranges": "bytes",
return "bytes"; "Content-Length": 8,
} else if (headerName === "Content-Encoding") { }),
return null;
} else if (headerName === "Content-Length") {
return 8;
}
throw new Error(`Unexpected headerName: ${headerName}`);
},
rangeChunkSize: 64, rangeChunkSize: 64,
}) })
).toEqual({ ).toEqual({
@ -210,16 +183,10 @@ describe("network_utils", function () {
validateRangeRequestCapabilities({ validateRangeRequestCapabilities({
disableRange: false, disableRange: false,
isHttp: true, isHttp: true,
getResponseHeader: headerName => { responseHeaders: new Headers({
if (headerName === "Accept-Ranges") { "Accept-Ranges": "bytes",
return "bytes"; "Content-Length": 8192,
} else if (headerName === "Content-Encoding") { }),
return null;
} else if (headerName === "Content-Length") {
return 8192;
}
throw new Error(`Unexpected headerName: ${headerName}`);
},
rangeChunkSize: 64, rangeChunkSize: 64,
}) })
).toEqual({ ).toEqual({
@ -232,194 +199,173 @@ describe("network_utils", function () {
describe("extractFilenameFromHeader", function () { describe("extractFilenameFromHeader", function () {
it("returns null when content disposition header is blank", function () { it("returns null when content disposition header is blank", function () {
expect( expect(
extractFilenameFromHeader(headerName => { extractFilenameFromHeader(
if (headerName === "Content-Disposition") { new Headers({
return null; // Empty headers.
}
throw new Error(`Unexpected headerName: ${headerName}`);
}) })
)
).toBeNull(); ).toBeNull();
expect( expect(
extractFilenameFromHeader(headerName => { extractFilenameFromHeader(
if (headerName === "Content-Disposition") { new Headers({
return undefined; "Content-Disposition": "",
}
throw new Error(`Unexpected headerName: ${headerName}`);
})
).toBeNull();
expect(
extractFilenameFromHeader(headerName => {
if (headerName === "Content-Disposition") {
return "";
}
throw new Error(`Unexpected headerName: ${headerName}`);
}) })
)
).toBeNull(); ).toBeNull();
}); });
it("gets the filename from the response header", function () { it("gets the filename from the response header", function () {
expect( expect(
extractFilenameFromHeader(headerName => { extractFilenameFromHeader(
if (headerName === "Content-Disposition") { new Headers({
return "inline"; "Content-Disposition": "inline",
}
throw new Error(`Unexpected headerName: ${headerName}`);
}) })
)
).toBeNull(); ).toBeNull();
expect( expect(
extractFilenameFromHeader(headerName => { extractFilenameFromHeader(
if (headerName === "Content-Disposition") { new Headers({
return "attachment"; "Content-Disposition": "attachment",
}
throw new Error(`Unexpected headerName: ${headerName}`);
}) })
)
).toBeNull(); ).toBeNull();
expect( expect(
extractFilenameFromHeader(headerName => { extractFilenameFromHeader(
if (headerName === "Content-Disposition") { new Headers({
return 'attachment; filename="filename.pdf"'; "Content-Disposition": 'attachment; filename="filename.pdf"',
}
throw new Error(`Unexpected headerName: ${headerName}`);
}) })
)
).toEqual("filename.pdf"); ).toEqual("filename.pdf");
expect( expect(
extractFilenameFromHeader(headerName => { extractFilenameFromHeader(
if (headerName === "Content-Disposition") { new Headers({
return 'attachment; filename="filename.pdf and spaces.pdf"'; "Content-Disposition":
} 'attachment; filename="filename.pdf and spaces.pdf"',
throw new Error(`Unexpected headerName: ${headerName}`);
}) })
)
).toEqual("filename.pdf and spaces.pdf"); ).toEqual("filename.pdf and spaces.pdf");
expect( expect(
extractFilenameFromHeader(headerName => { extractFilenameFromHeader(
if (headerName === "Content-Disposition") { new Headers({
return 'attachment; filename="tl;dr.pdf"'; "Content-Disposition": 'attachment; filename="tl;dr.pdf"',
}
throw new Error(`Unexpected headerName: ${headerName}`);
}) })
)
).toEqual("tl;dr.pdf"); ).toEqual("tl;dr.pdf");
expect( expect(
extractFilenameFromHeader(headerName => { extractFilenameFromHeader(
if (headerName === "Content-Disposition") { new Headers({
return "attachment; filename=filename.pdf"; "Content-Disposition": "attachment; filename=filename.pdf",
}
throw new Error(`Unexpected headerName: ${headerName}`);
}) })
)
).toEqual("filename.pdf"); ).toEqual("filename.pdf");
expect( expect(
extractFilenameFromHeader(headerName => { extractFilenameFromHeader(
if (headerName === "Content-Disposition") { new Headers({
return "attachment; filename=filename.pdf someotherparam"; "Content-Disposition":
} "attachment; filename=filename.pdf someotherparam",
throw new Error(`Unexpected headerName: ${headerName}`);
}) })
)
).toEqual("filename.pdf"); ).toEqual("filename.pdf");
expect( expect(
extractFilenameFromHeader(headerName => { extractFilenameFromHeader(
if (headerName === "Content-Disposition") { new Headers({
return 'attachment; filename="%e4%b8%ad%e6%96%87.pdf"'; "Content-Disposition":
} 'attachment; filename="%e4%b8%ad%e6%96%87.pdf"',
throw new Error(`Unexpected headerName: ${headerName}`);
}) })
)
).toEqual("中文.pdf"); ).toEqual("中文.pdf");
expect( expect(
extractFilenameFromHeader(headerName => { extractFilenameFromHeader(
if (headerName === "Content-Disposition") { new Headers({
return 'attachment; filename="100%.pdf"'; "Content-Disposition": 'attachment; filename="100%.pdf"',
}
throw new Error(`Unexpected headerName: ${headerName}`);
}) })
)
).toEqual("100%.pdf"); ).toEqual("100%.pdf");
}); });
it("gets the filename from the response header (RFC 6266)", function () { it("gets the filename from the response header (RFC 6266)", function () {
expect( expect(
extractFilenameFromHeader(headerName => { extractFilenameFromHeader(
if (headerName === "Content-Disposition") { new Headers({
return "attachment; filename*=filename.pdf"; "Content-Disposition": "attachment; filename*=filename.pdf",
}
throw new Error(`Unexpected headerName: ${headerName}`);
}) })
)
).toEqual("filename.pdf"); ).toEqual("filename.pdf");
expect( expect(
extractFilenameFromHeader(headerName => { extractFilenameFromHeader(
if (headerName === "Content-Disposition") { new Headers({
return "attachment; filename*=''filename.pdf"; "Content-Disposition": "attachment; filename*=''filename.pdf",
}
throw new Error(`Unexpected headerName: ${headerName}`);
}) })
)
).toEqual("filename.pdf"); ).toEqual("filename.pdf");
expect( expect(
extractFilenameFromHeader(headerName => { extractFilenameFromHeader(
if (headerName === "Content-Disposition") { new Headers({
return "attachment; filename*=utf-8''filename.pdf"; "Content-Disposition": "attachment; filename*=utf-8''filename.pdf",
}
throw new Error(`Unexpected headerName: ${headerName}`);
}) })
)
).toEqual("filename.pdf"); ).toEqual("filename.pdf");
expect( expect(
extractFilenameFromHeader(headerName => { extractFilenameFromHeader(
if (headerName === "Content-Disposition") { new Headers({
return "attachment; filename=no.pdf; filename*=utf-8''filename.pdf"; "Content-Disposition":
} "attachment; filename=no.pdf; filename*=utf-8''filename.pdf",
throw new Error(`Unexpected headerName: ${headerName}`);
}) })
)
).toEqual("filename.pdf"); ).toEqual("filename.pdf");
expect( expect(
extractFilenameFromHeader(headerName => { extractFilenameFromHeader(
if (headerName === "Content-Disposition") { new Headers({
return "attachment; filename*=utf-8''filename.pdf; filename=no.pdf"; "Content-Disposition":
} "attachment; filename*=utf-8''filename.pdf; filename=no.pdf",
throw new Error(`Unexpected headerName: ${headerName}`);
}) })
)
).toEqual("filename.pdf"); ).toEqual("filename.pdf");
}); });
it("gets the filename from the response header (RFC 2231)", function () { it("gets the filename from the response header (RFC 2231)", function () {
// Tests continuations (RFC 2231 section 3, via RFC 5987 section 3.1). // Tests continuations (RFC 2231 section 3, via RFC 5987 section 3.1).
expect( expect(
extractFilenameFromHeader(headerName => { extractFilenameFromHeader(
if (headerName === "Content-Disposition") { new Headers({
return "attachment; filename*0=filename; filename*1=.pdf"; "Content-Disposition":
} "attachment; filename*0=filename; filename*1=.pdf",
throw new Error(`Unexpected headerName: ${headerName}`);
}) })
)
).toEqual("filename.pdf"); ).toEqual("filename.pdf");
}); });
it("only extracts filename with pdf extension", function () { it("only extracts filename with pdf extension", function () {
expect( expect(
extractFilenameFromHeader(headerName => { extractFilenameFromHeader(
if (headerName === "Content-Disposition") { new Headers({
return 'attachment; filename="filename.png"'; "Content-Disposition": 'attachment; filename="filename.png"',
}
throw new Error(`Unexpected headerName: ${headerName}`);
}) })
)
).toBeNull(); ).toBeNull();
}); });
it("extension validation is case insensitive", function () { it("extension validation is case insensitive", function () {
expect( expect(
extractFilenameFromHeader(headerName => { extractFilenameFromHeader(
if (headerName === "Content-Disposition") { new Headers({
return 'form-data; name="fieldName"; filename="file.PdF"'; "Content-Disposition":
} 'form-data; name="fieldName"; filename="file.PdF"',
throw new Error(`Unexpected headerName: ${headerName}`);
}) })
)
).toEqual("file.PdF"); ).toEqual("file.PdF");
}); });
}); });