[英]Is there anything wrong with requiring the client to specify the charset in the http content-type header field?
I'm implementing a service (as rest) that receives a POST method. 我正在实现一个接收POST方法的服务(其余)。
The encoding in my sistem is UTF-8. 我系统中的编码是UTF-8。
I'm using jboss 5, in which the servlet that receives the requests follows the HTTP 1.1 specification of rfc2068 which states that: 我正在使用jboss 5,其中接收请求的servlet遵循rfc2068的HTTP 1.1规范,该规范指出:
When no explicit charset parameter is provided by the sender, media subtypes of the "text" type are defined to have a default charset value of " ISO-8859-1 " when received via HTTP.
当发送方未提供任何明确的字符集参数时,“文本”类型的媒体子类型将定义为在通过HTTP接收时具有默认的字符集值 “ ISO-8859-1 ”。
so when the client that invokes my service is using for example UTF-8 and doesn't specify a charset, and the body of the POST contains characters outside the US-ASCII, the Jboss servlet assumes " ISO-8859-1 " and does a "wrong" decodification and in my system i receive "broken" characters. 因此,当调用我的服务的客户端使用例如UTF-8且未指定字符集,并且POST的正文包含US-ASCII之外的字符时,Jboss servlet假定为“ ISO-8859-1 ”并执行“错误的”编解码,在我的系统中我收到“损坏的”字符。 For example instead of the string "día" i receive "dÂa".
例如,我收到的不是字符串“día”,而是“dÂa”。
The approach i found for "protecting" my system is to require the client to specify the charset in the content-type parameter. 我发现的用于“保护”系统的方法是要求客户端在content-type参数中指定字符集 。 If a charset is not specified then i respond with an http 403 and a text indicating that "the charset value must be specified".
如果未指定字符集,那么我将使用http 403和指示“必须指定字符集值”的文本进行响应。
Is there anything wrong with this approach? 这种方法有什么问题吗?
RFC 2068 has been obsoleted twice and really is irrelevant. RFC 2068已被废弃两次,实际上是无关紧要的。 You need to look at RFC 7231, which doesn't define a default anymore.
您需要查看RFC 7231,它不再定义默认值。 This means that the default is governed by the definition of the media type.
这意味着默认值由媒体类型的定义控制。
For text/plain, this implies US-ASCII (as far as I remember), so clients that want to send non-ASCII characters really need to specify the charset. 对于文本/纯文本,这暗示着US-ASCII(据我所记得),因此想要发送非ASCII字符的客户端确实需要指定字符集。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.