add HTTP Proxy Server (to 127.0.0.1 port 60001)
[multitaskhttpd.git] / httpd.py
index 7b5e2a61c98a7fd60c8eeb386154bbd6a63a15a7..55c10900a79298815fc15bc3854c453680210a8a 100644 (file)
--- a/httpd.py
+++ b/httpd.py
@@ -101,6 +101,39 @@ class MultitaskHTTPRequestHandler(BaseHTTPRequestHandler):
             val = v.OutputString()
             self.send_header("Set-Cookie", val)
 
+def process_cookies(headers, remote, cookie_key="Cookie", add_sess=True):
+    ch = headers.getheaders(cookie_key)
+    print "messageReceived cookieheaders=", '; '.join(ch)
+    res = []
+    for c in ch:
+        c = c.split(";")
+        if len(c) == 0:
+            continue
+        c = map(strip, c)
+        c = filter(lambda x: x, c)
+        res += c
+    has_sess = False
+    response_cookies = SimpleCookie()
+    for c in res:
+        print "found cookie", repr(c)
+        name, value = c.split("=")
+        response_cookies[name] = value
+        #response_cookies[name]['path'] = "/"
+        #response_cookies[name]['domain'] = remote[0]
+        #response_cookies[name]['version'] = 0
+        if name == "session":
+            response_cookies[name]['expires'] = 50000
+            has_sess = True
+    if not add_sess:
+        return response_cookies
+    if not has_sess:
+        response_cookies['session'] = uuid.uuid4().hex
+        response_cookies['session']['expires'] = 50000
+        #response_cookies['session']['path'] = '/'
+        #response_cookies['session']['domain'] = remote[0]
+        #response_cookies['session']['version'] = 0
+    return response_cookies
+
 class ConnectionClosed:
     'raised when the client closed the connection'
 
@@ -467,35 +500,7 @@ class Client(Protocol):
             
     def messageReceived(self, msg):
         if _debug: print 'messageReceived cmd=', msg.command, msg.path
-        ch = msg.headers.getheaders("Cookie")
-        ch += msg.headers.getheaders("cookie")
-        print "messageReceived cookieheaders=", '; '.join(ch)
-        res = []
-        for c in ch:
-            c = c.split(";")
-            if len(c) == 0:
-                continue
-            c = map(strip, c)
-            c = filter(lambda x: x, c)
-            res += c
-        has_sess = False
-        msg.response_cookies = SimpleCookie()
-        for c in res:
-            print "found cookie", repr(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
-        if not has_sess:
-            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
+        msg.response_cookies = process_cookies(msg.headers, self.remote)
 
         if msg.headers.has_key('content-length'):
             max_chunk_size = 10*1024*1024
@@ -672,22 +677,23 @@ class HTTPServer(object):
                         if result is True or result is None:
                             if session not in self.clients: 
                                 self.clients[session] = [inst]; inst._clients=self.clients[session]
-                            self.clients[session].append(client)
-                            msg.wfile.seek(0)
-                            data = msg.wfile.read()
-                            msg.wfile.seek(0)
-                            msg.wfile.truncate()
-                            yield client.writeMessage(data)
-                            if close_connection:
-                                if _debug:
-                                    print 'close_connection requested'
-                                try:
-                                    yield client.connectionClosed()
-                                except ClientClosed:
+                            if result is None:
+                                msg.wfile.seek(0)
+                                data = msg.wfile.read()
+                                msg.wfile.seek(0)
+                                msg.wfile.truncate()
+                                yield client.writeMessage(data)
+                                if close_connection:
                                     if _debug:
-                                        print 'close_connection done'
-                                    pass
+                                        print 'close_connection requested'
+                                    try:
+                                        yield client.connectionClosed()
+                                    except ConnectionClosed:
+                                        if _debug:
+                                            print 'close_connection done'
+                                        pass
                         else: 
+                            print "result", result
                             yield client.rejectConnection(reason='Rejected in onConnect')
         except StopIteration: raise
         except: