B6299712.java revision 14606:bc3775e25b52
1/*
2 * Copyright (c) 2005, 2016, 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 6299712 7150552
27 * @modules jdk.httpserver
28 * @run main/othervm B6299712
29 * @summary  NullPointerException in sun.net.www.protocol.http.HttpURLConnection.followRedirect
30 */
31
32import com.sun.net.httpserver.HttpExchange;
33import com.sun.net.httpserver.HttpHandler;
34import com.sun.net.httpserver.HttpServer;
35import java.net.*;
36import java.io.*;
37import java.util.*;
38
39/*
40 * Test Description:
41 *      - main thread is run as a http client
42 *      - another thread runs an http server, which redirects calls to "/" to
43 *        "/redirect" and returns '200 OK' for the successive call
44 *      - a global ResponseCache instance is installed, which returns DeployCacheResponse
45 *        for urls that end with "/redirect", i.e. the url redirected to by our simple http server,
46 *        and null for other urls.
47 *      - the whole result is that the first call will be served by our simple
48 *        http server and is redirected to "/redirect". The successive call will be done
49 *        automatically by HttpURLConnection, which will be served by DeployCacheResponse.
50 *        The NPE will be thrown on the second round if the bug is there.
51 */
52public class B6299712 {
53    static HttpServer server;
54
55    public static void main(String[] args) throws Exception {
56        ResponseCache.setDefault(new DeployCacheHandler());
57        startHttpServer();
58
59        makeHttpCall();
60    }
61
62    public static void startHttpServer() throws IOException {
63        server = HttpServer.create(new InetSocketAddress(0), 0);
64        server.createContext("/", new DefaultHandler());
65        server.createContext("/redirect", new RedirectHandler());
66        server.start();
67    }
68
69    public static void makeHttpCall() throws IOException {
70        try {
71            System.out.println("http server listen on: "
72                    + server.getAddress().getPort());
73            URL url = new URL("http",
74                               InetAddress.getLocalHost().getHostAddress(),
75                               server.getAddress().getPort(), "/");
76            HttpURLConnection uc = (HttpURLConnection)url.openConnection();
77            if (uc.getResponseCode() != 200)
78                throw new RuntimeException("Expected Response Code was 200,"
79                        + "received: " + uc.getResponseCode());
80            uc.disconnect();
81        } finally {
82            server.stop(0);
83        }
84    }
85
86    static class RedirectHandler implements HttpHandler {
87
88        @Override
89        public void handle(HttpExchange exchange) throws IOException {
90            exchange.sendResponseHeaders(200, -1);
91            exchange.close();
92        }
93
94    }
95
96    static class DefaultHandler implements HttpHandler {
97
98        @Override
99        public void handle(HttpExchange exchange) throws IOException {
100            exchange.getResponseHeaders().add("Location", "/redirect");
101            exchange.sendResponseHeaders(302, -1);
102            exchange.close();
103        }
104
105    }
106
107    static class DeployCacheHandler extends java.net.ResponseCache {
108
109        public synchronized CacheResponse get(final URI uri, String rqstMethod,
110                Map<String, List<String>> requestHeaders) throws IOException
111        {
112            System.out.println("get!!!: " + uri);
113            if (!uri.toString().endsWith("redirect")) {
114                return null;
115            }
116            System.out.println("Serving request from cache");
117            return new DeployCacheResponse(new EmptyInputStream(),
118                                           new HashMap<String, List<String>>());
119        }
120
121        public synchronized CacheRequest put(URI uri, URLConnection conn)
122            throws IOException
123        {
124            URL url = uri.toURL();
125            return new DeployCacheRequest(url, conn);
126
127        }
128    }
129
130    static class DeployCacheRequest extends java.net.CacheRequest {
131
132        private URL _url;
133        private URLConnection _conn;
134
135        DeployCacheRequest(URL url, URLConnection conn) {
136            _url = url;
137            _conn = conn;
138        }
139
140        public void abort() {
141
142        }
143
144        public OutputStream getBody() throws IOException {
145
146            return null;
147        }
148    }
149
150    static class DeployCacheResponse extends java.net.CacheResponse {
151        protected InputStream is;
152        protected Map<String, List<String>> headers;
153
154        DeployCacheResponse(InputStream is, Map<String, List<String>> headers) {
155            this.is = is;
156            this.headers = headers;
157        }
158
159        public InputStream getBody() throws IOException {
160            return is;
161        }
162
163        public Map<String, List<String>> getHeaders() throws IOException {
164            List<String> val = new ArrayList<>();
165            val.add("HTTP/1.1 200 OK");
166            headers.put(null, val);
167            return headers;
168        }
169    }
170
171    static class EmptyInputStream extends InputStream {
172
173        public int read() throws IOException {
174            return -1;
175        }
176    }
177}
178