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