Merge pull request #9447 from Rob--W/crx-fetch-expose-headers

Expose some headers to fetch requests from the PDF.js Chrome extension
This commit is contained in:
Brendan Dahl 2018-02-06 16:58:44 -08:00 committed by GitHub
commit 43f1f96b10
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

View File

@ -117,8 +117,17 @@ chrome.runtime.onConnect.addListener(function onReceivePort(port) {
delete g_referrers[tabId][frameId];
}
chrome.webRequest.onBeforeSendHeaders.removeListener(onBeforeSendHeaders);
chrome.webRequest.onHeadersReceived.removeListener(exposeOnHeadersReceived);
});
// Expose some response headers for fetch API calls from PDF.js;
// This is a work-around for https://crbug.com/784528
chrome.webRequest.onHeadersReceived.addListener(exposeOnHeadersReceived, {
urls: ['https://*/*'],
types: ['xmlhttprequest'],
tabId: tabId,
}, ['blocking', 'responseHeaders']);
function onBeforeSendHeaders(details) {
if (details.frameId !== frameId) {
return;
@ -137,4 +146,22 @@ chrome.runtime.onConnect.addListener(function onReceivePort(port) {
refererHeader.value = referer;
return { requestHeaders: headers, };
}
function exposeOnHeadersReceived(details) {
if (details.frameId !== frameId) {
return;
}
var headers = details.responseHeaders;
var aceh = getHeaderFromHeaders(headers, 'access-control-expose-headers');
// List of headers that PDF.js uses in src/display/network_utils.js
var acehValue =
'accept-ranges,content-encoding,content-length,content-disposition';
if (aceh) {
aceh.value += ',' + acehValue;
} else {
aceh = { name: 'Access-Control-Expose-Headers', value: acehValue, };
headers.push(aceh);
}
return { responseHeaders: headers, };
}
});