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