diff options
author | Aldo Cortesi <aldo@nullcube.com> | 2012-04-29 10:56:33 +1200 |
---|---|---|
committer | Aldo Cortesi <aldo@nullcube.com> | 2012-04-29 10:56:33 +1200 |
commit | 77eca33f2695eea690dff7999c0e1bd3df0e1733 (patch) | |
tree | d2ee69461276805bb2a5f295c18a5b7b4df2c973 /libpathod/app.py | |
parent | ee909e265b40743479ca2f3e8b518f76c91f83f8 (diff) | |
download | mitmproxy-77eca33f2695eea690dff7999c0e1bd3df0e1733.tar.gz mitmproxy-77eca33f2695eea690dff7999c0e1bd3df0e1733.tar.bz2 mitmproxy-77eca33f2695eea690dff7999c0e1bd3df0e1733.zip |
Refactor application definitions and startup.
Also, create one of the dodgiest web testing trusses in history. Tornado just
seems to have no nice way of doing this.
Diffstat (limited to 'libpathod/app.py')
-rw-r--r-- | libpathod/app.py | 83 |
1 files changed, 83 insertions, 0 deletions
diff --git a/libpathod/app.py b/libpathod/app.py new file mode 100644 index 00000000..15d99023 --- /dev/null +++ b/libpathod/app.py @@ -0,0 +1,83 @@ +import urllib +import tornado.web, tornado.template, tornado.ioloop, tornado.httpserver +import rparse, utils + +class _Page(tornado.web.RequestHandler): + def render(self, name, **kwargs): + b = self.application.templates.load(name + ".html").generate(**kwargs) + self.write(b) + + +class Index(_Page): + name = "index" + section = "main" + def get(self): + self.render(self.name, section=self.section) + + +class Preview(_Page): + name = "preview" + section = "main" + def get(self): + self.render(self.name, section=self.section) + + +class Help(_Page): + name = "help" + section = "help" + def get(self): + self.render(self.name, section=self.section) + + +class Log(_Page): + name = "log" + section = "log" + def get(self): + self.render(self.name, section=self.section) + + +class Pathod(object): + anchor = "/p/" + def __init__(self, application, request, **settings): + self.application, self.request, self.settings = application, request, settings + spec = urllib.unquote(self.request.uri)[len(self.anchor):] + try: + self.response = rparse.parse(self.settings, spec) + except rparse.ParseException, v: + self.response = rparse.InternalResponse( + 800, + "Error parsing response spec: %s\n"%v.msg + v.marked() + ) + + def _execute(self, transforms, *args, **kwargs): + self.response.render(self.request) + + +class PathodApp(tornado.web.Application): + def __init__(self, **settings): + self.templates = tornado.template.Loader(utils.data.path("templates")) + tornado.web.Application.__init__( + self, + [ + (r"/", Index), + (r"/log", Log), + (r"/help", Help), + (r"/preview", Preview), + (r"/p/.*", Pathod, settings), + ], + static_path = utils.data.path("static"), + template_path = utils.data.path("templates"), + debug=True + ) + + +# begin nocover +def run(application, port, ssl_options): + http_server = tornado.httpserver.HTTPServer( + application, + ssl_options=ssl_options + ) + http_server.listen(port) + tornado.ioloop.IOLoop.instance().start() + + |