Bug 61917

Summary: AddDefaultCharsetFilter only supports text/* response
Product: Tomcat 8 Reporter: Fuwei Chin <fuweichin>
Component: CatalinaAssignee: Tomcat Developers Mailing List <dev>
Status: RESOLVED WONTFIX    
Severity: enhancement    
Priority: P2    
Version: 8.5.24   
Target Milestone: ----   
Hardware: PC   
OS: All   

Description Fuwei Chin 2017-12-19 07:03:40 UTC
Overview:
I want to use AddDefaultCharsetFilter as a generic purpose filter not only for type text/* but also for type application/json, appliation/javascript, etc.

Steps to Reproduce:
1) Adding a filter registeration in web.xml
<filter>
	<filter-name>AddDefaultCharset</filter-name>
	<filter-class>org.apache.catalina.filters.AddDefaultCharsetFilter</filter-class>
	<init-param>
		<param-name>encoding</param-name>
		<param-value>UTF-8</param-value>
	</init-param>
</filter>
<filter-mapping>
	<filter-name>AddDefaultCharset</filter-name>
	<servlet-name>default</servlet-name>
</filter-mapping>

2. Requesting a static json(e.g. /test.json) resource

Actual Results:
No default charset added in response header 'Content-Type'.
(AddDefaultCharsetFilter only adds default charset for type 'text/*')

Expected Results:
Add default charset for specified types.
response types which will be processed by AddDefaultCharsetFilter can be specified through a init-param (e.g. with name 'accept' and with value 'application/json,text/*'),
if the init-param not present, use defaut value 'text/*'
Comment 1 Remy Maucherat 2017-12-19 07:20:45 UTC
As documented, the filter applies to text/* resources, so your expectation is wrong and this is a possible enhancement.
Comment 2 Konstantin Kolinko 2017-12-19 11:37:47 UTC
Please note that you have to adhere to specifications.

The mime-types have official specifications for them and are registered with IANA,
https://www.iana.org/assignments/media-types/media-types.xhtml

The application/json mime-type DOES NOT have charset parameter,
https://www.iana.org/assignments/media-types/application/json

The application/javascript has charset parameter,
though I am not sure whether it is actually respected by clients.
https://www.iana.org/assignments/media-types/application/javascript
https://tools.ietf.org/html/rfc4329#section-4.1
Comment 3 Christopher Schultz 2017-12-19 16:17:54 UTC
I'm -0 on supporting a change to add default charsets to MIME types other than text/* because those types (non-RFC) "should not" have them.

On the other hand, sometimes the only way to make this stuff work with certain clients is to specify the charset for a MIME type that isn't supposed to support it (e.g. application/json). I won't stop anyone from doing it, but it will just make services continue to refuse to adhere to standards if we support something like this.
Comment 4 Mark Thomas 2021-06-22 09:07:40 UTC
Note: enhancement request withdrawn by the original reporter.