问题
I need to minimize Tomcat's response header. The device sending requests to tomcat is very limited in memory, so I want to remove the headers Date, Server and Content-Type. I set up my own valve with an action hook. The hook gets called, but it seems to be impossible to remove the date and server header.
First I executed my method only when ActionCode == COMMIT
- didn't work.
Strange. So I looked in the Tomcat source code: In the method action() of Tomcat's AbstractHttp11Processor
when ActionCode==COMMIT
prepareResponse()
is called (which set's the server and date headers) and then- it writes the headers to the output buffer.
So it is not possible to intercept. When I set the headers before this action gets called they get overwritten in the AbstractHttp11Processor
. When I set the headers after that, they will be ignored, as they were already written to the buffer. My only chance seems to be to reset the output buffer and write the headers to the buffer or is there a better way? And is it even possible to do that?
回答1:
The date header cannot be removed. It is required by the HTTP 1.1 spec. See RFC2626, section 14.18.
There is currently no way to disable the server header but if you open an enhancement request against Tomcat I'll look into adding an option to disable it completely.
来源:https://stackoverflow.com/questions/19282231/how-to-modify-tomcats-response-header-with-valves