From: lkcl Date: Wed, 14 Jul 2010 18:03:50 +0000 (+0100) Subject: keep proxies in separate dictionary X-Git-Url: https://git.libre-soc.org/?a=commitdiff_plain;h=7e014a63ed4acd2c618e99d29f29018357cee0ab;p=multitaskhttpd.git keep proxies in separate dictionary --- diff --git a/ProxyServer.py b/ProxyServer.py index 875c913..cc22f44 100644 --- a/ProxyServer.py +++ b/ProxyServer.py @@ -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' diff --git a/httpd.py b/httpd.py index e559600..e87272e 100644 --- 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: diff --git a/proxyapp.py b/proxyapp.py index baf6027..5d89461 100644 --- a/proxyapp.py +++ b/proxyapp.py @@ -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()