author | mbaesken |
Thu, 28 Nov 2019 13:02:39 +0100 | |
changeset 59323 | ae2eb76c486d |
parent 52902 | e3398b2e1ab0 |
permissions | -rw-r--r-- |
42460
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
1 |
/* |
48703
3eae36c6baa5
8195823: Buffers given to response body subscribers should not contain unprocessed HTTP data
chegar
parents:
48376
diff
changeset
|
2 |
* Copyright (c) 2015, 2018, Oracle and/or its affiliates. All rights reserved. |
42460
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
3 |
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
4 |
* |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
5 |
* This code is free software; you can redistribute it and/or modify it |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
6 |
* under the terms of the GNU General Public License version 2 only, as |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
7 |
* published by the Free Software Foundation. Oracle designates this |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
8 |
* particular file as subject to the "Classpath" exception as provided |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
9 |
* by Oracle in the LICENSE file that accompanied this code. |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
10 |
* |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
11 |
* This code is distributed in the hope that it will be useful, but WITHOUT |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
12 |
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
13 |
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
14 |
* version 2 for more details (a copy is included in the LICENSE file that |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
15 |
* accompanied this code). |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
16 |
* |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
17 |
* You should have received a copy of the GNU General Public License version |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
18 |
* 2 along with this work; if not, write to the Free Software Foundation, |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
19 |
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
20 |
* |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
21 |
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
22 |
* or visit www.oracle.com if you need additional information or have any |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
23 |
* questions. |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
24 |
*/ |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
25 |
|
49765 | 26 |
package jdk.internal.net.http; |
42460
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
27 |
|
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
28 |
import java.io.IOException; |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
29 |
import java.nio.ByteBuffer; |
48083 | 30 |
import java.util.ArrayList; |
49765 | 31 |
import java.util.Collections; |
48083 | 32 |
import java.util.List; |
42460
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
33 |
import java.util.function.Consumer; |
49765 | 34 |
import java.net.http.HttpHeaders; |
35 |
import java.net.http.HttpResponse; |
|
36 |
import jdk.internal.net.http.common.Logger; |
|
37 |
import jdk.internal.net.http.common.Utils; |
|
50681 | 38 |
import static java.lang.String.format; |
42460
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
39 |
|
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
40 |
/** |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
41 |
* Implements chunked/fixed transfer encodings of HTTP/1.1 responses. |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
42 |
* |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
43 |
* Call pushBody() to read the body (blocking). Data and errors are provided |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
44 |
* to given Consumers. After final buffer delivered, empty optional delivered |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
45 |
*/ |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
46 |
class ResponseContent { |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
47 |
|
48083 | 48 |
final HttpResponse.BodySubscriber<?> pusher; |
52283
ef0fed0a3953
8212926: HttpClient does not retrieve files with large sizes over HTTP/1.1
michaelm
parents:
51462
diff
changeset
|
49 |
final long contentLength; |
48083 | 50 |
final HttpHeaders headers; |
42460
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
51 |
// this needs to run before we complete the body |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
52 |
// so that connection can be returned to pool |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
53 |
private final Runnable onFinished; |
48083 | 54 |
private final String dbgTag; |
42460
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
55 |
|
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
56 |
ResponseContent(HttpConnection connection, |
52283
ef0fed0a3953
8212926: HttpClient does not retrieve files with large sizes over HTTP/1.1
michaelm
parents:
51462
diff
changeset
|
57 |
long contentLength, |
48083 | 58 |
HttpHeaders h, |
59 |
HttpResponse.BodySubscriber<?> userSubscriber, |
|
42460
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
60 |
Runnable onFinished) |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
61 |
{ |
48083 | 62 |
this.pusher = userSubscriber; |
42460
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
63 |
this.contentLength = contentLength; |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
64 |
this.headers = h; |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
65 |
this.onFinished = onFinished; |
48083 | 66 |
this.dbgTag = connection.dbgString() + "/ResponseContent"; |
42460
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
67 |
} |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
68 |
|
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
69 |
static final int LF = 10; |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
70 |
static final int CR = 13; |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
71 |
|
48083 | 72 |
private boolean chunkedContent, chunkedContentInitialized; |
42460
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
73 |
|
48083 | 74 |
boolean contentChunked() throws IOException { |
42460
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
75 |
if (chunkedContentInitialized) { |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
76 |
return chunkedContent; |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
77 |
} |
51462
9d7d74c6f2cb
8207966: HttpClient response without content-length does not return body
michaelm
parents:
50681
diff
changeset
|
78 |
if (contentLength == -2) { |
9d7d74c6f2cb
8207966: HttpClient response without content-length does not return body
michaelm
parents:
50681
diff
changeset
|
79 |
// HTTP/1.0 content |
9d7d74c6f2cb
8207966: HttpClient response without content-length does not return body
michaelm
parents:
50681
diff
changeset
|
80 |
chunkedContentInitialized = true; |
9d7d74c6f2cb
8207966: HttpClient response without content-length does not return body
michaelm
parents:
50681
diff
changeset
|
81 |
chunkedContent = false; |
9d7d74c6f2cb
8207966: HttpClient response without content-length does not return body
michaelm
parents:
50681
diff
changeset
|
82 |
return chunkedContent; |
9d7d74c6f2cb
8207966: HttpClient response without content-length does not return body
michaelm
parents:
50681
diff
changeset
|
83 |
} |
42460
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
84 |
if (contentLength == -1) { |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
85 |
String tc = headers.firstValue("Transfer-Encoding") |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
86 |
.orElse(""); |
52902
e3398b2e1ab0
8214971: Replace use of string.equals("") with isEmpty()
rriggs
parents:
52283
diff
changeset
|
87 |
if (!tc.isEmpty()) { |
42460
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
88 |
if (tc.equalsIgnoreCase("chunked")) { |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
89 |
chunkedContent = true; |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
90 |
} else { |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
91 |
throw new IOException("invalid content"); |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
92 |
} |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
93 |
} else { |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
94 |
chunkedContent = false; |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
95 |
} |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
96 |
} |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
97 |
chunkedContentInitialized = true; |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
98 |
return chunkedContent; |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
99 |
} |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
100 |
|
48083 | 101 |
interface BodyParser extends Consumer<ByteBuffer> { |
102 |
void onSubscribe(AbstractSubscription sub); |
|
50681 | 103 |
// A current-state message suitable for inclusion in an exception |
104 |
// detail message. |
|
105 |
String currentStateMessage(); |
|
48083 | 106 |
} |
107 |
||
108 |
// Returns a parser that will take care of parsing the received byte |
|
109 |
// buffers and forward them to the BodySubscriber. |
|
110 |
// When the parser is done, it will call onComplete. |
|
111 |
// If parsing was successful, the throwable parameter will be null. |
|
112 |
// Otherwise it will be the exception that occurred |
|
113 |
// Note: revisit: it might be better to use a CompletableFuture than |
|
114 |
// a completion handler. |
|
115 |
BodyParser getBodyParser(Consumer<Throwable> onComplete) |
|
116 |
throws IOException { |
|
117 |
if (contentChunked()) { |
|
118 |
return new ChunkedBodyParser(onComplete); |
|
119 |
} else { |
|
51462
9d7d74c6f2cb
8207966: HttpClient response without content-length does not return body
michaelm
parents:
50681
diff
changeset
|
120 |
return contentLength == -2 |
9d7d74c6f2cb
8207966: HttpClient response without content-length does not return body
michaelm
parents:
50681
diff
changeset
|
121 |
? new UnknownLengthBodyParser(onComplete) |
9d7d74c6f2cb
8207966: HttpClient response without content-length does not return body
michaelm
parents:
50681
diff
changeset
|
122 |
: new FixedLengthBodyParser(contentLength, onComplete); |
42460
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
123 |
} |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
124 |
} |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
125 |
|
48083 | 126 |
|
127 |
static enum ChunkState {READING_LENGTH, READING_DATA, DONE} |
|
128 |
class ChunkedBodyParser implements BodyParser { |
|
129 |
final ByteBuffer READMORE = Utils.EMPTY_BYTEBUFFER; |
|
130 |
final Consumer<Throwable> onComplete; |
|
49765 | 131 |
final Logger debug = Utils.getDebugLogger(this::dbgString, Utils.DEBUG); |
48083 | 132 |
final String dbgTag = ResponseContent.this.dbgTag + "/ChunkedBodyParser"; |
133 |
||
134 |
volatile Throwable closedExceptionally; |
|
135 |
volatile int partialChunklen = 0; // partially read chunk len |
|
136 |
volatile int chunklen = -1; // number of bytes in chunk |
|
137 |
volatile int bytesremaining; // number of bytes in chunk left to be read incl CRLF |
|
138 |
volatile boolean cr = false; // tryReadChunkLength has found CR |
|
139 |
volatile int bytesToConsume; // number of bytes that still need to be consumed before proceeding |
|
140 |
volatile ChunkState state = ChunkState.READING_LENGTH; // current state |
|
141 |
volatile AbstractSubscription sub; |
|
142 |
ChunkedBodyParser(Consumer<Throwable> onComplete) { |
|
143 |
this.onComplete = onComplete; |
|
42460
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
144 |
} |
48083 | 145 |
|
146 |
String dbgString() { |
|
147 |
return dbgTag; |
|
148 |
} |
|
42460
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
149 |
|
48083 | 150 |
@Override |
151 |
public void onSubscribe(AbstractSubscription sub) { |
|
49765 | 152 |
if (debug.on()) |
153 |
debug.log("onSubscribe: " + pusher.getClass().getName()); |
|
48083 | 154 |
pusher.onSubscribe(this.sub = sub); |
155 |
} |
|
42460
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
156 |
|
48083 | 157 |
@Override |
50681 | 158 |
public String currentStateMessage() { |
159 |
return format("chunked transfer encoding, state: %s", state); |
|
160 |
} |
|
161 |
||
162 |
@Override |
|
48083 | 163 |
public void accept(ByteBuffer b) { |
164 |
if (closedExceptionally != null) { |
|
49765 | 165 |
if (debug.on()) |
166 |
debug.log("already closed: " + closedExceptionally); |
|
48083 | 167 |
return; |
42460
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
168 |
} |
48083 | 169 |
boolean completed = false; |
170 |
try { |
|
171 |
List<ByteBuffer> out = new ArrayList<>(); |
|
172 |
do { |
|
173 |
if (tryPushOneHunk(b, out)) { |
|
174 |
// We're done! (true if the final chunk was parsed). |
|
175 |
if (!out.isEmpty()) { |
|
176 |
// push what we have and complete |
|
177 |
// only reduce demand if we actually push something. |
|
178 |
// we would not have come here if there was no |
|
179 |
// demand. |
|
180 |
boolean hasDemand = sub.demand().tryDecrement(); |
|
181 |
assert hasDemand; |
|
49765 | 182 |
pusher.onNext(Collections.unmodifiableList(out)); |
183 |
if (debug.on()) debug.log("Chunks sent"); |
|
48083 | 184 |
} |
49765 | 185 |
if (debug.on()) debug.log("done!"); |
48083 | 186 |
assert closedExceptionally == null; |
187 |
assert state == ChunkState.DONE; |
|
188 |
onFinished.run(); |
|
189 |
pusher.onComplete(); |
|
49765 | 190 |
if (debug.on()) debug.log("subscriber completed"); |
48083 | 191 |
completed = true; |
192 |
onComplete.accept(closedExceptionally); // should be null |
|
193 |
break; |
|
194 |
} |
|
195 |
// the buffer may contain several hunks, and therefore |
|
196 |
// we must loop while it's not exhausted. |
|
197 |
} while (b.hasRemaining()); |
|
42460
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
198 |
|
48083 | 199 |
if (!completed && !out.isEmpty()) { |
200 |
// push what we have. |
|
201 |
// only reduce demand if we actually push something. |
|
202 |
// we would not have come here if there was no |
|
203 |
// demand. |
|
204 |
boolean hasDemand = sub.demand().tryDecrement(); |
|
205 |
assert hasDemand; |
|
49765 | 206 |
pusher.onNext(Collections.unmodifiableList(out)); |
207 |
if (debug.on()) debug.log("Chunk sent"); |
|
48083 | 208 |
} |
209 |
assert state == ChunkState.DONE || !b.hasRemaining(); |
|
210 |
} catch(Throwable t) { |
|
49765 | 211 |
if (debug.on()) |
212 |
debug.log("Error while processing buffer: %s", (Object)t ); |
|
48083 | 213 |
closedExceptionally = t; |
214 |
if (!completed) onComplete.accept(t); |
|
42460
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
215 |
} |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
216 |
} |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
217 |
|
48083 | 218 |
// reads and returns chunklen. Position of chunkbuf is first byte |
219 |
// of chunk on return. chunklen includes the CR LF at end of chunk |
|
220 |
// returns -1 if needs more bytes |
|
221 |
private int tryReadChunkLen(ByteBuffer chunkbuf) throws IOException { |
|
222 |
assert state == ChunkState.READING_LENGTH; |
|
223 |
while (chunkbuf.hasRemaining()) { |
|
224 |
int c = chunkbuf.get(); |
|
225 |
if (cr) { |
|
226 |
if (c == LF) { |
|
227 |
return partialChunklen; |
|
228 |
} else { |
|
229 |
throw new IOException("invalid chunk header"); |
|
230 |
} |
|
231 |
} |
|
232 |
if (c == CR) { |
|
233 |
cr = true; |
|
234 |
} else { |
|
235 |
int digit = toDigit(c); |
|
236 |
partialChunklen = partialChunklen * 16 + digit; |
|
237 |
} |
|
238 |
} |
|
239 |
return -1; |
|
240 |
} |
|
241 |
||
242 |
||
243 |
// try to consume as many bytes as specified by bytesToConsume. |
|
244 |
// returns the number of bytes that still need to be consumed. |
|
245 |
// In practice this method is only called to consume one CRLF pair |
|
246 |
// with bytesToConsume set to 2, so it will only return 0 (if completed), |
|
247 |
// 1, or 2 (if chunkbuf doesn't have the 2 chars). |
|
248 |
private int tryConsumeBytes(ByteBuffer chunkbuf) throws IOException { |
|
249 |
int n = bytesToConsume; |
|
250 |
if (n > 0) { |
|
251 |
int e = Math.min(chunkbuf.remaining(), n); |
|
252 |
||
253 |
// verifies some assertions |
|
254 |
// this methods is called only to consume CRLF |
|
255 |
if (Utils.ASSERTIONSENABLED) { |
|
256 |
assert n <= 2 && e <= 2; |
|
257 |
ByteBuffer tmp = chunkbuf.slice(); |
|
258 |
// if n == 2 assert that we will first consume CR |
|
259 |
assert (n == 2 && e > 0) ? tmp.get() == CR : true; |
|
260 |
// if n == 1 || n == 2 && e == 2 assert that we then consume LF |
|
261 |
assert (n == 1 || e == 2) ? tmp.get() == LF : true; |
|
262 |
} |
|
263 |
||
264 |
chunkbuf.position(chunkbuf.position() + e); |
|
265 |
n -= e; |
|
266 |
bytesToConsume = n; |
|
267 |
} |
|
268 |
assert n >= 0; |
|
269 |
return n; |
|
270 |
} |
|
271 |
||
272 |
/** |
|
273 |
* Returns a ByteBuffer containing chunk of data or a "hunk" of data |
|
274 |
* (a chunk of a chunk if the chunk size is larger than our ByteBuffers). |
|
275 |
* If the given chunk does not have enough data this method return |
|
276 |
* an empty ByteBuffer (READMORE). |
|
277 |
* If we encounter the final chunk (an empty chunk) this method |
|
278 |
* returns null. |
|
279 |
*/ |
|
280 |
ByteBuffer tryReadOneHunk(ByteBuffer chunk) throws IOException { |
|
281 |
int unfulfilled = bytesremaining; |
|
282 |
int toconsume = bytesToConsume; |
|
283 |
ChunkState st = state; |
|
284 |
if (st == ChunkState.READING_LENGTH && chunklen == -1) { |
|
49765 | 285 |
if (debug.on()) debug.log(() -> "Trying to read chunk len" |
48083 | 286 |
+ " (remaining in buffer:"+chunk.remaining()+")"); |
287 |
int clen = chunklen = tryReadChunkLen(chunk); |
|
288 |
if (clen == -1) return READMORE; |
|
49765 | 289 |
if (debug.on()) debug.log("Got chunk len %d", clen); |
48083 | 290 |
cr = false; partialChunklen = 0; |
291 |
unfulfilled = bytesremaining = clen; |
|
292 |
if (clen == 0) toconsume = bytesToConsume = 2; // that was the last chunk |
|
293 |
else st = state = ChunkState.READING_DATA; // read the data |
|
294 |
} |
|
295 |
||
296 |
if (toconsume > 0) { |
|
49765 | 297 |
if (debug.on()) |
298 |
debug.log("Trying to consume bytes: %d (remaining in buffer: %s)", |
|
299 |
toconsume, chunk.remaining()); |
|
48083 | 300 |
if (tryConsumeBytes(chunk) > 0) { |
301 |
return READMORE; |
|
302 |
} |
|
303 |
} |
|
304 |
||
305 |
toconsume = bytesToConsume; |
|
306 |
assert toconsume == 0; |
|
307 |
||
42460
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
308 |
|
48083 | 309 |
if (st == ChunkState.READING_LENGTH) { |
310 |
// we will come here only if chunklen was 0, after having |
|
311 |
// consumed the trailing CRLF |
|
312 |
int clen = chunklen; |
|
313 |
assert clen == 0; |
|
49765 | 314 |
if (debug.on()) debug.log("No more chunks: %d", clen); |
48083 | 315 |
// the DONE state is not really needed but it helps with |
316 |
// assertions... |
|
317 |
state = ChunkState.DONE; |
|
318 |
return null; |
|
319 |
} |
|
320 |
||
321 |
int clen = chunklen; |
|
322 |
assert clen > 0; |
|
323 |
assert st == ChunkState.READING_DATA; |
|
324 |
||
325 |
ByteBuffer returnBuffer = READMORE; // May be a hunk or a chunk |
|
326 |
if (unfulfilled > 0) { |
|
327 |
int bytesread = chunk.remaining(); |
|
49765 | 328 |
if (debug.on()) |
329 |
debug.log("Reading chunk: available %d, needed %d", |
|
330 |
bytesread, unfulfilled); |
|
48083 | 331 |
|
332 |
int bytes2return = Math.min(bytesread, unfulfilled); |
|
49765 | 333 |
if (debug.on()) |
334 |
debug.log( "Returning chunk bytes: %d", bytes2return); |
|
335 |
returnBuffer = Utils.sliceWithLimitedCapacity(chunk, bytes2return).asReadOnlyBuffer(); |
|
48083 | 336 |
unfulfilled = bytesremaining -= bytes2return; |
337 |
if (unfulfilled == 0) bytesToConsume = 2; |
|
338 |
} |
|
339 |
||
340 |
assert unfulfilled >= 0; |
|
341 |
||
342 |
if (unfulfilled == 0) { |
|
49765 | 343 |
if (debug.on()) |
344 |
debug.log("No more bytes to read - %d yet to consume.", |
|
345 |
unfulfilled); |
|
48083 | 346 |
// check whether the trailing CRLF is consumed, try to |
347 |
// consume it if not. If tryConsumeBytes needs more bytes |
|
348 |
// then we will come back here later - skipping the block |
|
349 |
// that reads data because remaining==0, and finding |
|
350 |
// that the two bytes are now consumed. |
|
351 |
if (tryConsumeBytes(chunk) == 0) { |
|
352 |
// we're done for this chunk! reset all states and |
|
353 |
// prepare to read the next chunk. |
|
354 |
chunklen = -1; |
|
355 |
partialChunklen = 0; |
|
356 |
cr = false; |
|
357 |
state = ChunkState.READING_LENGTH; |
|
49765 | 358 |
if (debug.on()) debug.log("Ready to read next chunk"); |
48083 | 359 |
} |
360 |
} |
|
361 |
if (returnBuffer == READMORE) { |
|
49765 | 362 |
if (debug.on()) debug.log("Need more data"); |
48083 | 363 |
} |
364 |
return returnBuffer; |
|
42460
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
365 |
} |
48083 | 366 |
|
367 |
||
368 |
// Attempt to parse and push one hunk from the buffer. |
|
369 |
// Returns true if the final chunk was parsed. |
|
370 |
// Returns false if we need to push more chunks. |
|
371 |
private boolean tryPushOneHunk(ByteBuffer b, List<ByteBuffer> out) |
|
372 |
throws IOException { |
|
373 |
assert state != ChunkState.DONE; |
|
374 |
ByteBuffer b1 = tryReadOneHunk(b); |
|
375 |
if (b1 != null) { |
|
376 |
//assert b1.hasRemaining() || b1 == READMORE; |
|
377 |
if (b1.hasRemaining()) { |
|
49765 | 378 |
if (debug.on()) |
379 |
debug.log("Sending chunk to consumer (%d)", b1.remaining()); |
|
48083 | 380 |
out.add(b1); |
381 |
} |
|
382 |
return false; // we haven't parsed the final chunk yet. |
|
383 |
} else { |
|
384 |
return true; // we're done! the final chunk was parsed. |
|
385 |
} |
|
386 |
} |
|
387 |
||
388 |
private int toDigit(int b) throws IOException { |
|
389 |
if (b >= 0x30 && b <= 0x39) { |
|
390 |
return b - 0x30; |
|
391 |
} |
|
392 |
if (b >= 0x41 && b <= 0x46) { |
|
393 |
return b - 0x41 + 10; |
|
394 |
} |
|
395 |
if (b >= 0x61 && b <= 0x66) { |
|
396 |
return b - 0x61 + 10; |
|
397 |
} |
|
398 |
throw new IOException("Invalid chunk header byte " + b); |
|
399 |
} |
|
400 |
||
42460
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
401 |
} |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
402 |
|
51462
9d7d74c6f2cb
8207966: HttpClient response without content-length does not return body
michaelm
parents:
50681
diff
changeset
|
403 |
class UnknownLengthBodyParser implements BodyParser { |
9d7d74c6f2cb
8207966: HttpClient response without content-length does not return body
michaelm
parents:
50681
diff
changeset
|
404 |
final Consumer<Throwable> onComplete; |
9d7d74c6f2cb
8207966: HttpClient response without content-length does not return body
michaelm
parents:
50681
diff
changeset
|
405 |
final Logger debug = Utils.getDebugLogger(this::dbgString, Utils.DEBUG); |
9d7d74c6f2cb
8207966: HttpClient response without content-length does not return body
michaelm
parents:
50681
diff
changeset
|
406 |
final String dbgTag = ResponseContent.this.dbgTag + "/UnknownLengthBodyParser"; |
9d7d74c6f2cb
8207966: HttpClient response without content-length does not return body
michaelm
parents:
50681
diff
changeset
|
407 |
volatile Throwable closedExceptionally; |
9d7d74c6f2cb
8207966: HttpClient response without content-length does not return body
michaelm
parents:
50681
diff
changeset
|
408 |
volatile AbstractSubscription sub; |
9d7d74c6f2cb
8207966: HttpClient response without content-length does not return body
michaelm
parents:
50681
diff
changeset
|
409 |
volatile int breceived = 0; |
9d7d74c6f2cb
8207966: HttpClient response without content-length does not return body
michaelm
parents:
50681
diff
changeset
|
410 |
|
9d7d74c6f2cb
8207966: HttpClient response without content-length does not return body
michaelm
parents:
50681
diff
changeset
|
411 |
UnknownLengthBodyParser(Consumer<Throwable> onComplete) { |
9d7d74c6f2cb
8207966: HttpClient response without content-length does not return body
michaelm
parents:
50681
diff
changeset
|
412 |
this.onComplete = onComplete; |
9d7d74c6f2cb
8207966: HttpClient response without content-length does not return body
michaelm
parents:
50681
diff
changeset
|
413 |
} |
9d7d74c6f2cb
8207966: HttpClient response without content-length does not return body
michaelm
parents:
50681
diff
changeset
|
414 |
|
9d7d74c6f2cb
8207966: HttpClient response without content-length does not return body
michaelm
parents:
50681
diff
changeset
|
415 |
String dbgString() { |
9d7d74c6f2cb
8207966: HttpClient response without content-length does not return body
michaelm
parents:
50681
diff
changeset
|
416 |
return dbgTag; |
9d7d74c6f2cb
8207966: HttpClient response without content-length does not return body
michaelm
parents:
50681
diff
changeset
|
417 |
} |
9d7d74c6f2cb
8207966: HttpClient response without content-length does not return body
michaelm
parents:
50681
diff
changeset
|
418 |
|
9d7d74c6f2cb
8207966: HttpClient response without content-length does not return body
michaelm
parents:
50681
diff
changeset
|
419 |
@Override |
9d7d74c6f2cb
8207966: HttpClient response without content-length does not return body
michaelm
parents:
50681
diff
changeset
|
420 |
public void onSubscribe(AbstractSubscription sub) { |
9d7d74c6f2cb
8207966: HttpClient response without content-length does not return body
michaelm
parents:
50681
diff
changeset
|
421 |
if (debug.on()) |
9d7d74c6f2cb
8207966: HttpClient response without content-length does not return body
michaelm
parents:
50681
diff
changeset
|
422 |
debug.log("onSubscribe: " + pusher.getClass().getName()); |
9d7d74c6f2cb
8207966: HttpClient response without content-length does not return body
michaelm
parents:
50681
diff
changeset
|
423 |
pusher.onSubscribe(this.sub = sub); |
9d7d74c6f2cb
8207966: HttpClient response without content-length does not return body
michaelm
parents:
50681
diff
changeset
|
424 |
} |
9d7d74c6f2cb
8207966: HttpClient response without content-length does not return body
michaelm
parents:
50681
diff
changeset
|
425 |
|
9d7d74c6f2cb
8207966: HttpClient response without content-length does not return body
michaelm
parents:
50681
diff
changeset
|
426 |
@Override |
9d7d74c6f2cb
8207966: HttpClient response without content-length does not return body
michaelm
parents:
50681
diff
changeset
|
427 |
public String currentStateMessage() { |
9d7d74c6f2cb
8207966: HttpClient response without content-length does not return body
michaelm
parents:
50681
diff
changeset
|
428 |
return format("http1_0 content, bytes received: %d", breceived); |
9d7d74c6f2cb
8207966: HttpClient response without content-length does not return body
michaelm
parents:
50681
diff
changeset
|
429 |
} |
9d7d74c6f2cb
8207966: HttpClient response without content-length does not return body
michaelm
parents:
50681
diff
changeset
|
430 |
|
9d7d74c6f2cb
8207966: HttpClient response without content-length does not return body
michaelm
parents:
50681
diff
changeset
|
431 |
@Override |
9d7d74c6f2cb
8207966: HttpClient response without content-length does not return body
michaelm
parents:
50681
diff
changeset
|
432 |
public void accept(ByteBuffer b) { |
9d7d74c6f2cb
8207966: HttpClient response without content-length does not return body
michaelm
parents:
50681
diff
changeset
|
433 |
if (closedExceptionally != null) { |
9d7d74c6f2cb
8207966: HttpClient response without content-length does not return body
michaelm
parents:
50681
diff
changeset
|
434 |
if (debug.on()) |
9d7d74c6f2cb
8207966: HttpClient response without content-length does not return body
michaelm
parents:
50681
diff
changeset
|
435 |
debug.log("already closed: " + closedExceptionally); |
9d7d74c6f2cb
8207966: HttpClient response without content-length does not return body
michaelm
parents:
50681
diff
changeset
|
436 |
return; |
9d7d74c6f2cb
8207966: HttpClient response without content-length does not return body
michaelm
parents:
50681
diff
changeset
|
437 |
} |
9d7d74c6f2cb
8207966: HttpClient response without content-length does not return body
michaelm
parents:
50681
diff
changeset
|
438 |
boolean completed = false; |
9d7d74c6f2cb
8207966: HttpClient response without content-length does not return body
michaelm
parents:
50681
diff
changeset
|
439 |
try { |
9d7d74c6f2cb
8207966: HttpClient response without content-length does not return body
michaelm
parents:
50681
diff
changeset
|
440 |
if (debug.on()) |
9d7d74c6f2cb
8207966: HttpClient response without content-length does not return body
michaelm
parents:
50681
diff
changeset
|
441 |
debug.log("Parser got %d bytes ", b.remaining()); |
9d7d74c6f2cb
8207966: HttpClient response without content-length does not return body
michaelm
parents:
50681
diff
changeset
|
442 |
|
9d7d74c6f2cb
8207966: HttpClient response without content-length does not return body
michaelm
parents:
50681
diff
changeset
|
443 |
if (b.hasRemaining()) { |
9d7d74c6f2cb
8207966: HttpClient response without content-length does not return body
michaelm
parents:
50681
diff
changeset
|
444 |
// only reduce demand if we actually push something. |
9d7d74c6f2cb
8207966: HttpClient response without content-length does not return body
michaelm
parents:
50681
diff
changeset
|
445 |
// we would not have come here if there was no |
9d7d74c6f2cb
8207966: HttpClient response without content-length does not return body
michaelm
parents:
50681
diff
changeset
|
446 |
// demand. |
9d7d74c6f2cb
8207966: HttpClient response without content-length does not return body
michaelm
parents:
50681
diff
changeset
|
447 |
boolean hasDemand = sub.demand().tryDecrement(); |
9d7d74c6f2cb
8207966: HttpClient response without content-length does not return body
michaelm
parents:
50681
diff
changeset
|
448 |
assert hasDemand; |
9d7d74c6f2cb
8207966: HttpClient response without content-length does not return body
michaelm
parents:
50681
diff
changeset
|
449 |
breceived += b.remaining(); |
9d7d74c6f2cb
8207966: HttpClient response without content-length does not return body
michaelm
parents:
50681
diff
changeset
|
450 |
pusher.onNext(List.of(b.asReadOnlyBuffer())); |
9d7d74c6f2cb
8207966: HttpClient response without content-length does not return body
michaelm
parents:
50681
diff
changeset
|
451 |
} |
9d7d74c6f2cb
8207966: HttpClient response without content-length does not return body
michaelm
parents:
50681
diff
changeset
|
452 |
} catch (Throwable t) { |
9d7d74c6f2cb
8207966: HttpClient response without content-length does not return body
michaelm
parents:
50681
diff
changeset
|
453 |
if (debug.on()) debug.log("Unexpected exception", t); |
9d7d74c6f2cb
8207966: HttpClient response without content-length does not return body
michaelm
parents:
50681
diff
changeset
|
454 |
closedExceptionally = t; |
9d7d74c6f2cb
8207966: HttpClient response without content-length does not return body
michaelm
parents:
50681
diff
changeset
|
455 |
if (!completed) { |
9d7d74c6f2cb
8207966: HttpClient response without content-length does not return body
michaelm
parents:
50681
diff
changeset
|
456 |
onComplete.accept(t); |
9d7d74c6f2cb
8207966: HttpClient response without content-length does not return body
michaelm
parents:
50681
diff
changeset
|
457 |
} |
9d7d74c6f2cb
8207966: HttpClient response without content-length does not return body
michaelm
parents:
50681
diff
changeset
|
458 |
} |
9d7d74c6f2cb
8207966: HttpClient response without content-length does not return body
michaelm
parents:
50681
diff
changeset
|
459 |
} |
9d7d74c6f2cb
8207966: HttpClient response without content-length does not return body
michaelm
parents:
50681
diff
changeset
|
460 |
|
9d7d74c6f2cb
8207966: HttpClient response without content-length does not return body
michaelm
parents:
50681
diff
changeset
|
461 |
/** |
9d7d74c6f2cb
8207966: HttpClient response without content-length does not return body
michaelm
parents:
50681
diff
changeset
|
462 |
* Must be called externally when connection has closed |
9d7d74c6f2cb
8207966: HttpClient response without content-length does not return body
michaelm
parents:
50681
diff
changeset
|
463 |
* and therefore no more bytes can be read |
9d7d74c6f2cb
8207966: HttpClient response without content-length does not return body
michaelm
parents:
50681
diff
changeset
|
464 |
*/ |
9d7d74c6f2cb
8207966: HttpClient response without content-length does not return body
michaelm
parents:
50681
diff
changeset
|
465 |
public void complete() { |
9d7d74c6f2cb
8207966: HttpClient response without content-length does not return body
michaelm
parents:
50681
diff
changeset
|
466 |
// We're done! All data has been received. |
9d7d74c6f2cb
8207966: HttpClient response without content-length does not return body
michaelm
parents:
50681
diff
changeset
|
467 |
if (debug.on()) |
9d7d74c6f2cb
8207966: HttpClient response without content-length does not return body
michaelm
parents:
50681
diff
changeset
|
468 |
debug.log("Parser got all expected bytes: completing"); |
9d7d74c6f2cb
8207966: HttpClient response without content-length does not return body
michaelm
parents:
50681
diff
changeset
|
469 |
assert closedExceptionally == null; |
9d7d74c6f2cb
8207966: HttpClient response without content-length does not return body
michaelm
parents:
50681
diff
changeset
|
470 |
onFinished.run(); |
9d7d74c6f2cb
8207966: HttpClient response without content-length does not return body
michaelm
parents:
50681
diff
changeset
|
471 |
pusher.onComplete(); |
9d7d74c6f2cb
8207966: HttpClient response without content-length does not return body
michaelm
parents:
50681
diff
changeset
|
472 |
onComplete.accept(closedExceptionally); // should be null |
9d7d74c6f2cb
8207966: HttpClient response without content-length does not return body
michaelm
parents:
50681
diff
changeset
|
473 |
} |
9d7d74c6f2cb
8207966: HttpClient response without content-length does not return body
michaelm
parents:
50681
diff
changeset
|
474 |
} |
9d7d74c6f2cb
8207966: HttpClient response without content-length does not return body
michaelm
parents:
50681
diff
changeset
|
475 |
|
48083 | 476 |
class FixedLengthBodyParser implements BodyParser { |
52283
ef0fed0a3953
8212926: HttpClient does not retrieve files with large sizes over HTTP/1.1
michaelm
parents:
51462
diff
changeset
|
477 |
final long contentLength; |
48083 | 478 |
final Consumer<Throwable> onComplete; |
49765 | 479 |
final Logger debug = Utils.getDebugLogger(this::dbgString, Utils.DEBUG); |
48083 | 480 |
final String dbgTag = ResponseContent.this.dbgTag + "/FixedLengthBodyParser"; |
52283
ef0fed0a3953
8212926: HttpClient does not retrieve files with large sizes over HTTP/1.1
michaelm
parents:
51462
diff
changeset
|
481 |
volatile long remaining; |
48083 | 482 |
volatile Throwable closedExceptionally; |
483 |
volatile AbstractSubscription sub; |
|
52283
ef0fed0a3953
8212926: HttpClient does not retrieve files with large sizes over HTTP/1.1
michaelm
parents:
51462
diff
changeset
|
484 |
FixedLengthBodyParser(long contentLength, Consumer<Throwable> onComplete) { |
48083 | 485 |
this.contentLength = this.remaining = contentLength; |
486 |
this.onComplete = onComplete; |
|
487 |
} |
|
488 |
||
489 |
String dbgString() { |
|
490 |
return dbgTag; |
|
491 |
} |
|
42460
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
492 |
|
48083 | 493 |
@Override |
494 |
public void onSubscribe(AbstractSubscription sub) { |
|
49765 | 495 |
if (debug.on()) |
496 |
debug.log("length=" + contentLength +", onSubscribe: " |
|
497 |
+ pusher.getClass().getName()); |
|
48083 | 498 |
pusher.onSubscribe(this.sub = sub); |
499 |
try { |
|
500 |
if (contentLength == 0) { |
|
48376
41ae5c69b09c
8192966: HttpClient should reuse TCP connection for h2c connections
michaelm
parents:
48083
diff
changeset
|
501 |
onFinished.run(); |
48083 | 502 |
pusher.onComplete(); |
503 |
onComplete.accept(null); |
|
504 |
} |
|
505 |
} catch (Throwable t) { |
|
506 |
closedExceptionally = t; |
|
507 |
try { |
|
508 |
pusher.onError(t); |
|
509 |
} finally { |
|
510 |
onComplete.accept(t); |
|
511 |
} |
|
512 |
} |
|
513 |
} |
|
42460
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
514 |
|
48083 | 515 |
@Override |
50681 | 516 |
public String currentStateMessage() { |
517 |
return format("fixed content-length: %d, bytes received: %d", |
|
518 |
contentLength, contentLength - remaining); |
|
519 |
} |
|
520 |
||
521 |
@Override |
|
48083 | 522 |
public void accept(ByteBuffer b) { |
523 |
if (closedExceptionally != null) { |
|
49765 | 524 |
if (debug.on()) |
525 |
debug.log("already closed: " + closedExceptionally); |
|
48083 | 526 |
return; |
527 |
} |
|
528 |
boolean completed = false; |
|
529 |
try { |
|
52283
ef0fed0a3953
8212926: HttpClient does not retrieve files with large sizes over HTTP/1.1
michaelm
parents:
51462
diff
changeset
|
530 |
long unfulfilled = remaining; |
49765 | 531 |
if (debug.on()) |
532 |
debug.log("Parser got %d bytes (%d remaining / %d)", |
|
533 |
b.remaining(), unfulfilled, contentLength); |
|
48083 | 534 |
assert unfulfilled != 0 || contentLength == 0 || b.remaining() == 0; |
535 |
||
536 |
if (unfulfilled == 0 && contentLength > 0) return; |
|
42460
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
537 |
|
48083 | 538 |
if (b.hasRemaining() && unfulfilled > 0) { |
539 |
// only reduce demand if we actually push something. |
|
540 |
// we would not have come here if there was no |
|
541 |
// demand. |
|
542 |
boolean hasDemand = sub.demand().tryDecrement(); |
|
543 |
assert hasDemand; |
|
52283
ef0fed0a3953
8212926: HttpClient does not retrieve files with large sizes over HTTP/1.1
michaelm
parents:
51462
diff
changeset
|
544 |
int amount = (int)Math.min(b.remaining(), unfulfilled); // safe cast |
48083 | 545 |
unfulfilled = remaining -= amount; |
48703
3eae36c6baa5
8195823: Buffers given to response body subscribers should not contain unprocessed HTTP data
chegar
parents:
48376
diff
changeset
|
546 |
ByteBuffer buffer = Utils.sliceWithLimitedCapacity(b, amount); |
49765 | 547 |
pusher.onNext(List.of(buffer.asReadOnlyBuffer())); |
42460
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
548 |
} |
48083 | 549 |
if (unfulfilled == 0) { |
550 |
// We're done! All data has been received. |
|
49765 | 551 |
if (debug.on()) |
552 |
debug.log("Parser got all expected bytes: completing"); |
|
48083 | 553 |
assert closedExceptionally == null; |
554 |
onFinished.run(); |
|
555 |
pusher.onComplete(); |
|
556 |
completed = true; |
|
557 |
onComplete.accept(closedExceptionally); // should be null |
|
558 |
} else { |
|
559 |
assert b.remaining() == 0; |
|
560 |
} |
|
561 |
} catch (Throwable t) { |
|
49765 | 562 |
if (debug.on()) debug.log("Unexpected exception", t); |
48083 | 563 |
closedExceptionally = t; |
564 |
if (!completed) { |
|
565 |
onComplete.accept(t); |
|
566 |
} |
|
42460
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
567 |
} |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
568 |
} |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
569 |
} |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
570 |
} |