aboutsummaryrefslogtreecommitdiffstats
path: root/libmproxy/flow.py
diff options
context:
space:
mode:
authorMaximilian Hils <git@maximilianhils.com>2014-01-07 02:29:10 +0100
committerMaximilian Hils <git@maximilianhils.com>2014-01-07 02:29:10 +0100
commitea2f17680b964e2e793ff804a6f4e7b9d2d7b1ac (patch)
tree84250064ffbebb2974849d083d0129e545848862 /libmproxy/flow.py
parentb34ad82b528b55dabc318f999577fb6a020ccad9 (diff)
parentd5f9b02615bffe56639a7250f31752cebd2b8d62 (diff)
downloadmitmproxy-ea2f17680b964e2e793ff804a6f4e7b9d2d7b1ac.tar.gz
mitmproxy-ea2f17680b964e2e793ff804a6f4e7b9d2d7b1ac.tar.bz2
mitmproxy-ea2f17680b964e2e793ff804a6f4e7b9d2d7b1ac.zip
continue work on the proxyhandler
Diffstat (limited to 'libmproxy/flow.py')
-rw-r--r--libmproxy/flow.py62
1 files changed, 52 insertions, 10 deletions
diff --git a/libmproxy/flow.py b/libmproxy/flow.py
index 41e08e4e..5e3ad8e9 100644
--- a/libmproxy/flow.py
+++ b/libmproxy/flow.py
@@ -6,7 +6,7 @@ import hashlib, Cookie, cookielib, copy, re, urlparse, os, threading
import time, urllib
import tnetstring, filt, script, utils, encoding, proxy
from email.utils import parsedate_tz, formatdate, mktime_tz
-from netlib import odict, http, certutils
+from netlib import odict, http, certutils, wsgi
import controller, version
import app
@@ -17,6 +17,28 @@ ODict = odict.ODict
ODictCaseless = odict.ODictCaseless
+class AppRegistry:
+ def __init__(self):
+ self.apps = {}
+
+ def add(self, app, domain, port):
+ """
+ Add a WSGI app to the registry, to be served for requests to the
+ specified domain, on the specified port.
+ """
+ self.apps[(domain, port)] = wsgi.WSGIAdaptor(app, domain, port, version.NAMEVERSION)
+
+ def get(self, request):
+ """
+ Returns an WSGIAdaptor instance if request matches an app, or None.
+ """
+ if (request.host, request.port) in self.apps:
+ return self.apps[(request.host, request.port)]
+ if "host" in request.headers:
+ host = request.headers["host"][0]
+ return self.apps.get((host, request.port), None)
+
+
class ReplaceHooks:
def __init__(self):
self.lst = []
@@ -289,8 +311,10 @@ class Request(HTTPMsg):
"""
def __init__(
- self, client_conn, httpversion, host, port, scheme, method, path, headers, content, timestamp_start=None,
- timestamp_end=None, tcp_setup_timestamp=None, ssl_setup_timestamp=None, ip=None):
+ self, client_conn, httpversion, host, port,
+ scheme, method, path, headers, content, timestamp_start=None,
+ timestamp_end=None, tcp_setup_timestamp=None,
+ ssl_setup_timestamp=None, ip=None):
assert isinstance(headers, ODictCaseless)
self.client_conn = client_conn
self.httpversion = httpversion
@@ -307,6 +331,15 @@ class Request(HTTPMsg):
self.stickycookie = 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):
"""
Modifies this request to remove headers that might produce a cached
@@ -1372,17 +1405,16 @@ class FlowMaster(controller.Master):
self.setheaders = SetHeaders()
self.stream = None
- app.mapp.config["PMASTER"] = self
+ self.apps = AppRegistry()
def start_app(self, host, port, external):
if not external:
- self.server.apps.add(
+ self.apps.add(
app.mapp,
host,
port
)
else:
- print host
threading.Thread(target=app.mapp.run,kwargs={
"use_reloader": False,
"host": host,
@@ -1430,7 +1462,7 @@ class FlowMaster(controller.Master):
def run_script_hook(self, name, *args, **kwargs):
for script in self.scripts:
self.run_single_script_hook(script, name, *args, **kwargs)
-
+
def set_stickycookie(self, txt):
if txt:
flt = filt.parse(txt)
@@ -1589,9 +1621,11 @@ class FlowMaster(controller.Master):
r.reply()
def handle_serverconnection(self, sc):
- # To unify the mitmproxy script API, we call the script hook "serverconnect" rather than "serverconnection".
- # As things are handled differently in libmproxy (ClientConnect + ClientDisconnect vs ServerConnection class),
- # there is no "serverdisonnect" event at the moment.
+ # To unify the mitmproxy script API, we call the script hook
+ # "serverconnect" rather than "serverconnection". As things are handled
+ # differently in libmproxy (ClientConnect + ClientDisconnect vs
+ # ServerConnection class), there is no "serverdisonnect" event at the
+ # moment.
self.run_script_hook("serverconnect", sc)
sc.reply()
@@ -1605,6 +1639,14 @@ class FlowMaster(controller.Master):
return f
def handle_request(self, r):
+ if r.is_live():
+ app = self.apps.get(r)
+ if app:
+ err = app.serve(r, r.wfile, **{"mitmproxy.master": self})
+ if err:
+ self.add_event("Error in wsgi app. %s"%err, "error")
+ r.reply(proxy.KILL)
+ return
f = self.state.add_request(r)
self.replacehooks.run(f)
self.setheaders.run(f)