diff 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
line wrap: on
line diff
--- a/fileserver/web.py	Wed Feb 29 13:17:16 2012 -0800
+++ b/fileserver/web.py	Wed Feb 29 13:37:57 2012 -0800
@@ -43,12 +43,25 @@
     def __init__(self, directory):
         assert os.path.exists(directory), "'%s' does not exist" % directory
         assert os.path.isdir(directory), "'%s' is not a directory" % directory
-        self.directory = directory
+        self.directory = self.normpath(directory)
 
     @staticmethod
     def normpath(path):
         return os.path.normcase(os.path.abspath(path))
 
+    def index(self, directory):
+        """
+        generate a directory listing for a given directory
+        """
+        parts = ['<html><head><title>Simple Index</title></head><body>']
+        listings = os.listdir(directory)
+        listings = [(os.path.isdir(os.path.join(directory, entry)) and entry + '/' or entry, entry)
+                    for entry in listings]
+        for link, entry in listings:
+            parts.append('<a href="%s">%s</a><br/>' % (link, entry))
+        parts.append('</body></html>')
+        return '\n'.join(parts)
+
     def __call__(self, environ, start_response):
         request = Request(environ)
         # TODO method_not_allowed: Allow: GET, HEAD
@@ -56,7 +69,10 @@
         if not path_info:
             pass # self.add slash
         full = self.normpath(os.path.join(self.directory, path_info.strip('/')))
+
         if not full.startswith(self.directory):
+            print 'OUT OF BOUNDS!'
+            import pdb; pdb.set_trace()
             # Out of bounds
             return exc.HTTPNotFound()(environ, start_response)
         if not os.path.exists(full):