]> git.ipfire.org Git - thirdparty/tornado.git/commitdiff
Convert dictionaries representing HTTP files to HTTPFile objects, with dictionary... 356/head
authorAlek Storm <alek.storm@gmail.com>
Sat, 10 Sep 2011 23:27:40 +0000 (23:27 +0000)
committerAlek Storm <alek.storm@gmail.com>
Sat, 10 Sep 2011 23:27:40 +0000 (23:27 +0000)
tornado/httpserver.py
tornado/httputil.py
tornado/test/httpserver_test.py

index 4a22ac69f538167a6e8fe3a48468138981950f8c..1d5c08ba6322a81596277bec96f18c43c5b34cc0 100644 (file)
@@ -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
 
index f27148c9ff4992504f9ff1e119b223e6ef70587e..013e4a1dac91089eea3fc9825dd769bd9b273685 100644 (file)
@@ -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:
index 40559cac599608714dcba60afab41466900f025a..5358ee29d12e264f42e8a7573cb82103fc05c774 100644 (file)
@@ -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"]),
                      })