仅包含字符集的内容类型

2024-04-17

我发现了这个有趣的标题:

Content-Type: charset=utf-8

使用 PHP 将 HTTP 标头设置为 UTF-8 https://stackoverflow.com/q/4279282/#22485939

回答者说这个语法是由定义的RFC 2616 //w3.org/Protocols/rfc2616/rfc2616-sec14#sec14.17, 但我不是 在提供的链接中看到它。这是有效的语法吗?如果是,在哪里 具体是这样定义的吗?


The RFC 2616 中的生产Content-Type header https://www.rfc-editor.org/rfc/rfc2616#section-14.17这是:

Content-Type   = "Content-Type" ":" media-type

And the media-type生产 https://www.rfc-editor.org/rfc/rfc2616#section-3.7这是:

media-type     = type "/" subtype *( ";" parameter )
type           = token
subtype        = token

这就是说,虽然参数部分(例如,charset=utf-8是可选的,type "/" subtype部分不是——也就是说,媒体类型必须具有类型,后跟斜杠,后跟子类型。

So Content-Type: charset=utf-8这不是有效的语法,也没有在其他地方规范/权威地专门定义。

RFC 2616 实际上已被 RFC 7231 和其他几个 RFC(当前的 HTTP RFC)所取代。

但 RFC 7231 的相应部分对于这种情况定义了本质上相同的产生式:

The RFC 7231 中的生产值Content-Type header https://www.rfc-editor.org/rfc/rfc7231#section-3.1.1.5这是:

Content-Type = media-type

And the media-type生产 https://www.rfc-editor.org/rfc/rfc7231#section-3.1.1.1这是:

media-type = type "/" subtype *( OWS ";" OWS parameter )
type       = token
subtype    = token

并且没有其他规范废弃或取代该部分 — RFC 7231 仍然对此具有权威性。


大多数编程语言都有良好的媒体类型解析库 语法检查;例子:

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
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

仅包含字符集的内容类型 的相关文章