author | dfuchs |
Tue, 06 Feb 2018 11:04:44 +0000 | |
branch | http-client-branch |
changeset 56076 | 9a2855e0a796 |
parent 56070 | 66a9c3185028 |
child 56089 | 42208b2f224e |
permissions | -rw-r--r-- |
36131 | 1 |
/* |
48083 | 2 |
* Copyright (c) 2015, 2017, Oracle and/or its affiliates. All rights reserved. |
36131 | 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 |
||
42460
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
38745
diff
changeset
|
24 |
import jdk.incubator.http.HttpClient; |
48083 | 25 |
import jdk.incubator.http.HttpClient.Version; |
26 |
import jdk.incubator.http.HttpHeaders; |
|
42460
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
38745
diff
changeset
|
27 |
import jdk.incubator.http.HttpRequest; |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
38745
diff
changeset
|
28 |
import jdk.incubator.http.HttpResponse; |
48083 | 29 |
import jdk.testlibrary.SimpleSSLContext; |
30 |
||
31 |
import javax.net.ServerSocketFactory; |
|
32 |
import javax.net.ssl.SSLContext; |
|
33 |
import javax.net.ssl.SSLException; |
|
34 |
import javax.net.ssl.SSLServerSocketFactory; |
|
35 |
import java.io.IOException; |
|
36 |
import java.net.SocketException; |
|
36131 | 37 |
import java.net.URI; |
48083 | 38 |
import java.nio.ByteBuffer; |
39 |
import java.util.List; |
|
36131 | 40 |
import java.util.concurrent.CompletableFuture; |
48083 | 41 |
import java.util.concurrent.CompletionException; |
42 |
import java.util.concurrent.CompletionStage; |
|
43 |
import java.util.concurrent.ExecutionException; |
|
44 |
import java.util.concurrent.Flow; |
|
45 |
import java.util.concurrent.atomic.AtomicBoolean; |
|
46 |
import java.util.concurrent.atomic.AtomicInteger; |
|
47 |
||
48 |
import jdk.incubator.http.HttpResponse.BodyHandler; |
|
49 |
import jdk.incubator.http.HttpResponse.BodySubscriber; |
|
50 |
||
51 |
import static java.lang.String.format; |
|
52 |
import static java.lang.System.out; |
|
56076
9a2855e0a796
http-client-branch: more binding to 0.0.0.0 instead of localhost/127.0.0.1
dfuchs
parents:
56070
diff
changeset
|
53 |
import static java.nio.charset.StandardCharsets.ISO_8859_1; |
36131 | 54 |
|
55 |
/** |
|
56 |
* @test |
|
57 |
* @bug 8087112 |
|
48083 | 58 |
* @library /lib/testlibrary |
59 |
* @build jdk.testlibrary.SimpleSSLContext |
|
60 |
* @build MockServer |
|
61 |
* @run main/othervm CancelledResponse |
|
62 |
* @run main/othervm CancelledResponse SSL |
|
36131 | 63 |
*/ |
64 |
||
65 |
/** |
|
48083 | 66 |
* Similar test to SplitResponse except that the client will cancel the response |
67 |
* before receiving it fully. |
|
36131 | 68 |
*/ |
48083 | 69 |
public class CancelledResponse { |
36131 | 70 |
|
48083 | 71 |
static String response(String body, boolean serverKeepalive) { |
72 |
StringBuilder sb = new StringBuilder(); |
|
73 |
sb.append("HTTP/1.1 200 OK\r\n"); |
|
74 |
if (!serverKeepalive) |
|
75 |
sb.append("Connection: Close\r\n"); |
|
36131 | 76 |
|
56076
9a2855e0a796
http-client-branch: more binding to 0.0.0.0 instead of localhost/127.0.0.1
dfuchs
parents:
56070
diff
changeset
|
77 |
sb.append("Content-length: ") |
9a2855e0a796
http-client-branch: more binding to 0.0.0.0 instead of localhost/127.0.0.1
dfuchs
parents:
56070
diff
changeset
|
78 |
.append(body.getBytes(ISO_8859_1).length) |
9a2855e0a796
http-client-branch: more binding to 0.0.0.0 instead of localhost/127.0.0.1
dfuchs
parents:
56070
diff
changeset
|
79 |
.append("\r\n"); |
48083 | 80 |
sb.append("\r\n"); |
81 |
sb.append(body); |
|
82 |
return sb.toString(); |
|
36131 | 83 |
} |
84 |
||
85 |
static final String responses[] = { |
|
48083 | 86 |
"Lorem ipsum dolor sit amet consectetur adipiscing elit,", |
87 |
"sed do eiusmod tempor quis nostrud exercitation ullamco laboris nisi ut", |
|
88 |
"aliquip ex ea commodo consequat." |
|
36131 | 89 |
}; |
90 |
||
48083 | 91 |
final ServerSocketFactory factory; |
92 |
final SSLContext context; |
|
93 |
final boolean useSSL; |
|
94 |
CancelledResponse(boolean useSSL) throws IOException { |
|
95 |
this.useSSL = useSSL; |
|
96 |
context = new SimpleSSLContext().get(); |
|
97 |
SSLContext.setDefault(context); |
|
98 |
factory = useSSL ? SSLServerSocketFactory.getDefault() |
|
99 |
: ServerSocketFactory.getDefault(); |
|
100 |
} |
|
101 |
||
102 |
public HttpClient newHttpClient() { |
|
103 |
HttpClient client; |
|
104 |
if (useSSL) { |
|
105 |
client = HttpClient.newBuilder() |
|
106 |
.sslContext(context) |
|
107 |
.build(); |
|
108 |
} else { |
|
109 |
client = HttpClient.newHttpClient(); |
|
110 |
} |
|
111 |
return client; |
|
112 |
} |
|
113 |
||
36131 | 114 |
public static void main(String[] args) throws Exception { |
48083 | 115 |
boolean useSSL = false; |
116 |
if (args != null && args.length == 1) { |
|
117 |
useSSL = "SSL".equals(args[0]); |
|
118 |
} |
|
119 |
CancelledResponse sp = new CancelledResponse(useSSL); |
|
120 |
||
121 |
for (Version version : Version.values()) { |
|
122 |
for (boolean serverKeepalive : new boolean[]{ true, false }) { |
|
123 |
// Note: the mock server doesn't support Keep-Alive, but |
|
124 |
// pretending that it might exercises code paths in and out of |
|
125 |
// the connection pool, and retry logic |
|
126 |
for (boolean async : new boolean[]{ true, false }) { |
|
127 |
sp.test(version, serverKeepalive, async); |
|
128 |
} |
|
129 |
} |
|
130 |
} |
|
131 |
} |
|
132 |
||
133 |
static class CancelException extends IOException { |
|
134 |
} |
|
135 |
||
136 |
// @Test |
|
137 |
void test(Version version, boolean serverKeepalive, boolean async) |
|
138 |
throws Exception |
|
139 |
{ |
|
140 |
out.println(format("*** version %s, serverKeepAlive: %s, async: %s ***", |
|
141 |
version, serverKeepalive, async)); |
|
142 |
MockServer server = new MockServer(0, factory); |
|
36131 | 143 |
URI uri = new URI(server.getURL()); |
48083 | 144 |
out.println("server is: " + uri); |
37816
b06d70715a79
8155888: java/net/httpclient/QuickResponses.java intermittently failed with java.util.ConcurrentModificationException
michaelm
parents:
36131
diff
changeset
|
145 |
server.start(); |
36131 | 146 |
|
48083 | 147 |
HttpClient client = newHttpClient(); |
148 |
HttpRequest request = HttpRequest.newBuilder(uri).version(version).build(); |
|
149 |
try { |
|
150 |
for (int i = 0; i < responses.length; i++) { |
|
151 |
HttpResponse<String> r = null; |
|
152 |
CompletableFuture<HttpResponse<String>> cf1; |
|
153 |
CancelException expected = null; |
|
154 |
AtomicBoolean cancelled = new AtomicBoolean(); |
|
36131 | 155 |
|
48083 | 156 |
out.println("----- iteration " + i + " -----"); |
42460
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
38745
diff
changeset
|
157 |
String body = responses[i]; |
48083 | 158 |
Thread t = sendSplitResponse(response(body, serverKeepalive), server, cancelled); |
36131 | 159 |
|
48083 | 160 |
try { |
161 |
if (async) { |
|
162 |
out.println("send async: " + request); |
|
163 |
cf1 = client.sendAsync(request, asString(body, cancelled)); |
|
164 |
r = cf1.get(); |
|
165 |
} else { // sync |
|
166 |
out.println("send sync: " + request); |
|
167 |
r = client.send(request, asString(body, cancelled)); |
|
168 |
} |
|
169 |
} catch (CancelException c1) { |
|
170 |
System.out.println("Got expected exception: " + c1); |
|
171 |
expected = c1; |
|
172 |
} catch (IOException | ExecutionException | CompletionException c2) { |
|
173 |
Throwable c = c2; |
|
174 |
while (c != null && !(c instanceof CancelException)) { |
|
175 |
c = c.getCause(); |
|
176 |
} |
|
177 |
if (c instanceof CancelException) { |
|
178 |
System.out.println("Got expected exception: " + c); |
|
179 |
expected = (CancelException)c; |
|
180 |
} else throw c2; |
|
181 |
} |
|
182 |
if (r != null) { |
|
183 |
if (r.statusCode() != 200) |
|
184 |
throw new RuntimeException("Failed"); |
|
36131 | 185 |
|
48083 | 186 |
String rxbody = r.body(); |
187 |
out.println("received " + rxbody); |
|
188 |
if (!rxbody.equals(body)) |
|
189 |
throw new RuntimeException(format("Expected:%s, got:%s", body, rxbody)); |
|
190 |
} |
|
191 |
t.join(); |
|
192 |
conn.close(); |
|
193 |
if (expected == null) { |
|
194 |
throw new RuntimeException("Expected exception not raised for " |
|
195 |
+ i + " cancelled=" + cancelled.get()); |
|
196 |
} |
|
42460
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
38745
diff
changeset
|
197 |
} |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
38745
diff
changeset
|
198 |
} finally { |
48083 | 199 |
server.close(); |
36131 | 200 |
} |
201 |
System.out.println("OK"); |
|
202 |
} |
|
203 |
||
48083 | 204 |
static class CancellingSubscriber implements BodySubscriber<String> { |
205 |
private final String expected; |
|
206 |
private final CompletableFuture<String> result; |
|
207 |
private Flow.Subscription subscription; |
|
208 |
final AtomicInteger index = new AtomicInteger(); |
|
209 |
final AtomicBoolean cancelled; |
|
210 |
CancellingSubscriber(String expected, AtomicBoolean cancelled) { |
|
211 |
this.cancelled = cancelled; |
|
212 |
this.expected = expected; |
|
213 |
result = new CompletableFuture<>(); |
|
214 |
} |
|
215 |
||
216 |
@Override |
|
217 |
public CompletionStage<String> getBody() { |
|
218 |
return result; |
|
219 |
} |
|
220 |
||
221 |
@Override |
|
222 |
public void onSubscribe(Flow.Subscription subscription) { |
|
223 |
this.subscription = subscription; |
|
224 |
subscription.request(1); |
|
225 |
} |
|
226 |
||
227 |
@Override |
|
228 |
public void onNext(List<ByteBuffer> item) { |
|
229 |
//if (result.isDone()) |
|
230 |
for (ByteBuffer b : item) { |
|
231 |
while (b.hasRemaining() && !result.isDone()) { |
|
232 |
int i = index.getAndIncrement(); |
|
233 |
char at = expected.charAt(i); |
|
234 |
byte[] data = new byte[b.remaining()]; |
|
235 |
b.get(data); // we know that the server writes 1 char |
|
236 |
String s = new String(data); |
|
237 |
char c = s.charAt(0); |
|
238 |
if (c != at) { |
|
239 |
Throwable x = new IllegalStateException("char at " |
|
240 |
+ i + " is '" + c + "' expected '" |
|
241 |
+ at + "' for \"" + expected +"\""); |
|
242 |
out.println("unexpected char received, cancelling"); |
|
243 |
subscription.cancel(); |
|
244 |
result.completeExceptionally(x); |
|
245 |
return; |
|
246 |
} |
|
247 |
} |
|
248 |
} |
|
249 |
if (index.get() > 0 && !result.isDone()) { |
|
250 |
// we should complete the result here, but let's |
|
251 |
// see if we get something back... |
|
252 |
out.println("Cancelling subscription after reading " + index.get()); |
|
253 |
cancelled.set(true); |
|
254 |
subscription.cancel(); |
|
255 |
result.completeExceptionally(new CancelException()); |
|
256 |
return; |
|
257 |
} |
|
258 |
if (!result.isDone()) { |
|
259 |
out.println("requesting 1 more"); |
|
260 |
subscription.request(1); |
|
261 |
} |
|
262 |
} |
|
263 |
||
264 |
@Override |
|
265 |
public void onError(Throwable throwable) { |
|
266 |
result.completeExceptionally(throwable); |
|
267 |
} |
|
268 |
||
269 |
@Override |
|
270 |
public void onComplete() { |
|
271 |
int len = index.get(); |
|
272 |
if (len == expected.length()) { |
|
273 |
result.complete(expected); |
|
274 |
} else { |
|
275 |
Throwable x = new IllegalStateException("received only " |
|
276 |
+ len + " chars, expected " + expected.length() |
|
277 |
+ " for \"" + expected +"\""); |
|
278 |
result.completeExceptionally(x); |
|
279 |
} |
|
280 |
} |
|
281 |
} |
|
282 |
||
283 |
static class CancellingHandler implements BodyHandler<String> { |
|
284 |
final String expected; |
|
285 |
final AtomicBoolean cancelled; |
|
286 |
CancellingHandler(String expected, AtomicBoolean cancelled) { |
|
287 |
this.expected = expected; |
|
288 |
this.cancelled = cancelled; |
|
289 |
} |
|
290 |
@Override |
|
291 |
public BodySubscriber<String> apply(int statusCode, HttpHeaders responseHeaders) { |
|
292 |
assert !cancelled.get(); |
|
293 |
return new CancellingSubscriber(expected, cancelled); |
|
294 |
} |
|
295 |
} |
|
296 |
||
297 |
BodyHandler<String> asString(String expected, AtomicBoolean cancelled) { |
|
298 |
return new CancellingHandler(expected, cancelled); |
|
299 |
} |
|
300 |
||
301 |
// required for cleanup |
|
302 |
volatile MockServer.Connection conn; |
|
303 |
||
304 |
// Sends the response, mostly, one byte at a time with a small delay |
|
305 |
// between bytes, to encourage that each byte is read in a separate read |
|
306 |
Thread sendSplitResponse(String s, MockServer server, AtomicBoolean cancelled) { |
|
36131 | 307 |
System.out.println("Sending: "); |
308 |
Thread t = new Thread(() -> { |
|
48083 | 309 |
System.out.println("Waiting for server to receive headers"); |
310 |
conn = server.activity(); |
|
311 |
System.out.println("Start sending response"); |
|
312 |
int sent = 0; |
|
36131 | 313 |
try { |
314 |
int len = s.length(); |
|
48083 | 315 |
out.println("sending " + s); |
36131 | 316 |
for (int i = 0; i < len; i++) { |
317 |
String onechar = s.substring(i, i + 1); |
|
318 |
conn.send(onechar); |
|
48083 | 319 |
sent++; |
320 |
Thread.sleep(10); |
|
36131 | 321 |
} |
48083 | 322 |
out.println("sent " + s); |
323 |
} catch (SSLException | SocketException x) { |
|
324 |
// if SSL then we might get a "Broken Pipe", otherwise |
|
325 |
// a "Socket closed". |
|
326 |
boolean expected = cancelled.get(); |
|
327 |
if (sent > 0 && expected) { |
|
328 |
System.out.println("Connection closed by peer as expected: " + x); |
|
329 |
return; |
|
330 |
} else { |
|
331 |
System.out.println("Unexpected exception (sent=" |
|
332 |
+ sent + ", cancelled=" + expected + "): " + x); |
|
333 |
throw new RuntimeException(x); |
|
334 |
} |
|
36131 | 335 |
} catch (IOException | InterruptedException e) { |
48083 | 336 |
throw new RuntimeException(e); |
36131 | 337 |
} |
338 |
}); |
|
339 |
t.setDaemon(true); |
|
340 |
t.start(); |
|
48083 | 341 |
return t; |
36131 | 342 |
} |
343 |
} |