Introduce file descriptor decorators for Request objects

Which lets us enable the apps again, now running from flow.py
This commit is contained in:
Aldo Cortesi 2014-01-05 10:58:53 +13:00
parent 45eab17e0c
commit a2261e3cf0
3 changed files with 36 additions and 18 deletions

View File

@ -3,6 +3,8 @@ import flask
mapp = flask.Flask(__name__) mapp = flask.Flask(__name__)
mapp.debug = True mapp.debug = True
def master():
return flask.request.environ["mitmproxy.master"]
@mapp.route("/") @mapp.route("/")
def index(): def index():

View File

@ -331,6 +331,15 @@ class Request(HTTPMsg):
self.stickycookie = False self.stickycookie = False
self.stickyauth = False self.stickyauth = False
# Live attributes - not serialized
self.wfile, self.rfile = None, None
def set_live(self, rfile, wfile):
self.wfile, self.rfile = wfile, rfile
def is_live(self):
return bool(self.wfile)
def anticache(self): def anticache(self):
""" """
Modifies this request to remove headers that might produce a cached Modifies this request to remove headers that might produce a cached
@ -1397,7 +1406,6 @@ class FlowMaster(controller.Master):
self.setheaders = SetHeaders() self.setheaders = SetHeaders()
self.stream = None self.stream = None
app.mapp.config["PMASTER"] = self
self.apps = AppRegistry() self.apps = AppRegistry()
def start_app(self, host, port, external): def start_app(self, host, port, external):
@ -1632,13 +1640,14 @@ class FlowMaster(controller.Master):
return f return f
def handle_request(self, r): def handle_request(self, r):
if r.is_live():
app = self.apps.get(r) app = self.apps.get(r)
if app: if app:
r.reply() err = app.serve(r, r.wfile, **{"mitmproxy.master": self})
#err = app.serve(r, self.wfile) if err:
#if err: self.add_event("Error in wsgi app. %s"%err, "error")
# self.add_event("Error in wsgi app. %s"%err, "error") r.reply(proxy.KILL)
else: return
f = self.state.add_request(r) f = self.state.add_request(r)
self.replacehooks.run(f) self.replacehooks.run(f)
self.setheaders.run(f) self.setheaders.run(f)

View File

@ -6,6 +6,8 @@ from netlib import odict, tcp, http, certutils, http_status, http_auth
import utils, flow, version, platform, controller import utils, flow, version, platform, controller
TRANSPARENT_SSL_PORTS = [443, 8443]
KILL = 0 KILL = 0
@ -425,10 +427,12 @@ class ProxyHandler(tcp.BaseHandler):
content = http.read_http_body_request( content = http.read_http_body_request(
self.rfile, self.wfile, headers, httpversion, self.config.body_size_limit self.rfile, self.wfile, headers, httpversion, self.config.body_size_limit
) )
return flow.Request( r = flow.Request(
client_conn, httpversion, host, port, scheme, method, path, headers, content, client_conn, httpversion, host, port, scheme, method, path, headers, content,
self.rfile.first_byte_timestamp, utils.timestamp() self.rfile.first_byte_timestamp, utils.timestamp()
) )
r.set_live(self.rfile, self.wfile)
return r
def _read_request_origin_form(self, client_conn, scheme, host, port): def _read_request_origin_form(self, client_conn, scheme, host, port):
""" """
@ -456,10 +460,12 @@ class ProxyHandler(tcp.BaseHandler):
content = http.read_http_body_request( content = http.read_http_body_request(
self.rfile, self.wfile, headers, httpversion, self.config.body_size_limit self.rfile, self.wfile, headers, httpversion, self.config.body_size_limit
) )
return flow.Request( r = flow.Request(
client_conn, httpversion, host, port, scheme, method, path, headers, content, client_conn, httpversion, host, port, scheme, method, path, headers, content,
self.rfile.first_byte_timestamp, utils.timestamp() self.rfile.first_byte_timestamp, utils.timestamp()
) )
r.set_live(self.rfile, self.wfile)
return r
def read_headers(self, authenticate=False): def read_headers(self, authenticate=False):
headers = http.read_headers(self.rfile) headers = http.read_headers(self.rfile)
@ -560,7 +566,6 @@ def certificate_option_group(parser):
) )
TRANSPARENT_SSL_PORTS = [443, 8443]
def process_proxy_options(parser, options): def process_proxy_options(parser, options):
if options.cert: if options.cert:
@ -603,7 +608,9 @@ def process_proxy_options(parser, options):
if options.clientcerts: if options.clientcerts:
options.clientcerts = os.path.expanduser(options.clientcerts) options.clientcerts = os.path.expanduser(options.clientcerts)
if not os.path.exists(options.clientcerts) or not os.path.isdir(options.clientcerts): if not os.path.exists(options.clientcerts) or not os.path.isdir(options.clientcerts):
return parser.error("Client certificate directory does not exist or is not a directory: %s"%options.clientcerts) return parser.error(
"Client certificate directory does not exist or is not a directory: %s"%options.clientcerts
)
if (options.auth_nonanonymous or options.auth_singleuser or options.auth_htpasswd): if (options.auth_nonanonymous or options.auth_singleuser or options.auth_htpasswd):
if options.auth_singleuser: if options.auth_singleuser: