简体   繁体   English

一个套接字,多个http请求,不同的主机头,但是服务器返回缓存的主机头

[英]One socket, multiple http request, different Host header but server returns cached Host header

I am using a custom servlet engine of a vendor product. 我正在使用供应商产品的定制servlet引擎。 Our server are fronted by different proxy with different host names. 我们的服务器以具有不同主机名的不同代理开头。 Assuming the host names are host1.localhost.com and host2.localhost.com . 假设主机名为host1.localhost.comhost2.localhost.com

We have following servlet 我们有以下servlet

public class MyServlet {

    public void doGet(...) {

             response.getOutputStream.write(request.getServerName().getBytes())
} }

We encountered an issue that some times if we make a request host1.localhost.com/my/servlet we see actually see host2.localhost.com/my/servlet in the response. 我们遇到了一个问题,有时如果我们发出一个请求host1.localhost.com/my/servlet我们实际上host2.localhost.com/my/servlet在响应中看到host2.localhost.com/my/servlet

Decompiling vendor product's code revealed that their servlet engine caches the Host header as long as the socket is kept alive. 反编译供应商产品的代码表明,只要套接字保持活动状态,他们的servlet引擎就会缓存Host标头。

In attempt to reproduce the problem I wrote some low level socket code to make HTTP requests: 为了重现该问题,我编写了一些低级套接字代码来发出HTTP请求:

Socket s = new Socket();
s.connect(new InetSocketAddress("host2.localhost.com", 8080));
OutputStream os = s.getOutputStream();

/*this thread keeps printing stuff in the input stream*/
Thread t = new ResponsePrintThread(s.getInputStream());
t.start()

os.write("GET /my/servlet/testservlet HTTP/1.1\r\n".getBytes());
os.write("Host: 12345\r\n".getBytes());
os.write("\r\n".getBytes());
os.flush();

os.write("GET /my/serlet/testservlet HTTP/1.1\r\n".getBytes());
os.write("Host: 7891011\r\n".getBytes());
os.write("\r\n".getBytes());
os.flush();

The above will print 上面会打印

12345
12345

But I would expect 但是我希望

12345
7891011

My question is, does the servlet engine behave correctly by caching and returning the same host header for the same socket connection, or should it re-parse the HTTP headers and update the cached host header? 我的问题是,servlet引擎是否通过缓存并为同一套接字连接返回相同的主机头来正确运行,还是应该重新解析HTTP头并更新缓存的主机头? My Thinking is that since HTTP suppose to be stateless, so any information in the HTTP request should be re-parsed and reloaded, even the host header. 我的想法是,由于HTTP假定是无状态的,因此应该重新解析和重新加载HTTP请求中的任何信息,甚至包括主机标头。

HTTP is a little vague on how connections are made between client and server: HTTP在客户端和服务器之间的连接方式上有点模糊:

http://tools.ietf.org/html/draft-ietf-httpbis-p1-messaging-22#section-6.2 http://tools.ietf.org/html/draft-ietf-httpbis-p1-messaging-22#section-6.2

It is beyond the scope of this specification to describe how connections are established via various transport or session-layer protocols. 描述如何通过各种传输或会话层协议建立连接超出了本规范的范围。

I don't see anything wrong if a client uses one persistent connection for two host names that resolve to the same IP. 如果客户端对一个解析为相同IP的两个主机名使用一个持久连接,我看不到任何错误。 It ought not to cause any problem on the server side. 它不应在服务器端引起任何问题。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM