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/https/ChunkedOutputStream.java
38867 views
1
/*
2
* Copyright (c) 2004, 2012, 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 5026745
27
* @build TestHttpsServer HttpCallback
28
* @run main/othervm ChunkedOutputStream
29
*
30
* SunJSSE does not support dynamic system properties, no way to re-use
31
* system properties in samevm/agentvm mode.
32
* @summary Cannot flush output stream when writing to an HttpUrlConnection
33
*/
34
35
import java.io.*;
36
import java.net.*;
37
import javax.net.ssl.*;
38
39
public class ChunkedOutputStream implements HttpCallback {
40
/*
41
* Where do we find the keystores for ssl?
42
*/
43
static String pathToStores = "../../../../../javax/net/ssl/etc";
44
static String keyStoreFile = "keystore";
45
static String trustStoreFile = "truststore";
46
static String passwd = "passphrase";
47
static int count = 0;
48
49
static final String str1 = "Helloworld1234567890abcdefghijklmnopqrstuvwxyz"+
50
"1234567890abcdefkjsdlkjflkjsldkfjlsdkjflkj"+
51
"1434567890abcdefkjsdlkjflkjsldkfjlsdkjflkj";
52
53
static final String str2 = "Helloworld1234567890abcdefghijklmnopqrstuvwxyz"+
54
"1234567890";
55
56
public void request (HttpTransaction req) {
57
try {
58
// this is needed (count++ doesn't work), 'cause we
59
// are doing concurrent tests
60
String path = req.getRequestURI().getPath();
61
if (path.equals("/d0")) {
62
count = 0;
63
} else if (path.equals("/d01")) {
64
count = 1;
65
} else if (path.equals("/d3")) {
66
count = 2;
67
} else if (path.equals("/d4") || path.equals("/d5")) {
68
count = 3;
69
} else if (path.equals("/d6")) {
70
count = 3;
71
} else if (path.equals("/d7")) {
72
count = 4;
73
} else if (path.equals("/d8")) {
74
count = 5;
75
}
76
77
switch (count) {
78
case 0: /* test1 -- keeps conn alive */
79
case 1: /* test2 -- closes conn */
80
String reqbody = req.getRequestEntityBody();
81
if (!reqbody.equals(str1)) {
82
req.sendResponse (500, "Internal server error");
83
req.orderlyClose();
84
}
85
String chunk = req.getRequestHeader ("Transfer-encoding");
86
if (!"chunked".equals (chunk)) {
87
req.sendResponse (501, "Internal server error");
88
req.orderlyClose();
89
}
90
req.setResponseEntityBody (reqbody);
91
if (count == 1) {
92
req.setResponseHeader ("Connection", "close");
93
}
94
req.sendResponse (200, "OK");
95
if (count == 1) {
96
req.orderlyClose();
97
}
98
break;
99
case 2: /* test 3 */
100
reqbody = req.getRequestEntityBody();
101
if (!reqbody.equals(str2)) {
102
req.sendResponse (500, "Internal server error");
103
req.orderlyClose();
104
}
105
int clen = Integer.parseInt (
106
req.getRequestHeader ("Content-length"));
107
if (clen != str2.length()) {
108
req.sendResponse (501, "Internal server error");
109
req.orderlyClose();
110
}
111
req.setResponseEntityBody (reqbody);
112
req.setResponseHeader ("Connection", "close");
113
req.sendResponse (200, "OK");
114
req.orderlyClose();
115
break;
116
case 3: /* test 6 */
117
req.setResponseHeader ("Location", "https://foo.bar/");
118
req.setResponseHeader ("Connection", "close");
119
req.sendResponse (307, "Temporary Redirect");
120
req.orderlyClose();
121
break;
122
case 4: /* test 7 */
123
case 5: /* test 8 */
124
reqbody = req.getRequestEntityBody();
125
if (reqbody != null && !"".equals (reqbody)) {
126
req.sendResponse (501, "Internal server error");
127
req.orderlyClose();
128
}
129
req.setResponseHeader ("Connection", "close");
130
req.sendResponse (200, "OK");
131
req.orderlyClose();
132
break;
133
}
134
} catch (IOException e) {
135
e.printStackTrace();
136
}
137
}
138
139
static void readAndCompare (InputStream is, String cmp) throws IOException {
140
int c;
141
byte buf[] = new byte [1024];
142
int off = 0;
143
int len = 1024;
144
while ((c=is.read(buf, off, len)) != -1) {
145
off += c;
146
len -= c;
147
}
148
String s1 = new String (buf, 0, off, "ISO8859_1");
149
if (!cmp.equals(s1)) {
150
throw new IOException ("strings not same");
151
}
152
}
153
154
/* basic chunked test (runs twice) */
155
156
static void test1 (String u) throws Exception {
157
URL url = new URL (u);
158
System.out.println ("client opening connection to: " + u);
159
HttpURLConnection urlc = (HttpURLConnection)url.openConnection ();
160
urlc.setChunkedStreamingMode (20);
161
urlc.setDoOutput(true);
162
urlc.setRequestMethod ("POST");
163
OutputStream os = urlc.getOutputStream ();
164
os.write (str1.getBytes());
165
os.close();
166
InputStream is = urlc.getInputStream();
167
readAndCompare (is, str1);
168
is.close();
169
}
170
171
/* basic fixed length test */
172
173
static void test3 (String u) throws Exception {
174
URL url = new URL (u);
175
System.out.println ("client opening connection to: " + u);
176
HttpURLConnection urlc = (HttpURLConnection)url.openConnection ();
177
urlc.setFixedLengthStreamingMode (str2.length());
178
urlc.setDoOutput(true);
179
urlc.setRequestMethod ("POST");
180
OutputStream os = urlc.getOutputStream ();
181
os.write (str2.getBytes());
182
os.close();
183
InputStream is = urlc.getInputStream();
184
readAndCompare (is, str2);
185
is.close();
186
}
187
188
/* write too few bytes */
189
190
static void test4 (String u) throws Exception {
191
URL url = new URL (u);
192
System.out.println ("client opening connection to: " + u);
193
HttpURLConnection urlc = (HttpURLConnection)url.openConnection ();
194
urlc.setFixedLengthStreamingMode (str2.length()+1);
195
urlc.setDoOutput(true);
196
urlc.setRequestMethod ("POST");
197
OutputStream os = urlc.getOutputStream ();
198
os.write (str2.getBytes());
199
try {
200
os.close();
201
throw new Exception ("should have thrown IOException");
202
} catch (IOException e) {}
203
}
204
205
/* write too many bytes */
206
207
static void test5 (String u) throws Exception {
208
URL url = new URL (u);
209
System.out.println ("client opening connection to: " + u);
210
HttpURLConnection urlc = (HttpURLConnection)url.openConnection ();
211
urlc.setFixedLengthStreamingMode (str2.length()-1);
212
urlc.setDoOutput(true);
213
urlc.setRequestMethod ("POST");
214
OutputStream os = urlc.getOutputStream ();
215
try {
216
os.write (str2.getBytes());
217
throw new Exception ("should have thrown IOException");
218
} catch (IOException e) {}
219
}
220
221
/* check for HttpRetryException on redirection */
222
223
static void test6 (String u) throws Exception {
224
URL url = new URL (u);
225
System.out.println ("client opening connection to: " + u);
226
HttpURLConnection urlc = (HttpURLConnection)url.openConnection ();
227
urlc.setChunkedStreamingMode (20);
228
urlc.setDoOutput(true);
229
urlc.setRequestMethod ("POST");
230
OutputStream os = urlc.getOutputStream ();
231
os.write (str1.getBytes());
232
os.close();
233
try {
234
InputStream is = urlc.getInputStream();
235
throw new Exception ("should have gotten HttpRetryException");
236
} catch (HttpRetryException e) {
237
if (e.responseCode() != 307) {
238
throw new Exception ("Wrong response code " + e.responseCode());
239
}
240
if (!e.getLocation().equals ("https://foo.bar/")) {
241
throw new Exception ("Wrong location " + e.getLocation());
242
}
243
}
244
}
245
246
/* next two tests send zero length posts */
247
248
static void test7 (String u) throws Exception {
249
URL url = new URL (u);
250
System.out.println ("client opening connection to: " + u);
251
HttpURLConnection urlc = (HttpURLConnection)url.openConnection ();
252
urlc.setChunkedStreamingMode (20);
253
urlc.setDoOutput(true);
254
urlc.setRequestMethod ("POST");
255
OutputStream os = urlc.getOutputStream ();
256
os.close();
257
int ret = urlc.getResponseCode();
258
if (ret != 200) {
259
throw new Exception ("Expected 200: got " + ret);
260
}
261
}
262
263
static void test8 (String u) throws Exception {
264
URL url = new URL (u);
265
System.out.println ("client opening connection to: " + u);
266
HttpURLConnection urlc = (HttpURLConnection)url.openConnection ();
267
urlc.setFixedLengthStreamingMode (0);
268
urlc.setDoOutput(true);
269
urlc.setRequestMethod ("POST");
270
OutputStream os = urlc.getOutputStream ();
271
os.close();
272
int ret = urlc.getResponseCode();
273
if (ret != 200) {
274
throw new Exception ("Expected 200: got " + ret);
275
}
276
}
277
278
static TestHttpsServer server;
279
280
public static void main (String[] args) throws Exception {
281
// setup properties to do ssl
282
String keyFilename =
283
System.getProperty("test.src", "./") + "/" + pathToStores +
284
"/" + keyStoreFile;
285
String trustFilename =
286
System.getProperty("test.src", "./") + "/" + pathToStores +
287
"/" + trustStoreFile;
288
289
HostnameVerifier reservedHV =
290
HttpsURLConnection.getDefaultHostnameVerifier();
291
try {
292
System.setProperty("javax.net.ssl.keyStore", keyFilename);
293
System.setProperty("javax.net.ssl.keyStorePassword", passwd);
294
System.setProperty("javax.net.ssl.trustStore", trustFilename);
295
System.setProperty("javax.net.ssl.trustStorePassword", passwd);
296
HttpsURLConnection.setDefaultHostnameVerifier(new NameVerifier());
297
298
try {
299
server = new TestHttpsServer(
300
new ChunkedOutputStream(), 1, 10, 0);
301
System.out.println ("Server started: listening on port: " + server.getLocalPort());
302
// the test server doesn't support keep-alive yet
303
// test1("http://localhost:"+server.getLocalPort()+"/d0");
304
test1("https://localhost:"+server.getLocalPort()+"/d01");
305
test3("https://localhost:"+server.getLocalPort()+"/d3");
306
test4("https://localhost:"+server.getLocalPort()+"/d4");
307
test5("https://localhost:"+server.getLocalPort()+"/d5");
308
test6("https://localhost:"+server.getLocalPort()+"/d6");
309
test7("https://localhost:"+server.getLocalPort()+"/d7");
310
test8("https://localhost:"+server.getLocalPort()+"/d8");
311
} catch (Exception e) {
312
if (server != null) {
313
server.terminate();
314
}
315
throw e;
316
}
317
server.terminate();
318
} finally {
319
HttpsURLConnection.setDefaultHostnameVerifier(reservedHV);
320
}
321
}
322
323
static class NameVerifier implements HostnameVerifier {
324
public boolean verify(String hostname, SSLSession session) {
325
return true;
326
}
327
}
328
329
public static void except (String s) {
330
server.terminate();
331
throw new RuntimeException (s);
332
}
333
}
334
335