1
0
Fork 0
mirror of https://github.com/mozilla/pdf.js.git synced 2025-04-19 22:58:07 +02:00

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._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,
@ -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.

View file

@ -273,11 +273,20 @@ 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 => {
const [key, ...val] = x.split(": ");
return [key, val.join(": ")];
})
);
const { allowRangeRequests, suggestedLength } =
validateRangeRequestCapabilities({
getResponseHeader,
responseHeaders,
isHttp: this._manager.isHttp,
rangeChunkSize: this._rangeChunkSize,
disableRange: this._disableRange,
@ -289,7 +298,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

View file

@ -37,7 +37,7 @@ function createHeaders(isHttp, httpHeaders) {
}
function validateRangeRequestCapabilities({
getResponseHeader,
responseHeaders,
isHttp,
rangeChunkSize,
disableRange,
@ -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;
}
@ -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;
}
@ -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("%")) {

View file

@ -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,
@ -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);