1. cherrypy
  2. CherryPy

Source

CherryPy / lib / filter / staticfilter.py

The branch 'cherrypy' does not exist.
import os
import urllib
from basefilter import BaseFilter


class StaticFilter(BaseFilter):
    """Filter that handles static content."""
    
    def beforeMain(self):
        from cherrypy import config, request
        from cherrypy.lib import cptools
        
        if not config.get('staticFilter.on', False):
            return
        
        path = request.objectPath
        
        regex = config.get('staticFilter.match', '')
        if regex:
            import re
            if not re.search(regex, path):
                return
        
        filename = config.get('staticFilter.file')
        if not filename:
            staticDir = config.get('staticFilter.dir')
            section = config.get('staticFilter.dir', returnSection=True)
            if section == 'global':
                section = "/"
            section = section.rstrip(r"\/")
            extraPath = path[len(section) + 1:]
            extraPath = extraPath.lstrip(r"\/")
            extraPath = urllib.unquote(extraPath)
            filename = os.path.join(staticDir, extraPath)
        
        # If filename is relative, make absolute using "root".
        if not os.path.isabs(filename):
            root = config.get('staticFilter.root', '').rstrip(r"\/")
            if root:
                filename = os.path.join(root, filename)
        
        cptools.serveFile(filename)