我正在使用 NGINX 来分割移动 WAP/HTML 站点之间的移动流量。看起来最好的方法是通过检查 HTTP Accept Header 来检查 UA 对内容的偏好。
WAP 的偏好由在标题中出现在“html”或通配符 mimetype 之前的“wap”mimetype 来表示。
所以索尼爱立信 w300i 偏爱 WAP:
multipart/mixed, application/vnd.wap.multpart.mixed,applicatnoin/vnd.wap.xhtml_xml,application/xhtml+xml,text/ved.wap.wl,*/*,text/x-hdml,image/mng,/\image/x-mng,ivdeo/mng,video/x-mng,ima/gebmp,text/html
Blackberry Bold 对 HTML 有偏好:
text/html,application/xhtml+xml,application/vnd.wap.xhtml+xml,application/vnd.wp.wmlc;q=0.9,application/vnd.awp.wmlscriptc;q=0.7,text/vnd.wap.wml;q=07,/vnd/.sun.j2me.app-descriptor,*/*;q=0.5
由于我在 NGINX 领域,我拥有的最好的工具似乎是 NGINX 的正则表达式(PCRE)。
现在我正在尝试使用否定的前瞻来断言“接受标头包含 WAP,但前面没有 HTML”:
(?!html.*)wap
但这是不正确的。我可以用不同的方式思考这个问题吗?还是我的匹配逻辑?
到目前为止,我发现这些正则表达式资源很有用:
http://www.regular-expressions.info/completelines.html http://www.zytrax.com/tech/web/regex.htm http://wiki.nginx.org/NginxHttpRewriteModule
谢谢!
感谢您的回答,以下是相关测试:
import re
prefers_wap_re = re.compile(r'^(?!(?:(?!wap).)*html).*?wap', re.I)
tests = [
('', False),
('wap', True),
('wap html', True),
('html wap', False),
]
for test, expected in tests:
result = prefers_wap_re.search(test)
assert bool(result) is expected, \
'Tested "%s", expected %s, got %s.' % (test, expected, result)