author | chegar |
Wed, 06 Dec 2017 11:11:59 -0800 | |
changeset 48083 | b1c1b4ef4be2 |
parent 47216 | 71c04702a3d5 |
child 49765 | ee6f7a61f3a5 |
child 56082 | 1da51fab3032 |
permissions | -rw-r--r-- |
36433
dcbc230cfa4f
8151441: Completion result in httpclient Exchange.java lost
michaelm
parents:
diff
changeset
|
1 |
/* |
48083 | 2 |
* Copyright (c) 2015, 2017, Oracle and/or its affiliates. All rights reserved. |
36433
dcbc230cfa4f
8151441: Completion result in httpclient Exchange.java lost
michaelm
parents:
diff
changeset
|
3 |
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. |
dcbc230cfa4f
8151441: Completion result in httpclient Exchange.java lost
michaelm
parents:
diff
changeset
|
4 |
* |
dcbc230cfa4f
8151441: Completion result in httpclient Exchange.java lost
michaelm
parents:
diff
changeset
|
5 |
* This code is free software; you can redistribute it and/or modify it |
dcbc230cfa4f
8151441: Completion result in httpclient Exchange.java lost
michaelm
parents:
diff
changeset
|
6 |
* under the terms of the GNU General Public License version 2 only, as |
dcbc230cfa4f
8151441: Completion result in httpclient Exchange.java lost
michaelm
parents:
diff
changeset
|
7 |
* published by the Free Software Foundation. |
dcbc230cfa4f
8151441: Completion result in httpclient Exchange.java lost
michaelm
parents:
diff
changeset
|
8 |
* |
dcbc230cfa4f
8151441: Completion result in httpclient Exchange.java lost
michaelm
parents:
diff
changeset
|
9 |
* This code is distributed in the hope that it will be useful, but WITHOUT |
dcbc230cfa4f
8151441: Completion result in httpclient Exchange.java lost
michaelm
parents:
diff
changeset
|
10 |
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or |
dcbc230cfa4f
8151441: Completion result in httpclient Exchange.java lost
michaelm
parents:
diff
changeset
|
11 |
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License |
dcbc230cfa4f
8151441: Completion result in httpclient Exchange.java lost
michaelm
parents:
diff
changeset
|
12 |
* version 2 for more details (a copy is included in the LICENSE file that |
dcbc230cfa4f
8151441: Completion result in httpclient Exchange.java lost
michaelm
parents:
diff
changeset
|
13 |
* accompanied this code). |
dcbc230cfa4f
8151441: Completion result in httpclient Exchange.java lost
michaelm
parents:
diff
changeset
|
14 |
* |
dcbc230cfa4f
8151441: Completion result in httpclient Exchange.java lost
michaelm
parents:
diff
changeset
|
15 |
* You should have received a copy of the GNU General Public License version |
dcbc230cfa4f
8151441: Completion result in httpclient Exchange.java lost
michaelm
parents:
diff
changeset
|
16 |
* 2 along with this work; if not, write to the Free Software Foundation, |
dcbc230cfa4f
8151441: Completion result in httpclient Exchange.java lost
michaelm
parents:
diff
changeset
|
17 |
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. |
dcbc230cfa4f
8151441: Completion result in httpclient Exchange.java lost
michaelm
parents:
diff
changeset
|
18 |
* |
dcbc230cfa4f
8151441: Completion result in httpclient Exchange.java lost
michaelm
parents:
diff
changeset
|
19 |
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA |
dcbc230cfa4f
8151441: Completion result in httpclient Exchange.java lost
michaelm
parents:
diff
changeset
|
20 |
* or visit www.oracle.com if you need additional information or have any |
dcbc230cfa4f
8151441: Completion result in httpclient Exchange.java lost
michaelm
parents:
diff
changeset
|
21 |
* questions. |
dcbc230cfa4f
8151441: Completion result in httpclient Exchange.java lost
michaelm
parents:
diff
changeset
|
22 |
*/ |
dcbc230cfa4f
8151441: Completion result in httpclient Exchange.java lost
michaelm
parents:
diff
changeset
|
23 |
|
48083 | 24 |
import java.io.IOException; |
25 |
import java.io.InputStream; |
|
26 |
import java.io.OutputStream; |
|
27 |
import java.io.UncheckedIOException; |
|
42460
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
36433
diff
changeset
|
28 |
import java.net.ServerSocket; |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
36433
diff
changeset
|
29 |
import java.net.Socket; |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
36433
diff
changeset
|
30 |
import java.net.URI; |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
36433
diff
changeset
|
31 |
import java.nio.file.Files; |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
36433
diff
changeset
|
32 |
import java.nio.file.Path; |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
36433
diff
changeset
|
33 |
import java.nio.file.Paths; |
36433
dcbc230cfa4f
8151441: Completion result in httpclient Exchange.java lost
michaelm
parents:
diff
changeset
|
34 |
import java.nio.ByteBuffer; |
48083 | 35 |
import java.util.ArrayList; |
36 |
import java.util.List; |
|
42460
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
36433
diff
changeset
|
37 |
import java.util.concurrent.CompletableFuture; |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
36433
diff
changeset
|
38 |
import java.util.concurrent.ExecutionException; |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
36433
diff
changeset
|
39 |
import java.util.concurrent.Flow; |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
36433
diff
changeset
|
40 |
import java.util.concurrent.TimeoutException; |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
36433
diff
changeset
|
41 |
import java.util.concurrent.TimeUnit; |
48083 | 42 |
import java.util.function.Supplier; |
43 |
import jdk.incubator.http.HttpClient; |
|
44 |
import jdk.incubator.http.HttpResponse; |
|
45 |
import jdk.incubator.http.HttpRequest; |
|
46 |
import jdk.incubator.http.HttpTimeoutException; |
|
47 |
||
48 |
import static java.lang.System.err; |
|
42460
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
36433
diff
changeset
|
49 |
import static java.nio.charset.StandardCharsets.US_ASCII; |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
36433
diff
changeset
|
50 |
import static jdk.incubator.http.HttpResponse.BodyHandler.discard; |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
36433
diff
changeset
|
51 |
import static java.nio.charset.StandardCharsets.UTF_8; |
36433
dcbc230cfa4f
8151441: Completion result in httpclient Exchange.java lost
michaelm
parents:
diff
changeset
|
52 |
|
dcbc230cfa4f
8151441: Completion result in httpclient Exchange.java lost
michaelm
parents:
diff
changeset
|
53 |
/** |
dcbc230cfa4f
8151441: Completion result in httpclient Exchange.java lost
michaelm
parents:
diff
changeset
|
54 |
* @test |
dcbc230cfa4f
8151441: Completion result in httpclient Exchange.java lost
michaelm
parents:
diff
changeset
|
55 |
* @bug 8151441 |
42460
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
36433
diff
changeset
|
56 |
* @summary Request body of incorrect (larger or smaller) sizes than that |
48083 | 57 |
* reported by the body publisher |
42460
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
36433
diff
changeset
|
58 |
* @run main/othervm ShortRequestBody |
36433
dcbc230cfa4f
8151441: Completion result in httpclient Exchange.java lost
michaelm
parents:
diff
changeset
|
59 |
*/ |
dcbc230cfa4f
8151441: Completion result in httpclient Exchange.java lost
michaelm
parents:
diff
changeset
|
60 |
|
dcbc230cfa4f
8151441: Completion result in httpclient Exchange.java lost
michaelm
parents:
diff
changeset
|
61 |
public class ShortRequestBody { |
dcbc230cfa4f
8151441: Completion result in httpclient Exchange.java lost
michaelm
parents:
diff
changeset
|
62 |
|
42460
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
36433
diff
changeset
|
63 |
static final Path testSrc = Paths.get(System.getProperty("test.src", ".")); |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
36433
diff
changeset
|
64 |
|
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
36433
diff
changeset
|
65 |
// Some body types ( sources ) for testing. |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
36433
diff
changeset
|
66 |
static final String STRING_BODY = "Hello world"; |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
36433
diff
changeset
|
67 |
static final byte[] BYTE_ARRAY_BODY = new byte[] { |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
36433
diff
changeset
|
68 |
(byte)0xCA, (byte)0xFE, (byte)0xBA, (byte)0xBE }; |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
36433
diff
changeset
|
69 |
static final Path FILE_BODY = testSrc.resolve("docs").resolve("files").resolve("foo.txt"); |
36433
dcbc230cfa4f
8151441: Completion result in httpclient Exchange.java lost
michaelm
parents:
diff
changeset
|
70 |
|
42460
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
36433
diff
changeset
|
71 |
// Body lengths and offsets ( amount to be wrong by ), to make coordination |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
36433
diff
changeset
|
72 |
// between client and server easier. |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
36433
diff
changeset
|
73 |
static final int[] BODY_LENGTHS = new int[] { STRING_BODY.length(), |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
36433
diff
changeset
|
74 |
BYTE_ARRAY_BODY.length, |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
36433
diff
changeset
|
75 |
fileSize(FILE_BODY) }; |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
36433
diff
changeset
|
76 |
static final int[] BODY_OFFSETS = new int[] { 0, +1, -1, +2, -2, +3, -3 }; |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
36433
diff
changeset
|
77 |
|
48083 | 78 |
// A delegating Body Publisher. Subtypes will have a concrete body type. |
42460
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
36433
diff
changeset
|
79 |
static abstract class AbstractDelegateRequestBody |
48083 | 80 |
implements HttpRequest.BodyPublisher { |
36433
dcbc230cfa4f
8151441: Completion result in httpclient Exchange.java lost
michaelm
parents:
diff
changeset
|
81 |
|
48083 | 82 |
final HttpRequest.BodyPublisher delegate; |
42460
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
36433
diff
changeset
|
83 |
final long contentLength; |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
36433
diff
changeset
|
84 |
|
48083 | 85 |
AbstractDelegateRequestBody(HttpRequest.BodyPublisher delegate, |
42460
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
36433
diff
changeset
|
86 |
long contentLength) { |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
36433
diff
changeset
|
87 |
this.delegate = delegate; |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
36433
diff
changeset
|
88 |
this.contentLength = contentLength; |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
36433
diff
changeset
|
89 |
} |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
36433
diff
changeset
|
90 |
|
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
36433
diff
changeset
|
91 |
@Override |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
36433
diff
changeset
|
92 |
public void subscribe(Flow.Subscriber<? super ByteBuffer> subscriber) { |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
36433
diff
changeset
|
93 |
delegate.subscribe(subscriber); |
36433
dcbc230cfa4f
8151441: Completion result in httpclient Exchange.java lost
michaelm
parents:
diff
changeset
|
94 |
} |
dcbc230cfa4f
8151441: Completion result in httpclient Exchange.java lost
michaelm
parents:
diff
changeset
|
95 |
|
42460
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
36433
diff
changeset
|
96 |
@Override |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
36433
diff
changeset
|
97 |
public long contentLength() { return contentLength; /* may be wrong! */ } |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
36433
diff
changeset
|
98 |
} |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
36433
diff
changeset
|
99 |
|
48083 | 100 |
// Request body Publishers that may generate a different number of actual |
42460
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
36433
diff
changeset
|
101 |
// bytes to that of what is reported through their {@code contentLength}. |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
36433
diff
changeset
|
102 |
|
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
36433
diff
changeset
|
103 |
static class StringRequestBody extends AbstractDelegateRequestBody { |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
36433
diff
changeset
|
104 |
StringRequestBody(String body, int additionalLength) { |
48083 | 105 |
super(HttpRequest.BodyPublisher.fromString(body), |
42460
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
36433
diff
changeset
|
106 |
body.getBytes(UTF_8).length + additionalLength); |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
36433
diff
changeset
|
107 |
} |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
36433
diff
changeset
|
108 |
} |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
36433
diff
changeset
|
109 |
|
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
36433
diff
changeset
|
110 |
static class ByteArrayRequestBody extends AbstractDelegateRequestBody { |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
36433
diff
changeset
|
111 |
ByteArrayRequestBody(byte[] body, int additionalLength) { |
48083 | 112 |
super(HttpRequest.BodyPublisher.fromByteArray(body), |
42460
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
36433
diff
changeset
|
113 |
body.length + additionalLength); |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
36433
diff
changeset
|
114 |
} |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
36433
diff
changeset
|
115 |
} |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
36433
diff
changeset
|
116 |
|
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
36433
diff
changeset
|
117 |
static class FileRequestBody extends AbstractDelegateRequestBody { |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
36433
diff
changeset
|
118 |
FileRequestBody(Path path, int additionalLength) throws IOException { |
48083 | 119 |
super(HttpRequest.BodyPublisher.fromFile(path), |
42460
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
36433
diff
changeset
|
120 |
Files.size(path) + additionalLength); |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
36433
diff
changeset
|
121 |
} |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
36433
diff
changeset
|
122 |
} |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
36433
diff
changeset
|
123 |
|
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
36433
diff
changeset
|
124 |
// --- |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
36433
diff
changeset
|
125 |
|
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
36433
diff
changeset
|
126 |
public static void main(String[] args) throws Exception { |
48083 | 127 |
HttpClient sharedClient = HttpClient.newHttpClient(); |
128 |
List<Supplier<HttpClient>> clientSuppliers = new ArrayList<>(); |
|
129 |
clientSuppliers.add(() -> HttpClient.newHttpClient()); |
|
130 |
clientSuppliers.add(() -> sharedClient); |
|
42460
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
36433
diff
changeset
|
131 |
|
48083 | 132 |
try (Server server = new Server()) { |
133 |
for (Supplier<HttpClient> cs : clientSuppliers) { |
|
134 |
err.println("\n---- next supplier ----\n"); |
|
135 |
URI uri = new URI("http://127.0.0.1:" + server.getPort() + "/"); |
|
42460
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
36433
diff
changeset
|
136 |
|
48083 | 137 |
// sanity ( 6 requests to keep client and server offsets easy to workout ) |
138 |
success(cs, uri, new StringRequestBody(STRING_BODY, 0)); |
|
139 |
success(cs, uri, new ByteArrayRequestBody(BYTE_ARRAY_BODY, 0)); |
|
140 |
success(cs, uri, new FileRequestBody(FILE_BODY, 0)); |
|
141 |
success(cs, uri, new StringRequestBody(STRING_BODY, 0)); |
|
142 |
success(cs, uri, new ByteArrayRequestBody(BYTE_ARRAY_BODY, 0)); |
|
143 |
success(cs, uri, new FileRequestBody(FILE_BODY, 0)); |
|
42460
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
36433
diff
changeset
|
144 |
|
48083 | 145 |
for (int i = 1; i < BODY_OFFSETS.length; i++) { |
146 |
failureBlocking(cs, uri, new StringRequestBody(STRING_BODY, BODY_OFFSETS[i])); |
|
147 |
failureBlocking(cs, uri, new ByteArrayRequestBody(BYTE_ARRAY_BODY, BODY_OFFSETS[i])); |
|
148 |
failureBlocking(cs, uri, new FileRequestBody(FILE_BODY, BODY_OFFSETS[i])); |
|
149 |
||
150 |
failureNonBlocking(cs, uri, new StringRequestBody(STRING_BODY, BODY_OFFSETS[i])); |
|
151 |
failureNonBlocking(cs, uri, new ByteArrayRequestBody(BYTE_ARRAY_BODY, BODY_OFFSETS[i])); |
|
152 |
failureNonBlocking(cs, uri, new FileRequestBody(FILE_BODY, BODY_OFFSETS[i])); |
|
153 |
} |
|
42460
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
36433
diff
changeset
|
154 |
} |
36433
dcbc230cfa4f
8151441: Completion result in httpclient Exchange.java lost
michaelm
parents:
diff
changeset
|
155 |
} |
dcbc230cfa4f
8151441: Completion result in httpclient Exchange.java lost
michaelm
parents:
diff
changeset
|
156 |
} |
dcbc230cfa4f
8151441: Completion result in httpclient Exchange.java lost
michaelm
parents:
diff
changeset
|
157 |
|
48083 | 158 |
static void success(Supplier<HttpClient> clientSupplier, |
159 |
URI uri, |
|
160 |
HttpRequest.BodyPublisher publisher) |
|
42460
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
36433
diff
changeset
|
161 |
throws Exception |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
36433
diff
changeset
|
162 |
{ |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
36433
diff
changeset
|
163 |
CompletableFuture<HttpResponse<Void>> cf; |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
36433
diff
changeset
|
164 |
HttpRequest request = HttpRequest.newBuilder(uri) |
48083 | 165 |
.POST(publisher) |
42460
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
36433
diff
changeset
|
166 |
.build(); |
48083 | 167 |
cf = clientSupplier.get().sendAsync(request, discard(null)); |
42460
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
36433
diff
changeset
|
168 |
|
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
36433
diff
changeset
|
169 |
HttpResponse<Void> resp = cf.get(30, TimeUnit.SECONDS); |
48083 | 170 |
err.println("Response code: " + resp.statusCode()); |
42460
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
36433
diff
changeset
|
171 |
check(resp.statusCode() == 200, "Expected 200, got ", resp.statusCode()); |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
36433
diff
changeset
|
172 |
} |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
36433
diff
changeset
|
173 |
|
48083 | 174 |
static void failureNonBlocking(Supplier<HttpClient> clientSupplier, |
175 |
URI uri, |
|
176 |
HttpRequest.BodyPublisher publisher) |
|
42460
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
36433
diff
changeset
|
177 |
throws Exception |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
36433
diff
changeset
|
178 |
{ |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
36433
diff
changeset
|
179 |
CompletableFuture<HttpResponse<Void>> cf; |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
36433
diff
changeset
|
180 |
HttpRequest request = HttpRequest.newBuilder(uri) |
48083 | 181 |
.POST(publisher) |
42460
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
36433
diff
changeset
|
182 |
.build(); |
48083 | 183 |
cf = clientSupplier.get().sendAsync(request, discard(null)); |
42460
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
36433
diff
changeset
|
184 |
|
36433
dcbc230cfa4f
8151441: Completion result in httpclient Exchange.java lost
michaelm
parents:
diff
changeset
|
185 |
try { |
42460
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
36433
diff
changeset
|
186 |
HttpResponse<Void> r = cf.get(30, TimeUnit.SECONDS); |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
36433
diff
changeset
|
187 |
throw new RuntimeException("Unexpected response: " + r.statusCode()); |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
36433
diff
changeset
|
188 |
} catch (TimeoutException x) { |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
36433
diff
changeset
|
189 |
throw new RuntimeException("Unexpected timeout", x); |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
36433
diff
changeset
|
190 |
} catch (ExecutionException expected) { |
48083 | 191 |
err.println("Caught expected: " + expected); |
192 |
Throwable t = expected.getCause(); |
|
193 |
check(t instanceof IOException, |
|
42460
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
36433
diff
changeset
|
194 |
"Expected cause IOException, but got: ", expected.getCause()); |
48083 | 195 |
String msg = t.getMessage(); |
196 |
check(msg.contains("Too many") || msg.contains("Too few"), |
|
197 |
"Expected Too many|Too few, got: ", t); |
|
42460
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
36433
diff
changeset
|
198 |
} |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
36433
diff
changeset
|
199 |
} |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
36433
diff
changeset
|
200 |
|
48083 | 201 |
static void failureBlocking(Supplier<HttpClient> clientSupplier, |
202 |
URI uri, |
|
203 |
HttpRequest.BodyPublisher publisher) |
|
42460
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
36433
diff
changeset
|
204 |
throws Exception |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
36433
diff
changeset
|
205 |
{ |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
36433
diff
changeset
|
206 |
HttpRequest request = HttpRequest.newBuilder(uri) |
48083 | 207 |
.POST(publisher) |
42460
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
36433
diff
changeset
|
208 |
.build(); |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
36433
diff
changeset
|
209 |
try { |
48083 | 210 |
HttpResponse<Void> r = clientSupplier.get().send(request, discard(null)); |
42460
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
36433
diff
changeset
|
211 |
throw new RuntimeException("Unexpected response: " + r.statusCode()); |
48083 | 212 |
} catch (HttpTimeoutException x) { |
213 |
throw new RuntimeException("Unexpected timeout", x); |
|
42460
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
36433
diff
changeset
|
214 |
} catch (IOException expected) { |
48083 | 215 |
err.println("Caught expected: " + expected); |
216 |
String msg = expected.getMessage(); |
|
217 |
check(msg.contains("Too many") || msg.contains("Too few"), |
|
218 |
"Expected Too many|Too few, got: ", expected); |
|
42460
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
36433
diff
changeset
|
219 |
} |
36433
dcbc230cfa4f
8151441: Completion result in httpclient Exchange.java lost
michaelm
parents:
diff
changeset
|
220 |
} |
dcbc230cfa4f
8151441: Completion result in httpclient Exchange.java lost
michaelm
parents:
diff
changeset
|
221 |
|
42460
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
36433
diff
changeset
|
222 |
static class Server extends Thread implements AutoCloseable { |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
36433
diff
changeset
|
223 |
|
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
36433
diff
changeset
|
224 |
static String RESPONSE = "HTTP/1.1 200 OK\r\n" + |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
36433
diff
changeset
|
225 |
"Connection: close\r\n"+ |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
36433
diff
changeset
|
226 |
"Content-length: 0\r\n\r\n"; |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
36433
diff
changeset
|
227 |
|
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
36433
diff
changeset
|
228 |
private final ServerSocket ss; |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
36433
diff
changeset
|
229 |
private volatile boolean closed; |
36433
dcbc230cfa4f
8151441: Completion result in httpclient Exchange.java lost
michaelm
parents:
diff
changeset
|
230 |
|
42460
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
36433
diff
changeset
|
231 |
Server() throws IOException { |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
36433
diff
changeset
|
232 |
super("Test-Server"); |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
36433
diff
changeset
|
233 |
ss = new ServerSocket(0); this.start(); |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
36433
diff
changeset
|
234 |
} |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
36433
diff
changeset
|
235 |
|
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
36433
diff
changeset
|
236 |
int getPort() { return ss.getLocalPort(); } |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
36433
diff
changeset
|
237 |
|
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
36433
diff
changeset
|
238 |
@Override |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
36433
diff
changeset
|
239 |
public void run() { |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
36433
diff
changeset
|
240 |
int count = 0; |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
36433
diff
changeset
|
241 |
int offset = 0; |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
36433
diff
changeset
|
242 |
|
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
36433
diff
changeset
|
243 |
while (!closed) { |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
36433
diff
changeset
|
244 |
try (Socket s = ss.accept()) { |
48083 | 245 |
err.println("Server: got connection"); |
42460
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
36433
diff
changeset
|
246 |
InputStream is = s.getInputStream(); |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
36433
diff
changeset
|
247 |
readRequestHeaders(is); |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
36433
diff
changeset
|
248 |
byte[] ba = new byte[1024]; |
36433
dcbc230cfa4f
8151441: Completion result in httpclient Exchange.java lost
michaelm
parents:
diff
changeset
|
249 |
|
42460
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
36433
diff
changeset
|
250 |
int length = BODY_LENGTHS[count % 3]; |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
36433
diff
changeset
|
251 |
length += BODY_OFFSETS[offset]; |
48083 | 252 |
err.println("Server: count=" + count + ", offset=" + offset); |
253 |
err.println("Server: expecting " +length+ " bytes"); |
|
254 |
int read = is.readNBytes(ba, 0, length); |
|
255 |
err.println("Server: actually read " + read + " bytes"); |
|
42460
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
36433
diff
changeset
|
256 |
|
48083 | 257 |
// Update the counts before replying, to prevent the |
258 |
// client-side racing reset with this thread. |
|
42460
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
36433
diff
changeset
|
259 |
count++; |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
36433
diff
changeset
|
260 |
if (count % 6 == 0) // 6 is the number of failure requests per offset |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
36433
diff
changeset
|
261 |
offset++; |
48083 | 262 |
if (count % 42 == 0) { |
263 |
count = 0; // reset, for second iteration |
|
264 |
offset = 0; |
|
265 |
} |
|
266 |
||
267 |
if (read < length) { |
|
268 |
// no need to reply, client has already closed |
|
269 |
// ensure closed |
|
270 |
if (is.read() != -1) |
|
271 |
new AssertionError("Unexpected read"); |
|
272 |
} else { |
|
273 |
OutputStream os = s.getOutputStream(); |
|
274 |
err.println("Server: writing " |
|
275 |
+ RESPONSE.getBytes(US_ASCII).length + " bytes"); |
|
276 |
os.write(RESPONSE.getBytes(US_ASCII)); |
|
277 |
} |
|
278 |
||
42460
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
36433
diff
changeset
|
279 |
} catch (IOException e) { |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
36433
diff
changeset
|
280 |
if (!closed) |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
36433
diff
changeset
|
281 |
System.out.println("Unexpected" + e); |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
36433
diff
changeset
|
282 |
} |
36433
dcbc230cfa4f
8151441: Completion result in httpclient Exchange.java lost
michaelm
parents:
diff
changeset
|
283 |
} |
42460
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
36433
diff
changeset
|
284 |
} |
36433
dcbc230cfa4f
8151441: Completion result in httpclient Exchange.java lost
michaelm
parents:
diff
changeset
|
285 |
|
42460
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
36433
diff
changeset
|
286 |
@Override |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
36433
diff
changeset
|
287 |
public void close() { |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
36433
diff
changeset
|
288 |
if (closed) |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
36433
diff
changeset
|
289 |
return; |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
36433
diff
changeset
|
290 |
closed = true; |
36433
dcbc230cfa4f
8151441: Completion result in httpclient Exchange.java lost
michaelm
parents:
diff
changeset
|
291 |
try { |
42460
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
36433
diff
changeset
|
292 |
ss.close(); |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
36433
diff
changeset
|
293 |
} catch (IOException e) { |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
36433
diff
changeset
|
294 |
throw new UncheckedIOException("Unexpected", e); |
36433
dcbc230cfa4f
8151441: Completion result in httpclient Exchange.java lost
michaelm
parents:
diff
changeset
|
295 |
} |
dcbc230cfa4f
8151441: Completion result in httpclient Exchange.java lost
michaelm
parents:
diff
changeset
|
296 |
} |
dcbc230cfa4f
8151441: Completion result in httpclient Exchange.java lost
michaelm
parents:
diff
changeset
|
297 |
} |
42460
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
36433
diff
changeset
|
298 |
|
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
36433
diff
changeset
|
299 |
static final byte[] requestEnd = new byte[] {'\r', '\n', '\r', '\n' }; |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
36433
diff
changeset
|
300 |
|
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
36433
diff
changeset
|
301 |
// Read until the end of a HTTP request headers |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
36433
diff
changeset
|
302 |
static void readRequestHeaders(InputStream is) throws IOException { |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
36433
diff
changeset
|
303 |
int requestEndCount = 0, r; |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
36433
diff
changeset
|
304 |
while ((r = is.read()) != -1) { |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
36433
diff
changeset
|
305 |
if (r == requestEnd[requestEndCount]) { |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
36433
diff
changeset
|
306 |
requestEndCount++; |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
36433
diff
changeset
|
307 |
if (requestEndCount == 4) { |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
36433
diff
changeset
|
308 |
break; |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
36433
diff
changeset
|
309 |
} |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
36433
diff
changeset
|
310 |
} else { |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
36433
diff
changeset
|
311 |
requestEndCount = 0; |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
36433
diff
changeset
|
312 |
} |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
36433
diff
changeset
|
313 |
} |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
36433
diff
changeset
|
314 |
} |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
36433
diff
changeset
|
315 |
|
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
36433
diff
changeset
|
316 |
static int fileSize(Path p) { |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
36433
diff
changeset
|
317 |
try { return (int) Files.size(p); } |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
36433
diff
changeset
|
318 |
catch (IOException x) { throw new UncheckedIOException(x); } |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
36433
diff
changeset
|
319 |
} |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
36433
diff
changeset
|
320 |
|
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
36433
diff
changeset
|
321 |
static boolean check(boolean cond, Object... failedArgs) { |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
36433
diff
changeset
|
322 |
if (cond) |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
36433
diff
changeset
|
323 |
return true; |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
36433
diff
changeset
|
324 |
// We are going to fail... |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
36433
diff
changeset
|
325 |
StringBuilder sb = new StringBuilder(); |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
36433
diff
changeset
|
326 |
for (Object o : failedArgs) |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
36433
diff
changeset
|
327 |
sb.append(o); |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
36433
diff
changeset
|
328 |
throw new RuntimeException(sb.toString()); |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
36433
diff
changeset
|
329 |
} |
36433
dcbc230cfa4f
8151441: Completion result in httpclient Exchange.java lost
michaelm
parents:
diff
changeset
|
330 |
} |