test/jdk/java/net/ResponseCache/Test2.java
changeset 47216 71c04702a3d5
parent 38883 d5de564f8089
child 54314 46cf212cdcca
equal deleted inserted replaced
47215:4ebc2e2fb97c 47216:71c04702a3d5
       
     1 /*
       
     2  * Copyright (c) 2014, 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 8042622
       
    27  * @summary Check for CRL results in IllegalArgumentException "white space not allowed"
       
    28  * @modules jdk.httpserver
       
    29  * @run main/othervm Test2
       
    30  */
       
    31 
       
    32 import com.sun.net.httpserver.*;
       
    33 
       
    34 import java.util.*;
       
    35 import java.util.concurrent.*;
       
    36 import java.io.*;
       
    37 import java.net.*;
       
    38 import java.security.*;
       
    39 import javax.security.auth.callback.*;
       
    40 import javax.net.ssl.*;
       
    41 
       
    42 public class Test2 {
       
    43 
       
    44     static volatile boolean failed = false;
       
    45 
       
    46     static class Cache extends ResponseCache {
       
    47         public CacheResponse get(URI uri, String method, Map<String,List<String>> headers) {
       
    48             Set<String> keys = headers.keySet();
       
    49             for (String key : keys) {
       
    50                 if (key.indexOf(' ') != -1 || key.indexOf('\t') != -1
       
    51                         || key.indexOf(':') != -1)
       
    52                 {
       
    53                     failed = true;
       
    54                 }
       
    55             }
       
    56             return null;
       
    57         }
       
    58 
       
    59         public CacheRequest put(URI uri, URLConnection c) throws IOException {
       
    60             return null;
       
    61         }
       
    62     }
       
    63 
       
    64     static int port;
       
    65 
       
    66     static String urlstring, redirstring;
       
    67 
       
    68     public static void main (String[] args) throws Exception {
       
    69         Handler handler = new Handler();
       
    70         InetSocketAddress addr = new InetSocketAddress (0);
       
    71         HttpServer server = HttpServer.create (addr, 0);
       
    72         port = server.getAddress().getPort();
       
    73         HttpContext ctx = server.createContext ("/test", handler);
       
    74         System.out.println ("Server: " + server.getAddress().getPort());
       
    75         ResponseCache.setDefault(new Cache());
       
    76 
       
    77         ExecutorService executor = Executors.newCachedThreadPool();
       
    78         server.setExecutor (executor);
       
    79         server.start ();
       
    80 
       
    81         urlstring = "http://127.0.0.1:" + Integer.toString(port)+"/test/foo";
       
    82         redirstring = urlstring + "/redirect/bar";
       
    83 
       
    84         URL url = new URL (urlstring);
       
    85         HttpURLConnection urlc = (HttpURLConnection)url.openConnection();
       
    86         urlc.addRequestProperty("X-Foo", "bar");
       
    87         urlc.setInstanceFollowRedirects(true);
       
    88         System.out.println(urlc.getResponseCode());
       
    89         InputStream i = urlc.getInputStream();
       
    90         int count=0;
       
    91         for (int c=i.read(); c!=-1; c=i.read()) {
       
    92             //System.out.write(c);
       
    93             count++;
       
    94         }
       
    95         System.out.println("Read " + count);
       
    96         System.out.println("FINISHED");
       
    97         server.stop(0);
       
    98         executor.shutdownNow();
       
    99         if (failed) {
       
   100             throw new RuntimeException("Test failed");
       
   101         }
       
   102     }
       
   103 
       
   104     public static boolean error = false;
       
   105     public static int count = 0;
       
   106 
       
   107     static class Handler implements HttpHandler {
       
   108         int invocation = 0;
       
   109         public void handle (HttpExchange t)
       
   110             throws IOException
       
   111         {
       
   112             InputStream is = t.getRequestBody();
       
   113             Headers map = t.getRequestHeaders();
       
   114             Headers rmap = t.getResponseHeaders();
       
   115             invocation ++;
       
   116             if (invocation == 1) {
       
   117                 rmap.add("Location", redirstring);
       
   118                 while (is.read () != -1) ;
       
   119                 is.close();
       
   120                 System.out.println ("sending response");
       
   121                 t.sendResponseHeaders (301, 0);
       
   122             } else {
       
   123                 byte[] buf = "Hello world".getBytes();
       
   124                 t.sendResponseHeaders (200, buf.length);
       
   125                 OutputStream os = t.getResponseBody();
       
   126                 try {
       
   127                         os.write(buf);
       
   128                 } catch (IOException e) {
       
   129                         System.out.println ("EX 1 " + e);
       
   130                 }
       
   131             }
       
   132             System.out.println ("Closing");
       
   133             t.close();
       
   134         }
       
   135     }
       
   136 }