Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
PojavLauncherTeam
GitHub Repository: PojavLauncherTeam/mobile
Path: blob/master/test/jdk/sun/net/www/protocol/http/HttpStreams.java
41159 views
1
/*
2
* Copyright (c) 2013, 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 8011719
27
* @library /test/lib
28
* @modules jdk.httpserver
29
* @summary Basic checks to verify behavior of returned input streams
30
*/
31
32
import com.sun.net.httpserver.HttpExchange;
33
import com.sun.net.httpserver.HttpHandler;
34
import com.sun.net.httpserver.HttpServer;
35
import java.io.*;
36
import java.net.*;
37
import java.nio.charset.StandardCharsets;
38
import java.util.*;
39
40
import jdk.test.lib.net.URIBuilder;
41
42
public class HttpStreams {
43
44
void client(String u) throws Exception {
45
byte[] ba = new byte[5];
46
HttpURLConnection urlc = (HttpURLConnection)(new URL(u)).openConnection();
47
int resp = urlc.getResponseCode();
48
InputStream is;
49
if (resp == 200)
50
is = urlc.getInputStream();
51
else
52
is = urlc.getErrorStream();
53
54
expectNoThrow(() -> { is.read(); }, "read on open stream should not throw :" + u);
55
expectNoThrow(() -> { is.close(); }, "close should never throw: " + u);
56
expectNoThrow(() -> { is.close(); }, "close should never throw: " + u);
57
expectThrow(() -> { is.read(); }, "read on closed stream should throw: " + u);
58
expectThrow(() -> { is.read(ba); }, "read on closed stream should throw: " + u);
59
expectThrow(() -> { is.read(ba, 0, 2); }, "read on closed stream should throw: " + u);
60
}
61
62
String constructUrlString(int port, String path) throws Exception {
63
return URIBuilder.newBuilder()
64
.scheme("http")
65
.port(port)
66
.loopback()
67
.path(path)
68
.toURL().toString();
69
}
70
71
void test() throws Exception {
72
HttpServer server = null;
73
try {
74
server = startHttpServer();
75
int serverPort = server.getAddress().getPort();
76
client(constructUrlString(serverPort, "/chunked/"));
77
client(constructUrlString(serverPort, "/fixed/"));
78
client(constructUrlString(serverPort, "/error/"));
79
client(constructUrlString(serverPort, "/chunkedError/"));
80
81
// Test with a response cache
82
ResponseCache ch = ResponseCache.getDefault();
83
ResponseCache.setDefault(new TrivialCacheHandler());
84
try {
85
client(constructUrlString(serverPort, "/chunked/"));
86
client(constructUrlString(serverPort, "/fixed/"));
87
client(constructUrlString(serverPort, "/error/"));
88
client(constructUrlString(serverPort, "/chunkedError/"));
89
} finally {
90
ResponseCache.setDefault(ch);
91
}
92
} finally {
93
if (server != null)
94
server.stop(0);
95
}
96
97
System.out.println("passed: " + pass + ", failed: " + fail);
98
if (fail > 0)
99
throw new RuntimeException("some tests failed check output");
100
}
101
102
public static void main(String[] args) throws Exception {
103
(new HttpStreams()).test();
104
}
105
106
// HTTP Server
107
HttpServer startHttpServer() throws IOException {
108
HttpServer httpServer = HttpServer.create();
109
httpServer.bind(new InetSocketAddress(InetAddress.getLoopbackAddress(), 0), 0);
110
httpServer.createContext("/chunked/", new ChunkedHandler());
111
httpServer.createContext("/fixed/", new FixedHandler());
112
httpServer.createContext("/error/", new ErrorHandler());
113
httpServer.createContext("/chunkedError/", new ChunkedErrorHandler());
114
httpServer.start();
115
return httpServer;
116
}
117
118
static abstract class AbstractHandler implements HttpHandler {
119
@Override
120
public void handle(HttpExchange t) throws IOException {
121
try (InputStream is = t.getRequestBody()) {
122
while (is.read() != -1);
123
}
124
t.sendResponseHeaders(respCode(), length());
125
try (OutputStream os = t.getResponseBody()) {
126
os.write(message());
127
}
128
t.close();
129
}
130
131
abstract int respCode();
132
abstract int length();
133
abstract byte[] message();
134
}
135
136
static class ChunkedHandler extends AbstractHandler {
137
static final byte[] ba =
138
"Hello there from chunked handler!".getBytes(StandardCharsets.US_ASCII);
139
int respCode() { return 200; }
140
int length() { return 0; }
141
byte[] message() { return ba; }
142
}
143
144
static class FixedHandler extends AbstractHandler {
145
static final byte[] ba =
146
"Hello there from fixed handler!".getBytes(StandardCharsets.US_ASCII);
147
int respCode() { return 200; }
148
int length() { return ba.length; }
149
byte[] message() { return ba; }
150
}
151
152
static class ErrorHandler extends AbstractHandler {
153
static final byte[] ba =
154
"This is an error mesg from the server!".getBytes(StandardCharsets.US_ASCII);
155
int respCode() { return 400; }
156
int length() { return ba.length; }
157
byte[] message() { return ba; }
158
}
159
160
static class ChunkedErrorHandler extends ErrorHandler {
161
int length() { return 0; }
162
}
163
164
static class TrivialCacheHandler extends ResponseCache
165
{
166
public CacheResponse get(URI uri, String rqstMethod, Map rqstHeaders) {
167
return null;
168
}
169
170
public CacheRequest put(URI uri, URLConnection conn) {
171
return new TrivialCacheRequest();
172
}
173
}
174
175
static class TrivialCacheRequest extends CacheRequest
176
{
177
ByteArrayOutputStream baos = new ByteArrayOutputStream();
178
public void abort() {}
179
public OutputStream getBody() throws IOException { return baos; }
180
}
181
182
static interface ThrowableRunnable {
183
void run() throws IOException;
184
}
185
186
void expectThrow(ThrowableRunnable r, String msg) {
187
try { r.run(); fail(msg); } catch (IOException x) { pass(); }
188
}
189
190
void expectNoThrow(ThrowableRunnable r, String msg) {
191
try { r.run(); pass(); } catch (IOException x) { fail(msg, x); }
192
}
193
194
private int pass;
195
private int fail;
196
void pass() { pass++; }
197
void fail(String msg, Exception x) { System.out.println(msg); x.printStackTrace(); fail++; }
198
void fail(String msg) { System.out.println(msg); Thread.dumpStack(); fail++; }
199
}
200
201