Skip to content

Commit 824a619

Browse files
committed
Fix error on empty response headers
Fixes #18957 #18682 introduced a regression that causes the following error: ``` Uncaught TypeError: Failed to construct 'Headers': Invalid name at PDFNetworkStreamFullRequestReader._onHeadersReceived (pdf.mjs:10214:29) at NetworkManager.onStateChange (pdf.mjs:10103:22) ``` The mentioned PR replaced a call to `getResponseHeader()` with `getAllResponseHeaders()` without handling cases where it may return null or an empty string. Quote from the [docs](https://developer.mozilla.org/en-US/docs/Web/API/XMLHttpRequest/getAllResponseHeaders#return_value): > Returns: > >A string representing all of the response's headers (except those whose field name is Set-Cookie) separated by CRLF, or null if no response has been received. If a network error happened, an empty string is returned. Run the following code and observe the error in the console. Note that the URL is intentionally set to an invalid value to simulate network error ```js <script src="//mozilla.github.io/pdf.js/build/pdf.mjs" type="module"></script> <script type="module"> var url = 'blob:'; pdfjsLib.GlobalWorkerOptions.workerSrc = '//mozilla.github.io/pdf.js/build/pdf.worker.mjs'; var loadingTask = pdfjsLib.getDocument(url); loadingTask.promise .then((pdf) => console.log('PDF loaded')) .catch((reason) => console.error(reason)); </script> ```
1 parent cefd1eb commit 824a619

File tree

1 file changed

+10
-8
lines changed

1 file changed

+10
-8
lines changed

src/display/network.js

+10-8
Original file line numberDiff line numberDiff line change
@@ -273,15 +273,17 @@ class PDFNetworkStreamFullRequestReader {
273273
const fullRequestXhrId = this._fullRequestId;
274274
const fullRequestXhr = this._manager.getRequestXhr(fullRequestXhrId);
275275

276+
const rawResponseHeaders = fullRequestXhr.getAllResponseHeaders();
276277
const responseHeaders = new Headers(
277-
fullRequestXhr
278-
.getAllResponseHeaders()
279-
.trim()
280-
.split(/[\r\n]+/)
281-
.map(x => {
282-
const [key, ...val] = x.split(": ");
283-
return [key, val.join(": ")];
284-
})
278+
rawResponseHeaders
279+
? rawResponseHeaders
280+
.trim()
281+
.split(/[\r\n]+/)
282+
.map(x => {
283+
const [key, ...val] = x.split(": ");
284+
return [key, val.join(": ")];
285+
})
286+
: []
285287
);
286288

287289
const { allowRangeRequests, suggestedLength } =

0 commit comments

Comments
 (0)