author | jdv |
Tue, 15 May 2018 11:34:25 +0530 | |
changeset 50147 | 23a8ccafa7ba |
parent 49765 | ee6f7a61f3a5 |
child 50681 | 4254bed3c09d |
child 56451 | 9585061fdb04 |
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; |
48083 | 29 |
import java.lang.System.Logger.Level; |
42460
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
30 |
import java.nio.ByteBuffer; |
48083 | 31 |
import java.util.ArrayList; |
49765 | 32 |
import java.util.Collections; |
48083 | 33 |
import java.util.List; |
42460
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
34 |
import java.util.function.Consumer; |
49765 | 35 |
import java.net.http.HttpHeaders; |
36 |
import java.net.http.HttpResponse; |
|
37 |
||
38 |
import jdk.internal.net.http.common.Logger; |
|
39 |
import jdk.internal.net.http.common.Utils; |
|
42460
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 |
/** |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
42 |
* 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
|
43 |
* |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
44 |
* 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
|
45 |
* 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
|
46 |
*/ |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
47 |
class ResponseContent { |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
48 |
|
48083 | 49 |
final HttpResponse.BodySubscriber<?> pusher; |
42460
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
50 |
final int contentLength; |
48083 | 51 |
final HttpHeaders headers; |
42460
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
52 |
// 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
|
53 |
// 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
|
54 |
private final Runnable onFinished; |
48083 | 55 |
private final String dbgTag; |
42460
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
56 |
|
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
57 |
ResponseContent(HttpConnection connection, |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
58 |
int contentLength, |
48083 | 59 |
HttpHeaders h, |
60 |
HttpResponse.BodySubscriber<?> userSubscriber, |
|
42460
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
61 |
Runnable onFinished) |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
62 |
{ |
48083 | 63 |
this.pusher = userSubscriber; |
42460
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
64 |
this.contentLength = contentLength; |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
65 |
this.headers = h; |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
66 |
this.onFinished = onFinished; |
48083 | 67 |
this.dbgTag = connection.dbgString() + "/ResponseContent"; |
42460
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 |
|
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
70 |
static final int LF = 10; |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
71 |
static final int CR = 13; |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
72 |
|
48083 | 73 |
private boolean chunkedContent, chunkedContentInitialized; |
42460
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
74 |
|
48083 | 75 |
boolean contentChunked() throws IOException { |
42460
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
76 |
if (chunkedContentInitialized) { |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
77 |
return chunkedContent; |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
78 |
} |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
79 |
if (contentLength == -1) { |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
80 |
String tc = headers.firstValue("Transfer-Encoding") |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
81 |
.orElse(""); |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
82 |
if (!tc.equals("")) { |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
83 |
if (tc.equalsIgnoreCase("chunked")) { |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
84 |
chunkedContent = true; |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
85 |
} else { |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
86 |
throw new IOException("invalid content"); |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
87 |
} |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
88 |
} else { |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
89 |
chunkedContent = false; |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
90 |
} |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
91 |
} |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
92 |
chunkedContentInitialized = true; |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
93 |
return chunkedContent; |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
94 |
} |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
95 |
|
48083 | 96 |
interface BodyParser extends Consumer<ByteBuffer> { |
97 |
void onSubscribe(AbstractSubscription sub); |
|
98 |
} |
|
99 |
||
100 |
// Returns a parser that will take care of parsing the received byte |
|
101 |
// buffers and forward them to the BodySubscriber. |
|
102 |
// When the parser is done, it will call onComplete. |
|
103 |
// If parsing was successful, the throwable parameter will be null. |
|
104 |
// Otherwise it will be the exception that occurred |
|
105 |
// Note: revisit: it might be better to use a CompletableFuture than |
|
106 |
// a completion handler. |
|
107 |
BodyParser getBodyParser(Consumer<Throwable> onComplete) |
|
108 |
throws IOException { |
|
109 |
if (contentChunked()) { |
|
110 |
return new ChunkedBodyParser(onComplete); |
|
111 |
} else { |
|
112 |
return new FixedLengthBodyParser(contentLength, onComplete); |
|
42460
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
113 |
} |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
114 |
} |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
115 |
|
48083 | 116 |
|
117 |
static enum ChunkState {READING_LENGTH, READING_DATA, DONE} |
|
118 |
class ChunkedBodyParser implements BodyParser { |
|
119 |
final ByteBuffer READMORE = Utils.EMPTY_BYTEBUFFER; |
|
120 |
final Consumer<Throwable> onComplete; |
|
49765 | 121 |
final Logger debug = Utils.getDebugLogger(this::dbgString, Utils.DEBUG); |
48083 | 122 |
final String dbgTag = ResponseContent.this.dbgTag + "/ChunkedBodyParser"; |
123 |
||
124 |
volatile Throwable closedExceptionally; |
|
125 |
volatile int partialChunklen = 0; // partially read chunk len |
|
126 |
volatile int chunklen = -1; // number of bytes in chunk |
|
127 |
volatile int bytesremaining; // number of bytes in chunk left to be read incl CRLF |
|
128 |
volatile boolean cr = false; // tryReadChunkLength has found CR |
|
129 |
volatile int bytesToConsume; // number of bytes that still need to be consumed before proceeding |
|
130 |
volatile ChunkState state = ChunkState.READING_LENGTH; // current state |
|
131 |
volatile AbstractSubscription sub; |
|
132 |
ChunkedBodyParser(Consumer<Throwable> onComplete) { |
|
133 |
this.onComplete = onComplete; |
|
42460
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
134 |
} |
48083 | 135 |
|
136 |
String dbgString() { |
|
137 |
return dbgTag; |
|
138 |
} |
|
42460
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
139 |
|
48083 | 140 |
@Override |
141 |
public void onSubscribe(AbstractSubscription sub) { |
|
49765 | 142 |
if (debug.on()) |
143 |
debug.log("onSubscribe: " + pusher.getClass().getName()); |
|
48083 | 144 |
pusher.onSubscribe(this.sub = sub); |
145 |
} |
|
42460
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
146 |
|
48083 | 147 |
@Override |
148 |
public void accept(ByteBuffer b) { |
|
149 |
if (closedExceptionally != null) { |
|
49765 | 150 |
if (debug.on()) |
151 |
debug.log("already closed: " + closedExceptionally); |
|
48083 | 152 |
return; |
42460
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
153 |
} |
48083 | 154 |
boolean completed = false; |
155 |
try { |
|
156 |
List<ByteBuffer> out = new ArrayList<>(); |
|
157 |
do { |
|
158 |
if (tryPushOneHunk(b, out)) { |
|
159 |
// We're done! (true if the final chunk was parsed). |
|
160 |
if (!out.isEmpty()) { |
|
161 |
// push what we have and complete |
|
162 |
// only reduce demand if we actually push something. |
|
163 |
// we would not have come here if there was no |
|
164 |
// demand. |
|
165 |
boolean hasDemand = sub.demand().tryDecrement(); |
|
166 |
assert hasDemand; |
|
49765 | 167 |
pusher.onNext(Collections.unmodifiableList(out)); |
168 |
if (debug.on()) debug.log("Chunks sent"); |
|
48083 | 169 |
} |
49765 | 170 |
if (debug.on()) debug.log("done!"); |
48083 | 171 |
assert closedExceptionally == null; |
172 |
assert state == ChunkState.DONE; |
|
173 |
onFinished.run(); |
|
174 |
pusher.onComplete(); |
|
49765 | 175 |
if (debug.on()) debug.log("subscriber completed"); |
48083 | 176 |
completed = true; |
177 |
onComplete.accept(closedExceptionally); // should be null |
|
178 |
break; |
|
179 |
} |
|
180 |
// the buffer may contain several hunks, and therefore |
|
181 |
// we must loop while it's not exhausted. |
|
182 |
} while (b.hasRemaining()); |
|
42460
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
183 |
|
48083 | 184 |
if (!completed && !out.isEmpty()) { |
185 |
// push what we have. |
|
186 |
// only reduce demand if we actually push something. |
|
187 |
// we would not have come here if there was no |
|
188 |
// demand. |
|
189 |
boolean hasDemand = sub.demand().tryDecrement(); |
|
190 |
assert hasDemand; |
|
49765 | 191 |
pusher.onNext(Collections.unmodifiableList(out)); |
192 |
if (debug.on()) debug.log("Chunk sent"); |
|
48083 | 193 |
} |
194 |
assert state == ChunkState.DONE || !b.hasRemaining(); |
|
195 |
} catch(Throwable t) { |
|
49765 | 196 |
if (debug.on()) |
197 |
debug.log("Error while processing buffer: %s", (Object)t ); |
|
48083 | 198 |
closedExceptionally = t; |
199 |
if (!completed) onComplete.accept(t); |
|
42460
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
200 |
} |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
201 |
} |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
202 |
|
48083 | 203 |
// reads and returns chunklen. Position of chunkbuf is first byte |
204 |
// of chunk on return. chunklen includes the CR LF at end of chunk |
|
205 |
// returns -1 if needs more bytes |
|
206 |
private int tryReadChunkLen(ByteBuffer chunkbuf) throws IOException { |
|
207 |
assert state == ChunkState.READING_LENGTH; |
|
208 |
while (chunkbuf.hasRemaining()) { |
|
209 |
int c = chunkbuf.get(); |
|
210 |
if (cr) { |
|
211 |
if (c == LF) { |
|
212 |
return partialChunklen; |
|
213 |
} else { |
|
214 |
throw new IOException("invalid chunk header"); |
|
215 |
} |
|
216 |
} |
|
217 |
if (c == CR) { |
|
218 |
cr = true; |
|
219 |
} else { |
|
220 |
int digit = toDigit(c); |
|
221 |
partialChunklen = partialChunklen * 16 + digit; |
|
222 |
} |
|
223 |
} |
|
224 |
return -1; |
|
225 |
} |
|
226 |
||
227 |
||
228 |
// try to consume as many bytes as specified by bytesToConsume. |
|
229 |
// returns the number of bytes that still need to be consumed. |
|
230 |
// In practice this method is only called to consume one CRLF pair |
|
231 |
// with bytesToConsume set to 2, so it will only return 0 (if completed), |
|
232 |
// 1, or 2 (if chunkbuf doesn't have the 2 chars). |
|
233 |
private int tryConsumeBytes(ByteBuffer chunkbuf) throws IOException { |
|
234 |
int n = bytesToConsume; |
|
235 |
if (n > 0) { |
|
236 |
int e = Math.min(chunkbuf.remaining(), n); |
|
237 |
||
238 |
// verifies some assertions |
|
239 |
// this methods is called only to consume CRLF |
|
240 |
if (Utils.ASSERTIONSENABLED) { |
|
241 |
assert n <= 2 && e <= 2; |
|
242 |
ByteBuffer tmp = chunkbuf.slice(); |
|
243 |
// if n == 2 assert that we will first consume CR |
|
244 |
assert (n == 2 && e > 0) ? tmp.get() == CR : true; |
|
245 |
// if n == 1 || n == 2 && e == 2 assert that we then consume LF |
|
246 |
assert (n == 1 || e == 2) ? tmp.get() == LF : true; |
|
247 |
} |
|
248 |
||
249 |
chunkbuf.position(chunkbuf.position() + e); |
|
250 |
n -= e; |
|
251 |
bytesToConsume = n; |
|
252 |
} |
|
253 |
assert n >= 0; |
|
254 |
return n; |
|
255 |
} |
|
256 |
||
257 |
/** |
|
258 |
* Returns a ByteBuffer containing chunk of data or a "hunk" of data |
|
259 |
* (a chunk of a chunk if the chunk size is larger than our ByteBuffers). |
|
260 |
* If the given chunk does not have enough data this method return |
|
261 |
* an empty ByteBuffer (READMORE). |
|
262 |
* If we encounter the final chunk (an empty chunk) this method |
|
263 |
* returns null. |
|
264 |
*/ |
|
265 |
ByteBuffer tryReadOneHunk(ByteBuffer chunk) throws IOException { |
|
266 |
int unfulfilled = bytesremaining; |
|
267 |
int toconsume = bytesToConsume; |
|
268 |
ChunkState st = state; |
|
269 |
if (st == ChunkState.READING_LENGTH && chunklen == -1) { |
|
49765 | 270 |
if (debug.on()) debug.log(() -> "Trying to read chunk len" |
48083 | 271 |
+ " (remaining in buffer:"+chunk.remaining()+")"); |
272 |
int clen = chunklen = tryReadChunkLen(chunk); |
|
273 |
if (clen == -1) return READMORE; |
|
49765 | 274 |
if (debug.on()) debug.log("Got chunk len %d", clen); |
48083 | 275 |
cr = false; partialChunklen = 0; |
276 |
unfulfilled = bytesremaining = clen; |
|
277 |
if (clen == 0) toconsume = bytesToConsume = 2; // that was the last chunk |
|
278 |
else st = state = ChunkState.READING_DATA; // read the data |
|
279 |
} |
|
280 |
||
281 |
if (toconsume > 0) { |
|
49765 | 282 |
if (debug.on()) |
283 |
debug.log("Trying to consume bytes: %d (remaining in buffer: %s)", |
|
284 |
toconsume, chunk.remaining()); |
|
48083 | 285 |
if (tryConsumeBytes(chunk) > 0) { |
286 |
return READMORE; |
|
287 |
} |
|
288 |
} |
|
289 |
||
290 |
toconsume = bytesToConsume; |
|
291 |
assert toconsume == 0; |
|
292 |
||
42460
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
293 |
|
48083 | 294 |
if (st == ChunkState.READING_LENGTH) { |
295 |
// we will come here only if chunklen was 0, after having |
|
296 |
// consumed the trailing CRLF |
|
297 |
int clen = chunklen; |
|
298 |
assert clen == 0; |
|
49765 | 299 |
if (debug.on()) debug.log("No more chunks: %d", clen); |
48083 | 300 |
// the DONE state is not really needed but it helps with |
301 |
// assertions... |
|
302 |
state = ChunkState.DONE; |
|
303 |
return null; |
|
304 |
} |
|
305 |
||
306 |
int clen = chunklen; |
|
307 |
assert clen > 0; |
|
308 |
assert st == ChunkState.READING_DATA; |
|
309 |
||
310 |
ByteBuffer returnBuffer = READMORE; // May be a hunk or a chunk |
|
311 |
if (unfulfilled > 0) { |
|
312 |
int bytesread = chunk.remaining(); |
|
49765 | 313 |
if (debug.on()) |
314 |
debug.log("Reading chunk: available %d, needed %d", |
|
315 |
bytesread, unfulfilled); |
|
48083 | 316 |
|
317 |
int bytes2return = Math.min(bytesread, unfulfilled); |
|
49765 | 318 |
if (debug.on()) |
319 |
debug.log( "Returning chunk bytes: %d", bytes2return); |
|
320 |
returnBuffer = Utils.sliceWithLimitedCapacity(chunk, bytes2return).asReadOnlyBuffer(); |
|
48083 | 321 |
unfulfilled = bytesremaining -= bytes2return; |
322 |
if (unfulfilled == 0) bytesToConsume = 2; |
|
323 |
} |
|
324 |
||
325 |
assert unfulfilled >= 0; |
|
326 |
||
327 |
if (unfulfilled == 0) { |
|
49765 | 328 |
if (debug.on()) |
329 |
debug.log("No more bytes to read - %d yet to consume.", |
|
330 |
unfulfilled); |
|
48083 | 331 |
// check whether the trailing CRLF is consumed, try to |
332 |
// consume it if not. If tryConsumeBytes needs more bytes |
|
333 |
// then we will come back here later - skipping the block |
|
334 |
// that reads data because remaining==0, and finding |
|
335 |
// that the two bytes are now consumed. |
|
336 |
if (tryConsumeBytes(chunk) == 0) { |
|
337 |
// we're done for this chunk! reset all states and |
|
338 |
// prepare to read the next chunk. |
|
339 |
chunklen = -1; |
|
340 |
partialChunklen = 0; |
|
341 |
cr = false; |
|
342 |
state = ChunkState.READING_LENGTH; |
|
49765 | 343 |
if (debug.on()) debug.log("Ready to read next chunk"); |
48083 | 344 |
} |
345 |
} |
|
346 |
if (returnBuffer == READMORE) { |
|
49765 | 347 |
if (debug.on()) debug.log("Need more data"); |
48083 | 348 |
} |
349 |
return returnBuffer; |
|
42460
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
350 |
} |
48083 | 351 |
|
352 |
||
353 |
// Attempt to parse and push one hunk from the buffer. |
|
354 |
// Returns true if the final chunk was parsed. |
|
355 |
// Returns false if we need to push more chunks. |
|
356 |
private boolean tryPushOneHunk(ByteBuffer b, List<ByteBuffer> out) |
|
357 |
throws IOException { |
|
358 |
assert state != ChunkState.DONE; |
|
359 |
ByteBuffer b1 = tryReadOneHunk(b); |
|
360 |
if (b1 != null) { |
|
361 |
//assert b1.hasRemaining() || b1 == READMORE; |
|
362 |
if (b1.hasRemaining()) { |
|
49765 | 363 |
if (debug.on()) |
364 |
debug.log("Sending chunk to consumer (%d)", b1.remaining()); |
|
48083 | 365 |
out.add(b1); |
366 |
} |
|
367 |
return false; // we haven't parsed the final chunk yet. |
|
368 |
} else { |
|
369 |
return true; // we're done! the final chunk was parsed. |
|
370 |
} |
|
371 |
} |
|
372 |
||
373 |
private int toDigit(int b) throws IOException { |
|
374 |
if (b >= 0x30 && b <= 0x39) { |
|
375 |
return b - 0x30; |
|
376 |
} |
|
377 |
if (b >= 0x41 && b <= 0x46) { |
|
378 |
return b - 0x41 + 10; |
|
379 |
} |
|
380 |
if (b >= 0x61 && b <= 0x66) { |
|
381 |
return b - 0x61 + 10; |
|
382 |
} |
|
383 |
throw new IOException("Invalid chunk header byte " + b); |
|
384 |
} |
|
385 |
||
42460
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
386 |
} |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
387 |
|
48083 | 388 |
class FixedLengthBodyParser implements BodyParser { |
389 |
final int contentLength; |
|
390 |
final Consumer<Throwable> onComplete; |
|
49765 | 391 |
final Logger debug = Utils.getDebugLogger(this::dbgString, Utils.DEBUG); |
48083 | 392 |
final String dbgTag = ResponseContent.this.dbgTag + "/FixedLengthBodyParser"; |
393 |
volatile int remaining; |
|
394 |
volatile Throwable closedExceptionally; |
|
395 |
volatile AbstractSubscription sub; |
|
396 |
FixedLengthBodyParser(int contentLength, Consumer<Throwable> onComplete) { |
|
397 |
this.contentLength = this.remaining = contentLength; |
|
398 |
this.onComplete = onComplete; |
|
399 |
} |
|
400 |
||
401 |
String dbgString() { |
|
402 |
return dbgTag; |
|
403 |
} |
|
42460
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
404 |
|
48083 | 405 |
@Override |
406 |
public void onSubscribe(AbstractSubscription sub) { |
|
49765 | 407 |
if (debug.on()) |
408 |
debug.log("length=" + contentLength +", onSubscribe: " |
|
409 |
+ pusher.getClass().getName()); |
|
48083 | 410 |
pusher.onSubscribe(this.sub = sub); |
411 |
try { |
|
412 |
if (contentLength == 0) { |
|
48376
41ae5c69b09c
8192966: HttpClient should reuse TCP connection for h2c connections
michaelm
parents:
48083
diff
changeset
|
413 |
onFinished.run(); |
48083 | 414 |
pusher.onComplete(); |
415 |
onComplete.accept(null); |
|
416 |
} |
|
417 |
} catch (Throwable t) { |
|
418 |
closedExceptionally = t; |
|
419 |
try { |
|
420 |
pusher.onError(t); |
|
421 |
} finally { |
|
422 |
onComplete.accept(t); |
|
423 |
} |
|
424 |
} |
|
425 |
} |
|
42460
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
426 |
|
48083 | 427 |
@Override |
428 |
public void accept(ByteBuffer b) { |
|
429 |
if (closedExceptionally != null) { |
|
49765 | 430 |
if (debug.on()) |
431 |
debug.log("already closed: " + closedExceptionally); |
|
48083 | 432 |
return; |
433 |
} |
|
434 |
boolean completed = false; |
|
435 |
try { |
|
436 |
int unfulfilled = remaining; |
|
49765 | 437 |
if (debug.on()) |
438 |
debug.log("Parser got %d bytes (%d remaining / %d)", |
|
439 |
b.remaining(), unfulfilled, contentLength); |
|
48083 | 440 |
assert unfulfilled != 0 || contentLength == 0 || b.remaining() == 0; |
441 |
||
442 |
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
|
443 |
|
48083 | 444 |
if (b.hasRemaining() && unfulfilled > 0) { |
445 |
// only reduce demand if we actually push something. |
|
446 |
// we would not have come here if there was no |
|
447 |
// demand. |
|
448 |
boolean hasDemand = sub.demand().tryDecrement(); |
|
449 |
assert hasDemand; |
|
450 |
int amount = Math.min(b.remaining(), unfulfilled); |
|
451 |
unfulfilled = remaining -= amount; |
|
48703
3eae36c6baa5
8195823: Buffers given to response body subscribers should not contain unprocessed HTTP data
chegar
parents:
48376
diff
changeset
|
452 |
ByteBuffer buffer = Utils.sliceWithLimitedCapacity(b, amount); |
49765 | 453 |
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
|
454 |
} |
48083 | 455 |
if (unfulfilled == 0) { |
456 |
// We're done! All data has been received. |
|
49765 | 457 |
if (debug.on()) |
458 |
debug.log("Parser got all expected bytes: completing"); |
|
48083 | 459 |
assert closedExceptionally == null; |
460 |
onFinished.run(); |
|
461 |
pusher.onComplete(); |
|
462 |
completed = true; |
|
463 |
onComplete.accept(closedExceptionally); // should be null |
|
464 |
} else { |
|
465 |
assert b.remaining() == 0; |
|
466 |
} |
|
467 |
} catch (Throwable t) { |
|
49765 | 468 |
if (debug.on()) debug.log("Unexpected exception", t); |
48083 | 469 |
closedExceptionally = t; |
470 |
if (!completed) { |
|
471 |
onComplete.accept(t); |
|
472 |
} |
|
42460
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
473 |
} |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
474 |
} |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
475 |
} |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
476 |
} |