Path: blob/master/test/jdk/java/net/ResponseCache/Test2.java
41149 views
/*1* Copyright (c) 2014, 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 804262226* @library /test/lib27* @summary Check for CRL results in IllegalArgumentException "white space not allowed"28* @modules jdk.httpserver29* @run main/othervm Test230* @run main/othervm -Djava.net.preferIPv6Addresses=true Test231*/3233import com.sun.net.httpserver.*;3435import java.util.*;36import java.util.concurrent.*;37import java.io.*;38import java.net.*;39import java.security.*;40import javax.security.auth.callback.*;41import javax.net.ssl.*;42import jdk.test.lib.net.URIBuilder;4344public class Test2 {4546static volatile boolean failed = false;4748static class Cache extends ResponseCache {49public CacheResponse get(URI uri, String method, Map<String,List<String>> headers) {50Set<String> keys = headers.keySet();51for (String key : keys) {52if (key.indexOf(' ') != -1 || key.indexOf('\t') != -153|| key.indexOf(':') != -1)54{55failed = true;56}57}58return null;59}6061public CacheRequest put(URI uri, URLConnection c) throws IOException {62return null;63}64}6566static int port;6768static URL url, redir;6970public static void main (String[] args) throws Exception {71Handler handler = new Handler();72InetAddress loopback = InetAddress.getLoopbackAddress();73InetSocketAddress addr = new InetSocketAddress(loopback, 0);74HttpServer server = HttpServer.create (addr, 0);75port = server.getAddress().getPort();76HttpContext ctx = server.createContext ("/test", handler);77System.out.println ("Server: " + server.getAddress());78ResponseCache.setDefault(new Cache());7980ExecutorService executor = Executors.newCachedThreadPool();81server.setExecutor (executor);82server.start ();8384url = URIBuilder.newBuilder()85.scheme("http")86.loopback()87.port(port)88.path("/test/foo")89.toURLUnchecked();90System.out.println("URL: " + url);91redir = URIBuilder.newBuilder()92.scheme("http")93.loopback()94.port(port)95.path("/test/foo/redirect/bar")96.toURLUnchecked();97System.out.println("Redir URL: " + redir);9899HttpURLConnection urlc = (HttpURLConnection)url.openConnection(Proxy.NO_PROXY);100urlc.addRequestProperty("X-Foo", "bar");101urlc.setInstanceFollowRedirects(true);102System.out.println(urlc.getResponseCode());103InputStream i = urlc.getInputStream();104int count=0;105for (int c=i.read(); c!=-1; c=i.read()) {106//System.out.write(c);107count++;108}109System.out.println("Read " + count);110System.out.println("FINISHED");111server.stop(0);112executor.shutdownNow();113if (failed) {114throw new RuntimeException("Test failed");115}116}117118public static boolean error = false;119public static int count = 0;120121static class Handler implements HttpHandler {122int invocation = 0;123public void handle (HttpExchange t)124throws IOException125{126InputStream is = t.getRequestBody();127Headers map = t.getRequestHeaders();128Headers rmap = t.getResponseHeaders();129invocation ++;130if (invocation == 1) {131rmap.add("Location", redir.toString());132while (is.read () != -1) ;133is.close();134System.out.println ("sending response");135t.sendResponseHeaders (301, 0);136} else {137byte[] buf = "Hello world".getBytes();138t.sendResponseHeaders (200, buf.length);139OutputStream os = t.getResponseBody();140try {141os.write(buf);142} catch (IOException e) {143System.out.println ("EX 1 " + e);144}145}146System.out.println ("Closing");147t.close();148}149}150}151152153