diff --git a/libmproxy/console/__init__.py b/libmproxy/console/__init__.py index 24b3ddae02..4f2986984c 100644 --- a/libmproxy/console/__init__.py +++ b/libmproxy/console/__init__.py @@ -568,13 +568,11 @@ def run(self): self.ui.set_terminal_properties(256) self.ui.register_palette(self.palette) self.flow_list_walker = flowlist.FlowListWalker(self, self.state) - self.view = None self.statusbar = None self.header = None self.body = None self.help_context = None - self.prompting = False self.onekey = False @@ -597,7 +595,6 @@ def run(self): print >> sys.stderr, traceback.format_exc() print >> sys.stderr, "mitmproxy has crashed!" print >> sys.stderr, "Please lodge a bug report at: https://github.com/mitmproxy/mitmproxy" - # If True, quit just pops out to flow list view. print >> sys.stderr, "Shutting down..." sys.stderr.flush() self.shutdown() @@ -1028,7 +1025,6 @@ def add_event(self, e, level="info"): e = urwid.Text(str(e)) elif level == "error": e = urwid.Text(("error", str(e))) - self.eventlist.append(e) if len(self.eventlist) > EVENTLOG_SIZE: self.eventlist.pop(0) diff --git a/libmproxy/flow.py b/libmproxy/flow.py index 4078663137..f4c24f28fc 100644 --- a/libmproxy/flow.py +++ b/libmproxy/flow.py @@ -5,7 +5,7 @@ import base64 import hashlib, Cookie, cookielib, re, threading import os -from flask import request +import flask import requests import tnetstring, filt, script from netlib import odict, wsgi @@ -455,7 +455,7 @@ def start_app(self, host, port, external): else: @app.mapp.before_request def patch_environ(*args, **kwargs): - request.environ["mitmproxy.master"] = self + flask.request.environ["mitmproxy.master"] = self # the only absurd way to shut down a flask/werkzeug server. # http://flask.pocoo.org/snippets/67/ @@ -464,7 +464,7 @@ def patch_environ(*args, **kwargs): @app.mapp.route('/shutdown/') def shutdown(secret): if secret == shutdown_secret: - request.environ.get('werkzeug.server.shutdown')() + flask.request.environ.get('werkzeug.server.shutdown')() # Workaround: Monkey-patch shutdown function to stop the app. # Improve this when we switch flask werkzeug for something useful. diff --git a/libmproxy/protocol/http.py b/libmproxy/protocol/http.py index 06465fc4ef..e91e865a22 100644 --- a/libmproxy/protocol/http.py +++ b/libmproxy/protocol/http.py @@ -877,11 +877,17 @@ def get_response_from_server(self, request): def handle_flow(self): flow = HTTPFlow(self.c.client_conn, self.c.server_conn, self.change_server) try: - flow.request = HTTPRequest.from_stream(self.c.client_conn.rfile, + req = HTTPRequest.from_stream(self.c.client_conn.rfile, body_size_limit=self.c.config.body_size_limit) - self.c.log("request", [flow.request._assemble_first_line(flow.request.form_in)]) - self.process_request(flow.request) - + self.c.log("request", [req._assemble_first_line(req.form_in)]) + self.process_request(flow, req) + + # Be careful NOT to assign the request to the flow before + # process_request completes. This is because the call can raise an + # exception. If the requets object is already attached, this results + # in an Error object that has an attached request that has not been + # sent through to the Master. + flow.request = req request_reply = self.c.channel.ask("request", flow.request) flow.server_conn = self.c.server_conn @@ -1004,7 +1010,7 @@ def ssl_upgrade(self): self.c.log("Upgrade to SSL completed.") raise ConnectionTypeChange - def process_request(self, request): + def process_request(self, flow, request): if self.c.mode == "regular": self.authenticate(request) if request.form_in == "authority" and self.c.client_conn.ssl_established: @@ -1015,7 +1021,7 @@ def process_request(self, request): directly_addressed_at_mitmproxy = (self.c.mode == "regular" and not self.c.config.forward_proxy) if directly_addressed_at_mitmproxy: self.c.set_server_address((request.host, request.port), AddressPriority.FROM_PROTOCOL) - request.flow.server_conn = self.c.server_conn # Update server_conn attribute on the flow + flow.server_conn = self.c.server_conn # Update server_conn attribute on the flow self.c.client_conn.wfile.write( 'HTTP/1.1 200 Connection established\r\n' + ('Proxy-agent: %s\r\n' % self.c.server_version) + @@ -1033,7 +1039,7 @@ def process_request(self, request): if not self.c.config.forward_proxy: request.form_out = "origin" self.c.set_server_address((request.host, request.port), AddressPriority.FROM_PROTOCOL) - request.flow.server_conn = self.c.server_conn # Update server_conn attribute on the flow + flow.server_conn = self.c.server_conn # Update server_conn attribute on the flow else: raise http.HttpError(400, "Invalid request form (absolute-form or authority-form required)") diff --git a/test/test_protocol_http.py b/test/test_protocol_http.py index 3bf5af226e..d2ba24de8e 100644 --- a/test/test_protocol_http.py +++ b/test/test_protocol_http.py @@ -44,7 +44,6 @@ def test_authority_form(self): r = HTTPRequest.from_stream(s) assert r._assemble() == "CONNECT address:22 HTTP/1.1\r\nHost: address:22\r\n\r\n" - def test_absolute_form(self): s = StringIO("GET oops-no-protocol.com HTTP/1.1") tutils.raises("Bad HTTP request line", HTTPRequest.from_stream, s)