Skip to content

Commit

Permalink
Fix error on empty response headers
Browse files Browse the repository at this point in the history
Fixes mozilla#18957

mozilla#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](https://developer.mozilla.org/en-US/docs/Glossary/CRLF), or null if no response has been received. If a network error happened, an empty string is returned.
  • Loading branch information
CyberAndrii committed Nov 5, 2024
1 parent cefd1eb commit 5d70d6b
Showing 1 changed file with 11 additions and 10 deletions.
21 changes: 11 additions & 10 deletions src/display/network.js
Original file line number Diff line number Diff line change
Expand Up @@ -273,16 +273,17 @@ class PDFNetworkStreamFullRequestReader {
const fullRequestXhrId = this._fullRequestId;
const fullRequestXhr = this._manager.getRequestXhr(fullRequestXhrId);

const responseHeaders = new Headers(
fullRequestXhr
.getAllResponseHeaders()
.trim()
.split(/[\r\n]+/)
.map(x => {
const [key, ...val] = x.split(": ");
return [key, val.join(": ")];
})
);
const rawResponseHeaders = fullRequestXhr.getAllResponseHeaders();
const responseHeadersMap = rawResponseHeaders
? rawResponseHeaders
.trim()
.split(/[\r\n]+/)
.map(x => {
const [key, ...val] = x.split(": ");
return [key, val.join(": ")];
})
: [];
const responseHeaders = new Headers(responseHeadersMap);

const { allowRangeRequests, suggestedLength } =
validateRangeRequestCapabilities({
Expand Down

0 comments on commit 5d70d6b

Please sign in to comment.