Tomcat可以接受没有Host标头的HTTP 1.1请求吗? [英] Can Tomcat accept HTTP 1.1 requests without a Host header?

查看:597
本文介绍了Tomcat可以接受没有Host标头的HTTP 1.1请求吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

HTTP 1.1状态: 客户端必须在所有HTTP/1.1请求消息中包含主机标头字段"

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

但是,我正在使用的计算机发出了此确切的POST(包含坐标),而我无权进行更改:

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>

由于缺少Host标头字段,Tomcat 7立即以400 Bad Request响应,并且POST从未到达我的servlet.有什么方法可以避免这种错误响应,并通过servlet处理POST以支持这些较旧的机器?

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?

推荐答案

您已经注意到,

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

客户端必须在所有HTTP/1.1请求消息中包含主机标头字段. [...] HTTP/1.1代理必须确保其转发的任何请求消息均包含适当的主机标头字段,该字段标识代理所请求的服务.所有基于Internet的HTTP/1.1服务器必须用400 (错误请求)状态码来响应任何缺少主机标头字段的HTTP/1.1请求消息.

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.

客户端使用的HTTP 1.1协议不正确,您不应尝试在服务器端解决该问题.您可以做的是设置一些自定义HTTP代理,该代理将仅添加Host标头.但这是一个肮脏的解决方法.或者将协议降级为1.0.

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.

还要注意,即使您设法设法使Tomcat接受此类请求(违反规范),但是如果两端之间有任何HTTP代理,您仍然会遇到一些问题.

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.

这篇关于Tomcat可以接受没有Host标头的HTTP 1.1请求吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆