12
12
13
13
from django .http import (
14
14
FileResponse , Http404 , HttpResponse , HttpResponseNotModified ,
15
- HttpResponseRedirect ,
16
15
)
17
16
from django .template import Context , Engine , TemplateDoesNotExist , loader
17
+ from django .utils ._os import safe_join
18
18
from django .utils .http import http_date , parse_http_date
19
19
from django .utils .six .moves .urllib .parse import unquote
20
20
from django .utils .translation import ugettext as _ , ugettext_lazy
@@ -36,25 +36,11 @@ def serve(request, path, document_root=None, show_indexes=False):
36
36
but if you'd like to override it, you can create a template called
37
37
``static/directory_index.html``.
38
38
"""
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 )
55
41
if os .path .isdir (fullpath ):
56
42
if show_indexes :
57
- return directory_index (newpath , fullpath )
43
+ return directory_index (path , fullpath )
58
44
raise Http404 (_ ("Directory indexes are not allowed here." ))
59
45
if not os .path .exists (fullpath ):
60
46
raise Http404 (_ ('"%(path)s" does not exist' ) % {'path' : fullpath })
0 commit comments