2
我有非ASCII字符类似于http://blå.no的域与其的Punycode等效注册的域:如何在Django的csrf中间件中处理utf-8与punycode问题?
xn--bl-zia.no
其也在Apache虚拟主机设置:
<VirtualHost *:443>
ServerName xn--bl-zia.no
...
I”的问题m看到来自请求包含:
'HTTP_USER_AGENT': 'Mozilla/5.0 (Windows NT 6.3; WOW64; Trident/7.0; rv:11.0) like Gecko',
'HTTP_HOST': 'xn--bl-zia.no',
'SERVER_NAME': 'xn--bl-zia.no',
'HTTP_REFERER': 'https://bl\xc3\xa5.no/login/ka/?next=/start-exam/participant-login/',
'HTTP_X_REQUESTED_WITH': 'XMLHttpRequest',
ie。引用者是以utf-8发送的,而不是punycode。我得到的例外是:
Traceback (most recent call last):
File "/srv/cleanup-project/venv/dev/lib/python2.7/site-packages/django/core/handlers/base.py", line 153, in get_response
response = callback(request, **param_dict)
File "/srv/cleanup-project/venv/dev/lib/python2.7/site-packages/django/utils/decorators.py", line 87, in _wrapped_view
result = middleware.process_view(request, view_func, args, kwargs)
File "/srv/cleanup-project/venv/dev/lib/python2.7/site-packages/django/middleware/csrf.py", line 157, in process_view
reason = REASON_BAD_REFERER % (referer, good_referer)
UnicodeDecodeError: 'ascii' codec can't decode byte 0xc3 in position 10: ordinal not in range(128)
在csrf.py
相关的代码是:
good_referer = 'https://%s/' % request.get_host()
if not same_origin(referer, good_referer):
reason = REASON_BAD_REFERER % (referer, good_referer)
(get_host()
从请求使用SERVER_NAME
)
有一个本地的Django的方式来处理这个,还是我需要编写一个中间件,将utf-8转换为referer头域部分的punycode?