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,
|
|
15 'subpath': 'False' }
|
0
|
16
|
|
17 def __init__(self, **kw):
|
|
18 for key in self.defaults:
|
|
19 setattr(self, key, kw.get(key, self.defaults[key]))
|
|
20 assert os.path.exists(self.directory)
|
1
|
21 self.auth = self.auth.lower() == 'true'
|
2
|
22 self.subpath = self.subpath.lower() == 'true'
|
|
23 if self.subpath:
|
|
24 if self.query_string:
|
|
25 self.handlers = [ SubpathGet, SubpathPost ]
|
|
26 else:
|
|
27 self.handlers = [ SubpathPost ]
|
|
28 else:
|
|
29 self.handlers = [ Get, Post ]
|
0
|
30
|
|
31 ### methods dealing with HTTP
|
|
32 def __call__(self, environ, start_response):
|
|
33 request = Request(environ)
|
1
|
34 if self.auth and not request.remote_user:
|
|
35 return exc.HTTPUnauthorized()(environ, start_response)
|
0
|
36 for h in self.handlers:
|
2
|
37 if h.match(self, request):
|
0
|
38 handler = h(self, request)
|
|
39 break
|
|
40 else:
|
|
41 handler = exc.HTTPNotFound
|
|
42 res = handler()
|
|
43 return res(environ, start_response)
|