Code

Ticket #16976: fixedwsgi.py

File fixedwsgi.py, 874 bytes (added by antmorozov, 3 years ago)

Custom WSGI handler that solves the problem

Line 
1import sys, re
2from urlparse import urlparse
3from django.core.handlers.wsgi import WSGIRequest, WSGIHandler
4from django.utils.encoding import force_unicode
5
6class 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
20class FixedWSGIHandler(WSGIHandler):
21    request_class = FixedWSGIRequest