Commit c71b32e2 authored by Jean-Francois Dockes's avatar Jean-Francois Dockes
Browse files

Allow streaming binary non-utf8 paths

parent 1b4f8e5b
......@@ -2866,9 +2866,12 @@ def static_file(filename, root,
check or continue partial downloads) are also handled automatically.
"""
if type(filename) == type(''):
root = os.path.join(os.path.abspath(root), '')
filename = os.path.abspath(os.path.join(root, filename.strip('/\\')))
else:
root = os.path.join(os.path.abspath(root), b'')
filename = os.path.abspath(os.path.join(root, filename.strip(b'/\\')))
headers = dict()
if not filename.startswith(root):
......
......@@ -85,7 +85,20 @@ class Streamer(object):
bottle.response.set_header("Content-type", ctype)
bottle.response.set_header("Content-Length", size)
return f
# Binary paths: transmitted as follows (See bottle._handle())
# binarypath->binarypath.decode('latin1')->urlquote()->NETWORK->
# urlunquote()->encode('latin1').decode('utf-8', 'ignore')
# If there are non utf-8 characters in the path, the result is
# missing them, and file access fails. However, we get the
# binary result from urlunquote)_ in the 'bottle.raw_path'
# request environment variable, and try to use it if the
# normal path is not accessible.
fullpath = os.path.join(self.root, filepath)
root = '/'
if not os.path.exists(fullpath):
fullpath = bottle.request.environ.get('bottle.raw_path')
fullpath = fullpath.encode('latin1')
root = b'/'
if not os.path.exists(fullpath):
uplog("uprcl: no such file: %s" % fullpath)
return bottle.HTTPResponse(status=404)
......@@ -93,10 +106,10 @@ class Streamer(object):
mutf = mutagen.File(fullpath)
if mutf:
return bottle.static_file(filepath, root=self.root,
return bottle.static_file(fullpath, root=root,
mimetype=mutf.mime[0])
else:
return bottle.static_file(filepath, root=self.root)
return bottle.static_file(fullpath, root=root)
# Bottle handle both the streaming and control requests.
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment