Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
PojavLauncherTeam
GitHub Repository: PojavLauncherTeam/jdk17u
Path: blob/master/test/jdk/sun/net/www/protocol/http/B6296310.java
66646 views
1
/*
2
* Copyright (c) 2005, 2021, 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 6296310
27
* @library /test/lib
28
* @run main/othervm B6296310
29
* @run main/othervm -Djava.net.preferIPv6Addresses=true B6296310
30
* @summary REGRESSION: AppletClassLoader.getResourceAsStream() behaviour is wrong in some cases
31
*/
32
33
import java.io.IOException;
34
import java.io.OutputStream;
35
import java.net.CacheRequest;
36
import java.net.CacheResponse;
37
import java.net.HttpURLConnection;
38
import java.net.InetAddress;
39
import java.net.InetSocketAddress;
40
import java.net.Proxy;
41
import java.net.ResponseCache;
42
import java.net.URI;
43
import java.net.URL;
44
import java.net.URLConnection;
45
import java.util.Map;
46
import java.util.concurrent.Executors;
47
48
import com.sun.net.httpserver.HttpExchange;
49
import com.sun.net.httpserver.HttpHandler;
50
import com.sun.net.httpserver.HttpServer;
51
52
/*
53
* http server returns 200 and content-length=0
54
* Test will throw NPE if bug still exists
55
*/
56
57
public class B6296310
58
{
59
static SimpleHttpTransaction httpTrans;
60
static HttpServer server;
61
62
public static void main(String[] args) throws Exception
63
{
64
ResponseCache.setDefault(new MyCacheHandler());
65
startHttpServer();
66
makeHttpCall();
67
}
68
69
public static void startHttpServer() throws IOException {
70
httpTrans = new SimpleHttpTransaction();
71
InetAddress loopback = InetAddress.getLoopbackAddress();
72
server = HttpServer.create(new InetSocketAddress(loopback, 0), 10);
73
server.createContext("/", httpTrans);
74
server.setExecutor(Executors.newSingleThreadExecutor());
75
server.start();
76
}
77
78
public static void makeHttpCall() throws IOException {
79
try {
80
System.out.println("http server listen on: " + server.getAddress().getPort());
81
URL url = new URL("http" , InetAddress.getLoopbackAddress().getHostAddress(),
82
server.getAddress().getPort(), "/");
83
HttpURLConnection uc = (HttpURLConnection)url.openConnection(Proxy.NO_PROXY);
84
System.out.println(uc.getResponseCode());
85
} finally {
86
server.stop(1);
87
}
88
}
89
}
90
91
class SimpleHttpTransaction implements HttpHandler
92
{
93
/*
94
* Our http server which simply retruns a file with no content
95
*/
96
@Override
97
public void handle(HttpExchange trans) {
98
try {
99
trans.sendResponseHeaders(200, 0);
100
trans.close();
101
} catch (Exception e) {
102
e.printStackTrace();
103
}
104
}
105
}
106
107
class MyCacheHandler extends ResponseCache
108
{
109
public CacheResponse get(URI uri, String rqstMethod, Map rqstHeaders)
110
{
111
return null;
112
}
113
114
public CacheRequest put(URI uri, URLConnection conn)
115
{
116
return new MyCacheRequest();
117
}
118
}
119
120
class MyCacheRequest extends CacheRequest
121
{
122
public void abort() {}
123
124
public OutputStream getBody() throws IOException {
125
return null;
126
}
127
}
128
129