diff options
Diffstat (limited to 'cherrypy/lib/static.py')
| -rw-r--r-- | cherrypy/lib/static.py | 101 |
1 files changed, 55 insertions, 46 deletions
diff --git a/cherrypy/lib/static.py b/cherrypy/lib/static.py index 6a78fc13..ed6467a0 100644 --- a/cherrypy/lib/static.py +++ b/cherrypy/lib/static.py @@ -1,23 +1,32 @@ +"""Module with helpers for serving static files.""" + import os +import platform import re import stat import mimetypes -try: - from io import UnsupportedOperation -except ImportError: - UnsupportedOperation = object() +from email.generator import _make_boundary as make_boundary +from io import UnsupportedOperation + +from six.moves import urllib import cherrypy -from cherrypy._cpcompat import ntob, unquote +from cherrypy._cpcompat import ntob from cherrypy.lib import cptools, httputil, file_generator_limited -mimetypes.init() -mimetypes.types_map['.dwg'] = 'image/x-dwg' -mimetypes.types_map['.ico'] = 'image/x-icon' -mimetypes.types_map['.bz2'] = 'application/x-bzip2' -mimetypes.types_map['.gz'] = 'application/x-gzip' +def _setup_mimetypes(): + """Pre-initialize global mimetype map.""" + if not mimetypes.inited: + mimetypes.init() + mimetypes.types_map['.dwg'] = 'image/x-dwg' + mimetypes.types_map['.ico'] = 'image/x-icon' + mimetypes.types_map['.bz2'] = 'application/x-bzip2' + mimetypes.types_map['.gz'] = 'application/x-gzip' + + +_setup_mimetypes() def serve_file(path, content_type=None, disposition=None, name=None, @@ -33,7 +42,6 @@ def serve_file(path, content_type=None, disposition=None, name=None, to the basename of path. If disposition is None, no Content-Disposition header will be written. """ - response = cherrypy.serving.response # If path is relative, users should fix it by making path absolute. @@ -71,7 +79,7 @@ def serve_file(path, content_type=None, disposition=None, name=None, if content_type is None: # Set content-type based on filename extension - ext = "" + ext = '' i = path.rfind('.') if i != -1: ext = path[i:].lower() @@ -86,7 +94,7 @@ def serve_file(path, content_type=None, disposition=None, name=None, if name is None: name = os.path.basename(path) cd = '%s; filename="%s"' % (disposition, name) - response.headers["Content-Disposition"] = cd + response.headers['Content-Disposition'] = cd if debug: cherrypy.log('Content-Disposition: %r' % cd, 'TOOLS.STATIC') @@ -115,7 +123,6 @@ def serve_fileobj(fileobj, content_type=None, disposition=None, name=None, serve_fileobj(), expecting that the data would be served starting from that position. """ - response = cherrypy.serving.response try: @@ -144,7 +151,7 @@ def serve_fileobj(fileobj, content_type=None, disposition=None, name=None, cd = disposition else: cd = '%s; filename="%s"' % (disposition, name) - response.headers["Content-Disposition"] = cd + response.headers['Content-Disposition'] = cd if debug: cherrypy.log('Content-Disposition: %r' % cd, 'TOOLS.STATIC') @@ -158,12 +165,12 @@ def _serve_fileobj(fileobj, content_type, content_length, debug=False): # HTTP/1.0 didn't have Range/Accept-Ranges headers, or the 206 code request = cherrypy.serving.request if request.protocol >= (1, 1): - response.headers["Accept-Ranges"] = "bytes" + response.headers['Accept-Ranges'] = 'bytes' r = httputil.get_ranges(request.headers.get('Range'), content_length) if r == []: - response.headers['Content-Range'] = "bytes */%s" % content_length - message = ("Invalid Range (first-byte-pos greater than " - "Content-Length)") + response.headers['Content-Range'] = 'bytes */%s' % content_length + message = ('Invalid Range (first-byte-pos greater than ' + 'Content-Length)') if debug: cherrypy.log(message, 'TOOLS.STATIC') raise cherrypy.HTTPError(416, message) @@ -179,31 +186,25 @@ def _serve_fileobj(fileobj, content_type, content_length, debug=False): cherrypy.log( 'Single part; start: %r, stop: %r' % (start, stop), 'TOOLS.STATIC') - response.status = "206 Partial Content" + response.status = '206 Partial Content' response.headers['Content-Range'] = ( - "bytes %s-%s/%s" % (start, stop - 1, content_length)) + 'bytes %s-%s/%s' % (start, stop - 1, content_length)) response.headers['Content-Length'] = r_len fileobj.seek(start) response.body = file_generator_limited(fileobj, r_len) else: # Return a multipart/byteranges response. - response.status = "206 Partial Content" - try: - # Python 3 - from email.generator import _make_boundary as make_boundary - except ImportError: - # Python 2 - from mimetools import choose_boundary as make_boundary + response.status = '206 Partial Content' boundary = make_boundary() - ct = "multipart/byteranges; boundary=%s" % boundary + ct = 'multipart/byteranges; boundary=%s' % boundary response.headers['Content-Type'] = ct - if "Content-Length" in response.headers: + if 'Content-Length' in response.headers: # Delete Content-Length header so finalize() recalcs it. - del response.headers["Content-Length"] + del response.headers['Content-Length'] def file_ranges(): # Apache compatibility: - yield ntob("\r\n") + yield ntob('\r\n') for start, stop in r: if debug: @@ -211,23 +212,23 @@ def _serve_fileobj(fileobj, content_type, content_length, debug=False): 'Multipart; start: %r, stop: %r' % ( start, stop), 'TOOLS.STATIC') - yield ntob("--" + boundary, 'ascii') - yield ntob("\r\nContent-type: %s" % content_type, + yield ntob('--' + boundary, 'ascii') + yield ntob('\r\nContent-type: %s' % content_type, 'ascii') yield ntob( - "\r\nContent-range: bytes %s-%s/%s\r\n\r\n" % ( + '\r\nContent-range: bytes %s-%s/%s\r\n\r\n' % ( start, stop - 1, content_length), 'ascii') fileobj.seek(start) gen = file_generator_limited(fileobj, stop - start) for chunk in gen: yield chunk - yield ntob("\r\n") + yield ntob('\r\n') # Final boundary - yield ntob("--" + boundary + "--", 'ascii') + yield ntob('--' + boundary + '--', 'ascii') # Apache compatibility: - yield ntob("\r\n") + yield ntob('\r\n') response.body = file_ranges() return response.body else: @@ -244,7 +245,7 @@ def _serve_fileobj(fileobj, content_type, content_length, debug=False): def serve_download(path, name=None): """Serve 'path' as an application/x-download attachment.""" # This is such a common idiom I felt it deserved its own wrapper. - return serve_file(path, "application/x-download", "attachment", name) + return serve_file(path, 'application/x-download', 'attachment', name) def _attempt(filename, content_types, debug=False): @@ -268,7 +269,7 @@ def _attempt(filename, content_types, debug=False): return False -def staticdir(section, dir, root="", match="", content_types=None, index="", +def staticdir(section, dir, root='', match='', content_types=None, index='', debug=False): """Serve a static resource from the given (root +) dir. @@ -306,7 +307,7 @@ def staticdir(section, dir, root="", match="", content_types=None, index="", # If dir is relative, make absolute using "root". if not os.path.isabs(dir): if not root: - msg = "Static dir requires an absolute dir (or root)." + msg = 'Static dir requires an absolute dir (or root).' if debug: cherrypy.log(msg, 'TOOLS.STATICDIR') raise ValueError(msg) @@ -315,10 +316,18 @@ def staticdir(section, dir, root="", match="", content_types=None, index="", # Determine where we are in the object tree relative to 'section' # (where the static tool was defined). if section == 'global': - section = "/" - section = section.rstrip(r"\/") + section = '/' + section = section.rstrip(r'\/') branch = request.path_info[len(section) + 1:] - branch = unquote(branch.lstrip(r"\/")) + branch = urllib.parse.unquote(branch.lstrip(r'\/')) + + # Requesting a file in sub-dir of the staticdir results + # in mixing of delimiter styles, e.g. C:\static\js/script.js. + # Windows accepts this form except not when the path is + # supplied in extended-path notation, e.g. \\?\C:\static\js/script.js. + # http://bit.ly/1vdioCX + if platform.system() == 'Windows': + branch = branch.replace('/', '\\') # If branch is "", filename will end in a slash filename = os.path.join(dir, branch) @@ -338,11 +347,11 @@ def staticdir(section, dir, root="", match="", content_types=None, index="", if index: handled = _attempt(os.path.join(filename, index), content_types) if handled: - request.is_index = filename[-1] in (r"\/") + request.is_index = filename[-1] in (r'\/') return handled -def staticfile(filename, root=None, match="", content_types=None, debug=False): +def staticfile(filename, root=None, match='', content_types=None, debug=False): """Serve a static resource from the given (root +) filename. match |
