Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
PojavLauncherTeam
GitHub Repository: PojavLauncherTeam/mobile
Path: blob/master/test/jdk/java/net/ResponseCache/Test.java
41149 views
1
/*
2
* Copyright (c) 2012, 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
/* @test
25
* @summary Fixed a potential NullPointerException when setting a ResponseCache that returns a null CacheRequest
26
* @bug 4837267
27
* @modules jdk.httpserver
28
* @library /test/lib
29
* @run main Test
30
* @run main/othervm -Djava.net.preferIPv6Addresses=true Test
31
* @author Michael McMahon
32
*/
33
34
import com.sun.net.httpserver.*;
35
import java.net.*;
36
import java.io.*;
37
import java.util.*;
38
39
import jdk.test.lib.net.URIBuilder;
40
41
public class Test
42
{
43
44
static class MyHandler implements HttpHandler {
45
public void handle(HttpExchange t) throws IOException {
46
byte[] b = new byte[1024];
47
int r = 0;
48
InputStream is = t.getRequestBody();
49
while (is.read(b) != -1) ;
50
String response = "This is the response";
51
t.sendResponseHeaders(200, response.length());
52
OutputStream os = t.getResponseBody();
53
os.write(response.getBytes());
54
os.close();
55
}
56
}
57
58
public static void main(String args[]) throws Exception {
59
InetAddress loopback = InetAddress.getLoopbackAddress();
60
HttpServer server = HttpServer.create(new InetSocketAddress(loopback, 0), 0);
61
server.createContext("/", new MyHandler());
62
server.start();
63
ResponseCache bak = ResponseCache.getDefault();
64
65
try {
66
ResponseCache.setDefault(new ResponseCache() {
67
public CacheResponse get(URI uri, String rqstMethod, Map<String,List<String>> rqstHeaders)
68
throws IOException {
69
return null;
70
}
71
public CacheRequest put(URI uri, URLConnection conn) throws IOException
72
{
73
return null;
74
}
75
});
76
77
URL url = URIBuilder.newBuilder()
78
.scheme("http")
79
.host(server.getAddress().getAddress())
80
.port(server.getAddress().getPort())
81
.path("/")
82
.toURL();
83
URLConnection urlc = url.openConnection(Proxy.NO_PROXY);
84
InputStream is = urlc.getInputStream();
85
while (is.read() != -1) ;
86
is.close();
87
} finally {
88
ResponseCache.setDefault(bak);
89
server.stop(0);
90
}
91
}
92
}
93
94