Path: blob/master/test/jdk/sun/net/www/protocol/http/B6299712.java
41159 views
/*1* Copyright (c) 2005, 2019, Oracle and/or its affiliates. All rights reserved.2* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.3*4* This code is free software; you can redistribute it and/or modify it5* under the terms of the GNU General Public License version 2 only, as6* published by the Free Software Foundation.7*8* This code is distributed in the hope that it will be useful, but WITHOUT9* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or10* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License11* version 2 for more details (a copy is included in the LICENSE file that12* accompanied this code).13*14* You should have received a copy of the GNU General Public License version15* 2 along with this work; if not, write to the Free Software Foundation,16* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.17*18* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA19* or visit www.oracle.com if you need additional information or have any20* questions.21*/2223/*24* @test25* @bug 6299712 715055226* @modules jdk.httpserver27* @run main/othervm B629971228* @run main/othervm -Djava.net.preferIPv6Addresses=true B629971229* @summary NullPointerException in sun.net.www.protocol.http.HttpURLConnection.followRedirect30*/3132import 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.*;3839/*40* Test Description:41* - main thread is run as a http client42* - another thread runs an http server, which redirects calls to "/" to43* "/redirect" and returns '200 OK' for the successive call44* - a global ResponseCache instance is installed, which returns DeployCacheResponse45* 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 simple48* http server and is redirected to "/redirect". The successive call will be done49* 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 {53static HttpServer server;5455public static void main(String[] args) throws Exception {56ResponseCache.setDefault(new DeployCacheHandler());57ProxySelector.setDefault(ProxySelector.of(null)); // no proxy58startHttpServer();5960makeHttpCall();61}6263public static void startHttpServer() throws IOException {64InetAddress address = InetAddress.getLocalHost();65server = HttpServer.create(new InetSocketAddress(address, 0), 0);66server.createContext("/", new DefaultHandler());67server.createContext("/redirect", new RedirectHandler());68server.start();69}7071public static void makeHttpCall() throws IOException {72try {73System.out.println("http server listen on: "74+ server.getAddress().getPort());75URL url = new URL("http",76InetAddress.getLocalHost().getHostAddress(),77server.getAddress().getPort(), "/");78HttpURLConnection uc = (HttpURLConnection)url.openConnection();79if (uc.getResponseCode() != 200)80throw new RuntimeException("Expected Response Code was 200,"81+ "received: " + uc.getResponseCode());82uc.disconnect();83} finally {84server.stop(0);85}86}8788static class RedirectHandler implements HttpHandler {8990@Override91public void handle(HttpExchange exchange) throws IOException {92exchange.sendResponseHeaders(200, -1);93exchange.close();94}9596}9798static class DefaultHandler implements HttpHandler {99100@Override101public void handle(HttpExchange exchange) throws IOException {102exchange.getResponseHeaders().add("Location", "/redirect");103exchange.sendResponseHeaders(302, -1);104exchange.close();105}106107}108109static class DeployCacheHandler extends java.net.ResponseCache {110111public synchronized CacheResponse get(final URI uri, String rqstMethod,112Map<String, List<String>> requestHeaders) throws IOException113{114System.out.println("get!!!: " + uri);115if (!uri.toString().endsWith("redirect")) {116return null;117}118System.out.println("Serving request from cache");119return new DeployCacheResponse(new EmptyInputStream(),120new HashMap<String, List<String>>());121}122123public synchronized CacheRequest put(URI uri, URLConnection conn)124throws IOException125{126URL url = uri.toURL();127return new DeployCacheRequest(url, conn);128129}130}131132static class DeployCacheRequest extends java.net.CacheRequest {133134private URL _url;135private URLConnection _conn;136137DeployCacheRequest(URL url, URLConnection conn) {138_url = url;139_conn = conn;140}141142public void abort() {143144}145146public OutputStream getBody() throws IOException {147148return null;149}150}151152static class DeployCacheResponse extends java.net.CacheResponse {153protected InputStream is;154protected Map<String, List<String>> headers;155156DeployCacheResponse(InputStream is, Map<String, List<String>> headers) {157this.is = is;158this.headers = headers;159}160161public InputStream getBody() throws IOException {162return is;163}164165public Map<String, List<String>> getHeaders() throws IOException {166List<String> val = new ArrayList<>();167val.add("HTTP/1.1 200 OK");168headers.put(null, val);169return headers;170}171}172173static class EmptyInputStream extends InputStream {174175public int read() throws IOException {176return -1;177}178}179}180181182