我正在使用供應商產品的自定義servlet引擎。我們的服務器由不同主機名的不同代理服務器。假設主機名是host1.localhost.com
和host2.localhost.com
。一個套接字,多個http請求,不同的主機頭,但服務器返回高速緩存的主機頭
我們有以下的servlet
public class MyServlet {
public void doGet(...) {
response.getOutputStream.write(request.getServerName().getBytes())
} }
我們遇到了一個問題,有些時候,如果我們提出一個要求host1.localhost.com/my/servlet
我們看到實際看到host2.localhost.com/my/servlet
的響應。
反編譯供應商產品的代碼表明,只要套接字保持活動,它們的servlet引擎就會緩存主機頭。
在試圖重現問題我寫了一些低級別插座的代碼,使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();
上面會打印
12345
12345
但我希望
12345
7891011
我的問題是,servlet引擎是否正確緩存並返回相同的主機頭相同的套接字連接,還是應該重新解析HTTP頭並更新緩存的主機頭?我的想法是,由於HTTP假設爲無狀態,因此HTTP請求中的任何信息都應該重新解析並重新加載,即使是主機頭。