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