diff options
Diffstat (limited to 'mitmproxy/addons/wsgiapp.py')
-rw-r--r-- | mitmproxy/addons/wsgiapp.py | 38 |
1 files changed, 38 insertions, 0 deletions
diff --git a/mitmproxy/addons/wsgiapp.py b/mitmproxy/addons/wsgiapp.py new file mode 100644 index 00000000..d83a1e2e --- /dev/null +++ b/mitmproxy/addons/wsgiapp.py @@ -0,0 +1,38 @@ +from mitmproxy import ctx +from mitmproxy import exceptions + +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.error("Error in wsgi app. %s" % err) + flow.reply.kill() + raise exceptions.AddonHalt() + + def request(self, f): + if (f.request.pretty_host, f.request.port) == (self.host, self.port): + self.serve(self.app, f) |