Mercurial > hg > FileServer
comparison fileserver/web.py @ 2:8fb047af207a
this now actually serves things
author | Jeff Hammel <jhammel@mozilla.com> |
---|---|
date | Wed, 29 Feb 2012 13:37:57 -0800 |
parents | 89d4f742ed1a |
children | 8127dde8da22 |
comparison
equal
deleted
inserted
replaced
1:89d4f742ed1a | 2:8fb047af207a |
---|---|
41 class DirectoryServer(object): | 41 class DirectoryServer(object): |
42 | 42 |
43 def __init__(self, directory): | 43 def __init__(self, directory): |
44 assert os.path.exists(directory), "'%s' does not exist" % directory | 44 assert os.path.exists(directory), "'%s' does not exist" % directory |
45 assert os.path.isdir(directory), "'%s' is not a directory" % directory | 45 assert os.path.isdir(directory), "'%s' is not a directory" % directory |
46 self.directory = directory | 46 self.directory = self.normpath(directory) |
47 | 47 |
48 @staticmethod | 48 @staticmethod |
49 def normpath(path): | 49 def normpath(path): |
50 return os.path.normcase(os.path.abspath(path)) | 50 return os.path.normcase(os.path.abspath(path)) |
51 | |
52 def index(self, directory): | |
53 """ | |
54 generate a directory listing for a given directory | |
55 """ | |
56 parts = ['<html><head><title>Simple Index</title></head><body>'] | |
57 listings = os.listdir(directory) | |
58 listings = [(os.path.isdir(os.path.join(directory, entry)) and entry + '/' or entry, entry) | |
59 for entry in listings] | |
60 for link, entry in listings: | |
61 parts.append('<a href="%s">%s</a><br/>' % (link, entry)) | |
62 parts.append('</body></html>') | |
63 return '\n'.join(parts) | |
51 | 64 |
52 def __call__(self, environ, start_response): | 65 def __call__(self, environ, start_response): |
53 request = Request(environ) | 66 request = Request(environ) |
54 # TODO method_not_allowed: Allow: GET, HEAD | 67 # TODO method_not_allowed: Allow: GET, HEAD |
55 path_info = request.path_info | 68 path_info = request.path_info |
56 if not path_info: | 69 if not path_info: |
57 pass # self.add slash | 70 pass # self.add slash |
58 full = self.normpath(os.path.join(self.directory, path_info.strip('/'))) | 71 full = self.normpath(os.path.join(self.directory, path_info.strip('/'))) |
72 | |
59 if not full.startswith(self.directory): | 73 if not full.startswith(self.directory): |
74 print 'OUT OF BOUNDS!' | |
75 import pdb; pdb.set_trace() | |
60 # Out of bounds | 76 # Out of bounds |
61 return exc.HTTPNotFound()(environ, start_response) | 77 return exc.HTTPNotFound()(environ, start_response) |
62 if not os.path.exists(full): | 78 if not os.path.exists(full): |
63 return exc.HTTPNotFound()(environ, start_response) | 79 return exc.HTTPNotFound()(environ, start_response) |
64 | 80 |