2014-01-30 04:00:13 +00:00
|
|
|
KILL = 0 # const for killed requests
|
|
|
|
|
|
|
|
|
|
|
|
class ConnectionTypeChange(Exception):
|
|
|
|
"""
|
|
|
|
Gets raised if the connetion type has been changed (e.g. after HTTP/1.1 101 Switching Protocols).
|
|
|
|
It's up to the raising ProtocolHandler to specify the new conntype before raising the exception.
|
|
|
|
"""
|
|
|
|
pass
|
|
|
|
|
|
|
|
|
|
|
|
class ProtocolHandler(object):
|
|
|
|
def __init__(self, c):
|
|
|
|
self.c = c
|
2014-01-30 17:56:23 +00:00
|
|
|
"""@type : libmproxy.proxy.ConnectionHandler"""
|
2014-01-30 04:00:13 +00:00
|
|
|
|
|
|
|
def handle_messages(self):
|
|
|
|
"""
|
|
|
|
This method gets called if a client connection has been made. Depending on the proxy settings,
|
|
|
|
a server connection might already exist as well.
|
|
|
|
"""
|
|
|
|
raise NotImplementedError
|
|
|
|
|
|
|
|
def handle_error(self, error):
|
|
|
|
"""
|
|
|
|
This method gets called should there be an uncaught exception during the connection.
|
|
|
|
This might happen outside of handle_messages, e.g. if the initial SSL handshake fails in transparent mode.
|
|
|
|
"""
|
|
|
|
raise NotImplementedError
|
|
|
|
|
2014-01-30 17:56:23 +00:00
|
|
|
from . import http, tcp
|
2014-01-30 04:00:13 +00:00
|
|
|
|
2014-01-30 17:56:23 +00:00
|
|
|
protocols = dict(
|
|
|
|
http = dict(handler=http.HTTPHandler, flow=http.HTTPFlow),
|
|
|
|
tcp = dict(handler=tcp.TCPHandler),
|
|
|
|
)
|
2014-01-30 04:00:13 +00:00
|
|
|
|
|
|
|
|
|
|
|
def _handler(conntype, connection_handler):
|
2014-01-30 17:56:23 +00:00
|
|
|
if conntype in protocols:
|
|
|
|
return protocols[conntype]["handler"](connection_handler)
|
2014-01-30 04:00:13 +00:00
|
|
|
|
|
|
|
raise NotImplementedError
|
|
|
|
|
|
|
|
|
|
|
|
def handle_messages(conntype, connection_handler):
|
|
|
|
return _handler(conntype, connection_handler).handle_messages()
|
|
|
|
|
|
|
|
|
|
|
|
def handle_error(conntype, connection_handler, error):
|
|
|
|
return _handler(conntype, connection_handler).handle_error(error)
|