~ubuntu-branches/ubuntu/jaunty/python-django/jaunty

« back to all changes in this revision

Viewing changes to django/views/static.py

  • Committer: Bazaar Package Importer
  • Author(s): Scott James Remnant, Eddy Mulyono
  • Date: 2008-09-16 12:18:47 UTC
  • mfrom: (1.1.5 upstream) (4.1.1 lenny)
  • Revision ID: james.westby@ubuntu.com-20080916121847-mg225rg5mnsdqzr0
Tags: 1.0-1ubuntu1
* Merge from Debian (LP: #264191), remaining changes:
  - Run test suite on build.

[Eddy Mulyono]
* Update patch to workaround network test case failures.

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
from django.template import loader
2
 
from django.http import Http404, HttpResponse, HttpResponseRedirect, HttpResponseNotModified
3
 
from django.template import Template, Context, TemplateDoesNotExist
 
1
"""
 
2
Views and functions for serving static files. These are only to be used
 
3
during development, and SHOULD NOT be used in a production setting.
 
4
"""
 
5
 
4
6
import mimetypes
5
7
import os
6
8
import posixpath
7
9
import re
8
 
import rfc822
9
10
import stat
10
11
import urllib
 
12
from email.Utils import parsedate_tz, mktime_tz
 
13
 
 
14
from django.template import loader
 
15
from django.http import Http404, HttpResponse, HttpResponseRedirect, HttpResponseNotModified
 
16
from django.template import Template, Context, TemplateDoesNotExist
 
17
from django.utils.http import http_date
11
18
 
12
19
def serve(request, path, document_root=None, show_indexes=False):
13
20
    """
26
33
 
27
34
    # Clean up given path to only allow serving files below document_root.
28
35
    path = posixpath.normpath(urllib.unquote(path))
 
36
    path = path.lstrip('/')
29
37
    newpath = ''
30
38
    for part in path.split('/'):
31
39
        if not part:
32
 
            # strip empty path components
 
40
            # Strip empty path components.
33
41
            continue
34
42
        drive, part = os.path.splitdrive(part)
35
43
        head, part = os.path.split(part)
36
44
        if part in (os.curdir, os.pardir):
37
 
            # strip '.' amd '..' in path
 
45
            # Strip '.' and '..' in path.
38
46
            continue
39
47
        newpath = os.path.join(newpath, part).replace('\\', '/')
40
48
    if newpath and path != newpath:
51
59
    if not was_modified_since(request.META.get('HTTP_IF_MODIFIED_SINCE'),
52
60
                              statobj[stat.ST_MTIME], statobj[stat.ST_SIZE]):
53
61
        return HttpResponseNotModified()
54
 
    mimetype = mimetypes.guess_type(fullpath)[0]
 
62
    mimetype = mimetypes.guess_type(fullpath)[0] or 'application/octet-stream'
55
63
    contents = open(fullpath, 'rb').read()
56
64
    response = HttpResponse(contents, mimetype=mimetype)
57
 
    response["Last-Modified"] = rfc822.formatdate(statobj[stat.ST_MTIME])
 
65
    response["Last-Modified"] = http_date(statobj[stat.ST_MTIME])
 
66
    response["Content-Length"] = len(contents)
58
67
    return response
59
68
 
60
69
DEFAULT_DIRECTORY_INDEX_TEMPLATE = """
113
122
            raise ValueError
114
123
        matches = re.match(r"^([^;]+)(; length=([0-9]+))?$", header,
115
124
                           re.IGNORECASE)
116
 
        header_mtime = rfc822.mktime_tz(rfc822.parsedate_tz(
117
 
            matches.group(1)))
 
125
        header_mtime = mktime_tz(parsedate_tz(matches.group(1)))
118
126
        header_len = matches.group(3)
119
127
        if header_len and int(header_len) != size:
120
128
            raise ValueError