[albatross-users] [patch] HTTPServer static resources

Matt Goodall matt at pollenation.net
Wed Aug 6 00:22:56 EST 2003


Attached is another patch against 1.10 for the same purpose as described 
below. This version works on MS Windows (I was opening the file in text 
mode before!), sends the 'Content-Length' header for the file and is 
also a bit neater.

Cheers, Matt


Matt Goodall wrote:

> Hi,
>
> Attached is a patch against 1.10 that allows HTTPServer to serve 
> static resources (images, stylesheets etc) directly from the 
> filesystem with no help from the Albatross application. An example of 
> its use is:
>
>    from albatross import httpdapp
>
>    app = new MyAlbatrossApp()
>    static_resources = {
>        '/images': './images',
>        '/styles': 'somewhere/styles'
>        }
>    httpd = httpdapp.HTTPServer(app, 8000, static_resources)
>    httpd.serve_forever()
>
> static_resources is an _optional_ dictionary, mapping uri to 
> filesystem paths. Note that this change is not yet reflected in the 
> al-httpd script but the script still works in the same way as before.
>
> Hope this is useful.
>
> Cheers, Matt
>
>------------------------------------------------------------------------
>
>diff -ru ./albatross/httpdapp.py ../albatross-1.10-matt/albatross/httpdapp.py
>--- ./albatross/httpdapp.py	2003-07-12 05:42:51.000000000 +0100
>+++ ../albatross-1.10-matt/albatross/httpdapp.py	2003-07-21 15:27:07.000000000 +0100
>@@ -8,6 +8,7 @@
> 
> import BaseHTTPServer
> import cgi
>+import mimetypes
> from cStringIO import StringIO
> from urlparse import urlsplit
> 
>@@ -116,12 +117,19 @@
>     """
> 
>     def do_GET(self):
>+        # Try the static resources first
>+        for uri_path, fs_path in self.server.static_resources.items():
>+            if self.path.startswith(uri_path):
>+                path = self.path.split(uri_path)[1]
>+                self.send_file('%s%s' % (fs_path, path))
>+                return
>         self.process_request()
> 
>     def do_POST(self):
>         self.process_request()
> 
>     def process_request(self):
>+        '''Process a request to the Albatross app'''
>         self.log_request()
>         req = Request(self)
>         self.server.app.run(req)
>@@ -132,11 +140,54 @@
>         if req.status() == 200:
>             self.wfile.write(req.data.getvalue())
> 
>+    def send_file(self, path):
>+        '''Send a file directly from the filesystem'''
>+        
>+        self.log_request()
>+        
>+        f = None
>+        data = None
>+        mime_type = None
>+
>+        try:
>+            try:
>+                f = file(path)
>+                data = f.read()
>+            except IOError, e:
>+                print e
>+        finally:
>+            if f:
>+                f.close()
>+
>+        if not data:
>+            self.send_response(404)
>+            self.end_headers()
>+        else:
>+            self.send_response(200)
>+            self.send_header('Content-Type', mimetypes.guess_type(path)[0])
>+            self.end_headers()
>+            self.wfile.write(data)
>+
> 
> class HTTPServer(BaseHTTPServer.HTTPServer):
> 
>-    """Simple, standalone HTTP server for Albatross applications."""
>+    """
>+    Simple, standalone HTTP server for Albatross applications.
>+    
>+    HTTPServer is a simple web server dedicated to processing requests and
>+    mapping that request through to Albatross. It can also serve static
>+    resources such as images, stylesheets etc directly from the filesystem.
>+    """
> 
>-    def __init__(self, app, port):
>-        BaseHTTPServer.HTTPServer.__init__(self, ('', port), RequestHandler)
>+    def __init__(self, app, port, static_resources={}):
>+        '''
>+        Create an HTTP server for the Albatross application, app,
>+        and listen for requests on the specified port.
>+
>+        static_resources is an optional dictionary that maps uri paths to
>+        a corresponding filesystem path. Static resources are served directly
>+        from the filesystem without involving the Albatross application.
>+        '''
>+        BaseHTTPServer.HTTPServer.__init__( self, ('', port), RequestHandler)
>         self.app = app
>+        self.static_resources = static_resources
>  
>

-- 
Matt Goodall, Pollenation Internet Ltd
w: http://www.pollenationinternet.com
e: matt at pollenation.net
t: +44 (0)113 2252500

-------------- next part --------------
An embedded and charset-unspecified text was scrubbed...
Name: httpdapp-static-resources.patch
URL: <http://www.object-craft.com.au/pipermail/albatross-users/attachments/20030805/bdadf945/attachment.ksh>


More information about the Albatross-users mailing list