From 11969d79cc1822e04e900b007751b2d5b6d348a2 Mon Sep 17 00:00:00 2001 From: Rizky Maulana Nugraha Date: Tue, 8 Aug 2017 19:54:43 +0700 Subject: [PATCH] Fix handle empty content-disposition headers. --- headless/tasks/utilities.py | 12 +++++++----- 1 file changed, 7 insertions(+), 5 deletions(-) diff --git a/headless/tasks/utilities.py b/headless/tasks/utilities.py index 14a58e1c5..affc552c5 100644 --- a/headless/tasks/utilities.py +++ b/headless/tasks/utilities.py @@ -56,11 +56,13 @@ def download_file(url, direct_access=False, user=None, password=None): f.write(chunk) # get extension - content_disposition = r.headers['content-disposition'] - fname = re.findall("filename=[\'\"]?(.+)[\'\"]", content_disposition) - _, ext = os.path.splitext(fname[0]) - shutil.move(tmpfile, '%s%s' % (tmpfile, ext)) - tmpfile = '%s%s' % (tmpfile, ext) + # Headers is a CaseInsensitive keys. check content-disposition + if 'content-disposition' in r.headers: + content_disposition = r.headers['content-disposition'] + fname = re.findall("filename=[\'\"]?(.+)[\'\"]", content_disposition) + _, ext = os.path.splitext(fname[0]) + shutil.move(tmpfile, '%s%s' % (tmpfile, ext)) + tmpfile = '%s%s' % (tmpfile, ext) return tmpfile elif parsed_uri.scheme == 'file': file_path = urllib.unquote_plus(parsed_uri.path).decode('utf-8')