-
Notifications
You must be signed in to change notification settings - Fork 0
/
apache_redirect.py
91 lines (76 loc) · 2.7 KB
/
apache_redirect.py
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
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
# Copyright 2008 Kumina bv.
#
# This file is part of webredirects.
#
# webredirects is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 2 of the License, or
# (at your option) any later version.
#
# webredirects is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with webredirects. If not, see <http://www.gnu.org/licenses/>.
from mod_python import apache, util
import os, re
import settings
if (settings.MEMCACHED_SERVERS is not None) and (len(settings.MEMCACHED_SERVERS) > 0):
import cmemcache
use_memcache = True
else:
use_memcache = False
os.environ["DJANGO_SETTINGS_MODULE"] = "webredirects.settings"
from django.contrib.sites.models import Site
from webredirects.redirects.models import SiteAlias, Redirect
from django.core.exceptions import ObjectDoesNotExist
def get_target_uri(hostname, path):
target_uri = False
target_domain = hostname
# Redirect "foo.nl" to "www.foo.nl".
m = re.compile('^([^.]+\.[^.]+$)').match(target_domain)
if m:
target_domain = "www.%s" % m.group(1)
target_uri = "http://%s%s" % (target_domain, path)
# Find aliases for domains
try:
sitealias = SiteAlias.objects.get(alias = hostname)
target_domain = sitealias.site.domain
target_uri = "http://%s%s" % (target_domain, path)
except ObjectDoesNotExist:
pass
# Find redirects for urls
try:
site = Site.objects.get(domain = target_domain)
target_uri = site.redirect_set.get(source_url = path).target_url
if (target_uri[0] == '/') and (target_domain == hostname):
# Internal redirect
target_uri = "%s" % target_uri
elif target_uri.startswith("http://") or target_uri.startswith("https://"):
# External redirect
target_uri = target_uri
except ObjectDoesNotExist:
pass
return target_uri
def headerparserhandler(req):
target_uri = None
# Only redirect if a Host header has been set.
if req.hostname is None:
return apache.DECLINED
# Try a cached lookup first.
if use_memcache:
mc = cmemcache.Client(settings.MEMCACHED_SERVERS)
target_uri = mc.get(req.hostname + req.uri)
# Do a full lookup if there is no cache entry.
if target_uri is None:
target_uri = get_target_uri(req.hostname, req.uri)
if use_memcache:
mc.set(req.hostname + req.uri, target_uri)
if not target_uri:
return apache.DECLINED
else:
req.headers_out['Location'] = target_uri
req.status = apache.HTTP_MOVED_PERMANENTLY
return apache.HTTP_MOVED_PERMANENTLY