sort out cookie session headers
authorLuke Kenneth Casson Leighton <lkcl@lkcl.net>
Mon, 12 Jul 2010 19:52:30 +0000 (20:52 +0100)
committerLuke Kenneth Casson Leighton <lkcl@lkcl.net>
Mon, 12 Jul 2010 19:52:30 +0000 (20:52 +0100)
SimpleAppHTTPServer.py
httpd.py

index a615e2b4798f38f7b4d78ae9e0932753342a1d01..bcf368529539e106b9063c266a4bab42829b4382 100644 (file)
@@ -102,7 +102,7 @@ class SimpleAppHTTPRequestHandler(object):
         hr.send_header("Last-Modified", hr.date_time_string(fs.st_mtime))
         if ka:
             hr.send_header("Connection", "keep-alive")
         hr.send_header("Last-Modified", hr.date_time_string(fs.st_mtime))
         if ka:
             hr.send_header("Connection", "keep-alive")
-        self.client.cookies.add_cookie_header(hr)
+        hr.add_cookies()
         hr.end_headers()
         return f
 
         hr.end_headers()
         return f
 
@@ -145,7 +145,7 @@ class SimpleAppHTTPRequestHandler(object):
         hr.send_header("Content-Length", str(length))
         if ka:
             hr.send_header("Connection", "keep-alive")
         hr.send_header("Content-Length", str(length))
         if ka:
             hr.send_header("Connection", "keep-alive")
-        self.client.cookies.add_cookie_header(hr)
+        hr.add_cookies()
         hr.end_headers()
         return f
 
         hr.end_headers()
         return f
 
index a7cd598dd2aab918da3f696831ad4780fe68ed8c..e85058e6764183d75d904388313412920f08c6f3 100644 (file)
--- a/httpd.py
+++ b/httpd.py
@@ -63,11 +63,13 @@ throw an exception and display the error message.
 import os, sys, time, struct, socket, traceback, multitask
 import threading, Queue
 import uuid
 import os, sys, time, struct, socket, traceback, multitask
 import threading, Queue
 import uuid
+from string import strip
 
 from BaseHTTPServer import BaseHTTPRequestHandler
 from SimpleAppHTTPServer import SimpleAppHTTPRequestHandler
 from cStringIO import StringIO
 from cookielib import parse_ns_headers, CookieJar
 
 from BaseHTTPServer import BaseHTTPRequestHandler
 from SimpleAppHTTPServer import SimpleAppHTTPRequestHandler
 from cStringIO import StringIO
 from cookielib import parse_ns_headers, CookieJar
+from Cookie import SimpleCookie
 
 _debug = False
 
 
 _debug = False
 
@@ -90,14 +92,9 @@ class MultitaskHTTPRequestHandler(BaseHTTPRequestHandler):
     def get_header(self, hdr, default):
         return self.headers.getheader(hdr, default)
 
     def get_header(self, hdr, default):
         return self.headers.getheader(hdr, default)
 
-    def has_header(self, hdr):
-        return False
-
-    def is_unverifiable(self):
-        return False
-
-    def add_unredirected_header(self, name, val):
-        if name == 'Cookie':
+    def add_cookies(self):
+        for k, v in self.response_cookies.items():
+            val = v.OutputString()
             self.send_header("Set-Cookie", val)
 
 class ConnectionClosed:
             self.send_header("Set-Cookie", val)
 
 class ConnectionClosed:
@@ -473,18 +470,30 @@ class Client(Protocol):
     def messageReceived(self, msg):
         if _debug: print 'messageReceived cmd=', msg.command, msg.path
         ch = msg.headers.getheaders("Cookie")
     def messageReceived(self, msg):
         if _debug: print 'messageReceived cmd=', msg.command, msg.path
         ch = msg.headers.getheaders("Cookie")
-        ch = parse_ns_headers(ch)
-        cookies = self.cookies._cookies_from_attrs_set(ch, msg)
+        print "messageReceived cookieheaders=", '; '.join(ch)
+        res = []
+        for c in ch:
+            c = c.split(";")
+            c = map(strip, c)
+            res += c
         has_sess = False
         has_sess = False
-        for c in cookies:
-            self.cookies.set_cookie(c)
-            if c.name == "session":
+        msg.response_cookies = SimpleCookie()
+        for c in res:
+            print "found cookie", str(c)
+            name, value = c.split("=")
+            msg.response_cookies[name] = value
+            msg.response_cookies[name]['path'] = "/"
+            msg.response_cookies[name]['domain'] = self.remote[0]
+            #msg.response_cookies[name]['expires'] = 'None'
+            msg.response_cookies[name]['version'] = 0
+            if name == "session":
                 has_sess = True
                 has_sess = True
-                msg.sess_cookie = c
         if not has_sess:
         if not has_sess:
-            t = ("session", uuid.uuid4().hex, {}, {})
-            msg.sess_cookie = self.cookies._cookie_from_cookie_tuple(t, msg)
-            self.cookies.set_cookie(msg.sess_cookie)
+            msg.response_cookies['session'] = uuid.uuid4().hex
+            #msg.response_cookies['session']['expires'] = 'None'
+            msg.response_cookies['session']['path'] = '/'
+            msg.response_cookies['session']['domain'] = self.remote[0]
+            msg.response_cookies['session']['version'] = 0
         yield self.server.queue.put((self, msg)) # new connection
 
     def accept(self):
         yield self.server.queue.put((self, msg)) # new connection
 
     def accept(self):
@@ -619,14 +628,15 @@ class HttpServer(object):
                     yield client.rejectConnection(reason='Unsupported encoding ' + str(client.objectEncoding) + '. Please use NetConnection.defaultObjectEncoding=ObjectEncoding.AMF0')
                     yield client.connectionClosed()
                 else:
                     yield client.rejectConnection(reason='Unsupported encoding ' + str(client.objectEncoding) + '. Please use NetConnection.defaultObjectEncoding=ObjectEncoding.AMF0')
                     yield client.connectionClosed()
                 else:
-                    print "cookies", str(client.cookies)
-                    session = msg.sess_cookie.value
+                    print "cookies", str(msg.response_cookies)
+                    session = msg.response_cookies['session'].value
                     name, ignore, scope = msg.path.partition('/')
                     if '*' not in self.apps and name not in self.apps:
                         yield client.rejectConnection(reason='Application not found: ' + name)
                     else: # create application instance as needed and add in our list
                         if _debug: print 'name=', name, 'name in apps', str(name in self.apps)
                         app = self.apps[name] if name in self.apps else self.apps['*'] # application class
                     name, ignore, scope = msg.path.partition('/')
                     if '*' not in self.apps and name not in self.apps:
                         yield client.rejectConnection(reason='Application not found: ' + name)
                     else: # create application instance as needed and add in our list
                         if _debug: print 'name=', name, 'name in apps', str(name in self.apps)
                         app = self.apps[name] if name in self.apps else self.apps['*'] # application class
+                        print "clients", self.clients.keys()
                         if session in self.clients: inst = self.clients[session][0]
                         else: inst = app()
                         try: 
                         if session in self.clients: inst = self.clients[session][0]
                         else: inst = app()
                         try: