2

我正在使用 pylons,并且我的一些 url 包含非英文字符,例如:

http://localhost:5000/article/111/文章标题

在大多数情况下,这不是问题,但是在我的登录模块中,在用户注销后,我尝试referer从中获取request.headers,并重定向到该 url。

if user_logout:
    referer = request.headers.get('referer', '/')
    redirect(referer)

不幸的是,如果url中包含非英文字符,并且用IE浏览器会报这样的错误(Firefox是可以的):

  WebError Traceback:
  UnicodeDecodeError: 'ascii' codec can't decode byte 0xd5 in position 140: ordinal not in range(128) 
View as:   Interactive (full)  |  Text (full)  |  XML (full) clear this 
clear this 
URL: http://localhost:5000/users/logout

Module weberror.evalexception:431 in respond          view

有一种方法可以修复它(但不好),用于urllib.quote()在重定向之前转换 url。

referer = quote_path(url) # only quote the path of the url
redirect(referer)

这不是一个好的解决方案,因为它只有在浏览器是 IE 的情况下才有效,而且非常无聊。有什么好的解决办法吗?

4

3 回答 3

1

尝试检查非 ascii URL 的 RFC。如果我没记错的话,它们会被转换为 ascii 等价物。然后你可以重定向到那个。

编辑:根据@ssokolov(见下面的评论):

要查找的具体术语是 IDN(国际化域名)和 Punycode

于 2010-09-02T06:51:22.103 回答
1

最后,我还是没有找到好的解决方案,并使用以下代码:

referer = urllib.quote(referer, '.:/?=;-%#')

现在看起来效果很好,但我觉得不安全。

于 2010-09-02T09:16:27.990 回答
0

重定向通过引发异常来工作。这被捕获并转换为 HTTP 响应如何为您的响应指定字符集?

response.charset = 'utf8'

于 2010-09-02T05:57:51.033 回答