This repository has been archived by the owner on Nov 11, 2018. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 2
/
redirects.py
64 lines (49 loc) · 2.19 KB
/
redirects.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
# redirects handlers
import webapp2
import re
from webapp2_extras import json
from models import Domain
from urlparse import urlparse
import csv
class List(webapp2.RequestHandler):
"""
List configured redirects
TODO: Implement ETag / Last-Modified handling
"""
def get(self):
if 'Accept' in self.request.headers:
if self.request.headers['Accept'] == 'application/json':
self.list_redirects_json()
return
if self.request.headers['Accept'] == 'text/csv':
self.list_redirects_csv()
return
webapp2.abort(406)
def list_redirects_json(self):
redirects = []
domains = Domain.query(Domain.redirect_enabled == True)
for domain in domains.iter():
host = urlparse(domain.redirect_url)
rule = dict(hosts=[host.netloc])
rule["rules"] = [
{"from": "^" + re.escape(domain.redirect_url), "to": "http://" + domain.name + "/"}]
if re.match('^www\.', host.netloc):
# Add rule without www
rule["hosts"].append(host.netloc[4:])
nowww_redirect_url = re.sub('//www\.', '//', domain.redirect_url)
rule["rules"].append({"from": "^" + re.escape(nowww_redirect_url), "to": "http://" + domain.name + "/"})
else:
# Add rule with www
rule["hosts"].append("www." + host.netloc)
www_redirect_url = re.sub('(https*://)', '\g<1>www.', domain.redirect_url)
rule["rules"].append({"from": "^" + re.escape(www_redirect_url), "to": "http://" + domain.name + "/"})
rule["exceptions"] = []
redirects.append(rule)
self.response.headers['Content-Type'] = 'application/json'
self.response.write(json.encode(redirects))
def list_redirects_csv(self):
self.response.headers['Content-Type'] = 'text/csv'
writer = csv.writer(self.response, delimiter=',', quotechar='"', quoting=csv.QUOTE_MINIMAL)
domains = Domain.query(Domain.redirect_enabled == True)
for domain in domains.iter():
writer.writerow([domain.name, domain.redirect_url])