Ticket #987: redirect_absoluteuri.patch

File redirect_absoluteuri.patch, 2.0 KB (added by Chris Beaven, 17 years ago)
  • django/middleware/common.py

     
    11from django.conf import settings
    22from django import http
    33from django.core.mail import mail_managers
     4from urlparse import urljoin
    45import md5
    56import re
    67
     
    1718        - ETags: If the USE_ETAGS setting is set, ETags will be calculated from
    1819          the entire page content and Not Modified responses will be returned
    1920          appropriately.
     21
     22        - Absolute URI redirects: Ensures that any redirect attempt (which uses
     23          the HTTP Location header) to a relative location is converted to an
     24          absolute URI, as required by RFC 2616, section 14.30.
    2025    """
    2126
    2227    def process_request(self, request):
     
    8489            else:
    8590                response['ETag'] = etag
    8691
     92        # Absolute URI redirects.
     93        if 'Location' in response.headers:
     94            location = response['Location']
     95            if not ':' in location:
     96                current_uri = '%s://%s%s' % (request.is_secure() and 'https' or 'http',
     97                                             http.get_host(request),
     98                                             request.path)
     99                response['Location'] = urljoin(current_uri, location)
     100
    87101        return response
    88102
    89103def _is_ignorable_404(uri):
  • docs/middleware.txt

     
    7878  MD5-hashing the page content, and it'll take care of sending
    7979  ``Not Modified`` responses, if appropriate.
    8080
     81* Ensures that any redirect attempt to a relative location is converted to an
     82  absolute URI, as required by `RFC 2616`_.
     83
     84.. _RFC 2616: http://www.w3.org/Protocols/rfc2616/rfc2616-sec14.html#sec14.30
     85
    8186django.middleware.doc.XViewMiddleware
    8287-------------------------------------
    8388
Back to Top