1 /*
2  * Copyright (c) 2017, 2018, Oracle and/or its affiliates. All rights reserved.
3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4  *
5  * This code is free software; you can redistribute it and/or modify it
6  * under the terms of the GNU General Public License version 2 only, as
7  * published by the Free Software Foundation.
8  *
9  * This code is distributed in the hope that it will be useful, but WITHOUT
10  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
11  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
12  * version 2 for more details (a copy is included in the LICENSE file that
13  * accompanied this code).
14  *
15  * You should have received a copy of the GNU General Public License version
16  * 2 along with this work; if not, write to the Free Software Foundation,
17  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
18  *
19  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
20  * or visit www.oracle.com if you need additional information or have any
21  * questions.
22  */
23 
24 /*
25  * @test
26  * @bug 8175814
27  * @modules java.net.http java.logging jdk.httpserver
28  * @library /lib/testlibrary/ /
29  * @build ProxyServer
30  * @run main/othervm -Djdk.httpclient.HttpClient.log=errors,requests,headers,trace VersionTest
31  */
32 
33 import com.sun.net.httpserver.Headers;
34 import com.sun.net.httpserver.HttpContext;
35 import com.sun.net.httpserver.HttpExchange;
36 import com.sun.net.httpserver.HttpHandler;
37 import com.sun.net.httpserver.HttpServer;
38 import java.io.IOException;
39 import java.io.OutputStream;
40 import java.net.InetAddress;
41 import java.net.URI;
42 import java.net.InetSocketAddress;
43 import java.net.ProxySelector;
44 import java.util.concurrent.Executors;
45 import java.util.concurrent.ExecutorService;
46 import java.net.http.HttpClient;
47 import java.net.http.HttpRequest;
48 import java.net.http.HttpResponse;
49 import java.net.http.HttpResponse.BodyHandlers;
50 import static java.net.http.HttpClient.Version.HTTP_1_1;
51 import static java.net.http.HttpClient.Version.HTTP_2;
52 
53 public class VersionTest {
54     static HttpServer s1 ;
55     static ProxyServer proxy;
56     static ExecutorService executor;
57     static int port;
58     static InetSocketAddress proxyAddr;
59     static HttpClient client, clientWithProxy;
60     static URI uri;
61     static volatile boolean error = false;
62 
main(String[] args)63     public static void main(String[] args) throws Exception {
64         initServer();
65 
66         client = HttpClient.newBuilder()
67                            .executor(executor)
68                            .build();
69 
70         clientWithProxy = HttpClient.newBuilder()
71                            .executor(executor)
72                            .proxy(ProxySelector.of(proxyAddr))
73                            .build();
74 
75         // first check that the version is HTTP/2
76         if (client.version() != HttpClient.Version.HTTP_2) {
77             throw new RuntimeException("Default version not HTTP_2");
78         }
79         try {
80             test(HTTP_1_1, false);
81             test(HTTP_2, false);
82             test(HTTP_2, true);
83         } finally {
84             s1.stop(0);
85             executor.shutdownNow();
86         }
87         if (error)
88             throw new RuntimeException();
89     }
90 
test(HttpClient.Version version, boolean proxy)91     public static void test(HttpClient.Version version, boolean proxy) throws Exception {
92         HttpRequest r = HttpRequest.newBuilder(uri)
93                 .version(version)
94                 .GET()
95                 .build();
96         HttpClient c = proxy ? clientWithProxy : client;
97         HttpResponse<Void> resp = c.send(r, BodyHandlers.discarding());
98         System.out.printf("Client: response is %d\n", resp.statusCode());
99         if (resp.version() != HTTP_1_1) {
100             throw new RuntimeException();
101         }
102         //System.out.printf("Client: response body is %s\n", resp.body());
103     }
104 
initServer()105     static void initServer() throws Exception {
106         InetSocketAddress addr = new InetSocketAddress(InetAddress.getLoopbackAddress(), 0);
107         s1 = HttpServer.create (addr, 0);
108         HttpHandler h = new Handler();
109 
110         HttpContext c1 = s1.createContext("/", h);
111 
112         executor = Executors.newCachedThreadPool();
113         s1.setExecutor(executor);
114         s1.start();
115 
116         port = s1.getAddress().getPort();
117         uri = new URI("http://localhost:" + Integer.toString(port) + "/foo");
118         System.out.println("HTTP server port = " + port);
119         proxy = new ProxyServer(0, false);
120         int proxyPort = proxy.getPort();
121         proxyAddr = new InetSocketAddress(InetAddress.getLoopbackAddress(), proxyPort);
122     }
123 
124     static class Handler implements HttpHandler {
125         int counter;
126 
checkHeader(Headers h)127         void checkHeader(Headers h) {
128             counter++;
129             if (counter == 1 && h.containsKey("Upgrade")) {
130                 VersionTest.error = true;
131             }
132             if (counter == 2 && !h.containsKey("Upgrade")) {
133                 VersionTest.error = true;
134             }
135             if (counter == 3 && h.containsKey("Upgrade")) {
136                 VersionTest.error = true;
137             }
138         }
139 
140         @Override
handle(HttpExchange t)141         public synchronized void handle(HttpExchange t) throws IOException {
142             String reply = "Hello world";
143             int len = reply.length();
144             Headers h = t.getRequestHeaders();
145             checkHeader(h);
146             System.out.printf("Sending response 200\n");
147             t.sendResponseHeaders(200, len);
148             OutputStream o = t.getResponseBody();
149             o.write(reply.getBytes());
150             t.close();
151         }
152     }
153 }
154