1212
1313from django .http import (
1414 FileResponse , Http404 , HttpResponse , HttpResponseNotModified ,
15- HttpResponseRedirect ,
1615)
1716from django .template import Context , Engine , TemplateDoesNotExist , loader
17+ from django .utils ._os import safe_join
1818from django .utils .http import http_date , parse_http_date
1919from django .utils .six .moves .urllib .parse import unquote
2020from django .utils .translation import ugettext as _ , ugettext_lazy
@@ -36,25 +36,11 @@ def serve(request, path, document_root=None, show_indexes=False):
3636 but if you'd like to override it, you can create a template called
3737 ``static/directory_index.html``.
3838 """
39- path = posixpath .normpath (unquote (path ))
40- path = path .lstrip ('/' )
41- newpath = ''
42- for part in path .split ('/' ):
43- if not part :
44- # Strip empty path components.
45- continue
46- drive , part = os .path .splitdrive (part )
47- head , part = os .path .split (part )
48- if part in (os .curdir , os .pardir ):
49- # Strip '.' and '..' in path.
50- continue
51- newpath = os .path .join (newpath , part ).replace ('\\ ' , '/' )
52- if newpath and path != newpath :
53- return HttpResponseRedirect (newpath )
54- fullpath = os .path .join (document_root , newpath )
39+ path = posixpath .normpath (unquote (path )).lstrip ('/' )
40+ fullpath = safe_join (document_root , path )
5541 if os .path .isdir (fullpath ):
5642 if show_indexes :
57- return directory_index (newpath , fullpath )
43+ return directory_index (path , fullpath )
5844 raise Http404 (_ ("Directory indexes are not allowed here." ))
5945 if not os .path .exists (fullpath ):
6046 raise Http404 (_ ('"%(path)s" does not exist' ) % {'path' : fullpath })
0 commit comments