#
# middleware
#
from django.conf import settings
from django import http
class VhostMiddleware(object):
def process_request(self, req):
'''
Pull HTTP header containing the vhost base path into the request object
'''
vhost_header_name = getattr(settings, "VHOST_HEADER", "HTTP_X_BASE_PATH")
base_path = req.META.get(vhost_header_name, "")
## eliminate trailing slash ##
if base_path.endswith('/'):
base_path = base_path[:-1]
req.base_path = base_path
return None
def process_response(self, req, res):
'''
Adjust redirects to include the base path
'''
if res.status_code in (301,302) and not res["Location"].startswith(req.base_path) and res["Location"].startswith("/"):
res["Location"] = "%s%s" % (req.base_path,res["Location"])
return res
#
# Context Processor
#
def VhostContextProcessor(request):
ctx = {"base_path": ""}
try:
ctx["base_path"] = request.base_path
except:
pass
return ctx
Comments