title: HttpClient : http 2 request sent via proxy has no upgrade headers
body: <p>So, I am managing a company http server and have been asked to upgrade the server to support proxy requests using http 2 protocol</p>
  <p>This is the test client. I am using <a href="https://docs.oracle.com/en/java/javase/17/docs/api/java.net.http/java/net/http/HttpClient.html?msclkid=cd54d5c7c88411ecb4c9d5cfd8ea4129" rel="nofollow noreferrer">HttpClient</a> in jdk 17. Here is the test case.</p>
  <pre><code>    public static void main(String[] args)throws Exception
       {
        HttpClient client=HttpClient.newBuilder()
                                    .proxy(ProxySelector.of(new InetSocketAddress(&quot;192.168.1.2&quot;,1000))) //proxy to 192.168.1.2:1000 
                                    .version(HttpClient.Version.HTTP_2)    //use http 2                      
                                    .build();
        
        //request to 192.168.1.2:2000
        HttpRequest request=HttpRequest.newBuilder(URI.create(&quot;http://192.168.1.2:2000/Test.txt&quot;))
                                        .build();
        
        HttpResponse response=client.send(request,BodyHandlers.ofString());
        System.out.println(&quot;Status code: &quot; + response.statusCode());                            
        System.out.println(&quot;Headers: &quot; + response.headers().map());
        System.out.println(&quot;Body: &quot; + response.body());
       } 
  </code></pre>
  <p>My server is running on http 2 protocol and I wasn't getting any response. Upon debugging the packet received on my server looked like this:</p>
  <pre><code>    GET http://192.168.1.2:2000/Test.txt HTTP/1.1
      Content-Length: 0
      Host: 192.168.1.2:2000
      User-Agent: Java-http-client/17.0.2
  </code></pre>
  <p>This is neither in http 2 format nor are there any upgrade headers which my server was designed to parse, which typically looks like this without using proxy:</p>
  <pre><code>    GET /Test.txt HTTP/1.1
      Connection: Upgrade, HTTP2-Settings
      Content-Length: 0
      Host: 192.168.1.2:2000
      HTTP2-Settings: AAEAAEAAAAIAAAABAAMAAABkAAQBAAAAAAUAAEAA
      Upgrade: h2c
      User-Agent: Java-http-client/17.0.2
  </code></pre>
  <p>My server was designed to parse this request as it is the <a href="https://www.rfc-editor.org/rfc/rfc7540?msclkid=198be25fc87b11eca21146cdba8d08f5#section-3.2" rel="nofollow noreferrer">upgrade request</a> which contains the http 2 settings for the client.</p>
  <p>So, my questions are when I get a request like this:</p>
  <pre><code>     GET http://192.168.1.2:2000/Test.txt HTTP/1.1
       Content-Length: 0
       Host: 192.168.1.2:2000
       User-Agent: Java-http-client/17.0.2
  </code></pre>
  <ol>
  <li><p>Do I respond back to the client in HTTP 1.1 or HTTP 2 Format? The doubt arises because in my client test case I have clearly specified version(HTTP_2) but the request has no upgrade headers, so should I maybe respond back in HTTP 1.1 format?</p>
  </li>
  <li><p>Is there any way to make the HttpClient include the Upgrade headers so I can respond back in HTTP 2 format?</p>
  </li>
  </ol>
  <p><strong>EDIT.</strong></p>
  <p>To debug the response i created an simple TCP server running on port 1000 and proxied through it. I simply print the bytes received at this proxy and get the exact same output</p>
  <pre><code>//Test Proxy server running on port 1000. No parsing data simply print and close for debugging purposes
  public static void main(String[] args)throws Exception
   {
    try(ServerSocket server=new ServerSocket(1000))
    {
     try(Socket client=server.accept())
     {
      try(InputStream input=client.getInputStream())
      {
       byte[] data=new byte[8196];
       int length=input.read(data);
       System.out.println(new String(data,0,length));
      } 
     } 
    } 
   }
  </code></pre>