| 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 |
|
|---|
| 44 |
filename = os.path.join(static_dir, extra_path) |
|---|
| 45 |
|
|---|
| 46 |
|
|---|
| 47 |
|
|---|
| 48 |
|
|---|
| 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 |
|
|---|
| 57 |
|
|---|
| 58 |
|
|---|
| 59 |
|
|---|
| 60 |
|
|---|
| 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) |
|---|
| 66 |
|
|---|
| 67 |
try: |
|---|
| 68 |
|
|---|
| 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 |
|
|---|
| 78 |
|
|---|
| 79 |
|
|---|
| 80 |
|
|---|
| 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 |
|---|