aboutsummaryrefslogtreecommitdiffstats
path: root/libpathod/app.py
blob: 7059f9ba324cb6bac5375a0f6a3d6a10bc79e915 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
import urllib, pprint
import tornado.web, tornado.template, tornado.ioloop, tornado.httpserver
import rparse, utils


class APILog(tornado.web.RequestHandler):
    def get(self):
        self.write(
            dict(
                d = self.application.get_log()
            )
        )


class _Page(tornado.web.RequestHandler):
    def render(self, name, **kwargs):
        tornado.web.RequestHandler.render(self, name + ".html", **kwargs)


class Index(_Page):
    name = "index"
    section = "main"
    def get(self):
        self.render(self.name, section=self.section, spec="")


class Preview(_Page):
    name = "preview"
    section = "main"
    SANITY = 1024*1024
    def get(self):
        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)


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, 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")


class Pathod(object):
    def __init__(self, spec, application, request, **settings):
        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):
        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)


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)


class PathodApp(tornado.web.Application):
    LOGBUF = 500
    def __init__(self, **settings):
        self.appsettings = settings
        tornado.web.Application.__init__(
            self,
            [
                (r"/", Index),
                (r"/log", Log),
                (r"/log/clear", ClearLog),
                (r"/log/([0-9]+)", OneLog),
                (r"/help", Help),
                (r"/preview", Preview),
                (r"/api/log", APILog),
                (r"/p/.*", RequestPathod, settings),
            ],
            static_path = utils.data.path("static"),
            template_path = utils.data.path("templates"),
            debug=True
        )
        self.log = []
        self.logid = 0

    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
        FixedPathod.pattern = pattern
        l.insert(0, tornado.web.URLSpec(pattern, FixedPathod, self.appsettings))

    def get_anchors(self):
        """
            Anchors are added to the beginning of the handlers.
        """
        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

    def remove_anchor(self, pattern, spec):
        """
            Anchors are added to the beginning of the handlers.
        """
        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

    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

    def log_by_id(self, id):
        for i in self.log:
            if i["id"] == id:
                return i

    def clear_log(self):
        self.log = []

    def get_log(self):
        return self.log


# 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()