User-Agent 标头是RFC7231 的一部分,它是RFC1945 的改进版本,其中指出:
User-Agent request-header 字段包含有关
发起请求的用户代理。这是出于统计目的,
跟踪协议违规,自动识别用户
代理以定制响应以避免特定用户
代理限制。用户代理应该包含这个字段
要求。该字段可以包含多个 product 标记(第 3.8 节)
和comments 识别代理和任何形成
用户代理的重要组成部分。按照惯例,product 令牌
列出了它们对识别的重要性的顺序
应用。
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