From: Mike Thompson Date: Tue, 30 Aug 2011 04:18:35 +0000 (-0700) Subject: add 'cookies' property to wsgi.HTTPRequest X-Git-Tag: v2.1.0~37^2 X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=3627fb540b1cf6e76525a7ff662d131f56150b1b;p=thirdparty%2Ftornado.git add 'cookies' property to wsgi.HTTPRequest --- diff --git a/tornado/wsgi.py b/tornado/wsgi.py index fb8bd4967..e8f878bb4 100644 --- a/tornado/wsgi.py +++ b/tornado/wsgi.py @@ -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