diff options
Diffstat (limited to 'libmproxy/protocol.py')
-rw-r--r-- | libmproxy/protocol.py | 45 |
1 files changed, 26 insertions, 19 deletions
diff --git a/libmproxy/protocol.py b/libmproxy/protocol.py index 5999fb06..cd9b4ce5 100644 --- a/libmproxy/protocol.py +++ b/libmproxy/protocol.py @@ -1,6 +1,7 @@ from libmproxy.proxy import ProxyError, ConnectionHandler from netlib import http + def handle_messages(conntype, connection_handler): handler = None if conntype == "http": @@ -11,10 +12,15 @@ def handle_messages(conntype, connection_handler): return handler.handle_messages() +class ConnectionTypeChange(Exception): + pass + + class ProtocolHandler(object): def __init__(self, c): self.c = c + class HTTPHandler(ProtocolHandler): def handle_messages(self): @@ -35,33 +41,34 @@ class HTTPHandler(ProtocolHandler): method, path, httpversion = http.parse_init(request_line) headers = self.read_headers(authenticate=True) - if self.mode == "regular": + if self.c.mode == "regular": if method == "CONNECT": r = http.parse_init_connect(request_line) if not r: raise ProxyError(400, "Bad HTTP request line: %s"%repr(request_line)) host, port, _ = r - if self.config.forward_proxy: - self.server_conn.wfile.write(request_line) + if self.c.config.forward_proxy: + #FIXME: Treat as request, no custom handling + self.c.server_conn.wfile.write(request_line) for key, value in headers.items(): - self.server_conn.wfile.write("%s: %s\r\n"%(key, value)) - self.server_conn.wfile.write("\r\n") + self.c.server_conn.wfile.write("%s: %s\r\n"%(key, value)) + self.c.server_conn.wfile.write("\r\n") else: - self.server_address = (host, port) - self.establish_server_connection() + self.c.server_address = (host, port) + self.c.establish_server_connection() - self.handle_ssl() - self.mode = "transparent" - return + self.c.handle_ssl() + self.c.determine_conntype("transparent", host, port) + raise ConnectionTypeChange else: r = http.parse_init_proxy(request_line) if not r: raise ProxyError(400, "Bad HTTP request line: %s"%repr(request_line)) method, scheme, host, port, path, httpversion = r - if not self.config.forward_proxy: - if (not self.server_conn) or (self.server_address != (host, port)): - self.server_address = (host, port) - self.establish_server_connection() + if not self.c.config.forward_proxy: + if (not self.c.server_conn) or (self.c.server_address != (host, port)): + self.c.server_address = (host, port) + self.c.establish_server_connection() def get_line(self, fp): """ @@ -73,16 +80,16 @@ class HTTPHandler(ProtocolHandler): return line def read_headers(self, authenticate=False): - headers = http.read_headers(self.client_conn.rfile) + headers = http.read_headers(self.c.client_conn.rfile) if headers is None: raise ProxyError(400, "Invalid headers") - if authenticate and self.config.authenticator: - if self.config.authenticator.authenticate(headers): - self.config.authenticator.clean(headers) + if authenticate and self.c.config.authenticator: + if self.c.config.authenticator.authenticate(headers): + self.c.config.authenticator.clean(headers) else: raise ProxyError( 407, "Proxy Authentication Required", - self.config.authenticator.auth_challenge_headers() + self.c.config.authenticator.auth_challenge_headers() ) return headers
\ No newline at end of file |