keep proxies in separate dictionary
authorlkcl <lkcl@teenymac.(none)>
Wed, 14 Jul 2010 18:03:50 +0000 (19:03 +0100)
committerlkcl <lkcl@teenymac.(none)>
Wed, 14 Jul 2010 18:03:50 +0000 (19:03 +0100)
ProxyServer.py
httpd.py
proxyapp.py

index 875c913c83333c577d4aa8fe5e4b29e72171bb93..cc22f4476f9a165db2988ab0ab91c883281aac14 100644 (file)
@@ -127,10 +127,12 @@ class ProxyServerRequestHandler(object):
         self.client = client
         self.hr = args[0]
         print "on_query", reqtype, repr(self.hr.headers), str(self.hr.headers)
-        if not hasattr(self.client, "proxy"):
-            print "new proxy"
-            self.client.proxy = ProxyConnection()
-            self.client.proxy.connect()
+        session = self.client.session 
+        p = self.proxies.get(session, None)
+        if not p:
+            proxy = ProxyConnection()
+            proxy.connect()
+            self.proxies[session] = proxy
 
         multitask.add(self.proxy_relay(reqtype))
 
@@ -146,7 +148,8 @@ class ProxyServerRequestHandler(object):
 
     def proxy_relay(self, reqtype):
 
-        p = self.client.proxy 
+        session = self.client.session 
+        p = self.proxies[session]
 
         # send command
         req = "%s %s %s\n" % (reqtype, self.hr.path, self.hr.request_version)
@@ -258,6 +261,7 @@ class ProxyServerRequestHandler(object):
         if self.hr.close_connection:
             print 'proxy wants client to close_connection'
             try:
+                raise httpd.ConnectionClosed
                 yield self.client.connectionClosed()
             except httpd.ConnectionClosed:
                 print 'close_connection done'
index e5596008db886e92184fdf64e19f84821cfbc19a..e87272ed81412974714a5f564d1e0dd7a947352b 100644 (file)
--- a/httpd.py
+++ b/httpd.py
@@ -63,6 +63,7 @@ throw an exception and display the error message.
 import os, sys, time, struct, socket, traceback, multitask
 import threading, Queue
 import uuid
+import select
 from string import strip
 
 from BaseHTTPServer import BaseHTTPRequestHandler
@@ -310,7 +311,12 @@ class Protocol(object):
             # over to "standard" HTTPRequestHandler, the data's already
             # there.
             print "parseRequests"
-            readok = (yield multitask.readable(self.stream.sock, 5000))
+            try:
+                readok = (yield multitask.readable(self.stream.sock, 5000))
+            except select.error:
+                print "select error: connection closed"
+                raise ConnectionClosed
+
             print "readok", readok
             print
             raw_requestline = (yield self.stream.readline())
@@ -677,6 +683,7 @@ class HTTPServer(object):
                 else:
                     print "cookies", str(msg.response_cookies)
                     session = msg.response_cookies['session'].value
+                    client.session = session
                     name = msg.path
                     print "serverlistener", name
                     if '*' not in self.apps and name not in self.apps:
index baf60271ac25386cbee0823096779073ba3ef248..5d8946176879ee4b2867eb6b08c01ee71587e1e5 100644 (file)
@@ -8,6 +8,7 @@ class MyApp(ProxyServerRequestHandler, BaseApp):
     def __init__(self):
         BaseApp.__init__(self)
         ProxyServerRequestHandler.__init__(self)
+        self.proxies = {}
 
 httpd.set_debug(True)
 agent = HTTPServer()