Skip to content

Commit

Permalink
Refactor how XStaticFileHandler interacts with parent class
Browse files Browse the repository at this point in the history
Hopefully fixes issue gh-2
  • Loading branch information
takluyver committed Jul 22, 2015
1 parent d9499b5 commit dfed651
Showing 1 changed file with 21 additions and 27 deletions.
48 changes: 21 additions & 27 deletions tornado_xstatic.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,32 +6,20 @@

class XStaticFileHandler(tornado.web.StaticFileHandler):
_cached_xstatic_data_dirs = {}
default_filename = None

def initialize(self, allowed_modules=None, **kwargs):
if allowed_modules:
self.allowed_modules = set(allowed_modules)
else:
self.allowed_modules = None

assert 'root' not in kwargs
# NOTE: Not wild on passing path=/ , because StaticFileHandler's own
# validation will let this serve any file. If this subclass is working
# correctly, that shouldn't be an issue, but...
super(XStaticFileHandler, self).initialize(path="/")

def parse_url_path(self, url_path):
if '/' not in url_path:
raise tornado.web.HTTPError(403, "XStatic module, not a file")
if self.allowed_modules is not None:
module_name = url_path.split('/', 1)[0]
if module_name not in self.allowed_modules:
raise tornado.web.HTTPError(
403, 'Access to XStatic module %s denied', module_name)

return super(XStaticFileHandler, self).parse_url_path(url_path)
# Not calling parent initialize() here because there's no root to set.
# If SFH ever gains further attributes set in initialize(), we'll need
# to copy them here.

@classmethod
def _get_xstatic_data_dir(cls, mod_name):
def get_xstatic_data_dir(cls, mod_name):
try:
return cls._cached_xstatic_data_dirs[mod_name]
except KeyError:
Expand All @@ -43,23 +31,29 @@ def _get_xstatic_data_dir(cls, mod_name):
cls._cached_xstatic_data_dirs[mod_name] = data_dir
return data_dir

@classmethod
def get_absolute_path(cls, root, path):
mod_name, path = path.split(os.path.sep, 1)
root = cls._get_xstatic_data_dir(mod_name)
abs_path = os.path.join(root, path)
if not abs_path.startswith(root):
def get(self, path, include_body=True):
if '/' not in path:
raise tornado.web.HTTPError(403, "XStatic module, not a file")

mod, path = path.split('/')
if (self.allowed_modules is not None) and (mod not in self.allowed_modules):
raise tornado.web.HTTPError(
403, "Request for file outside XStatic package %s: %s", mod_name, path)
403, 'Access to XStatic module %s denied', mod)

return abs_path
# This stateful setting of root seems awkward, but SFH is already stateful
# (self.path, self.absolute_path, self.modified), and from examining
# the code of SFH, this should work.
self.root = self.get_xstatic_data_dir(mod)
return super(XStaticFileHandler, self).get(path, include_body=include_body)


def url_maker(prefix, include_version=True):
def make_url(package, path):
if include_version:
fs_style_path = package + os.path.sep + path.replace("/", os.path.sep)
version_bit = "?v=" + XStaticFileHandler.get_version({'static_path': ''}, fs_style_path)
fs_style_path = path.replace("/", os.path.sep)
pkg_dir = XStaticFileHandler.get_xstatic_data_dir(package)
version_bit = "?v=" + XStaticFileHandler.get_version(
{'static_path': pkg_dir}, fs_style_path)
else:
version_bit = ""
return prefix + package + "/" + path + version_bit
Expand Down

0 comments on commit dfed651

Please sign in to comment.