Merge pull request #1054 from andkit/fix-1034

Fix unicode errors when getting static files (fixes #1034)
This commit is contained in:
Oskar Hahn 2013-11-14 13:53:36 -08:00
commit 89c7bd1277

View File

@ -1,5 +1,6 @@
# -*- coding: utf-8 -*- # -*- coding: utf-8 -*-
import os
import posixpath import posixpath
from urllib import unquote from urllib import unquote
@ -10,7 +11,12 @@ from sockjs.tornado import SockJSRouter, SockJSConnection
from tornado.httpserver import HTTPServer from tornado.httpserver import HTTPServer
from tornado.ioloop import IOLoop from tornado.ioloop import IOLoop
from tornado.options import parse_command_line from tornado.options import parse_command_line
from tornado.web import Application, FallbackHandler, StaticFileHandler from tornado.web import (
Application,
FallbackHandler,
StaticFileHandler,
HTTPError
)
from tornado.wsgi import WSGIContainer from tornado.wsgi import WSGIContainer
@ -19,14 +25,29 @@ class DjangoStaticFileHandler(StaticFileHandler):
def initialize(self): def initialize(self):
"""Overwrite some attributes.""" """Overwrite some attributes."""
self.root = '' # NOTE: root is never actually used and default_filename is not
# supported (must always be None)
self.root = u''
self.default_filename = None self.default_filename = None
def get(self, path, include_body=True): @classmethod
def get_absolute_path(cls, root, path):
from django.contrib.staticfiles import finders from django.contrib.staticfiles import finders
normalized_path = posixpath.normpath(unquote(path)).lstrip('/') normalized_path = posixpath.normpath(unquote(path)).lstrip('/')
absolute_path = finders.find(normalized_path) absolute_path = finders.find(normalized_path)
return super(DjangoStaticFileHandler, self).get(absolute_path, include_body) return absolute_path
def validate_absolute_path(self, root, absolute_path):
# differences from base implementation:
# - we ignore self.root since our files do not necessarily have
# a shared root prefix
# - we do not handle self.default_filename (we do not use it and it
# does not make much sense here anyway)
if not os.path.exists(absolute_path):
raise HTTPError(404)
if not os.path.isfile(absolute_path):
raise HTTPError(403, "%s is not a file", self.path)
return absolute_path
class ProjectorSocketHandler(SockJSConnection): class ProjectorSocketHandler(SockJSConnection):