2012-04-29 02:59:54 +00:00
|
|
|
import urllib, pprint
|
2012-04-28 22:56:33 +00:00
|
|
|
import tornado.web, tornado.template, tornado.ioloop, tornado.httpserver
|
|
|
|
import rparse, utils
|
|
|
|
|
2012-04-29 21:46:49 +00:00
|
|
|
|
|
|
|
class APILog(tornado.web.RequestHandler):
|
|
|
|
def get(self):
|
|
|
|
self.write(
|
|
|
|
dict(
|
|
|
|
d = self.application.get_log()
|
|
|
|
)
|
|
|
|
)
|
|
|
|
|
|
|
|
|
2012-04-29 21:54:49 +00:00
|
|
|
class APILogClear(tornado.web.RequestHandler):
|
|
|
|
def post(self):
|
|
|
|
self.application.clear_log()
|
|
|
|
self.write("OK")
|
|
|
|
|
|
|
|
|
2012-06-06 23:23:23 +00:00
|
|
|
class APIShutdown(tornado.web.RequestHandler):
|
|
|
|
def post(self):
|
|
|
|
tornado.ioloop.IOLoop.instance().stop()
|
|
|
|
self.write("OK")
|
|
|
|
|
|
|
|
|
2012-04-28 22:56:33 +00:00
|
|
|
class _Page(tornado.web.RequestHandler):
|
|
|
|
def render(self, name, **kwargs):
|
2012-04-29 04:22:33 +00:00
|
|
|
tornado.web.RequestHandler.render(self, name + ".html", **kwargs)
|
2012-04-28 22:56:33 +00:00
|
|
|
|
2012-04-29 09:15:02 +00:00
|
|
|
|
2012-04-28 22:56:33 +00:00
|
|
|
class Index(_Page):
|
|
|
|
name = "index"
|
|
|
|
section = "main"
|
|
|
|
def get(self):
|
2012-04-29 09:22:00 +00:00
|
|
|
self.render(self.name, section=self.section, spec="")
|
2012-04-28 22:56:33 +00:00
|
|
|
|
|
|
|
|
|
|
|
class Preview(_Page):
|
|
|
|
name = "preview"
|
|
|
|
section = "main"
|
2012-04-29 09:15:02 +00:00
|
|
|
SANITY = 1024*1024
|
2012-04-28 22:56:33 +00:00
|
|
|
def get(self):
|
2012-04-29 09:15:02 +00:00
|
|
|
spec = self.get_argument("spec", None)
|
|
|
|
args = dict(
|
|
|
|
spec = spec,
|
|
|
|
section = self.section,
|
|
|
|
syntaxerror = None,
|
|
|
|
error = None
|
|
|
|
)
|
|
|
|
try:
|
|
|
|
r = rparse.parse(self.application.settings, spec)
|
|
|
|
except rparse.ParseException, v:
|
|
|
|
args["syntaxerror"] = str(v)
|
|
|
|
args["marked"] = v.marked()
|
|
|
|
return self.render(self.name, **args)
|
|
|
|
if r.length() > self.SANITY:
|
|
|
|
error = "Refusing to preview a response of %s bytes. This is for your own good."%r.length()
|
|
|
|
args["error"] = error
|
|
|
|
else:
|
|
|
|
d = utils.DummyRequest()
|
|
|
|
r.serve(d)
|
|
|
|
args["output"] = d.getvalue()
|
|
|
|
self.render(self.name, **args)
|
2012-04-28 22:56:33 +00:00
|
|
|
|
|
|
|
|
|
|
|
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):
|
2012-04-29 04:22:33 +00:00
|
|
|
self.render(self.name, section=self.section, log=self.application.log)
|
|
|
|
|
|
|
|
|
|
|
|
class OneLog(_Page):
|
|
|
|
name = "onelog"
|
|
|
|
section = "log"
|
|
|
|
def get(self, lid):
|
|
|
|
l = pprint.pformat(self.application.log_by_id(int(lid)))
|
|
|
|
self.render(self.name, section=self.section, alog=l, lid=lid)
|
|
|
|
|
|
|
|
|
|
|
|
class ClearLog(_Page):
|
|
|
|
def post(self):
|
|
|
|
self.application.clear_logs()
|
|
|
|
self.redirect("/log")
|
2012-04-28 22:56:33 +00:00
|
|
|
|
|
|
|
|
|
|
|
class Pathod(object):
|
2012-04-29 00:05:38 +00:00
|
|
|
def __init__(self, spec, application, request, **settings):
|
2012-04-28 22:56:33 +00:00
|
|
|
self.application, self.request, self.settings = application, request, settings
|
|
|
|
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):
|
2012-04-29 02:59:54 +00:00
|
|
|
d = self.response.serve(self.request)
|
|
|
|
d["request"] = dict(
|
|
|
|
path = self.request.path,
|
|
|
|
method = self.request.method,
|
|
|
|
headers = self.request.headers,
|
|
|
|
host = self.request.host,
|
|
|
|
protocol = self.request.protocol,
|
|
|
|
remote_address = self.request.connection.address,
|
|
|
|
full_url = self.request.full_url(),
|
|
|
|
query = self.request.query,
|
|
|
|
version = self.request.version,
|
|
|
|
uri = self.request.uri,
|
|
|
|
)
|
|
|
|
self.application.add_log(d)
|
2012-04-28 22:56:33 +00:00
|
|
|
|
|
|
|
|
2012-04-29 00:05:38 +00:00
|
|
|
class RequestPathod(Pathod):
|
|
|
|
anchor = "/p/"
|
|
|
|
def __init__(self, application, request, **settings):
|
|
|
|
spec = urllib.unquote(request.uri)[len(self.anchor):]
|
|
|
|
Pathod.__init__(self, spec, application, request, **settings)
|
|
|
|
|
|
|
|
|
2012-04-28 22:56:33 +00:00
|
|
|
class PathodApp(tornado.web.Application):
|
2012-04-29 02:59:54 +00:00
|
|
|
LOGBUF = 500
|
2012-04-28 22:56:33 +00:00
|
|
|
def __init__(self, **settings):
|
2012-04-29 00:05:38 +00:00
|
|
|
self.appsettings = settings
|
2012-04-28 22:56:33 +00:00
|
|
|
tornado.web.Application.__init__(
|
|
|
|
self,
|
|
|
|
[
|
|
|
|
(r"/", Index),
|
|
|
|
(r"/log", Log),
|
2012-04-29 04:22:33 +00:00
|
|
|
(r"/log/clear", ClearLog),
|
|
|
|
(r"/log/([0-9]+)", OneLog),
|
2012-04-28 22:56:33 +00:00
|
|
|
(r"/help", Help),
|
|
|
|
(r"/preview", Preview),
|
2012-06-06 23:23:23 +00:00
|
|
|
(r"/api/shutdown", APIShutdown),
|
2012-04-29 21:46:49 +00:00
|
|
|
(r"/api/log", APILog),
|
2012-04-29 21:54:49 +00:00
|
|
|
(r"/api/log/clear", APILogClear),
|
2012-04-29 00:05:38 +00:00
|
|
|
(r"/p/.*", RequestPathod, settings),
|
2012-04-28 22:56:33 +00:00
|
|
|
],
|
|
|
|
static_path = utils.data.path("static"),
|
|
|
|
template_path = utils.data.path("templates"),
|
|
|
|
debug=True
|
|
|
|
)
|
2012-04-29 02:59:54 +00:00
|
|
|
self.log = []
|
|
|
|
self.logid = 0
|
2012-04-28 22:56:33 +00:00
|
|
|
|
2012-04-29 00:05:38 +00:00
|
|
|
def add_anchor(self, pattern, spec):
|
|
|
|
"""
|
|
|
|
Anchors are added to the beginning of the handlers.
|
|
|
|
"""
|
|
|
|
# We assume we have only one host...
|
|
|
|
l = self.handlers[0][1]
|
|
|
|
class FixedPathod(Pathod):
|
|
|
|
def __init__(self, application, request, **settings):
|
|
|
|
Pathod.__init__(self, spec, application, request, **settings)
|
|
|
|
FixedPathod.spec = spec
|
2012-04-29 00:15:25 +00:00
|
|
|
FixedPathod.pattern = pattern
|
2012-04-29 00:05:38 +00:00
|
|
|
l.insert(0, tornado.web.URLSpec(pattern, FixedPathod, self.appsettings))
|
|
|
|
|
2012-04-29 00:15:25 +00:00
|
|
|
def get_anchors(self):
|
2012-04-29 00:05:38 +00:00
|
|
|
"""
|
|
|
|
Anchors are added to the beginning of the handlers.
|
|
|
|
"""
|
2012-04-29 00:15:25 +00:00
|
|
|
l = self.handlers[0][1]
|
|
|
|
a = []
|
|
|
|
for i in l:
|
|
|
|
if i.handler_class.__name__ == "FixedPathod":
|
|
|
|
a.append(
|
|
|
|
(
|
|
|
|
i.handler_class.pattern,
|
|
|
|
i.handler_class.spec
|
|
|
|
)
|
|
|
|
)
|
|
|
|
return a
|
2012-04-29 00:05:38 +00:00
|
|
|
|
|
|
|
def remove_anchor(self, pattern, spec):
|
|
|
|
"""
|
|
|
|
Anchors are added to the beginning of the handlers.
|
|
|
|
"""
|
2012-04-29 00:15:25 +00:00
|
|
|
l = self.handlers[0][1]
|
|
|
|
for i, h in enumerate(l):
|
|
|
|
if h.handler_class.__name__ == "FixedPathod":
|
|
|
|
if (h.handler_class.pattern, h.handler_class.spec) == (pattern, spec):
|
|
|
|
del l[i]
|
|
|
|
return
|
2012-04-29 00:05:38 +00:00
|
|
|
|
2012-04-29 02:59:54 +00:00
|
|
|
def add_log(self, d):
|
|
|
|
d["id"] = self.logid
|
|
|
|
self.log.insert(0, d)
|
|
|
|
if len(self.log) > self.LOGBUF:
|
|
|
|
self.log.pop()
|
|
|
|
self.logid += 1
|
|
|
|
|
2012-04-29 04:22:33 +00:00
|
|
|
def log_by_id(self, id):
|
|
|
|
for i in self.log:
|
|
|
|
if i["id"] == id:
|
|
|
|
return i
|
|
|
|
|
2012-04-29 21:46:49 +00:00
|
|
|
def clear_log(self):
|
2012-04-29 04:22:33 +00:00
|
|
|
self.log = []
|
|
|
|
|
2012-04-29 21:46:49 +00:00
|
|
|
def get_log(self):
|
|
|
|
return self.log
|
|
|
|
|
2012-04-28 22:56:33 +00:00
|
|
|
|
2012-06-06 23:23:23 +00:00
|
|
|
def make_app(staticdir=None, anchors=()):
|
|
|
|
"""
|
|
|
|
staticdir: A directory for static assets referenced in response patterns.
|
|
|
|
anchors: A sequence of strings of the form "pattern=pagespec"
|
|
|
|
"""
|
|
|
|
settings = dict(
|
|
|
|
staticdir=staticdir
|
|
|
|
)
|
|
|
|
application = PathodApp(**settings)
|
|
|
|
for i in anchors:
|
|
|
|
rex, spec = utils.parse_anchor_spec(i, settings)
|
|
|
|
application.add_anchor(rex, spec)
|
|
|
|
return application
|
|
|
|
|
|
|
|
|
2012-06-06 22:17:30 +00:00
|
|
|
def make_server(application, port, address, ssl_options):
|
|
|
|
"""
|
2012-06-06 23:23:23 +00:00
|
|
|
Returns a (server, port) tuple.
|
|
|
|
|
|
|
|
The returned port will match the passed port, unless the passed port
|
|
|
|
was 0. In that case, an arbitrary empty port will be bound to, and this
|
|
|
|
new port will be returned.
|
2012-06-06 22:17:30 +00:00
|
|
|
"""
|
2012-04-28 22:56:33 +00:00
|
|
|
http_server = tornado.httpserver.HTTPServer(
|
|
|
|
application,
|
|
|
|
ssl_options=ssl_options
|
|
|
|
)
|
2012-06-06 22:17:30 +00:00
|
|
|
http_server.listen(port, address)
|
|
|
|
port = port
|
|
|
|
for i in http_server._sockets.values():
|
|
|
|
sn = i.getsockname()
|
|
|
|
if sn[0] == address:
|
|
|
|
port = sn[1]
|
2012-06-06 23:23:23 +00:00
|
|
|
return http_server, port
|
2012-06-06 22:17:30 +00:00
|
|
|
|
2012-04-28 22:56:33 +00:00
|
|
|
|
2012-06-06 23:23:23 +00:00
|
|
|
# begin nocover
|
|
|
|
def run(server):
|
2012-06-06 22:17:30 +00:00
|
|
|
tornado.ioloop.IOLoop.instance().start()
|
2012-06-06 23:23:23 +00:00
|
|
|
server.stop()
|
|
|
|
tornado.ioloop.IOLoop.instance().close()
|
|
|
|
|