From bee7d368dde2c0275459fb61fa9e544faa784a19 Mon Sep 17 00:00:00 2001 From: Thomas Kriechbaumer Date: Thu, 18 Aug 2016 17:33:48 +0200 Subject: refactor http --- mitmproxy/protocol/http.py | 18 +++++++++++------- 1 file changed, 11 insertions(+), 7 deletions(-) diff --git a/mitmproxy/protocol/http.py b/mitmproxy/protocol/http.py index 309a0844..a765d97c 100644 --- a/mitmproxy/protocol/http.py +++ b/mitmproxy/protocol/http.py @@ -158,7 +158,6 @@ class HttpLayer(base.Layer): if self.mode == "regular" and request.first_line_format == "authority": self.handle_regular_mode_connect(request) return - except netlib.exceptions.HttpReadDisconnect: # don't throw an error for disconnects that happen before/between requests. return @@ -170,13 +169,18 @@ class HttpLayer(base.Layer): try: flow = models.HTTPFlow(self.client_conn, self.server_conn, live=self) flow.request = request + # set upstream auth if self.mode == "upstream" and self.config.upstream_auth is not None: flow.request.headers["Proxy-Authorization"] = self.config.upstream_auth self.process_request_hook(flow) if not flow.response: - self.establish_server_connection(flow) + self.establish_server_connection( + flow.request.host, + flow.request.port, + flow.request.scheme + ) self.get_response_from_server(flow) else: # response was set by an inline script. @@ -227,9 +231,9 @@ class HttpLayer(base.Layer): request.body = b"".join(self.read_request_body(request)) return request - def send_error_response(self, code, message): + def send_error_response(self, code, message, headers=None): try: - response = models.make_error_response(code, message) + response = models.make_error_response(code, message, headers) self.send_response(response) except (netlib.exceptions.NetlibException, h2.exceptions.H2Error, exceptions.Http2ProtocolException): self.log(traceback.format_exc(), "debug") @@ -343,9 +347,9 @@ class HttpLayer(base.Layer): flow.response = request_reply return - def establish_server_connection(self, flow): - address = tcp.Address((flow.request.host, flow.request.port)) - tls = (flow.request.scheme == "https") + def establish_server_connection(self, host, port, scheme): + address = tcp.Address((host, port)) + tls = (scheme == "https") if self.mode == "regular" or self.mode == "transparent": # If there's an existing connection that doesn't match our expectations, kill it. -- cgit v1.2.3