web/lib/django/views/static.py
changeset 29 cc9b7e14412b
parent 0 0d40e90630ef
equal deleted inserted replaced
28:b758351d191f 29:cc9b7e14412b
    49         return HttpResponseRedirect(newpath)
    49         return HttpResponseRedirect(newpath)
    50     fullpath = os.path.join(document_root, newpath)
    50     fullpath = os.path.join(document_root, newpath)
    51     if os.path.isdir(fullpath):
    51     if os.path.isdir(fullpath):
    52         if show_indexes:
    52         if show_indexes:
    53             return directory_index(newpath, fullpath)
    53             return directory_index(newpath, fullpath)
    54         raise Http404, "Directory indexes are not allowed here."
    54         raise Http404("Directory indexes are not allowed here.")
    55     if not os.path.exists(fullpath):
    55     if not os.path.exists(fullpath):
    56         raise Http404, '"%s" does not exist' % fullpath
    56         raise Http404('"%s" does not exist' % fullpath)
    57     # Respect the If-Modified-Since header.
    57     # Respect the If-Modified-Since header.
    58     statobj = os.stat(fullpath)
    58     statobj = os.stat(fullpath)
       
    59     mimetype = mimetypes.guess_type(fullpath)[0] or 'application/octet-stream'
    59     if not was_modified_since(request.META.get('HTTP_IF_MODIFIED_SINCE'),
    60     if not was_modified_since(request.META.get('HTTP_IF_MODIFIED_SINCE'),
    60                               statobj[stat.ST_MTIME], statobj[stat.ST_SIZE]):
    61                               statobj[stat.ST_MTIME], statobj[stat.ST_SIZE]):
    61         return HttpResponseNotModified()
    62         return HttpResponseNotModified(mimetype=mimetype)
    62     mimetype = mimetypes.guess_type(fullpath)[0] or 'application/octet-stream'
       
    63     contents = open(fullpath, 'rb').read()
    63     contents = open(fullpath, 'rb').read()
    64     response = HttpResponse(contents, mimetype=mimetype)
    64     response = HttpResponse(contents, mimetype=mimetype)
    65     response["Last-Modified"] = http_date(statobj[stat.ST_MTIME])
    65     response["Last-Modified"] = http_date(statobj[stat.ST_MTIME])
    66     response["Content-Length"] = len(contents)
    66     response["Content-Length"] = len(contents)
    67     return response
    67     return response