List Info

Thread: Adding StaticFilter directories




Adding StaticFilter directories
user name
2006-08-26 18:02:18

Sheco wrote:
> I would want to add StaticFilter directories, without
> using the config file, but using the _cp_filters list,
>; so I can have modules which have static directories,
> and be able to move the modules around to different
> "mount points" without having to modify any file.
>; Is it possible?

In the general case, no, because a static filter MUST know its own "mount point";, so that it can correctly parse the URL. For example, you could write something like the following stripped-down version of staticfilter:


import os
import urllib

import cherrypy
from cherrypy.lib import cptools
from cherrypy.filters.basefilter import BaseFilter


class MountedStatic(BaseFilter):
    """Filter that handles static content."""
   
   ; def __init__(self, dir, section):
        self.dir = dir
 ;       self.section = section
   
    def before_main(self):
    ;    request = cherrypy.request
        path = request.object_path
  ;      extra_path = path[len(self.section) + 1:]
 ;       extra_path = extra_path.lstrip(r"\/";)
     ;   extra_path = urllib.unquote(extra_path)
        # If extra_path is "", filename will end in a slash
        filename = os.path.join(self.dir, extra_path)
       ; # Deny uplevel attacks.
  ;      if not os.path.normpath(filename).startswith(os.path.normpath(self.dir)):
       ;     raise cherrypy.HTTPError(403) # Forbidden
        try:
          ;  cptools.serveFile(filename)
        ;    request.execute_main = False
        except cherrypy.NotFound:
    ;        # If we didn't find the static file, continue handling the
 ;           # request. We might find a dynamic handler instead.
  ;          pass


class Static:
   
    _cp_filters = [MountedStatic(os.path.dirname(__file__), ??????)]
  
    def index(self):
     ;   return "Hullabaloo";
    index.exposed = True


The filesystem directory is the easy part: just use dirname(__file__) as shown. But the '??????' is the tricky part, and needs to be changed depending on where you mount it.

You may be able to come up with some convention of your own that allows you to use the above for your specific situation, but there's no general solution.


Robert Brewer
System Architect
Amor Ministries
fumanchuamor.org


--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups "cherrypy-users" group.
To post to this group, send email to cherrypy-usersgooglegroups.com
To unsubscribe from this group, send email to cherrypy-users-unsubscribegooglegroups.com
For more options, visit this group at http://groups.google.com/group/cherrypy-users
-~----------~----~----~----~------~----~------~--~---
[1]

about | contact  Other archives ( Real Estate discussion Medical topics )