From 1b976c9272089b91c2e5c9be57c9a8446d9a0016 Mon Sep 17 00:00:00 2001 From: kmarty Date: Mon, 1 Dec 2014 16:46:27 +0100 Subject: - Extended Content-Disposition filename support. - Change slashes to underscores in filename --- module/network/HTTPChunk.py | 21 ++++++++++++++++++--- 1 file changed, 18 insertions(+), 3 deletions(-) diff --git a/module/network/HTTPChunk.py b/module/network/HTTPChunk.py index 719c3ed0b..047ccc5a0 100644 --- a/module/network/HTTPChunk.py +++ b/module/network/HTTPChunk.py @@ -22,6 +22,7 @@ from re import search from module.utils import fs_encode import codecs import pycurl +import urllib from HTTPRequest import HTTPRequest @@ -255,9 +256,16 @@ class HTTPChunk(HTTPRequest): if line.startswith("accept-ranges") and "bytes" in line: self.p.chunkSupport = True - if line.startswith("content-disposition") and "filename=" in line: - name = orgline.partition("filename=")[2] - name = name.replace('"', "").replace("'", "").replace(";", "").strip() + if line.startswith("content-disposition") and ("filename=" in line or "filename*=" in line): + if "filename*=" in line: + # extended version according to RFC 6266 and RFC 5987. + encoding = line.partition("*=")[2].partition("''")[0] + name = orgline.partition("''")[2] + name = urllib.unquote(name.encode('ascii')).decode(charEnc(encoding)) + else: + # basic version, US-ASCII only + name = orgline.partition("filename=")[2] + name = name.replace('"', "").replace("'", "").replace(";", "").replace("/", "_").strip() self.p.nameDisposition = name self.log.debug("Content-Disposition: %s" % name) @@ -290,3 +298,10 @@ class HTTPChunk(HTTPRequest): if self.fp: self.fp.close() self.c.close() if hasattr(self, "p"): del self.p + + +def charEnc(enc): + return { + 'utf-8': 'utf_8', + 'iso-8859-1': 'latin_1', + }.get(enc, 'unknown') -- cgit v1.2.3 From 066b118e641ad612106eca4904c307e4c128e00e Mon Sep 17 00:00:00 2001 From: kmarty Date: Mon, 1 Dec 2014 17:50:16 +0100 Subject: Better solution how to change unicode string to string. --- module/network/HTTPChunk.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/module/network/HTTPChunk.py b/module/network/HTTPChunk.py index 047ccc5a0..4ba819615 100644 --- a/module/network/HTTPChunk.py +++ b/module/network/HTTPChunk.py @@ -261,7 +261,7 @@ class HTTPChunk(HTTPRequest): # extended version according to RFC 6266 and RFC 5987. encoding = line.partition("*=")[2].partition("''")[0] name = orgline.partition("''")[2] - name = urllib.unquote(name.encode('ascii')).decode(charEnc(encoding)) + name = urllib.unquote(str(name)).decode(charEnc(encoding)) else: # basic version, US-ASCII only name = orgline.partition("filename=")[2] -- cgit v1.2.3