是否有用于创建用户代理字符串的 RFC、官方标准或模板? iphone的用户代理字符串似乎很奇怪......
Mozilla/5.0 (iPhone; U; CPU iPhone OS 3_1_2 like Mac OS X; en-us) AppleWebKit/528.18 (KHTML, like Gecko) Version/4.0 Mobile/7D11 Safari/528.16
Mozilla/5.0
放在其用户代理的开头?
Mozilla/4.0
开头。请记住,Mozilla 是最早制作的浏览器之一,所有其他浏览器都在不同程度上包含了其基础的一部分。
Mozilla/
视为“不是 Lynx”。通常纯文本 = 不兼容 Mozilla。一些旧的 WML/HDML 功能手机浏览器也不识别为 Mozilla。 (有趣的事实:Lynx 之前的所有浏览器都死于痢疾或被咕噜肉吃掉。)
User-Agent
标头是 RFC7231
的一部分,它是 RFC1945
的改进版本,其中指出:
User-Agent request-header 字段包含有关发起请求的用户代理的信息。这是出于统计目的、协议违规的跟踪以及用户代理的自动识别,以便定制响应以避免特定的用户代理限制。用户代理应该在请求中包含这个字段。该字段可以包含多个产品令牌(第 3.8 节)和标识代理的注释以及构成用户代理重要部分的任何子产品。按照惯例,产品令牌是按照它们对识别应用程序的重要性的顺序列出的。
EBNF 定义:
User-Agent = "User-Agent" ":" 1*( product | comment )
其中 product
定义为:
product = token ["/" product-version]
product-version = token
token = 1*<any CHAR except CTLs or separators>
comment
为:
comment = "(" *( ctext | quoted-pair | comment ) ")"
ctext = <any TEXT excluding "(" and ")">
及其他规则,供参考:
CTL = <control characters, e.g. ASCII 0x00 through 0x0F and 0x7F>
separators = "(" | ")" | "<" | ">" | "@"
"," | ";" | ":" | "\" | <">
"/" | "[" | "]" | "?" | "="
"{" | "}" | SP | HT
SP = <ASCII space 0x20, i.e. " ">
HT = <ASCII horizontal tab 0x09, aka '\t'>
请注意,这意味着 product
字符串不能包含空格,但 comment
字符串可以。
例子:
以下是 product
字符串的一些有效示例(带有和不带有 product-version
字符串):
# Single `product` without product-version:
Foobar
Foobar-baz
# Single `product` with product-version:
Foobar/abc
Foobar/1.0.0
Foobar/2021.44.30.15-b917dc
以下是 comment
字符串的一些有效示例;请注意如何将所有字符串括在匹配的括号 (
)
中:
# This was the default `comment` used by Internet Explorer 11:
(Windows NT 6.1; WOW64; Trident/7.0; rv:11.0)
# You can put almost any text inside a comment:
(Why are you looking at HTTP headers? Go outside, find love, do some good in the world)
# Note that `comment` strings can also be nested, provided their delimiting parentheses are matched, for example:
(Outer comment (Inner comment))
由于 User-Agent
标头的值由任意 product
和 comment
字符串组成,因此这些都是有效的 User-Agent
标头:
User-Agent: Foobar
User-Agent: Foobar/2021.44.30.15-b917dc
User-Agent: MyProduct Foobar/2021.44.30.15-b917dc
User-Agent: Tsom/OfraHaza (Life is short and love is always over in the morning) AnotherProduct
这在请求标头部分的 RFC 1945 中指定。不过,这不是一种非常标准化的格式,用户代理倾向于将他们想要的任何东西放在那里。