1/*
2 * Copyright (c) 2017, 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 jdk.incubator.httpclient java.logging jdk.httpserver
28 * @run main/othervm -Djdk.httpclient.HttpClient.log=errors,requests,headers,trace VersionTest
29 */
30
31import com.sun.net.httpserver.Headers;
32import com.sun.net.httpserver.HttpContext;
33import com.sun.net.httpserver.HttpExchange;
34import com.sun.net.httpserver.HttpHandler;
35import com.sun.net.httpserver.HttpServer;
36import java.io.IOException;
37import java.io.OutputStream;
38import java.net.URI;
39import java.util.concurrent.Executors;
40import java.util.concurrent.ExecutorService;
41import java.net.InetSocketAddress;
42import jdk.incubator.http.HttpClient;
43import jdk.incubator.http.HttpRequest;
44import jdk.incubator.http.HttpResponse;
45import static jdk.incubator.http.HttpRequest.BodyProcessor.fromString;
46import static jdk.incubator.http.HttpResponse.*;
47import static jdk.incubator.http.HttpResponse.BodyHandler.asString;
48import static jdk.incubator.http.HttpResponse.BodyHandler.discard;
49import static jdk.incubator.http.HttpClient.Version.HTTP_1_1;
50import static jdk.incubator.http.HttpClient.Version.HTTP_2;
51
52/**
53 */
54public class VersionTest {
55    static HttpServer s1 ;
56    static ExecutorService executor;
57    static int port;
58    static HttpClient client;
59    static URI uri;
60    static volatile boolean error = false;
61
62    public static void main(String[] args) throws Exception {
63        initServer();
64
65        client = HttpClient.newBuilder()
66                           .executor(executor)
67                           .build();
68        // first check that the version is HTTP/2
69        if (client.version() != HttpClient.Version.HTTP_2) {
70            throw new RuntimeException("Default version not HTTP_2");
71        }
72        try {
73            test(HTTP_1_1);
74            test(HTTP_2);
75        } finally {
76            s1.stop(0);
77            executor.shutdownNow();
78        }
79        if (error)
80            throw new RuntimeException();
81    }
82
83    public static void test(HttpClient.Version version) throws Exception {
84        HttpRequest r = HttpRequest.newBuilder(uri)
85                .version(version)
86                .GET()
87                .build();
88        HttpResponse<Void> resp = client.send(r, discard(null));
89        System.out.printf("Client: response is %d\n", resp.statusCode());
90        if (resp.version() != HTTP_1_1) {
91            throw new RuntimeException();
92        }
93        //System.out.printf("Client: response body is %s\n", resp.body());
94    }
95
96    static void initServer() throws Exception {
97        InetSocketAddress addr = new InetSocketAddress (0);
98        s1 = HttpServer.create (addr, 0);
99        HttpHandler h = new Handler();
100
101        HttpContext c1 = s1.createContext("/", h);
102
103        executor = Executors.newCachedThreadPool();
104        s1.setExecutor(executor);
105        s1.start();
106
107        port = s1.getAddress().getPort();
108        uri = new URI("http://127.0.0.1:" + Integer.toString(port) + "/foo");
109        System.out.println("HTTP server port = " + port);
110    }
111}
112
113class Handler implements HttpHandler {
114    int counter = 0;
115
116    void checkHeader(Headers h) {
117        counter++;
118        if (counter == 1 && h.containsKey("Upgrade")) {
119            VersionTest.error = true;
120        }
121        if (counter > 1 && !h.containsKey("Upgrade")) {
122            VersionTest.error = true;
123        }
124    }
125
126    @Override
127    public synchronized void handle(HttpExchange t)
128        throws IOException
129    {
130        String reply = "Hello world";
131        int len = reply.length();
132        Headers h = t.getRequestHeaders();
133        checkHeader(h);
134        System.out.printf("Sending response 200\n");
135        t.sendResponseHeaders(200, len);
136        OutputStream o = t.getResponseBody();
137        o.write(reply.getBytes());
138        t.close();
139    }
140}
141