title: One socket, multiple http request, different Host header but server returns cached Host header
body: <p>I am using a custom servlet engine of a vendor product. Our server are fronted by different proxy with different host names. Assuming the host names are <code>host1.localhost.com</code> and  <code>host2.localhost.com</code>.</p>
  
  <p>We have following servlet</p>
  
  <pre><code>public class MyServlet {
  
      public void doGet(...) {
  
               response.getOutputStream.write(request.getServerName().getBytes())
  } }
  </code></pre>
  
  <p>We encountered an issue that some times if we make a request <code>host1.localhost.com/my/servlet</code> we see actually see <code>host2.localhost.com/my/servlet</code> in the response. </p>
  
  <p>Decompiling vendor product's code revealed that their servlet engine caches the Host header as long as the socket is kept alive. </p>
  
  <p>In attempt to reproduce the problem I wrote some low level socket code to make HTTP requests:</p>
  
  <pre><code>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();
  </code></pre>
  
  <p>The above will print</p>
  
  <pre><code>12345
  12345
  </code></pre>
  
  <p>But I would expect</p>
  
  <pre><code>12345
  7891011
  </code></pre>
  
  <p>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? 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.</p>