Skip to content

Commit

Permalink
Avoid using the Fetch API, in GENERIC builds, for unsupported proto…
Browse files Browse the repository at this point in the history
…cols (issue 10587)
  • Loading branch information
Snuffleupagus committed Feb 27, 2019
1 parent 47a2e01 commit 366178c
Show file tree
Hide file tree
Showing 3 changed files with 36 additions and 8 deletions.
1 change: 1 addition & 0 deletions src/display/display_utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -486,5 +486,6 @@ export {
DOMSVGFactory,
StatTimer,
DummyStatTimer,
isValidFetchUrl,
loadScript,
};
17 changes: 10 additions & 7 deletions src/pdf.js
Original file line number Diff line number Diff line change
Expand Up @@ -38,14 +38,17 @@ if (typeof PDFJSDev === 'undefined' || PDFJSDev.test('GENERIC')) {
pdfjsDisplayAPI.setPDFNetworkStreamFactory((params) => {
return new PDFNodeStream(params);
});
} else if (isFetchSupported()) {
let PDFFetchStream = require('./display/fetch_stream.js').PDFFetchStream;
pdfjsDisplayAPI.setPDFNetworkStreamFactory((params) => {
return new PDFFetchStream(params);
});
} else {
let PDFNetworkStream = require('./display/network.js').PDFNetworkStream;
let PDFFetchStream;
if (isFetchSupported()) {
PDFFetchStream = require('./display/fetch_stream.js').PDFFetchStream;
}
pdfjsDisplayAPI.setPDFNetworkStreamFactory((params) => {
if (PDFFetchStream &&
pdfjsDisplayDisplayUtils.isValidFetchUrl(params.url)) {
return new PDFFetchStream(params);
}
return new PDFNetworkStream(params);
});
}
Expand All @@ -70,8 +73,8 @@ if (typeof PDFJSDev === 'undefined' || PDFJSDev.test('GENERIC')) {
PDFFetchStream = require('./display/fetch_stream.js').PDFFetchStream;
}
pdfjsDisplayAPI.setPDFNetworkStreamFactory((params) => {
if (PDFFetchStream && /^https?:/i.test(params.url)) {
// "fetch" is only supported for http(s), not file/ftp.
if (PDFFetchStream &&
pdfjsDisplayDisplayUtils.isValidFetchUrl(params.url)) {
return new PDFFetchStream(params);
}
return new PDFNetworkStream(params);
Expand Down
26 changes: 25 additions & 1 deletion test/unit/display_utils_spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
*/

import {
DOMSVGFactory, getFilenameFromUrl
DOMSVGFactory, getFilenameFromUrl, isValidFetchUrl
} from '../../src/display/display_utils';
import isNodeJS from '../../src/shared/is_node';

Expand Down Expand Up @@ -94,4 +94,28 @@ describe('display_utils', function() {
expect(result).toEqual(expected);
});
});

describe('isValidFetchUrl', function() {
it('handles invalid Fetch URLs', function() {
expect(isValidFetchUrl(null)).toEqual(false);
expect(isValidFetchUrl(100)).toEqual(false);
expect(isValidFetchUrl('foo')).toEqual(false);
expect(isValidFetchUrl('/foo', 100)).toEqual(false);
});

it('handles relative Fetch URLs', function() {
expect(isValidFetchUrl('/foo', 'file://www.example.com')).toEqual(false);
expect(isValidFetchUrl('/foo', 'http://www.example.com')).toEqual(true);
});

it('handles unsupported Fetch protocols', function() {
expect(isValidFetchUrl('file://www.example.com')).toEqual(false);
expect(isValidFetchUrl('ftp://www.example.com')).toEqual(false);
});

it('handles supported Fetch protocols', function() {
expect(isValidFetchUrl('http://www.example.com')).toEqual(true);
expect(isValidFetchUrl('https://www.example.com')).toEqual(true);
});
});
});

0 comments on commit 366178c

Please sign in to comment.