From ec953d2dac6704d2b91c4bce0df0529f1321741c Mon Sep 17 00:00:00 2001 From: Alek Storm Date: Sat, 10 Sep 2011 23:27:40 +0000 Subject: [PATCH] Convert dictionaries representing HTTP files to HTTPFile objects, with dictionary access for attributes retained for backwards compatibility --- tornado/httpserver.py | 5 +---- tornado/httputil.py | 15 ++++++++++++++- tornado/test/httpserver_test.py | 2 +- 3 files changed, 16 insertions(+), 6 deletions(-) diff --git a/tornado/httpserver.py b/tornado/httpserver.py index 4a22ac69f..1d5c08ba6 100644 --- a/tornado/httpserver.py +++ b/tornado/httpserver.py @@ -452,10 +452,7 @@ class HTTPRequest(object): .. attribute:: files File uploads are available in the files property, which maps file - names to list of files. Each file is a dictionary of the form - {"filename":..., "content_type":..., "body":...}. The content_type - comes from the provided HTTP header and should not be trusted - outright given that it can be easily forged. + names to lists of :class:`HTTPFile`. .. attribute:: connection diff --git a/tornado/httputil.py b/tornado/httputil.py index f27148c9f..013e4a1da 100644 --- a/tornado/httputil.py +++ b/tornado/httputil.py @@ -173,6 +173,19 @@ def url_concat(url, args): url += '&' if ('?' in url) else '?' return url + urllib.urlencode(args) + +class HTTPFile(ObjectDict): + """Represents an HTTP file. For backwards compatibility, its instance + attributes are also accessible as dictionary keys. + + :ivar filename: + :ivar body: + :ivar content_type: The content_type comes from the provided HTTP header + and should not be trusted outright given that it can be easily forged. + """ + pass + + def parse_multipart_form_data(boundary, data, arguments, files): """Parses a multipart/form-data body. @@ -211,7 +224,7 @@ def parse_multipart_form_data(boundary, data, arguments, files): name = disp_params["name"] if disp_params.get("filename"): ctype = headers.get("Content-Type", "application/unknown") - files.setdefault(name, []).append(dict( + files.setdefault(name, []).append(HTTPFile( filename=disp_params["filename"], body=value, content_type=ctype)) else: diff --git a/tornado/test/httpserver_test.py b/tornado/test/httpserver_test.py index 40559cac5..5358ee29d 100644 --- a/tornado/test/httpserver_test.py +++ b/tornado/test/httpserver_test.py @@ -86,7 +86,7 @@ class MultipartTestHandler(RequestHandler): def post(self): self.finish({"header": self.request.headers["X-Header-Encoding-Test"], "argument": self.get_argument("argument"), - "filename": self.request.files["files"][0]["filename"], + "filename": self.request.files["files"][0].filename, "filebody": _unicode(self.request.files["files"][0]["body"]), }) -- 2.47.2