0
|
1 """
|
|
2 request dispatcher
|
|
3 """
|
|
4
|
|
5 import os
|
2
|
6 from handlers import Get, Post, SubpathGet, SubpathPost
|
0
|
7 from webob import Request, exc
|
|
8
|
|
9 class Dispatcher(object):
|
|
10
|
|
11 ### class level variables
|
1
|
12 defaults = { 'directory': None,
|
2
|
13 'auth': 'False',
|
|
14 'query_string': None,
|
3
|
15 'subpath': 'False',
|
|
16 'app': None}
|
0
|
17
|
|
18 def __init__(self, **kw):
|
|
19 for key in self.defaults:
|
|
20 setattr(self, key, kw.get(key, self.defaults[key]))
|
|
21 assert os.path.exists(self.directory)
|
1
|
22 self.auth = self.auth.lower() == 'true'
|
2
|
23 self.subpath = self.subpath.lower() == 'true'
|
|
24 if self.subpath:
|
|
25 if self.query_string:
|
|
26 self.handlers = [ SubpathGet, SubpathPost ]
|
|
27 else:
|
|
28 self.handlers = [ SubpathPost ]
|
|
29 else:
|
|
30 self.handlers = [ Get, Post ]
|
0
|
31
|
|
32 ### methods dealing with HTTP
|
|
33 def __call__(self, environ, start_response):
|
|
34 request = Request(environ)
|
1
|
35 if self.auth and not request.remote_user:
|
|
36 return exc.HTTPUnauthorized()(environ, start_response)
|
0
|
37 for h in self.handlers:
|
2
|
38 if h.match(self, request):
|
0
|
39 handler = h(self, request)
|
|
40 break
|
|
41 else:
|
3
|
42 if self.app:
|
|
43 return self.app(environ, start_response)
|
0
|
44 handler = exc.HTTPNotFound
|
|
45 res = handler()
|
|
46 return res(environ, start_response)
|