1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39 | #
# 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