1 | import sys, re
|
---|
2 | from urlparse import urlparse
|
---|
3 | from django.core.handlers.wsgi import WSGIRequest, WSGIHandler
|
---|
4 | from django.utils.encoding import force_unicode
|
---|
5 |
|
---|
6 | class FixedWSGIRequest(WSGIRequest):
|
---|
7 | def __init__(self, environ):
|
---|
8 | super(FixedWSGIRequest, self).__init__(environ)
|
---|
9 | if 'HTTP_REFERER' in self.META:
|
---|
10 | parsed_url = urlparse(self.META['HTTP_REFERER'])
|
---|
11 | if sys.version_info >= (2, 6):
|
---|
12 | host = parsed_url.netloc
|
---|
13 | else:
|
---|
14 | host = parsed_url[1]
|
---|
15 | domain = host.split(':')[0]
|
---|
16 | if not re.match(r'^[a-zA-Z\d\-\.]+$', domain):
|
---|
17 | punycode_domain = force_unicode(domain).encode('idna')
|
---|
18 | self.META['HTTP_REFERER'] = self.META['HTTP_REFERER'].replace(domain, punycode_domain, 1)
|
---|
19 |
|
---|
20 | class FixedWSGIHandler(WSGIHandler):
|
---|
21 | request_class = FixedWSGIRequest
|
---|