summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorCalum Lind <calumlind+deluge@gmail.com>2017-10-29 12:27:26 +0000
committerCalum Lind <calumlind+deluge@gmail.com>2017-10-29 12:34:31 +0000
commitffb8d9f8c3e6fa00659c7613bd295ed1b06097f1 (patch)
tree3a0410fb5f89d7e54cee4e22d1389e90a8f1b333
parent396417bcd045c387fd5ee6cae6a18106324a06a4 (diff)
downloaddeluge-ffb8d9f8c3e6fa00659c7613bd295ed1b06097f1.zip
deluge-ffb8d9f8c3e6fa00659c7613bd295ed1b06097f1.tar.gz
deluge-ffb8d9f8c3e6fa00659c7613bd295ed1b06097f1.tar.bz2
[#3070] Fix httpdownloader error with missing content-disposition filename
The parsing of the content-disposition in httpdownloader was not able to handle missing parameters e.g. "Content-Disposition: attachment" and would result in an IndexError. Added a test for this use-case. Fixed the issue using the cgi.parse_header to extract the parameters.
-rw-r--r--deluge/httpdownloader.py32
1 files changed, 18 insertions, 14 deletions
diff --git a/deluge/httpdownloader.py b/deluge/httpdownloader.py
index 678f781..171e521 100644
--- a/deluge/httpdownloader.py
+++ b/deluge/httpdownloader.py
@@ -38,6 +38,7 @@ from twisted.python.failure import Failure
from twisted.internet import reactor
from deluge.log import setupLogger, LOG as log
from common import get_version
+import cgi
import os.path
import zlib
@@ -86,20 +87,23 @@ class HTTPDownloader(client.HTTPDownloader):
self.decoder = zlib.decompressobj(zlib.MAX_WBITS + 32)
if "content-disposition" in headers and not self.force_filename:
- new_file_name = str(headers["content-disposition"][0]).split(";")[1].split("=")[1]
- new_file_name = sanitise_filename(new_file_name)
- new_file_name = os.path.join(os.path.split(self.fileName)[0], new_file_name)
-
- count = 1
- fileroot = os.path.splitext(new_file_name)[0]
- fileext = os.path.splitext(new_file_name)[1]
- while os.path.isfile(new_file_name):
- # Increment filename if already exists
- new_file_name = "%s-%s%s" % (fileroot, count, fileext)
- count += 1
-
- self.fileName = new_file_name
- self.value = new_file_name
+ content_disp = str(headers["content-disposition"][0])
+ content_disp_params = cgi.parse_header(content_disp)[1]
+ if "filename" in content_disp_params:
+ new_file_name = content_disp_params["filename"]
+ new_file_name = sanitise_filename(new_file_name)
+ new_file_name = os.path.join(os.path.split(self.fileName)[0], new_file_name)
+
+ count = 1
+ fileroot = os.path.splitext(new_file_name)[0]
+ fileext = os.path.splitext(new_file_name)[1]
+ while os.path.isfile(new_file_name):
+ # Increment filename if already exists
+ new_file_name = "%s-%s%s" % (fileroot, count, fileext)
+ count += 1
+
+ self.fileName = new_file_name
+ self.value = new_file_name
elif self.code in (http.MOVED_PERMANENTLY, http.FOUND, http.SEE_OTHER, http.TEMPORARY_REDIRECT):
location = headers["location"][0]