Mercurial > hg > simpypi
comparison simpypi/factory.py @ 25:511210365ce3
WIP getting paste StaticURLParser to serve directories
author | Jeff Hammel <jhammel@mozilla.com> |
---|---|
date | Wed, 29 Feb 2012 10:29:11 -0800 |
parents | 13ed82d10144 |
children | fb03f34a982f |
comparison
equal
deleted
inserted
replaced
24:13ed82d10144 | 25:511210365ce3 |
---|---|
18 | 18 |
19 class DirectoryServer(StaticURLParser): | 19 class DirectoryServer(StaticURLParser): |
20 def __init__(self, directory): | 20 def __init__(self, directory): |
21 StaticURLParser.__init__(self, directory) | 21 StaticURLParser.__init__(self, directory) |
22 | 22 |
23 def index(self, directory): | |
24 """ | |
25 generate a directory listing for a given directory | |
26 """ | |
27 parts = ['<html><head><title>Simple Index</title></head><body>'] | |
28 listings = os.listdir(directory) | |
29 listings = [(os.path.isdir(os.path.join(directory, entry)) and entry + '/' or entry, entry) | |
30 for entry in listings] | |
31 for link, entry in listings: | |
32 parts.append('<a href="%s">%s</a><br/>' % (link, entry)) | |
33 parts.append('</body></html>') | |
34 return '\n'.join(parts) | |
35 | |
23 def __call__(self, environ, start_response): | 36 def __call__(self, environ, start_response): |
24 import pdb; pdb.set_trace() | 37 |
38 # normalize path | |
39 # from paste.urlparser.StaticURLParser | |
40 path_info = environ.get('PATH_INFO', '') | |
41 if not path_info: | |
42 return self.add_slash(environ, start_response) | |
43 full = self.normpath(os.path.join(self.directory, path_info.strip('/'))) | |
44 if not full.startswith(self.root_directory): | |
45 # Out of bounds | |
46 return self.not_found(environ, start_response) | |
47 | |
48 # return index listing | |
49 if os.path.isdir(full): | |
50 if not path_info.endswith('/'): | |
51 return self.add_slash(environ, start_response) | |
52 index = self.index(full) | |
53 response_headers = [('Content-Type', 'text/html'), | |
54 ('Content-Length', str(len(index)))] | |
55 start_response('200 OK', response_headers) | |
56 return [index] | |
57 | |
58 return StaticURLParser.__call__(self, environ, start_response) | |
25 | 59 |
26 class PassthroughFileserver(object): | 60 class PassthroughFileserver(object): |
27 """serve files if they exist""" | 61 """serve files if they exist""" |
28 | 62 |
29 def __init__(self, app, directory): | 63 def __init__(self, app, directory): |
45 self.namespace = namespace | 79 self.namespace = namespace |
46 | 80 |
47 def __call__(self, environ, start_response): | 81 def __call__(self, environ, start_response): |
48 path = environ['PATH_INFO'] | 82 path = environ['PATH_INFO'] |
49 if path == self.namespace: | 83 if path == self.namespace: |
84 return self.add_slash(environ, start_response) | |
50 environ['PATH_INFO'] = '/' | 85 environ['PATH_INFO'] = '/' |
51 return DirectoryServer.__call__(self, environ, start_response) | 86 return DirectoryServer.__call__(self, environ, start_response) |
52 elif path.startswith(self.namespace + '/'): | 87 elif path.startswith(self.namespace + '/'): |
53 environ['PATH_INFO'] = path[len(self.namespace):] | 88 environ['PATH_INFO'] = path[len(self.namespace):] |
54 return DirectoryServer.__call__(self, environ, start_response) | 89 return DirectoryServer.__call__(self, environ, start_response) |