aboutsummaryrefslogtreecommitdiffstats
path: root/mitmproxy/builtins/wsgiapp.py
diff options
context:
space:
mode:
Diffstat (limited to 'mitmproxy/builtins/wsgiapp.py')
-rw-r--r--mitmproxy/builtins/wsgiapp.py36
1 files changed, 36 insertions, 0 deletions
diff --git a/mitmproxy/builtins/wsgiapp.py b/mitmproxy/builtins/wsgiapp.py
new file mode 100644
index 00000000..07ec8a6f
--- /dev/null
+++ b/mitmproxy/builtins/wsgiapp.py
@@ -0,0 +1,36 @@
+from mitmproxy import ctx
+
+from netlib import wsgi
+from netlib import version
+
+
+class WSGIApp:
+ """
+ An addon that hosts a WSGI app withing mitproxy, at a specified
+ hostname and port.
+ """
+ def __init__(self, app, host, port):
+ self.app, self.host, self.port = app, host, port
+
+ def serve(self, app, flow):
+ """
+ Serves app on flow, and prevents further handling of the flow.
+ """
+ app = wsgi.WSGIAdaptor(
+ app,
+ flow.request.pretty_host,
+ flow.request.port,
+ version.MITMPROXY
+ )
+ err = app.serve(
+ flow,
+ flow.client_conn.wfile,
+ **{"mitmproxy.master": ctx.master}
+ )
+ if err:
+ ctx.log.warn("Error in wsgi app. %s" % err, "error")
+ flow.reply.kill()
+
+ def request(self, f):
+ if (f.request.pretty_host, f.request.port) == (self.host, self.port):
+ self.serve(self.app, f)