Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
PojavLauncherTeam
GitHub Repository: PojavLauncherTeam/jdk17u
Path: blob/master/test/jdk/sun/net/www/protocol/http/ResponseCacheStream.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 6262486
27
* @library /test/lib
28
* @run main/othervm -Dhttp.keepAlive=false ResponseCacheStream
29
* @summary COMPATIBILITY: jagex_com - Monkey Puzzle applet fails to load
30
*/
31
32
import java.io.ByteArrayOutputStream;
33
import java.io.IOException;
34
import java.io.InputStream;
35
import java.io.OutputStream;
36
import java.io.PrintWriter;
37
import java.net.CacheRequest;
38
import java.net.CacheResponse;
39
import java.net.HttpURLConnection;
40
import java.net.InetAddress;
41
import java.net.InetSocketAddress;
42
import java.net.ResponseCache;
43
import java.net.URI;
44
import java.net.URL;
45
import java.net.URLConnection;
46
import java.util.List;
47
import java.util.Map;
48
import java.util.concurrent.Executors;
49
50
import com.sun.net.httpserver.HttpExchange;
51
import com.sun.net.httpserver.HttpHandler;
52
import com.sun.net.httpserver.HttpServer;
53
import jdk.test.lib.net.URIBuilder;
54
55
public class ResponseCacheStream implements HttpHandler {
56
57
void okReply (HttpExchange req) throws IOException {
58
req.sendResponseHeaders(200, 0);
59
try(PrintWriter pw = new PrintWriter(req.getResponseBody())) {
60
pw.print("Hello, This is the response body. Let's make it as long as possible since we need to test the cache mechanism.");
61
}
62
System.out.println ("Server: sent response");
63
}
64
65
66
@Override
67
public void handle(HttpExchange exchange) throws IOException {
68
okReply(exchange);
69
exchange.close();
70
}
71
72
static class MyCacheRequest extends CacheRequest {
73
private OutputStream buf = null;
74
75
public MyCacheRequest(OutputStream out) {
76
buf = out;
77
}
78
79
public OutputStream getBody() throws IOException {
80
return buf;
81
}
82
83
/**
84
* Aborts the attempt to cache the response. If an IOException is
85
* encountered while reading the response or writing to the cache,
86
* the current cache store operation will be abandoned.
87
*/
88
public void abort() {
89
}
90
91
}
92
93
static class MyResponseCache extends ResponseCache {
94
private ByteArrayOutputStream buf = new ByteArrayOutputStream(1024);
95
96
public MyResponseCache() {
97
}
98
99
public CacheRequest put(URI uri, URLConnection conn) throws IOException {
100
return new MyCacheRequest(buf);
101
}
102
103
public CacheResponse get(URI uri, String rqstMethod, Map<String, List<String>> rqstHeaders) throws IOException {
104
return null;
105
}
106
107
public byte[] getBuffer() {
108
return buf.toByteArray();
109
}
110
}
111
112
static HttpServer server;
113
114
public static void main(String[] args) throws Exception {
115
MyResponseCache cache = new MyResponseCache();
116
try {
117
InetAddress loopback = InetAddress.getLoopbackAddress();
118
ResponseCache.setDefault(cache);
119
server = HttpServer.create(new InetSocketAddress(loopback, 0), 10);
120
server.createContext("/", new ResponseCacheStream());
121
server.setExecutor(Executors.newSingleThreadExecutor());
122
server.start();
123
System.out.println("Server: listening on port: " + server.getAddress().getPort());
124
URL url = URIBuilder.newBuilder()
125
.scheme("http")
126
.loopback()
127
.port(server.getAddress().getPort())
128
.path("/")
129
.toURL();
130
System.out.println ("Client: connecting to " + url);
131
HttpURLConnection urlc = (HttpURLConnection)url.openConnection();
132
InputStream is = urlc.getInputStream();
133
System.out.println("is is " + is.getClass() + ". And markSupported: " + is.markSupported());
134
if (is.markSupported()) {
135
byte[] b = new byte[1024];
136
byte[] b2 = new byte[32];
137
int len;
138
int count;
139
is.mark(10);
140
len = is.read(b, 0, 10);
141
is.reset();
142
len = 0;
143
count = 0;
144
do {
145
len = is.read(b, count, 40 - count);
146
if (len > 0)
147
count += len;
148
} while (len > 0);
149
is.mark(20);
150
len = is.read(b2, 0, 20);
151
is.reset();
152
len = is.read(b, count, 10);
153
count += len;
154
is.mark(20);
155
len = is.read(b2, 0, 20);
156
is.reset();
157
do {
158
len = is.read(b, count, 1024 - count);
159
if (len > 0)
160
count += len;
161
} while (len > 0);
162
is.close();
163
String s1 = new String(b, 0 , count);
164
String s2 = new String(cache.getBuffer(), 0 , count);
165
if (! s1.equals(s2))
166
throw new RuntimeException("cache got corrupted!");
167
}
168
} catch (Exception e) {
169
if (server != null) {
170
server.stop(1);
171
}
172
throw e;
173
}
174
server.stop(1);
175
}
176
}
177
178