[rhmessaging-commits] rhmessaging commits: r3203 - in mgmt/trunk: wooly/python/wooly and 1 other directory.

rhmessaging-commits at lists.jboss.org rhmessaging-commits at lists.jboss.org
Mon Mar 23 17:51:14 EDT 2009


Author: justi9
Date: 2009-03-23 17:51:14 -0400 (Mon, 23 Mar 2009)
New Revision: 3203

Modified:
   mgmt/trunk/cumin/python/cumin/__init__.py
   mgmt/trunk/wooly/python/wooly/__init__.py
   mgmt/trunk/wooly/python/wooly/pages.py
   mgmt/trunk/wooly/python/wooly/parameters.py
   mgmt/trunk/wooly/python/wooly/server.py
Log:
Move the user-agent based content-type switching off of Page and to
HtmlPage.  Get_content_type on Page now raises a not implemented
exception.

Refactor WebServer for clarity, and to make sure Session state is
always merged into the response before it is sent.

Adapt http request headers to the session, and store them there.
HtmlPage now uses that data to implement get_content_type.

Eliminate response headers on Session.  We were using them for
cookies, and it's simpler and better to simply have a cookie store on
Session.  Add a Session method to marshal a cookie.



Modified: mgmt/trunk/cumin/python/cumin/__init__.py
===================================================================
--- mgmt/trunk/cumin/python/cumin/__init__.py	2009-03-23 21:46:16 UTC (rev 3202)
+++ mgmt/trunk/cumin/python/cumin/__init__.py	2009-03-23 21:51:14 UTC (rev 3203)
@@ -95,7 +95,7 @@
         if page in self.app.unprotected_pages:
             return True
 
-        id = session.cookies.get("session")
+        id = session.get_cookie("session")
 
         if id is not None:
             usess = self.app.user_sessions_by_id.get(id)

Modified: mgmt/trunk/wooly/python/wooly/__init__.py
===================================================================
--- mgmt/trunk/wooly/python/wooly/__init__.py	2009-03-23 21:46:16 UTC (rev 3202)
+++ mgmt/trunk/wooly/python/wooly/__init__.py	2009-03-23 21:51:14 UTC (rev 3203)
@@ -332,9 +332,6 @@
         self.redirect = Attribute(app, "redirect")
         self.add_attribute(self.redirect)
 
-        self.agent = Attribute(app, "agent")
-        self.add_attribute(self.agent)
-
     def init(self):
         assert not self.sealed
         assert self.parent is None
@@ -381,11 +378,7 @@
         return datetime.utcnow()
 
     def get_content_type(self, session):
-        agent = self.agent.get(session)
-        if self.name.endswith(".html") and (agent.find("MSIE 6") == -1):
-            return self.xhtml_content_type
-        else:
-            return self.html_content_type
+        raise Exception("Not implemented")
 
     def get_cache_control(self, session):
         return None
@@ -469,20 +462,18 @@
 class Session(object):
     http_date_gmt = "%a, %d %b %Y %H:%M:%S GMT"
 
-    def __init__(self, page, response_headers=None):
+    def __init__(self, page):
         assert isinstance(page, Page)
 
         self.app = page.app
         self.page = page
         self.trunk = None
-        self.values = dict()
+
         self.origin = None
-        self.cookies = dict()
 
-        if response_headers is None:
-            self.response_headers = list()
-        else:
-            self.response_headers = response_headers
+        self.values_by_path = dict()
+        self.cookies_by_name = dict() # name => (value, expires)
+        self.headers_by_name = dict()
 
         if self.app.debug:
             self.debug = self.Debug(self)
@@ -512,29 +503,24 @@
     def branch(self):
         session = Session(self.page)
         session.trunk = self
-        session.response_headers = list(self.response_headers)
         return session
 
-    def add_header(self, name, value):
-        self.response_headers.append((name, value))
+    def get_cookie(self, name):
+        try:
+            return self.cookies_by_name[name][0]
+        except KeyError:
+            pass
 
     def set_cookie(self, name, value, expires=None):
-        crumbs = list()
-        crumbs.append("%s=%s" % (name, value))
+        self.cookies_by_name[name] = (value, expires)
 
-        if expires:
-            when = expires.strftime(self.http_date_gmt)
-            crumbs.append("expires=%s" % when)
-
-        self.add_header("Set-Cookie", "; ".join(crumbs))
-
     def expire_cookie(self, name):
         self.set_cookie(name, "", datetime(*gmtime(1)[0:6]))
 
     # This is the biggest hotspot in cumin-bench profiling
     def get(self, key):
-        if key in self.values:
-            value = self.values[key]
+        if key in self.values_by_path:
+            value = self.values_by_path[key]
         elif self.trunk:
             value = self.trunk.get(key)
         else:
@@ -546,13 +532,13 @@
         #if key.startswith("server.vhost.queue.edit.durable"):
         #    raise Exception()
 
-        self.values[key] = value
+        self.values_by_path[key] = value
 
         return value
 
     def unset(self, key):
-        if key in self.values:
-            del self.values[key]
+        if key in self.values_by_path:
+            del self.values_by_path[key]
 
     def marshal(self):
         page = self.page.name
@@ -588,8 +574,8 @@
                 # Inlined below saving about a second in a
                 # 1000-hit profile
 
-                if key in self.values:
-                    value = self.values[key]
+                if key in self.values_by_path:
+                    value = self.values_by_path[key]
                 elif self.trunk:
                     value = self.trunk.get(key)
                 else:
@@ -646,17 +632,23 @@
             except ValueError:
                 pass
 
-    def unmarshal_cookies(self, string):
-        if string is not None:
-            cookies = dict()
-            crumbs = string.split(";")
+    def marshal_cookie(self, name):
+        value, expires = self.cookies_by_name[name]
 
-            for crumb in crumbs:
-                name, value = crumb.split("=", 1)
-                cookies[name.strip()] = value.strip()
+        crumbs = list()
+        crumbs.append("%s=%s" % (name, value))
 
-        self.cookies = cookies
+        if expires:
+            when = expires.strftime(self.http_date_gmt)
+            crumbs.append("expires=%s" % when)
 
+        return "; ".join(crumbs)
+
+    def unmarshal_cookies(self, string):
+        for crumb in string.split(";"):
+            name, value = crumb.split("=", 1)
+            self.cookies_by_name[name.strip()] = (value.strip(), None)
+
     def set_origin(self, origin):
         self.origin = self.fix_origin(origin)
 
@@ -835,7 +827,7 @@
         self.stack = stack
         self.widget = widget
         self.session = session
-        self.session_values = copy(session.values)
+        self.session_values = copy(session.values_by_path)
         self.object = object
 
         self.caller = None

Modified: mgmt/trunk/wooly/python/wooly/pages.py
===================================================================
--- mgmt/trunk/wooly/python/wooly/pages.py	2009-03-23 21:46:16 UTC (rev 3202)
+++ mgmt/trunk/wooly/python/wooly/pages.py	2009-03-23 21:51:14 UTC (rev 3203)
@@ -91,6 +91,16 @@
         self.javascript_page = JavascriptPage(app, name + ".js", self)
         self.app.add_page(self.javascript_page)
 
+    def get_content_type(self, session):
+        values = session.headers_by_name.get("user_agent")
+
+        if values and values[0].find("MSIE 6") != -1:
+            content_type = self.html_content_type
+        else:
+            content_type = self.xhtml_content_type
+
+        return content_type
+
     def enable_update(self, session, widget):
         self.updates.get(session).append(widget)
 

Modified: mgmt/trunk/wooly/python/wooly/parameters.py
===================================================================
--- mgmt/trunk/wooly/python/wooly/parameters.py	2009-03-23 21:46:16 UTC (rev 3202)
+++ mgmt/trunk/wooly/python/wooly/parameters.py	2009-03-23 21:51:14 UTC (rev 3203)
@@ -94,7 +94,7 @@
 
 class VoidBooleanParameter(Parameter):
     def get(self, session):
-        return self.path in session.values
+        return self.path in session.values_by_path
 
     def set(self, session, value):
         key = self.path

Modified: mgmt/trunk/wooly/python/wooly/server.py
===================================================================
--- mgmt/trunk/wooly/python/wooly/server.py	2009-03-23 21:46:16 UTC (rev 3202)
+++ mgmt/trunk/wooly/python/wooly/server.py	2009-03-23 21:51:14 UTC (rev 3203)
@@ -17,7 +17,7 @@
         self.port = port
 
         fqaddr = (self.addr, self.port)
-        apps = [("", self.service)]
+        apps = [("", self.service_request)]
         self.__server = CherryPyWSGIServer(fqaddr, apps)
 
     def set_ssl_cert_path(self, path):
@@ -35,7 +35,7 @@
     def authorized(self, session):
         return False
 
-    def service(self, env, respond):
+    def service_request(self, env, response):
         headers = list()
 
         name = env["PATH_INFO"][1:]
@@ -43,51 +43,29 @@
         try:
             page = self.app.pages_by_name[name]
         except KeyError:
-            return self.not_found(respond, headers)
+            return self.send_not_found(response, headers)
 
-        session = Session(page, headers)
-        session.unmarshal_url_vars(env["QUERY_STRING"])
+        session = Session(page)
 
-        if "HTTP_USER_AGENT" in env:
-            page.set_agent(session, env["HTTP_USER_AGENT"])
+        self.adapt_request_environment(env, session)
 
-        if env["REQUEST_METHOD"] == "POST":
-            if env["CONTENT_TYPE"] == "application/x-www-form-urlencoded":
-                length = int(env["CONTENT_LENGTH"])
-                vars = env["wsgi.input"].read(length)
-            else:
-                raise Exception("Content type '%s' is not supported" \
-                                % env["CONTENT_TYPE"])
-
-            if vars:
-                session.unmarshal_url_vars(vars, "&")
-
-        try:
-            session.unmarshal_cookies(env["HTTP_COOKIE"])
-        except KeyError:
-            pass
-
-        if "HTTP_REFERER" in env:
-            # XXX set this on page instead
-            session.set_origin(env["HTTP_REFERER"])
-
         if not self.authorized(session):
             url = page.get_redirect_url(session)
 
             if url:
-                return self.redirect(respond, headers, url)
+                return self.send_redirect(response, headers, session, url)
             else:
-                return self.unauthorized(respond, headers)
+                return self.send_unauthorized(response, headers, session)
 
         try:
             page.process(session)
         except:
-            return self.error(respond, session)
+            return self.send_error(response, headers, session)
 
         url = page.get_redirect_url(session)
 
         if url:
-            return self.redirect(respond, headers, url)
+            return self.send_redirect(response, headers, session, url)
 
         ims = env.get("HTTP_IF_MODIFIED_SINCE")
         modified = page.get_last_modified(session).replace(microsecond=0)
@@ -96,11 +74,11 @@
             since = datetime(*strptime(str(ims), self.http_date)[0:6])
 
             if modified <= since:
-                return self.not_modified(respond, headers)
+                return self.send_not_modified(response, headers, session)
 
         if modified:
-            ts = modified.strftime(self.http_date_gmt)
-            headers.append(("Last-Modified", ts))
+            when = modified.strftime(self.http_date_gmt)
+            headers.append(("Last-Modified", when))
 
         type = page.get_content_type(session)
 
@@ -113,51 +91,88 @@
             headers.append(("Cache-Control", cache))
 
         try:
-            response = page.render(session)
+            content = page.render(session)
         except:
-            return self.error(respond, session)
+            return self.send_error(response, headers, session)
 
-        headers.append(("Content-Length", str(len(response))))
+        headers.append(("Content-Length", str(len(content))))
 
-        respond("200 OK", headers)
+        self.send_response(response, headers, session, "200 OK")
 
         page.save_session(session)
 
-        return (response,)
+        return (content,)
 
-    def message(self, respond, headers, message):
+    def adapt_request_environment(self, env, session):
+        session.unmarshal_url_vars(env["QUERY_STRING"])
+
+        if env["REQUEST_METHOD"] == "POST":
+            content_type = env["CONTENT_TYPE"]
+
+            if content_type == "application/x-www-form-urlencoded":
+                length = int(env["CONTENT_LENGTH"])
+                vars = env["wsgi.input"].read(length)
+            else:
+                raise Exception \
+                    ("Content type '%s' is not supported" % content_type)
+
+            if vars:
+                session.unmarshal_url_vars(vars, "&")
+
+        try:
+            session.unmarshal_cookies(env["HTTP_COOKIE"])
+        except KeyError:
+            pass
+
+        # XXX set this on page instead
+        session.set_origin(env.get("HTTP_REFERER"))
+
+        for key, value in env.iteritems():
+            if key.startswith("HTTP_"):
+                name = key[5:].lower()
+                session.headers_by_name[name] = value
+
+    def send_not_found(self, response, headers):
+        response(message, headers)
+        return (message,)
+
+    def send_response(self, response, headers, session, message):
+        for name in session.cookies_by_name:
+            headers.append(("Set-Cookie", session.marshal_cookie(name)))
+
+        response(message, headers)
+
+    def send_message(self, response, headers, session, message):
         headers.append(("Content-Length", str(len(message))))
         headers.append(("Content-Type", "text/plain"))
 
-        respond(message, headers)
+        self.send_response(response, headers, session, message)
 
         return (message,)
 
-    def not_found(self, respond, headers):
-        return self.message(respond, headers, "404 Not Found")
+    def send_unauthorized(self, response, headers, session):
+        return self.send_message \
+            (response, headers, session, "401 Unauthorized")
 
-    def unauthorized(self, respond, headers):
-        return self.message(respond, headers, "401 Unauthorized")
-
-    def redirect(self, respond, headers, url):
+    def send_redirect(self, response, headers, session, url):
         headers.append(("Location", url))
         headers.append(("Content-Length", "0"))
 
-        respond("303 See Other", headers)
+        self.send_response(response, headers, session, "303 See Other")
 
         return ()
 
-    def not_modified(self, respond, headers):
+    def send_not_modified(self, response, headers, session):
         headers.append(("Content-Length", "0"))
 
-        respond("304 Not Modified", headers)
+        self.send_response(response, headers, session, "304 Not Modified")
 
         return ()
 
-    def error(self, respond, session):
-        session.add_header("Content-Type", "text/plain")
+    def send_error(self, response, headers, session):
+        headers.append(("Content-Type", "text/plain"))
 
-        respond("500 Error", session.response_headers)
+        self.send_response(response, headers, session)
 
         writer = Writer()
         writer.write("APPLICATION ERROR\n")




More information about the rhmessaging-commits mailing list