Path: blob/aarch64-shenandoah-jdk8u272-b10/jdk/test/sun/net/www/protocol/http/B6299712.java
38867 views
/*1* Copyright (c) 2005, 2013, 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* @run main/othervm B629971227* @summary NullPointerException in sun.net.www.protocol.http.HttpURLConnection.followRedirect28*/2930import com.sun.net.httpserver.HttpExchange;31import com.sun.net.httpserver.HttpHandler;32import com.sun.net.httpserver.HttpServer;33import java.net.*;34import java.io.*;35import java.util.*;3637/*38* Test Description:39* - main thread is run as a http client40* - another thread runs an http server, which redirects calls to "/" to41* "/redirect" and returns '200 OK' for the successive call42* - a global ResponseCache instance is installed, which returns DeployCacheResponse43* for urls that end with "/redirect", i.e. the url redirected to by our simple http server,44* and null for other urls.45* - the whole result is that the first call will be served by our simple46* http server and is redirected to "/redirect". The successive call will be done47* automatically by HttpURLConnection, which will be served by DeployCacheResponse.48* The NPE will be thrown on the second round if the bug is there.49*/50public class B6299712 {51static HttpServer server;5253public static void main(String[] args) throws Exception {54ResponseCache.setDefault(new DeployCacheHandler());55startHttpServer();5657makeHttpCall();58}5960public static void startHttpServer() throws IOException {61server = HttpServer.create(new InetSocketAddress(0), 0);62server.createContext("/", new DefaultHandler());63server.createContext("/redirect", new RedirectHandler());64server.start();65}6667public static void makeHttpCall() throws IOException {68try {69System.out.println("http server listen on: "70+ server.getAddress().getPort());71URL url = new URL("http",72InetAddress.getLocalHost().getHostAddress(),73server.getAddress().getPort(), "/");74HttpURLConnection uc = (HttpURLConnection)url.openConnection();75if (uc.getResponseCode() != 200)76throw new RuntimeException("Expected Response Code was 200,"77+ "received: " + uc.getResponseCode());78uc.disconnect();79} finally {80server.stop(0);81}82}8384static class RedirectHandler implements HttpHandler {8586@Override87public void handle(HttpExchange exchange) throws IOException {88exchange.sendResponseHeaders(200, -1);89exchange.close();90}9192}9394static class DefaultHandler implements HttpHandler {9596@Override97public void handle(HttpExchange exchange) throws IOException {98exchange.getResponseHeaders().add("Location", "/redirect");99exchange.sendResponseHeaders(302, -1);100exchange.close();101}102103}104105static class DeployCacheHandler extends java.net.ResponseCache {106107public synchronized CacheResponse get(final URI uri, String rqstMethod,108Map<String, List<String>> requestHeaders) throws IOException109{110System.out.println("get!!!: " + uri);111if (!uri.toString().endsWith("redirect")) {112return null;113}114System.out.println("Serving request from cache");115return new DeployCacheResponse(new EmptyInputStream(),116new HashMap<String, List<String>>());117}118119public synchronized CacheRequest put(URI uri, URLConnection conn)120throws IOException121{122URL url = uri.toURL();123return new DeployCacheRequest(url, conn);124125}126}127128static class DeployCacheRequest extends java.net.CacheRequest {129130private URL _url;131private URLConnection _conn;132133DeployCacheRequest(URL url, URLConnection conn) {134_url = url;135_conn = conn;136}137138public void abort() {139140}141142public OutputStream getBody() throws IOException {143144return null;145}146}147148static class DeployCacheResponse extends java.net.CacheResponse {149protected InputStream is;150protected Map<String, List<String>> headers;151152DeployCacheResponse(InputStream is, Map<String, List<String>> headers) {153this.is = is;154this.headers = headers;155}156157public InputStream getBody() throws IOException {158return is;159}160161public Map<String, List<String>> getHeaders() throws IOException {162List<String> val = new ArrayList<>();163val.add("HTTP/1.1 200 OK");164headers.put(null, val);165return headers;166}167}168169static class EmptyInputStream extends InputStream {170171public int read() throws IOException {172return -1;173}174}175}176177178