Changed CommonMiddleware so it doesn't assume HTTP_HOST is set.

git-svn-id: http://code.djangoproject.com/svn/django/trunk@1548 bcc190cf-cafb-0310-a4f2-bffc1f526a37
This commit is contained in:
Adrian Holovaty 2005-12-05 15:25:55 +00:00
parent 3cd7755ec6
commit 79be9b2e7d
1 changed files with 6 additions and 3 deletions

View File

@ -30,9 +30,9 @@ class CommonMiddleware:
return httpwrappers.HttpResponseForbidden('<h1>Forbidden</h1>') return httpwrappers.HttpResponseForbidden('<h1>Forbidden</h1>')
# Check for a redirect based on settings.APPEND_SLASH and settings.PREPEND_WWW # Check for a redirect based on settings.APPEND_SLASH and settings.PREPEND_WWW
old_url = [request.META['HTTP_HOST'], request.path] old_url = [request.META.get('HTTP_HOST', ''), request.path]
new_url = old_url[:] new_url = old_url[:]
if settings.PREPEND_WWW and not old_url[0].startswith('www.'): if settings.PREPEND_WWW and old_url[0] and not old_url[0].startswith('www.'):
new_url[0] = 'www.' + old_url[0] new_url[0] = 'www.' + old_url[0]
# Append a slash if append_slash is set and the URL doesn't have a # Append a slash if append_slash is set and the URL doesn't have a
# trailing slash or a file extension. # trailing slash or a file extension.
@ -40,7 +40,10 @@ class CommonMiddleware:
new_url[1] = new_url[1] + '/' new_url[1] = new_url[1] + '/'
if new_url != old_url: if new_url != old_url:
# Redirect # Redirect
newurl = "%s://%s%s" % (os.environ.get('HTTPS') == 'on' and 'https' or 'http', new_url[0], new_url[1]) if new_url[0]:
newurl = "%s://%s%s" % (os.environ.get('HTTPS') == 'on' and 'https' or 'http', new_url[0], new_url[1])
else:
newurl = new_url[1]
if request.GET: if request.GET:
newurl += '?' + request.GET.urlencode() newurl += '?' + request.GET.urlencode()
return httpwrappers.HttpResponseRedirect(newurl) return httpwrappers.HttpResponseRedirect(newurl)