From 1e276f74c366a992b2d48c8d5f3dda913ca22116 Mon Sep 17 00:00:00 2001 From: Rob Wu Date: Wed, 15 Nov 2017 12:51:07 +0100 Subject: [PATCH] Do not show dialog when content and type are missing The site from #14 appears to respond without Content-Type header, and `Content-Length: 0`. --- extension/background.js | 12 ++++++++---- 1 file changed, 8 insertions(+), 4 deletions(-) diff --git a/extension/background.js b/extension/background.js index a621c57..f39b461 100644 --- a/extension/background.js +++ b/extension/background.js @@ -21,6 +21,9 @@ chrome.webRequest.onHeadersReceived.addListener(async function(details) { var contentDisposition = getHeader(details.responseHeaders, 'content-disposition'); var isSniffingTextPlain = ContentHandlers.isSniffableTextPlain(originalCT.contentType, getHeader(details.responseHeaders, 'content-encoding')); + var contentLength = getHeader(details.responseHeaders, 'content-length'); + if (contentLength !== undefined) contentLength = parseInt(contentLength); + contentLength = contentLength >= 0 ? contentLength : -1; var {mimeType} = originalCT; if (!contentDisposition || !r_contentDispositionAttachment.test(contentDisposition)) { @@ -50,6 +53,11 @@ chrome.webRequest.onHeadersReceived.addListener(async function(details) { // Uncertain whether MIME-type triggers download. Exit now, to be on the safe side. return; } + if (contentLength <= 0 && !mimeType) { + // No specified content type, so defaulting to content sniffing. + // There is however no content to sniff, so do not show a dialog. + return; + } } // Determine file name @@ -61,10 +69,6 @@ chrome.webRequest.onHeadersReceived.addListener(async function(details) { filename = getFilenameFromURL(details.url); } - var contentLength = getHeader(details.responseHeaders, 'content-length'); - if (contentLength !== undefined) contentLength = parseInt(contentLength); - contentLength = contentLength >= 0 ? contentLength : -1; - var guessedMimeType = mime_fromFilename(filename) || mimeType; var isSniffingMimeType = mimeType === 'application/octet-stream' && Prefs.get('octet-sniff-mime');