]> git.ipfire.org Git - thirdparty/tornado.git/commitdiff
add 'cookies' property to wsgi.HTTPRequest 346/head
authorMike Thompson <mike@mikejthompson.com>
Tue, 30 Aug 2011 04:18:35 +0000 (21:18 -0700)
committerMike Thompson <mike@mikejthompson.com>
Tue, 30 Aug 2011 04:18:35 +0000 (21:18 -0700)
tornado/wsgi.py

index fb8bd49678251cb58c1cf4332a81f1c56d7feda5..e8f878bb4b14ece798995a66294c14136a3513ee 100644 (file)
@@ -29,6 +29,7 @@ provides WSGI support in two ways:
   and Tornado handlers in a single server.
 """
 
+import Cookie
 import cgi
 import httplib
 import logging
@@ -159,6 +160,19 @@ class HTTPRequest(object):
         """Returns True if this request supports HTTP/1.1 semantics"""
         return self.version == "HTTP/1.1"
 
+    @property
+    def cookies(self):
+        """A dictionary of Cookie.Morsel objects."""
+        if not hasattr(self, "_cookies"):
+            self._cookies = Cookie.SimpleCookie()
+            if "Cookie" in self.headers:
+                try:
+                    self._cookies.load(
+                        native_str(self.headers["Cookie"]))
+                except Exception:
+                    self._cookies = None
+        return self._cookies
+
     def full_url(self):
         """Reconstructs the full URL for this request."""
         return self.protocol + "://" + self.host + self.uri