Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
PojavLauncherTeam
GitHub Repository: PojavLauncherTeam/mobile
Path: blob/master/test/jdk/sun/net/www/protocol/http/B6299712.java
41159 views
1
/*
2
* Copyright (c) 2005, 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 6299712 7150552
27
* @modules jdk.httpserver
28
* @run main/othervm B6299712
29
* @run main/othervm -Djava.net.preferIPv6Addresses=true B6299712
30
* @summary NullPointerException in sun.net.www.protocol.http.HttpURLConnection.followRedirect
31
*/
32
33
import com.sun.net.httpserver.HttpExchange;
34
import com.sun.net.httpserver.HttpHandler;
35
import com.sun.net.httpserver.HttpServer;
36
import java.net.*;
37
import java.io.*;
38
import java.util.*;
39
40
/*
41
* Test Description:
42
* - main thread is run as a http client
43
* - another thread runs an http server, which redirects calls to "/" to
44
* "/redirect" and returns '200 OK' for the successive call
45
* - a global ResponseCache instance is installed, which returns DeployCacheResponse
46
* for urls that end with "/redirect", i.e. the url redirected to by our simple http server,
47
* and null for other urls.
48
* - the whole result is that the first call will be served by our simple
49
* http server and is redirected to "/redirect". The successive call will be done
50
* automatically by HttpURLConnection, which will be served by DeployCacheResponse.
51
* The NPE will be thrown on the second round if the bug is there.
52
*/
53
public class B6299712 {
54
static HttpServer server;
55
56
public static void main(String[] args) throws Exception {
57
ResponseCache.setDefault(new DeployCacheHandler());
58
ProxySelector.setDefault(ProxySelector.of(null)); // no proxy
59
startHttpServer();
60
61
makeHttpCall();
62
}
63
64
public static void startHttpServer() throws IOException {
65
InetAddress address = InetAddress.getLocalHost();
66
server = HttpServer.create(new InetSocketAddress(address, 0), 0);
67
server.createContext("/", new DefaultHandler());
68
server.createContext("/redirect", new RedirectHandler());
69
server.start();
70
}
71
72
public static void makeHttpCall() throws IOException {
73
try {
74
System.out.println("http server listen on: "
75
+ server.getAddress().getPort());
76
URL url = new URL("http",
77
InetAddress.getLocalHost().getHostAddress(),
78
server.getAddress().getPort(), "/");
79
HttpURLConnection uc = (HttpURLConnection)url.openConnection();
80
if (uc.getResponseCode() != 200)
81
throw new RuntimeException("Expected Response Code was 200,"
82
+ "received: " + uc.getResponseCode());
83
uc.disconnect();
84
} finally {
85
server.stop(0);
86
}
87
}
88
89
static class RedirectHandler implements HttpHandler {
90
91
@Override
92
public void handle(HttpExchange exchange) throws IOException {
93
exchange.sendResponseHeaders(200, -1);
94
exchange.close();
95
}
96
97
}
98
99
static class DefaultHandler implements HttpHandler {
100
101
@Override
102
public void handle(HttpExchange exchange) throws IOException {
103
exchange.getResponseHeaders().add("Location", "/redirect");
104
exchange.sendResponseHeaders(302, -1);
105
exchange.close();
106
}
107
108
}
109
110
static class DeployCacheHandler extends java.net.ResponseCache {
111
112
public synchronized CacheResponse get(final URI uri, String rqstMethod,
113
Map<String, List<String>> requestHeaders) throws IOException
114
{
115
System.out.println("get!!!: " + uri);
116
if (!uri.toString().endsWith("redirect")) {
117
return null;
118
}
119
System.out.println("Serving request from cache");
120
return new DeployCacheResponse(new EmptyInputStream(),
121
new HashMap<String, List<String>>());
122
}
123
124
public synchronized CacheRequest put(URI uri, URLConnection conn)
125
throws IOException
126
{
127
URL url = uri.toURL();
128
return new DeployCacheRequest(url, conn);
129
130
}
131
}
132
133
static class DeployCacheRequest extends java.net.CacheRequest {
134
135
private URL _url;
136
private URLConnection _conn;
137
138
DeployCacheRequest(URL url, URLConnection conn) {
139
_url = url;
140
_conn = conn;
141
}
142
143
public void abort() {
144
145
}
146
147
public OutputStream getBody() throws IOException {
148
149
return null;
150
}
151
}
152
153
static class DeployCacheResponse extends java.net.CacheResponse {
154
protected InputStream is;
155
protected Map<String, List<String>> headers;
156
157
DeployCacheResponse(InputStream is, Map<String, List<String>> headers) {
158
this.is = is;
159
this.headers = headers;
160
}
161
162
public InputStream getBody() throws IOException {
163
return is;
164
}
165
166
public Map<String, List<String>> getHeaders() throws IOException {
167
List<String> val = new ArrayList<>();
168
val.add("HTTP/1.1 200 OK");
169
headers.put(null, val);
170
return headers;
171
}
172
}
173
174
static class EmptyInputStream extends InputStream {
175
176
public int read() throws IOException {
177
return -1;
178
}
179
}
180
}
181
182