gpt4 book ai didi

utf-8 - 仅包含字符集的内容类型

转载 作者:行者123 更新时间:2023-12-01 11:04:37 24 4
gpt4 key购买 nike

我遇到了这个有趣的标题:

Content-Type: charset=utf-8

Set HTTP header to UTF-8 using PHP

回答者说这个语法是由RFC 2616定义的, 但我不是在提供的链接中看到它。这是有效的语法吗,如果是的话在哪里具体是这样定义的吗?

最佳答案

production in RFC 2616 for the Content-Type header这是:

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

the media-type production这是:

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

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

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

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

但 RFC 7231 的相应部分为这种情况定义了基本相同的产品:

production in RFC 7231 for the value of the Content-Type header这是:

Content-Type = media-type

the media-type production这是:

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

关于utf-8 - 仅包含字符集的内容类型,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41994062/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com