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