Can Tomcat accept HTTP 1.1 requests without a Host header?

江枫思渺然 提交于 2019-12-07 03:44:33

问题


HTTP 1.1 states: "A client MUST include a Host header field in all HTTP/1.1 request messages"

However, the machines I am working on send out this exact POST (containing coordinates), which I do not have access to change:

POST /touch HTTP/1.1
Content-type: application/x-www-form-urlencoded
Content-Length: <n>

x=<int x>&y=<int y>

Tomcat 7 immediately responds with 400 Bad Request due to the lack of a Host header field, and the POST never gets to my servlet. Is there any way I can avoid this error response and handle the POST with the servlet to support these older machines?


回答1:


As you already noted, HTTP 1.1 spec says (bold mine):

A client MUST include a Host header field in all HTTP/1.1 request messages. [...] An HTTP/1.1 proxy MUST ensure that any request message it forwards does contain an appropriate Host header field that identifies the service being requested by the proxy. All Internet-based HTTP/1.1 servers MUST respond with a 400 (Bad Request) status code to any HTTP/1.1 request message which lacks a Host header field.

The client is using HTTP 1.1 protocol incorrectly, you should not try to work around that on the server side. What you can do is setup some custom HTTP proxy that will just add the Host header. But that's a dirty workaround. Alternatively downgrade the protocol to 1.0.

Also note that even if you somehow manage to make Tomcat accept such requests (which is against the specification), you'll still run into some issues if any HTTP proxy is between ends.



来源:https://stackoverflow.com/questions/12808451/can-tomcat-accept-http-1-1-requests-without-a-host-header

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