author | dfuchs |
Wed, 20 Jun 2018 14:22:24 +0100 | |
branch | http-client-branch |
changeset 56788 | 7d40b235de18 |
parent 56763 | 25821dd1d917 |
child 56795 | 03ece2518428 |
permissions | -rw-r--r-- |
42460
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
1 |
/* |
49765 | 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 |
|
48083 | 28 |
import java.io.EOFException; |
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; |
49765 | 31 |
import java.util.List; |
42460
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
32 |
import java.util.concurrent.CompletableFuture; |
48083 | 33 |
import java.util.concurrent.CompletionStage; |
42460
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
34 |
import java.util.concurrent.Executor; |
49765 | 35 |
import java.util.concurrent.Flow; |
36 |
import java.util.concurrent.atomic.AtomicBoolean; |
|
37 |
import java.util.concurrent.atomic.AtomicLong; |
|
48083 | 38 |
import java.util.function.Consumer; |
39 |
import java.util.function.Function; |
|
49765 | 40 |
import java.net.http.HttpHeaders; |
41 |
import java.net.http.HttpResponse; |
|
42 |
import jdk.internal.net.http.ResponseContent.BodyParser; |
|
43 |
import jdk.internal.net.http.common.Log; |
|
44 |
import jdk.internal.net.http.common.Logger; |
|
45 |
import jdk.internal.net.http.common.MinimalFuture; |
|
46 |
import jdk.internal.net.http.common.Utils; |
|
47 |
import static java.net.http.HttpClient.Version.HTTP_1_1; |
|
48 |
import static java.net.http.HttpResponse.BodySubscribers.discarding; |
|
56682
9822bbe48b9b
http-client-branch: more descriptive HTTP/1.1 exception detail messages
chegar
parents:
56631
diff
changeset
|
49 |
import static jdk.internal.net.http.common.Utils.wrapWithExtraDetail; |
56788
7d40b235de18
http-client-branch: review comment - fix some static imports
dfuchs
parents:
56763
diff
changeset
|
50 |
import static jdk.internal.net.http.RedirectFilter.HTTP_NOT_MODIFIED; |
42460
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
51 |
|
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
52 |
/** |
48083 | 53 |
* Handles a HTTP/1.1 response (headers + body). |
54 |
* There can be more than one of these per Http exchange. |
|
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 |
class Http1Response<T> { |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
57 |
|
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
58 |
private volatile ResponseContent content; |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
59 |
private final HttpRequestImpl request; |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
60 |
private Response response; |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
61 |
private final HttpConnection connection; |
48083 | 62 |
private HttpHeaders headers; |
42460
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
63 |
private int responseCode; |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
64 |
private final Http1Exchange<T> exchange; |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
65 |
private boolean return2Cache; // return connection to cache when finished |
48083 | 66 |
private final HeadersReader headersReader; // used to read the headers |
67 |
private final BodyReader bodyReader; // used to read the body |
|
68 |
private final Http1AsyncReceiver asyncReceiver; |
|
69 |
private volatile EOFException eof; |
|
70 |
// max number of bytes of (fixed length) body to ignore on redirect |
|
71 |
private final static int MAX_IGNORE = 1024; |
|
42460
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
72 |
|
48083 | 73 |
// Revisit: can we get rid of this? |
74 |
static enum State {INITIAL, READING_HEADERS, READING_BODY, DONE} |
|
75 |
private volatile State readProgress = State.INITIAL; |
|
76 |
||
49765 | 77 |
final Logger debug = Utils.getDebugLogger(this::dbgString, Utils.DEBUG); |
78 |
final static AtomicLong responseCount = new AtomicLong(); |
|
79 |
final long id = responseCount.incrementAndGet(); |
|
56730
b08918259eed
http-client-branch: review comment: better handling for error conditions and tests to verify
chegar
parents:
56682
diff
changeset
|
80 |
private Http1HeaderParser hd; |
48083 | 81 |
|
82 |
Http1Response(HttpConnection conn, |
|
83 |
Http1Exchange<T> exchange, |
|
84 |
Http1AsyncReceiver asyncReceiver) { |
|
85 |
this.readProgress = State.INITIAL; |
|
42460
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
86 |
this.request = exchange.request(); |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
87 |
this.exchange = exchange; |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
88 |
this.connection = conn; |
48083 | 89 |
this.asyncReceiver = asyncReceiver; |
90 |
headersReader = new HeadersReader(this::advance); |
|
91 |
bodyReader = new BodyReader(this::advance); |
|
56730
b08918259eed
http-client-branch: review comment: better handling for error conditions and tests to verify
chegar
parents:
56682
diff
changeset
|
92 |
|
b08918259eed
http-client-branch: review comment: better handling for error conditions and tests to verify
chegar
parents:
56682
diff
changeset
|
93 |
hd = new Http1HeaderParser(); |
b08918259eed
http-client-branch: review comment: better handling for error conditions and tests to verify
chegar
parents:
56682
diff
changeset
|
94 |
readProgress = State.READING_HEADERS; |
b08918259eed
http-client-branch: review comment: better handling for error conditions and tests to verify
chegar
parents:
56682
diff
changeset
|
95 |
headersReader.start(hd); |
b08918259eed
http-client-branch: review comment: better handling for error conditions and tests to verify
chegar
parents:
56682
diff
changeset
|
96 |
asyncReceiver.subscribe(headersReader); |
42460
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
97 |
} |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
98 |
|
49765 | 99 |
String dbgTag; |
100 |
private String dbgString() { |
|
101 |
String dbg = dbgTag; |
|
102 |
if (dbg == null) { |
|
103 |
String cdbg = connection.dbgTag; |
|
104 |
if (cdbg != null) { |
|
105 |
dbgTag = dbg = "Http1Response(id=" + id + ", " + cdbg + ")"; |
|
106 |
} else { |
|
107 |
dbg = "Http1Response(id=" + id + ")"; |
|
108 |
} |
|
109 |
} |
|
110 |
return dbg; |
|
111 |
} |
|
112 |
||
113 |
// The ClientRefCountTracker is used to track the state |
|
114 |
// of a pending operation. Altough there usually is a single |
|
115 |
// point where the operation starts, it may terminate at |
|
116 |
// different places. |
|
117 |
private final class ClientRefCountTracker { |
|
118 |
final HttpClientImpl client = connection.client(); |
|
119 |
// state & 0x01 != 0 => acquire called |
|
120 |
// state & 0x02 != 0 => tryRelease called |
|
121 |
byte state; |
|
122 |
||
123 |
public synchronized void acquire() { |
|
124 |
if (state == 0) { |
|
125 |
// increment the reference count on the HttpClientImpl |
|
126 |
// to prevent the SelectorManager thread from exiting |
|
127 |
// until our operation is complete. |
|
128 |
if (debug.on()) |
|
129 |
debug.log("Operation started: incrementing ref count for %s", client); |
|
130 |
client.reference(); |
|
131 |
state = 0x01; |
|
132 |
} else { |
|
133 |
if (debug.on()) |
|
134 |
debug.log("Operation ref count for %s is already %s", |
|
135 |
client, ((state & 0x2) == 0x2) ? "released." : "incremented!" ); |
|
136 |
assert (state & 0x01) == 0 : "reference count already incremented"; |
|
137 |
} |
|
138 |
} |
|
139 |
||
140 |
public synchronized void tryRelease() { |
|
141 |
if (state == 0x01) { |
|
142 |
// decrement the reference count on the HttpClientImpl |
|
143 |
// to allow the SelectorManager thread to exit if no |
|
144 |
// other operation is pending and the facade is no |
|
145 |
// longer referenced. |
|
146 |
if (debug.on()) |
|
147 |
debug.log("Operation finished: decrementing ref count for %s", client); |
|
148 |
client.unreference(); |
|
149 |
} else if (state == 0) { |
|
150 |
if (debug.on()) |
|
151 |
debug.log("Operation finished: releasing ref count for %s", client); |
|
152 |
} else if ((state & 0x02) == 0x02) { |
|
153 |
if (debug.on()) |
|
154 |
debug.log("ref count for %s already released", client); |
|
155 |
} |
|
156 |
state |= 0x02; |
|
157 |
} |
|
158 |
} |
|
159 |
||
56730
b08918259eed
http-client-branch: review comment: better handling for error conditions and tests to verify
chegar
parents:
56682
diff
changeset
|
160 |
private volatile boolean firstTimeAround = true; |
b08918259eed
http-client-branch: review comment: better handling for error conditions and tests to verify
chegar
parents:
56682
diff
changeset
|
161 |
|
49765 | 162 |
public CompletableFuture<Response> readHeadersAsync(Executor executor) { |
163 |
if (debug.on()) |
|
164 |
debug.log("Reading Headers: (remaining: " |
|
165 |
+ asyncReceiver.remaining() +") " + readProgress); |
|
56730
b08918259eed
http-client-branch: review comment: better handling for error conditions and tests to verify
chegar
parents:
56682
diff
changeset
|
166 |
|
b08918259eed
http-client-branch: review comment: better handling for error conditions and tests to verify
chegar
parents:
56682
diff
changeset
|
167 |
if (firstTimeAround) { |
56756
ba60eaef37d7
http-client-branch: fixed ShortBodyResponse issues
dfuchs
parents:
56730
diff
changeset
|
168 |
if (debug.on()) debug.log("First time around"); |
56730
b08918259eed
http-client-branch: review comment: better handling for error conditions and tests to verify
chegar
parents:
56682
diff
changeset
|
169 |
firstTimeAround = false; |
b08918259eed
http-client-branch: review comment: better handling for error conditions and tests to verify
chegar
parents:
56682
diff
changeset
|
170 |
} else { |
b08918259eed
http-client-branch: review comment: better handling for error conditions and tests to verify
chegar
parents:
56682
diff
changeset
|
171 |
// with expect continue we will resume reading headers + body. |
b08918259eed
http-client-branch: review comment: better handling for error conditions and tests to verify
chegar
parents:
56682
diff
changeset
|
172 |
asyncReceiver.unsubscribe(bodyReader); |
b08918259eed
http-client-branch: review comment: better handling for error conditions and tests to verify
chegar
parents:
56682
diff
changeset
|
173 |
bodyReader.reset(); |
b08918259eed
http-client-branch: review comment: better handling for error conditions and tests to verify
chegar
parents:
56682
diff
changeset
|
174 |
|
b08918259eed
http-client-branch: review comment: better handling for error conditions and tests to verify
chegar
parents:
56682
diff
changeset
|
175 |
hd = new Http1HeaderParser(); |
b08918259eed
http-client-branch: review comment: better handling for error conditions and tests to verify
chegar
parents:
56682
diff
changeset
|
176 |
readProgress = State.READING_HEADERS; |
b08918259eed
http-client-branch: review comment: better handling for error conditions and tests to verify
chegar
parents:
56682
diff
changeset
|
177 |
headersReader.reset(); |
b08918259eed
http-client-branch: review comment: better handling for error conditions and tests to verify
chegar
parents:
56682
diff
changeset
|
178 |
headersReader.start(hd); |
b08918259eed
http-client-branch: review comment: better handling for error conditions and tests to verify
chegar
parents:
56682
diff
changeset
|
179 |
asyncReceiver.subscribe(headersReader); |
b08918259eed
http-client-branch: review comment: better handling for error conditions and tests to verify
chegar
parents:
56682
diff
changeset
|
180 |
} |
b08918259eed
http-client-branch: review comment: better handling for error conditions and tests to verify
chegar
parents:
56682
diff
changeset
|
181 |
|
48083 | 182 |
CompletableFuture<State> cf = headersReader.completion(); |
183 |
assert cf != null : "parsing not started"; |
|
56756
ba60eaef37d7
http-client-branch: fixed ShortBodyResponse issues
dfuchs
parents:
56730
diff
changeset
|
184 |
if (debug.on()) { |
ba60eaef37d7
http-client-branch: fixed ShortBodyResponse issues
dfuchs
parents:
56730
diff
changeset
|
185 |
debug.log("headersReader is %s", |
ba60eaef37d7
http-client-branch: fixed ShortBodyResponse issues
dfuchs
parents:
56730
diff
changeset
|
186 |
cf == null ? "not yet started" |
ba60eaef37d7
http-client-branch: fixed ShortBodyResponse issues
dfuchs
parents:
56730
diff
changeset
|
187 |
: cf.isDone() ? "already completed" |
ba60eaef37d7
http-client-branch: fixed ShortBodyResponse issues
dfuchs
parents:
56730
diff
changeset
|
188 |
: "not yet completed"); |
ba60eaef37d7
http-client-branch: fixed ShortBodyResponse issues
dfuchs
parents:
56730
diff
changeset
|
189 |
} |
48083 | 190 |
|
191 |
Function<State, Response> lambda = (State completed) -> { |
|
192 |
assert completed == State.READING_HEADERS; |
|
49765 | 193 |
if (debug.on()) |
194 |
debug.log("Reading Headers: creating Response object;" |
|
195 |
+ " state is now " + readProgress); |
|
48083 | 196 |
asyncReceiver.unsubscribe(headersReader); |
197 |
responseCode = hd.responseCode(); |
|
198 |
headers = hd.headers(); |
|
199 |
||
200 |
response = new Response(request, |
|
201 |
exchange.getExchange(), |
|
202 |
headers, |
|
49765 | 203 |
connection, |
48083 | 204 |
responseCode, |
205 |
HTTP_1_1); |
|
49765 | 206 |
|
207 |
if (Log.headers()) { |
|
208 |
StringBuilder sb = new StringBuilder("RESPONSE HEADERS:\n"); |
|
209 |
Log.dumpHeaders(sb, " ", headers); |
|
210 |
Log.logHeaders(sb.toString()); |
|
211 |
} |
|
212 |
||
48083 | 213 |
return response; |
214 |
}; |
|
215 |
||
216 |
if (executor != null) { |
|
217 |
return cf.thenApplyAsync(lambda, executor); |
|
218 |
} else { |
|
219 |
return cf.thenApply(lambda); |
|
42460
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
220 |
} |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
221 |
} |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
222 |
|
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
223 |
private boolean finished; |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
224 |
|
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
225 |
synchronized void completed() { |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
226 |
finished = true; |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
227 |
} |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
228 |
|
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
229 |
synchronized boolean finished() { |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
230 |
return finished; |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
231 |
} |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
232 |
|
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
233 |
int fixupContentLen(int clen) { |
56631
30b27fe75b0a
http-client-branch: 8203433 (httpclient) Add tests for HEAD and 304 responses.
michaelm
parents:
56507
diff
changeset
|
234 |
if (request.method().equalsIgnoreCase("HEAD") || responseCode == HTTP_NOT_MODIFIED) { |
42460
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
235 |
return 0; |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
236 |
} |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
237 |
if (clen == -1) { |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
238 |
if (headers.firstValue("Transfer-encoding").orElse("") |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
239 |
.equalsIgnoreCase("chunked")) { |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
240 |
return -1; |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
241 |
} |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
242 |
return 0; |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
243 |
} |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
244 |
return clen; |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
245 |
} |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
246 |
|
48083 | 247 |
/** |
248 |
* Read up to MAX_IGNORE bytes discarding |
|
249 |
*/ |
|
250 |
public CompletableFuture<Void> ignoreBody(Executor executor) { |
|
251 |
int clen = (int)headers.firstValueAsLong("Content-Length").orElse(-1); |
|
252 |
if (clen == -1 || clen > MAX_IGNORE) { |
|
253 |
connection.close(); |
|
254 |
return MinimalFuture.completedFuture(null); // not treating as error |
|
255 |
} else { |
|
49765 | 256 |
return readBody(discarding(), true, executor); |
257 |
} |
|
258 |
} |
|
259 |
||
260 |
static final Flow.Subscription NOP = new Flow.Subscription() { |
|
261 |
@Override |
|
262 |
public void request(long n) { } |
|
263 |
public void cancel() { } |
|
264 |
}; |
|
265 |
||
266 |
/** |
|
267 |
* The Http1AsyncReceiver ensures that all calls to |
|
268 |
* the subscriber, including onSubscribe, occur sequentially. |
|
269 |
* There could however be some race conditions that could happen |
|
270 |
* in case of unexpected errors thrown at unexpected places, which |
|
271 |
* may cause onError to be called multiple times. |
|
272 |
* The Http1BodySubscriber will ensure that the user subscriber |
|
273 |
* is actually completed only once - and only after it is |
|
274 |
* subscribed. |
|
275 |
* @param <U> The type of response. |
|
276 |
*/ |
|
277 |
final static class Http1BodySubscriber<U> implements HttpResponse.BodySubscriber<U> { |
|
278 |
final HttpResponse.BodySubscriber<U> userSubscriber; |
|
279 |
final AtomicBoolean completed = new AtomicBoolean(); |
|
280 |
volatile Throwable withError; |
|
281 |
volatile boolean subscribed; |
|
282 |
Http1BodySubscriber(HttpResponse.BodySubscriber<U> userSubscriber) { |
|
283 |
this.userSubscriber = userSubscriber; |
|
284 |
} |
|
285 |
||
286 |
// propagate the error to the user subscriber, even if not |
|
287 |
// subscribed yet. |
|
288 |
private void propagateError(Throwable t) { |
|
289 |
assert t != null; |
|
290 |
try { |
|
291 |
// if unsubscribed at this point, it will not |
|
292 |
// get subscribed later - so do it now and |
|
293 |
// propagate the error |
|
294 |
if (subscribed == false) { |
|
295 |
subscribed = true; |
|
296 |
userSubscriber.onSubscribe(NOP); |
|
297 |
} |
|
298 |
} finally { |
|
299 |
// if onError throws then there is nothing to do |
|
300 |
// here: let the caller deal with it by logging |
|
301 |
// and closing the connection. |
|
302 |
userSubscriber.onError(t); |
|
303 |
} |
|
304 |
} |
|
305 |
||
306 |
// complete the subscriber, either normally or exceptionally |
|
307 |
// ensure that the subscriber is completed only once. |
|
308 |
private void complete(Throwable t) { |
|
309 |
if (completed.compareAndSet(false, true)) { |
|
310 |
t = withError = Utils.getCompletionCause(t); |
|
311 |
if (t == null) { |
|
312 |
assert subscribed; |
|
313 |
try { |
|
314 |
userSubscriber.onComplete(); |
|
315 |
} catch (Throwable x) { |
|
56763
25821dd1d917
http-client-branch: improved logging and fixed race condition with throwing subscribers in onComplete
dfuchs
parents:
56756
diff
changeset
|
316 |
// Simply propagate the error by calling |
25821dd1d917
http-client-branch: improved logging and fixed race condition with throwing subscribers in onComplete
dfuchs
parents:
56756
diff
changeset
|
317 |
// onError on the user subscriber, and let the |
25821dd1d917
http-client-branch: improved logging and fixed race condition with throwing subscribers in onComplete
dfuchs
parents:
56756
diff
changeset
|
318 |
// connection be reused since we should have received |
25821dd1d917
http-client-branch: improved logging and fixed race condition with throwing subscribers in onComplete
dfuchs
parents:
56756
diff
changeset
|
319 |
// and parsed all the bytes when we reach here. |
25821dd1d917
http-client-branch: improved logging and fixed race condition with throwing subscribers in onComplete
dfuchs
parents:
56756
diff
changeset
|
320 |
// If onError throws in turn, then we will simply |
25821dd1d917
http-client-branch: improved logging and fixed race condition with throwing subscribers in onComplete
dfuchs
parents:
56756
diff
changeset
|
321 |
// let that new exception flow up to the caller |
25821dd1d917
http-client-branch: improved logging and fixed race condition with throwing subscribers in onComplete
dfuchs
parents:
56756
diff
changeset
|
322 |
// and let it deal with it. |
49765 | 323 |
// (i.e: log and close the connection) |
56763
25821dd1d917
http-client-branch: improved logging and fixed race condition with throwing subscribers in onComplete
dfuchs
parents:
56756
diff
changeset
|
324 |
// Note that rethrowing here could introduce a |
25821dd1d917
http-client-branch: improved logging and fixed race condition with throwing subscribers in onComplete
dfuchs
parents:
56756
diff
changeset
|
325 |
// race that might cause the next send() operation to |
25821dd1d917
http-client-branch: improved logging and fixed race condition with throwing subscribers in onComplete
dfuchs
parents:
56756
diff
changeset
|
326 |
// fail as the connection has already been put back |
25821dd1d917
http-client-branch: improved logging and fixed race condition with throwing subscribers in onComplete
dfuchs
parents:
56756
diff
changeset
|
327 |
// into the cache when we reach here. |
25821dd1d917
http-client-branch: improved logging and fixed race condition with throwing subscribers in onComplete
dfuchs
parents:
56756
diff
changeset
|
328 |
propagateError(t = withError = Utils.getCompletionCause(x)); |
49765 | 329 |
} |
330 |
} else { |
|
331 |
propagateError(t); |
|
332 |
} |
|
333 |
} |
|
334 |
} |
|
335 |
||
336 |
@Override |
|
337 |
public CompletionStage<U> getBody() { |
|
338 |
return userSubscriber.getBody(); |
|
339 |
} |
|
340 |
@Override |
|
341 |
public void onSubscribe(Flow.Subscription subscription) { |
|
342 |
if (!subscribed) { |
|
343 |
subscribed = true; |
|
344 |
userSubscriber.onSubscribe(subscription); |
|
345 |
} else { |
|
346 |
// could be already subscribed and completed |
|
347 |
// if an unexpected error occurred before the actual |
|
348 |
// subscription - though that's not supposed |
|
349 |
// happen. |
|
350 |
assert completed.get(); |
|
351 |
} |
|
352 |
} |
|
353 |
@Override |
|
354 |
public void onNext(List<ByteBuffer> item) { |
|
355 |
assert !completed.get(); |
|
356 |
userSubscriber.onNext(item); |
|
357 |
} |
|
358 |
@Override |
|
359 |
public void onError(Throwable throwable) { |
|
360 |
complete(throwable); |
|
361 |
} |
|
362 |
@Override |
|
363 |
public void onComplete() { |
|
364 |
complete(null); |
|
48083 | 365 |
} |
42460
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
366 |
} |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
367 |
|
48083 | 368 |
public <U> CompletableFuture<U> readBody(HttpResponse.BodySubscriber<U> p, |
369 |
boolean return2Cache, |
|
370 |
Executor executor) { |
|
42460
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
371 |
this.return2Cache = return2Cache; |
49765 | 372 |
final Http1BodySubscriber<U> subscriber = new Http1BodySubscriber<>(p); |
373 |
||
374 |
final CompletableFuture<U> cf = new MinimalFuture<>(); |
|
42460
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
375 |
|
48083 | 376 |
int clen0 = (int)headers.firstValueAsLong("Content-Length").orElse(-1); |
377 |
||
42460
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
378 |
final int clen = fixupContentLen(clen0); |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
379 |
|
48083 | 380 |
// expect-continue reads headers and body twice. |
381 |
// if we reach here, we must reset the headersReader state. |
|
382 |
asyncReceiver.unsubscribe(headersReader); |
|
383 |
headersReader.reset(); |
|
49765 | 384 |
ClientRefCountTracker refCountTracker = new ClientRefCountTracker(); |
48083 | 385 |
|
49765 | 386 |
// We need to keep hold on the client facade until the |
387 |
// tracker has been incremented. |
|
388 |
connection.client().reference(); |
|
42460
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
389 |
executor.execute(() -> { |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
390 |
try { |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
391 |
content = new ResponseContent( |
49765 | 392 |
connection, clen, headers, subscriber, |
48083 | 393 |
this::onFinished |
42460
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
394 |
); |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
395 |
if (cf.isCompletedExceptionally()) { |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
396 |
// if an error occurs during subscription |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
397 |
connection.close(); |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
398 |
return; |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
399 |
} |
48083 | 400 |
// increment the reference count on the HttpClientImpl |
401 |
// to prevent the SelectorManager thread from exiting until |
|
402 |
// the body is fully read. |
|
49765 | 403 |
refCountTracker.acquire(); |
48083 | 404 |
bodyReader.start(content.getBodyParser( |
405 |
(t) -> { |
|
406 |
try { |
|
407 |
if (t != null) { |
|
49944 | 408 |
try { |
409 |
subscriber.onError(t); |
|
410 |
} finally { |
|
411 |
cf.completeExceptionally(t); |
|
412 |
} |
|
48083 | 413 |
} |
414 |
} finally { |
|
415 |
bodyReader.onComplete(t); |
|
49944 | 416 |
if (t != null) { |
417 |
connection.close(); |
|
418 |
} |
|
48083 | 419 |
} |
420 |
})); |
|
421 |
CompletableFuture<State> bodyReaderCF = bodyReader.completion(); |
|
422 |
asyncReceiver.subscribe(bodyReader); |
|
423 |
assert bodyReaderCF != null : "parsing not started"; |
|
424 |
// Make sure to keep a reference to asyncReceiver from |
|
425 |
// within this |
|
426 |
CompletableFuture<?> trailingOp = bodyReaderCF.whenComplete((s,t) -> { |
|
427 |
t = Utils.getCompletionCause(t); |
|
428 |
try { |
|
49765 | 429 |
if (t == null) { |
430 |
if (debug.on()) debug.log("Finished reading body: " + s); |
|
48083 | 431 |
assert s == State.READING_BODY; |
432 |
} |
|
49765 | 433 |
if (t != null) { |
434 |
subscriber.onError(t); |
|
48083 | 435 |
cf.completeExceptionally(t); |
436 |
} |
|
437 |
} catch (Throwable x) { |
|
438 |
// not supposed to happen |
|
439 |
asyncReceiver.onReadError(x); |
|
49765 | 440 |
} finally { |
441 |
// we're done: release the ref count for |
|
442 |
// the current operation. |
|
443 |
refCountTracker.tryRelease(); |
|
48083 | 444 |
} |
445 |
}); |
|
446 |
connection.addTrailingOperation(trailingOp); |
|
42460
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
447 |
} catch (Throwable t) { |
49765 | 448 |
if (debug.on()) debug.log("Failed reading body: " + t); |
48083 | 449 |
try { |
49765 | 450 |
subscriber.onError(t); |
451 |
cf.completeExceptionally(t); |
|
48083 | 452 |
} finally { |
453 |
asyncReceiver.onReadError(t); |
|
454 |
} |
|
49765 | 455 |
} finally { |
456 |
connection.client().unreference(); |
|
42460
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
457 |
} |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
458 |
}); |
49765 | 459 |
try { |
460 |
p.getBody().whenComplete((U u, Throwable t) -> { |
|
461 |
if (t == null) |
|
462 |
cf.complete(u); |
|
463 |
else |
|
464 |
cf.completeExceptionally(t); |
|
465 |
}); |
|
466 |
} catch (Throwable t) { |
|
467 |
cf.completeExceptionally(t); |
|
468 |
asyncReceiver.setRetryOnError(false); |
|
469 |
asyncReceiver.onReadError(t); |
|
470 |
} |
|
471 |
||
472 |
return cf.whenComplete((s,t) -> { |
|
473 |
if (t != null) { |
|
474 |
// If an exception occurred, release the |
|
475 |
// ref count for the current operation, as |
|
476 |
// it may never be triggered otherwise |
|
477 |
// (BodySubscriber ofInputStream) |
|
478 |
// If there was no exception then the |
|
479 |
// ref count will be/have been released when |
|
480 |
// the last byte of the response is/was received |
|
481 |
refCountTracker.tryRelease(); |
|
482 |
} |
|
483 |
}); |
|
42460
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
484 |
} |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
485 |
|
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
486 |
|
48083 | 487 |
private void onFinished() { |
488 |
asyncReceiver.clear(); |
|
489 |
if (return2Cache) { |
|
490 |
Log.logTrace("Attempting to return connection to the pool: {0}", connection); |
|
491 |
// TODO: need to do something here? |
|
492 |
// connection.setAsyncCallbacks(null, null, null); |
|
42460
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
493 |
|
48083 | 494 |
// don't return the connection to the cache if EOF happened. |
49765 | 495 |
if (debug.on()) |
496 |
debug.log(connection.getConnectionFlow() + ": return to HTTP/1.1 pool"); |
|
48083 | 497 |
connection.closeOrReturnToCache(eof == null ? headers : null); |
498 |
} |
|
42460
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
499 |
} |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
500 |
|
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
501 |
HttpHeaders responseHeaders() { |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
502 |
return headers; |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
503 |
} |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
504 |
|
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
505 |
int responseCode() { |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
506 |
return responseCode; |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
507 |
} |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
508 |
|
48083 | 509 |
// ================ Support for plugging into Http1Receiver ================= |
510 |
// ============================================================================ |
|
42460
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
511 |
|
48083 | 512 |
// Callback: Error receiver: Consumer of Throwable. |
513 |
void onReadError(Throwable t) { |
|
514 |
Log.logError(t); |
|
515 |
Receiver<?> receiver = receiver(readProgress); |
|
516 |
if (t instanceof EOFException) { |
|
517 |
debug.log(Level.DEBUG, "onReadError: received EOF"); |
|
518 |
eof = (EOFException) t; |
|
519 |
} |
|
520 |
CompletableFuture<?> cf = receiver == null ? null : receiver.completion(); |
|
521 |
debug.log(Level.DEBUG, () -> "onReadError: cf is " |
|
522 |
+ (cf == null ? "null" |
|
523 |
: (cf.isDone() ? "already completed" |
|
524 |
: "not yet completed"))); |
|
49765 | 525 |
if (cf != null) { |
526 |
cf.completeExceptionally(t); |
|
527 |
} else { |
|
528 |
debug.log(Level.DEBUG, "onReadError", t); |
|
529 |
} |
|
48083 | 530 |
debug.log(Level.DEBUG, () -> "closing connection: cause is " + t); |
531 |
connection.close(); |
|
532 |
} |
|
533 |
||
534 |
// ======================================================================== |
|
535 |
||
536 |
private State advance(State previous) { |
|
537 |
assert readProgress == previous; |
|
538 |
switch(previous) { |
|
539 |
case READING_HEADERS: |
|
540 |
asyncReceiver.unsubscribe(headersReader); |
|
541 |
return readProgress = State.READING_BODY; |
|
542 |
case READING_BODY: |
|
543 |
asyncReceiver.unsubscribe(bodyReader); |
|
544 |
return readProgress = State.DONE; |
|
545 |
default: |
|
546 |
throw new InternalError("can't advance from " + previous); |
|
547 |
} |
|
548 |
} |
|
42460
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
549 |
|
48083 | 550 |
Receiver<?> receiver(State state) { |
551 |
switch(state) { |
|
552 |
case READING_HEADERS: return headersReader; |
|
553 |
case READING_BODY: return bodyReader; |
|
554 |
default: return null; |
|
42460
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
555 |
} |
48083 | 556 |
|
557 |
} |
|
558 |
||
559 |
static abstract class Receiver<T> |
|
560 |
implements Http1AsyncReceiver.Http1AsyncDelegate { |
|
561 |
abstract void start(T parser); |
|
562 |
abstract CompletableFuture<State> completion(); |
|
563 |
// accepts a buffer from upstream. |
|
564 |
// this should be implemented as a simple call to |
|
565 |
// accept(ref, parser, cf) |
|
566 |
public abstract boolean tryAsyncReceive(ByteBuffer buffer); |
|
567 |
public abstract void onReadError(Throwable t); |
|
568 |
// handle a byte buffer received from upstream. |
|
569 |
// this method should set the value of Http1Response.buffer |
|
570 |
// to ref.get() before beginning parsing. |
|
571 |
abstract void handle(ByteBuffer buf, T parser, |
|
572 |
CompletableFuture<State> cf); |
|
573 |
// resets this objects state so that it can be reused later on |
|
574 |
// typically puts the reference to parser and completion to null |
|
575 |
abstract void reset(); |
|
576 |
||
577 |
// accepts a byte buffer received from upstream |
|
578 |
// returns true if the buffer is fully parsed and more data can |
|
579 |
// be accepted, false otherwise. |
|
580 |
final boolean accept(ByteBuffer buf, T parser, |
|
581 |
CompletableFuture<State> cf) { |
|
582 |
if (cf == null || parser == null || cf.isDone()) return false; |
|
583 |
handle(buf, parser, cf); |
|
584 |
return !cf.isDone(); |
|
585 |
} |
|
586 |
public abstract void onSubscribe(AbstractSubscription s); |
|
587 |
public abstract AbstractSubscription subscription(); |
|
588 |
||
42460
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
589 |
} |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
590 |
|
48083 | 591 |
// Invoked with each new ByteBuffer when reading headers... |
592 |
final class HeadersReader extends Receiver<Http1HeaderParser> { |
|
593 |
final Consumer<State> onComplete; |
|
594 |
volatile Http1HeaderParser parser; |
|
595 |
volatile CompletableFuture<State> cf; |
|
596 |
volatile long count; // bytes parsed (for debug) |
|
597 |
volatile AbstractSubscription subscription; |
|
598 |
||
599 |
HeadersReader(Consumer<State> onComplete) { |
|
600 |
this.onComplete = onComplete; |
|
601 |
} |
|
602 |
||
603 |
@Override |
|
604 |
public AbstractSubscription subscription() { |
|
605 |
return subscription; |
|
606 |
} |
|
607 |
||
608 |
@Override |
|
609 |
public void onSubscribe(AbstractSubscription s) { |
|
610 |
this.subscription = s; |
|
611 |
s.request(1); |
|
612 |
} |
|
42460
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
613 |
|
48083 | 614 |
@Override |
615 |
void reset() { |
|
616 |
cf = null; |
|
617 |
parser = null; |
|
618 |
count = 0; |
|
619 |
subscription = null; |
|
620 |
} |
|
621 |
||
622 |
// Revisit: do we need to support restarting? |
|
623 |
@Override |
|
624 |
final void start(Http1HeaderParser hp) { |
|
625 |
count = 0; |
|
626 |
cf = new MinimalFuture<>(); |
|
627 |
parser = hp; |
|
628 |
} |
|
629 |
||
630 |
@Override |
|
631 |
CompletableFuture<State> completion() { |
|
632 |
return cf; |
|
633 |
} |
|
634 |
||
635 |
@Override |
|
636 |
public final boolean tryAsyncReceive(ByteBuffer ref) { |
|
637 |
boolean hasDemand = subscription.demand().tryDecrement(); |
|
638 |
assert hasDemand; |
|
639 |
boolean needsMore = accept(ref, parser, cf); |
|
640 |
if (needsMore) subscription.request(1); |
|
641 |
return needsMore; |
|
642 |
} |
|
643 |
||
644 |
@Override |
|
645 |
public final void onReadError(Throwable t) { |
|
56682
9822bbe48b9b
http-client-branch: more descriptive HTTP/1.1 exception detail messages
chegar
parents:
56631
diff
changeset
|
646 |
t = wrapWithExtraDetail(t, parser::currentStateMessage); |
48083 | 647 |
Http1Response.this.onReadError(t); |
648 |
} |
|
42460
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
649 |
|
48083 | 650 |
@Override |
651 |
final void handle(ByteBuffer b, |
|
652 |
Http1HeaderParser parser, |
|
653 |
CompletableFuture<State> cf) { |
|
654 |
assert cf != null : "parsing not started"; |
|
655 |
assert parser != null : "no parser"; |
|
656 |
try { |
|
657 |
count += b.remaining(); |
|
49765 | 658 |
if (debug.on()) |
659 |
debug.log("Sending " + b.remaining() + "/" + b.capacity() |
|
660 |
+ " bytes to header parser"); |
|
48083 | 661 |
if (parser.parse(b)) { |
662 |
count -= b.remaining(); |
|
49765 | 663 |
if (debug.on()) |
664 |
debug.log("Parsing headers completed. bytes=" + count); |
|
48083 | 665 |
onComplete.accept(State.READING_HEADERS); |
666 |
cf.complete(State.READING_HEADERS); |
|
42460
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
667 |
} |
48083 | 668 |
} catch (Throwable t) { |
49765 | 669 |
if (debug.on()) |
670 |
debug.log("Header parser failed to handle buffer: " + t); |
|
48083 | 671 |
cf.completeExceptionally(t); |
672 |
} |
|
673 |
} |
|
49765 | 674 |
|
675 |
@Override |
|
676 |
public void close(Throwable error) { |
|
677 |
// if there's no error nothing to do: the cf should/will |
|
678 |
// be completed. |
|
679 |
if (error != null) { |
|
680 |
CompletableFuture<State> cf = this.cf; |
|
681 |
if (cf != null) { |
|
682 |
if (debug.on()) |
|
683 |
debug.log("close: completing header parser CF with " + error); |
|
684 |
cf.completeExceptionally(error); |
|
685 |
} |
|
686 |
} |
|
687 |
} |
|
48083 | 688 |
} |
689 |
||
690 |
// Invoked with each new ByteBuffer when reading bodies... |
|
691 |
final class BodyReader extends Receiver<BodyParser> { |
|
692 |
final Consumer<State> onComplete; |
|
693 |
volatile BodyParser parser; |
|
694 |
volatile CompletableFuture<State> cf; |
|
695 |
volatile AbstractSubscription subscription; |
|
696 |
BodyReader(Consumer<State> onComplete) { |
|
697 |
this.onComplete = onComplete; |
|
698 |
} |
|
699 |
||
700 |
@Override |
|
701 |
void reset() { |
|
702 |
parser = null; |
|
703 |
cf = null; |
|
704 |
subscription = null; |
|
705 |
} |
|
706 |
||
707 |
// Revisit: do we need to support restarting? |
|
708 |
@Override |
|
709 |
final void start(BodyParser parser) { |
|
710 |
cf = new MinimalFuture<>(); |
|
711 |
this.parser = parser; |
|
712 |
} |
|
713 |
||
714 |
@Override |
|
715 |
CompletableFuture<State> completion() { |
|
716 |
return cf; |
|
717 |
} |
|
718 |
||
719 |
@Override |
|
720 |
public final boolean tryAsyncReceive(ByteBuffer b) { |
|
721 |
return accept(b, parser, cf); |
|
722 |
} |
|
723 |
||
724 |
@Override |
|
725 |
public final void onReadError(Throwable t) { |
|
56682
9822bbe48b9b
http-client-branch: more descriptive HTTP/1.1 exception detail messages
chegar
parents:
56631
diff
changeset
|
726 |
t = wrapWithExtraDetail(t, parser::currentStateMessage); |
48083 | 727 |
Http1Response.this.onReadError(t); |
728 |
} |
|
729 |
||
730 |
@Override |
|
731 |
public AbstractSubscription subscription() { |
|
732 |
return subscription; |
|
733 |
} |
|
734 |
||
735 |
@Override |
|
736 |
public void onSubscribe(AbstractSubscription s) { |
|
737 |
this.subscription = s; |
|
49765 | 738 |
try { |
739 |
parser.onSubscribe(s); |
|
740 |
} catch (Throwable t) { |
|
741 |
cf.completeExceptionally(t); |
|
742 |
throw t; |
|
743 |
} |
|
48083 | 744 |
} |
745 |
||
746 |
@Override |
|
747 |
final void handle(ByteBuffer b, |
|
748 |
BodyParser parser, |
|
749 |
CompletableFuture<State> cf) { |
|
750 |
assert cf != null : "parsing not started"; |
|
751 |
assert parser != null : "no parser"; |
|
752 |
try { |
|
49765 | 753 |
if (debug.on()) |
754 |
debug.log("Sending " + b.remaining() + "/" + b.capacity() |
|
755 |
+ " bytes to body parser"); |
|
48083 | 756 |
parser.accept(b); |
757 |
} catch (Throwable t) { |
|
49765 | 758 |
if (debug.on()) |
759 |
debug.log("Body parser failed to handle buffer: " + t); |
|
48083 | 760 |
if (!cf.isDone()) { |
761 |
cf.completeExceptionally(t); |
|
42460
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
762 |
} |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
763 |
} |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
764 |
} |
48083 | 765 |
|
766 |
final void onComplete(Throwable closedExceptionally) { |
|
767 |
if (cf.isDone()) return; |
|
768 |
if (closedExceptionally != null) { |
|
769 |
cf.completeExceptionally(closedExceptionally); |
|
770 |
} else { |
|
771 |
onComplete.accept(State.READING_BODY); |
|
772 |
cf.complete(State.READING_BODY); |
|
773 |
} |
|
774 |
} |
|
775 |
||
776 |
@Override |
|
49765 | 777 |
public final void close(Throwable error) { |
778 |
CompletableFuture<State> cf = this.cf; |
|
779 |
if (cf != null && !cf.isDone()) { |
|
780 |
// we want to make sure dependent actions are triggered |
|
781 |
// in order to make sure the client reference count |
|
782 |
// is decremented |
|
783 |
if (error != null) { |
|
784 |
if (debug.on()) |
|
785 |
debug.log("close: completing body parser CF with " + error); |
|
786 |
cf.completeExceptionally(error); |
|
787 |
} else { |
|
788 |
if (debug.on()) |
|
789 |
debug.log("close: completing body parser CF"); |
|
790 |
cf.complete(State.READING_BODY); |
|
791 |
} |
|
792 |
} |
|
793 |
} |
|
794 |
||
795 |
@Override |
|
48083 | 796 |
public String toString() { |
797 |
return super.toString() + "/parser=" + String.valueOf(parser); |
|
798 |
} |
|
42460
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
799 |
} |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
800 |
} |