How to modify Tomcat's response header with valves?

老子叫甜甜 提交于 2019-12-11 18:03:49

问题


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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!