From ae74e1bbd6779357688f77108a504753af3c7c1b Mon Sep 17 00:00:00 2001 From: Rob Wu Date: Sat, 17 Sep 2016 22:21:24 -0700 Subject: [PATCH] Deduct file type from content-disposition A user encountered a response that looks like: URL: some gibberish Headers: Content-Type: application/octet-stream Content-Disposition: attachment; filename="something.pdf" In the Chrome extension, the "attachment" content disposition is almost always ignored (i.e. the PDF Viewer will try to view it anyway). So we need to fall back to the Content-Disposition header if the URL check is inconclusive. --- extensions/chromium/pdfHandler.js | 16 +++++++++++++--- 1 file changed, 13 insertions(+), 3 deletions(-) diff --git a/extensions/chromium/pdfHandler.js b/extensions/chromium/pdfHandler.js index 7f685aacc..d32491f6c 100644 --- a/extensions/chromium/pdfHandler.js +++ b/extensions/chromium/pdfHandler.js @@ -73,9 +73,19 @@ function isPdfFile(details) { var header = getHeaderFromHeaders(details.responseHeaders, 'content-type'); if (header) { var headerValue = header.value.toLowerCase().split(';',1)[0].trim(); - return (headerValue === 'application/pdf' || - headerValue === 'application/octet-stream' && - details.url.toLowerCase().indexOf('.pdf') > 0); + if (headerValue === 'application/pdf') { + return true; + } + if (headerValue === 'application/octet-stream') { + if (details.url.toLowerCase().indexOf('.pdf') > 0) { + return true; + } + var cdHeader = + getHeaderFromHeaders(details.responseHeaders, 'content-disposition'); + if (cdHeader && /\.pdf(["']|$)/i.test(cdHeader.value)) { + return true; + } + } } }