Download Install Tutorial Docs FAQ Tools WikiLicense Team IRC Planet Involvement Shop Book

root/branches/cherrypy-2.x/cherrypy/filters/staticfilter.py

Revision 1003 (checked in by rdelon, 3 years ago)

PEP8ifying static filter

  • Property svn:eol-style set to native
Line 
1 import os
2 import urllib
3
4 import cherrypy
5 from cherrypy.lib import cptools
6 from cherrypy.filters.basefilter import BaseFilter
7
8
9 class StaticFilter(BaseFilter):
10     """Filter that handles static content."""
11    
12     def before_main(self):
13         config = cherrypy.config
14         if not config.get('static_filter.on', False):
15             return
16        
17         request = cherrypy.request
18         path = request.object_path
19        
20         regex = config.get('static_filter.match', '')
21         if regex:
22             import re
23             if not re.search(regex, path):
24                 return
25        
26         root = config.get('static_filter.root', '').rstrip(r"\/")
27         filename = config.get('static_filter.file')
28         if filename:
29             static_dir = None
30         else:
31             static_dir = config.get('static_filter.dir')
32             if not static_dir:
33                 msg = ("StaticFilter requires either static_filter.file "
34                        "or static_filter.dir (%s)" % request.path)
35                 raise cherrypy.WrongConfigValue(msg)
36             section = config.get('static_filter.dir', return_section = True)
37             if section == 'global':
38                 section = "/"
39             section = section.rstrip(r"\/")
40             extra_path = path[len(section) + 1:]
41             extra_path = extra_path.lstrip(r"\/")
42             extra_path = urllib.unquote(extra_path)
43             # If extra_path is "", filename will end in a slash
44             filename = os.path.join(static_dir, extra_path)
45        
46         # If filename is relative, make absolute using "root".
47         # Note that, if "root" isn't defined, we still may send
48         # a relative path to serveFile.
49         if not os.path.isabs(filename):
50             if not root:
51                 msg = ("StaticFilter requires an absolute final path. "
52                        "Make static_filter.dir, .file, or .root absolute.")
53                 raise cherrypy.WrongConfigValue(msg)
54             filename = os.path.join(root, filename)
55        
56         # If we used static_filter.dir, then there's a chance that the
57         # extra_path pulled from the URL might have ".." or similar uplevel
58         # attacks in it. Check that the final file is a child of static_dir.
59         # Note that we do not check static_filter.file--that can point
60         # anywhere (since it does not use the request URL).
61         if static_dir:
62             if not os.path.isabs(static_dir):
63                 static_dir = os.path.join(root, static_dir)
64             if not os.path.normpath(filename).startswith(os.path.normpath(static_dir)):
65                 raise cherrypy.HTTPError(403) # Forbidden
66            
67         try:
68             # you can set the content types for a complete directory per extension
69             content_types = config.get('static_filter.content_types', None)
70             content_type = None
71             if content_types:
72                 root, ext = os.path.splitext(filename)
73                 content_type = content_types.get(ext[1:], None)
74             cptools.serveFile(filename, contentType=content_type)
75             request.execute_main = False
76         except cherrypy.NotFound:
77             # If we didn't find the static file, continue handling the
78             # request. We might find a dynamic handler instead.
79            
80             # But first check for an index file if a folder was requested.
81             if filename[-1:] in ("/", "\\"):
82                 idx = config.get('static_filter.index', '')
83                 if idx:
84                     try:
85                         cptools.serveFile(os.path.join(filename, idx))
86                         request.execute_main = False
87                     except cherrypy.NotFound:
88                         pass
Note: See TracBrowser for help on using the browser.

Hosted by WebFaction

Log in as guest/cpguest to create tickets