Skip to content

Commit

Permalink
Fix flow initialization order error
Browse files Browse the repository at this point in the history
Resolves mitmproxy#210
  • Loading branch information
cortesi committed Feb 8, 2014
1 parent b642b48 commit a85974e
Show file tree
Hide file tree
Showing 4 changed files with 16 additions and 15 deletions.
4 changes: 0 additions & 4 deletions libmproxy/console/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand All @@ -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()
Expand Down Expand Up @@ -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)
Expand Down
6 changes: 3 additions & 3 deletions libmproxy/flow.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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/
Expand All @@ -464,7 +464,7 @@ def patch_environ(*args, **kwargs):
@app.mapp.route('/shutdown/<secret>')
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.
Expand Down
20 changes: 13 additions & 7 deletions libmproxy/protocol/http.py
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down Expand Up @@ -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:
Expand All @@ -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) +
Expand All @@ -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)")

Expand Down
1 change: 0 additions & 1 deletion test/test_protocol_http.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down

0 comments on commit a85974e

Please sign in to comment.