问题
We have a legacy client consuming a servlet deployed on tomcat 8.5. This legacy client is waiting for the content-type header response in this format (note the whitespace between the semicolon and 'charset'):
Content-type: text/xml; charset=utf-8
However, in the server side, Tomcat is always trimming this whitespace, althought we force it in the servlet:
response.setContentType("text/xml; charset=utf-8");
Or even in a filter with a custom extension of HttpServletResponseWrapper
, as suggested in this question.
Whatever I try, the content-type header is always returned without the whitespace.
Content-type: text/xml;charset=utf-8
As a reference, I found this in the tomcat official documentation:
Tomcat removes blank chars between the ';' and the 'charset' keyword
I know the http standard says that the whitespace is optional after the semicolon, as mentioned in this other SO post but as the client with the problem runs a legacy version, I'm wondering if there is any way to force tomcat to keep this whitespace.
I'm thinking about other choices such as modifying the content-type header in apache or nginx (above tomcat), but if it's possible I would prefer a solution at tomcat level.
Any help would be appreciated.
回答1:
As is posted in the comments, this is something that we cannot avoid as it's hardcoded in the tomcat source code
In our particular use case, we finally have been able to change a little bit the legacy-client, so now it can process the content type header in the standard format (without space).
Thanks @zack6849 por pointing that.
来源:https://stackoverflow.com/questions/49409116/keep-the-space-after-semicolon-in-content-type-header-response-using-tomcat