Path: blob/aarch64-shenandoah-jdk8u272-b10/jdk/test/java/net/ResponseCache/Test2.java
38811 views
/*1* Copyright (c) 2014, 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 804262226* @summary Check for CRL results in IllegalArgumentException "white space not allowed"27* @run main/othervm Test228*/2930import com.sun.net.httpserver.*;3132import java.util.*;33import java.util.concurrent.*;34import java.io.*;35import java.net.*;36import java.security.*;37import javax.security.auth.callback.*;38import javax.net.ssl.*;3940public class Test2 {4142static volatile boolean failed = false;4344static class Cache extends ResponseCache {45public CacheResponse get(URI uri, String method, Map<String,List<String>> headers) {46Set<String> keys = headers.keySet();47for (String key : keys) {48if (key.indexOf(' ') != -1 || key.indexOf('\t') != -149|| key.indexOf(':') != -1)50{51failed = true;52}53}54return null;55}5657public CacheRequest put(URI uri, URLConnection c) throws IOException {58return null;59}60}6162static int port;6364static String urlstring, redirstring;6566public static void main (String[] args) throws Exception {67Handler handler = new Handler();68InetSocketAddress addr = new InetSocketAddress (0);69HttpServer server = HttpServer.create (addr, 0);70port = server.getAddress().getPort();71HttpContext ctx = server.createContext ("/test", handler);72System.out.println ("Server: " + server.getAddress().getPort());73ResponseCache.setDefault(new Cache());7475ExecutorService executor = Executors.newCachedThreadPool();76server.setExecutor (executor);77server.start ();7879urlstring = "http://127.0.0.1:" + Integer.toString(port)+"/test/foo";80redirstring = urlstring + "/redirect/bar";8182URL url = new URL (urlstring);83HttpURLConnection urlc = (HttpURLConnection)url.openConnection();84urlc.addRequestProperty("X-Foo", "bar");85urlc.setInstanceFollowRedirects(true);86System.out.println(urlc.getResponseCode());87InputStream i = urlc.getInputStream();88int count=0;89for (int c=i.read(); c!=-1; c=i.read()) {90//System.out.write(c);91count++;92}93System.out.println("Read " + count);94System.out.println("FINISHED");95server.stop(0);96executor.shutdownNow();97if (failed) {98throw new RuntimeException("Test failed");99}100}101102public static boolean error = false;103public static int count = 0;104105static class Handler implements HttpHandler {106int invocation = 0;107public void handle (HttpExchange t)108throws IOException109{110InputStream is = t.getRequestBody();111Headers map = t.getRequestHeaders();112Headers rmap = t.getResponseHeaders();113invocation ++;114if (invocation == 1) {115rmap.add("Location", redirstring);116while (is.read () != -1) ;117is.close();118System.out.println ("sending response");119t.sendResponseHeaders (301, 0);120} else {121byte[] buf = "Hello world".getBytes();122t.sendResponseHeaders (200, buf.length);123OutputStream os = t.getResponseBody();124try {125os.write(buf);126} catch (IOException e) {127System.out.println ("EX 1 " + e);128}129}130System.out.println ("Closing");131t.close();132}133}134}135136137