问题描述
我遇到了这个有趣的标题:
I came across this interesting header:
Content-Type: charset=utf-8
回答者说这个语法是由,但我不是在提供的链接中看到它的
。这是有效的语法,如果是这样,那么
具体是这个定义的吗?
The answerer says that this syntax is defined by RFC 2616, but I am notseeing it in the provided link. Is this valid syntax, and if so wherespecifically is this defined?
推荐答案
是这样的:
And the media-type
production is this:
media-type = type "/" subtype *( ";" parameter )
type = token
subtype = token
这表示虽然参数部分(例如 charset = utf-8
是可选的,但类型/subtype
part不是 - 也就是说,媒体类型必须有一个斜杠后跟一个子类型。
That says that while the parameter part (e.g., charset=utf-8
is optional, the type "/" subtype
part is not—that is, a media type must have type followed by a slash followed by a subtype.
所以 Content-Type:charset = utf-8
无效每个语法都没有特别定义,也没有在规范/权威的任何地方特别定义。
So Content-Type: charset=utf-8
isn’t valid syntax per that, and not specially defined anywhere else normatively/authoritatively to be either.
RFC 2616实际上已被RFC 7231和其他几个RFC(当前的HTTP RFC)淘汰。
RFC 2616 is actually obsoleted by RFC 7231 and several other RFCs (the current HTTP RFCs).
但是RFC 7231的相应部分为这种情况定义了基本相同的产品:
But the corresponding parts of RFC 7231 define essentially the same productions for this case:
是这样的:
The production in RFC 7231 for the value of the Content-Type
header is this:
Content-Type = media-type
并且是这样的:
And the media-type
production is this:
media-type = type "/" subtype *( OWS ";" OWS parameter )
type = token
subtype = token
没有其他规范废弃或取代该部分 - RFC 7231仍然是授权ive on this。
And no other spec obsoletes or supersedes that part—RFC 7231 remains authoritative on this.
大多数编程语言都有良好的媒体类型解析库,用于
语法检查;示例:
Most programming languages have good media-type parsing libs forsyntax checking; example:
npm install content-type
node -e "var ct = require('content-type'); ct.parse('charset=utf-8')"
=> TypeError: invalid media type
node -e "var ct = require('content-type'); ct.parse('image; charset=utf-8')"
=> TypeError: invalid media type
这篇关于Content-Type仅限charset的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!