Skip to content
This repository has been archived by the owner on Apr 25, 2018. It is now read-only.

Commit

Permalink
Limit PDFFetchStream to http(s) in the Chrome extension
Browse files Browse the repository at this point in the history
The `fetch` API is only supported for http(s), even in Chrome extensions.
Because of this limitation, we should use the XMLHttpRequest API when the
requested URL is not a http(s) URL.

Fixes mozilla#9361
  • Loading branch information
Rob--W authored and ltetzlaff committed Apr 24, 2018
1 parent 0bf8934 commit 61ffdee
Show file tree
Hide file tree
Showing 3 changed files with 51 additions and 17 deletions.
26 changes: 18 additions & 8 deletions src/display/api.js
Original file line number Diff line number Diff line change
Expand Up @@ -83,15 +83,25 @@ if (typeof PDFJSDev !== 'undefined' &&
}) : null;
}

/** @type IPDFStream */
var PDFNetworkStream;
/**
* @typedef {function} IPDFStreamFactory
* @param {DocumentInitParameters} params The document initialization
* parameters. The "url" key is always present.
* @return {IPDFStream}
*/

/** @type IPDFStreamFactory */
var createPDFNetworkStream;

/**
* Sets PDFNetworkStream class to be used as alternative PDF data transport.
* @param {IPDFStream} cls - the PDF data transport.
* Sets the function that instantiates a IPDFStream as an alternative PDF data
* transport.
* @param {IPDFStreamFactory} pdfNetworkStreamFactory - the factory function
* that takes document initialization parameters (including a "url") and returns
* an instance of IPDFStream.
*/
function setPDFNetworkStreamClass(cls) {
PDFNetworkStream = cls;
function setPDFNetworkStreamFactory(pdfNetworkStreamFactory) {
createPDFNetworkStream = pdfNetworkStreamFactory;
}

/**
Expand Down Expand Up @@ -252,7 +262,7 @@ function getDocument(src) {
if (rangeTransport) {
networkStream = new PDFDataTransportStream(params, rangeTransport);
} else if (!params.data) {
networkStream = new PDFNetworkStream(params);
networkStream = createPDFNetworkStream(params);
}

var messageHandler = new MessageHandler(docId, workerId, worker.port);
Expand Down Expand Up @@ -2344,7 +2354,7 @@ export {
PDFWorker,
PDFDocumentProxy,
PDFPageProxy,
setPDFNetworkStreamClass,
setPDFNetworkStreamFactory,
version,
build,
};
34 changes: 27 additions & 7 deletions src/pdf.js
Original file line number Diff line number Diff line change
Expand Up @@ -30,19 +30,39 @@ var pdfjsDisplayDOMUtils = require('./display/dom_utils.js');
var pdfjsDisplaySVG = require('./display/svg.js');

if (typeof PDFJSDev === 'undefined' ||
!PDFJSDev.test('FIREFOX || MOZCENTRAL')) {
!PDFJSDev.test('FIREFOX || MOZCENTRAL || CHROME')) {
const isNodeJS = require('./shared/is_node.js');
if (isNodeJS()) {
var PDFNodeStream = require('./display/node_stream.js').PDFNodeStream;
pdfjsDisplayAPI.setPDFNetworkStreamClass(PDFNodeStream);
let PDFNodeStream = require('./display/node_stream.js').PDFNodeStream;
pdfjsDisplayAPI.setPDFNetworkStreamFactory((params) => {
return new PDFNodeStream(params);
});
} else if (typeof Response !== 'undefined' && 'body' in Response.prototype &&
typeof ReadableStream !== 'undefined') {
var PDFFetchStream = require('./display/fetch_stream.js').PDFFetchStream;
pdfjsDisplayAPI.setPDFNetworkStreamClass(PDFFetchStream);
let PDFFetchStream = require('./display/fetch_stream.js').PDFFetchStream;
pdfjsDisplayAPI.setPDFNetworkStreamFactory((params) => {
return new PDFFetchStream(params);
});
} else {
var PDFNetworkStream = require('./display/network.js').PDFNetworkStream;
pdfjsDisplayAPI.setPDFNetworkStreamClass(PDFNetworkStream);
let PDFNetworkStream = require('./display/network.js').PDFNetworkStream;
pdfjsDisplayAPI.setPDFNetworkStreamFactory((params) => {
return new PDFNetworkStream(params);
});
}
} else if (typeof PDFJSDev !== 'undefined' && PDFJSDev.test('CHROME')) {
let PDFNetworkStream = require('./display/network.js').PDFNetworkStream;
let PDFFetchStream;
if (typeof Response !== 'undefined' && 'body' in Response.prototype &&
typeof ReadableStream !== 'undefined') {
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.
return new PDFFetchStream(params);
}
return new PDFNetworkStream(params);
});
}

exports.PDFJS = pdfjsDisplayGlobal.PDFJS;
Expand Down
8 changes: 6 additions & 2 deletions test/unit/jasmine-boot.js
Original file line number Diff line number Diff line change
Expand Up @@ -85,9 +85,13 @@ function initializePDFJS(callback) {
// Set network stream class for unit tests.
if (typeof Response !== 'undefined' && 'body' in Response.prototype &&
typeof ReadableStream !== 'undefined') {
displayApi.setPDFNetworkStreamClass(PDFFetchStream);
displayApi.setPDFNetworkStreamFactory(function(params) {
return new PDFFetchStream(params);
});
} else {
displayApi.setPDFNetworkStreamClass(PDFNetworkStream);
displayApi.setPDFNetworkStreamFactory(function(params) {
return new PDFNetworkStream(params);
});
}

// Configure the worker.
Expand Down

0 comments on commit 61ffdee

Please sign in to comment.