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