From 18a03c063e4c0f8023c5ae53fdceac6684ab7bff Mon Sep 17 00:00:00 2001 From: Aldo Cortesi Date: Sat, 16 Jun 2012 13:53:24 +1200 Subject: [PATCH] Simplify netlib and improve API. --- libmproxy/netlib.py | 32 ++++++++++++++++++++------------ libmproxy/proxy.py | 9 +-------- 2 files changed, 21 insertions(+), 20 deletions(-) diff --git a/libmproxy/netlib.py b/libmproxy/netlib.py index 65dbee63d..7294cbe0b 100644 --- a/libmproxy/netlib.py +++ b/libmproxy/netlib.py @@ -1,4 +1,4 @@ -import select, socket, threading +import select, socket, threading, traceback from OpenSSL import SSL @@ -124,9 +124,6 @@ class TCPServer: self.server_address = self.socket.getsockname() self.socket.listen(self.request_queue_size) - def fileno(self): - return self.socket.fileno() - def request_thread(self, request, client_address): try: self.handle_connection(request, client_address) @@ -139,8 +136,8 @@ class TCPServer: self.__is_shut_down.clear() try: while not self.__shutdown_request: - r, w, e = select.select([self], [], [], poll_interval) - if self in r: + r, w, e = select.select([self.socket], [], [], poll_interval) + if self.socket in r: try: request, client_address = self.socket.accept() except socket.error: @@ -160,14 +157,25 @@ class TCPServer: def shutdown(self): self.__shutdown_request = True self.__is_shut_down.wait() + self.handle_shutdown() def handle_error(self, request, client_address): - print '-'*40 - print 'Exception happened during processing of request from', - print client_address - import traceback - traceback.print_exc() # XXX But this goes to stderr! - print '-'*40 + """ + Called when handle_connection raises an exception. + """ + print >> sys.stderr, '-'*40 + print >> sys.stderr, "Error processing of request from %s"%client_address + traceback.print_exc() + print >> sys.stderr, '-'*40 def handle_connection(self, request, client_address): + """ + Called after client connection. + """ raise NotImplementedError + + def handle_shutdown(self): + """ + Called after server shutdown. + """ + pass diff --git a/libmproxy/proxy.py b/libmproxy/proxy.py index 9febba72b..54536b39a 100644 --- a/libmproxy/proxy.py +++ b/libmproxy/proxy.py @@ -12,12 +12,6 @@ # # You should have received a copy of the GNU General Public License # along with this program. If not, see . - -""" - A simple proxy server implementation, which always reads all of a server - response into memory, performs some transformation, and then writes it back - to the client. -""" import sys, os, string, socket, time import shutil, tempfile, threading import optparse, SocketServer @@ -535,8 +529,7 @@ class ProxyServer(netlib.TCPServer): def handle_connection(self, request, client_address): ProxyHandler(self.config, request, client_address, self, self.masterq) - def shutdown(self): - netlib.TCPServer.shutdown(self) + def handle_shutdown(self): try: shutil.rmtree(self.certdir) except OSError: