author | chegar |
Tue, 06 Mar 2018 18:43:27 +0000 | |
branch | http-client-branch |
changeset 56253 | 875dbf6234f2 |
parent 56167 | 96fa4f49a9ff |
child 56288 | 2de1aa88cf06 |
permissions | -rw-r--r-- |
42460
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
1 |
/* |
56079
d23b02f37fce
http-client-branch: more remaining impl types to internal
chegar
parents:
55973
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 |
|
56092
fd85b2bf2b0d
http-client-branch: move implementation to jdk.internal.net.http
chegar
parents:
56089
diff
changeset
|
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; |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
31 |
import java.util.concurrent.CompletableFuture; |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
32 |
import java.util.concurrent.Executor; |
56165
8a6065d830b9
http-client-branch: Add a test for BodySubscribers throwing in getBody() or returning exceptionally completed CFs
dfuchs
parents:
56126
diff
changeset
|
33 |
import java.util.concurrent.atomic.AtomicLong; |
48083 | 34 |
import java.util.function.Consumer; |
35 |
import java.util.function.Function; |
|
56089
42208b2f224e
http-client-branch: move to standard package and module name
chegar
parents:
56082
diff
changeset
|
36 |
import java.net.http.HttpHeaders; |
42208b2f224e
http-client-branch: move to standard package and module name
chegar
parents:
56082
diff
changeset
|
37 |
import java.net.http.HttpResponse; |
56092
fd85b2bf2b0d
http-client-branch: move implementation to jdk.internal.net.http
chegar
parents:
56089
diff
changeset
|
38 |
import jdk.internal.net.http.ResponseContent.BodyParser; |
fd85b2bf2b0d
http-client-branch: move implementation to jdk.internal.net.http
chegar
parents:
56089
diff
changeset
|
39 |
import jdk.internal.net.http.common.Log; |
fd85b2bf2b0d
http-client-branch: move implementation to jdk.internal.net.http
chegar
parents:
56089
diff
changeset
|
40 |
import jdk.internal.net.http.common.MinimalFuture; |
fd85b2bf2b0d
http-client-branch: move implementation to jdk.internal.net.http
chegar
parents:
56089
diff
changeset
|
41 |
import jdk.internal.net.http.common.Utils; |
56089
42208b2f224e
http-client-branch: move to standard package and module name
chegar
parents:
56082
diff
changeset
|
42 |
import static java.net.http.HttpClient.Version.HTTP_1_1; |
56167
96fa4f49a9ff
http-client-branch: CSR review commet - outboard pre-defined BP/BH/BS
chegar
parents:
56165
diff
changeset
|
43 |
import static java.net.http.HttpResponse.BodySubscribers.discarding; |
42460
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
44 |
|
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
45 |
/** |
48083 | 46 |
* Handles a HTTP/1.1 response (headers + body). |
47 |
* 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
|
48 |
*/ |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
49 |
class Http1Response<T> { |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
50 |
|
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
51 |
private volatile ResponseContent content; |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
52 |
private final HttpRequestImpl request; |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
53 |
private Response response; |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
54 |
private final HttpConnection connection; |
48083 | 55 |
private HttpHeaders headers; |
42460
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
56 |
private int responseCode; |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
57 |
private final Http1Exchange<T> exchange; |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
58 |
private boolean return2Cache; // return connection to cache when finished |
48083 | 59 |
private final HeadersReader headersReader; // used to read the headers |
60 |
private final BodyReader bodyReader; // used to read the body |
|
61 |
private final Http1AsyncReceiver asyncReceiver; |
|
62 |
private volatile EOFException eof; |
|
63 |
// max number of bytes of (fixed length) body to ignore on redirect |
|
64 |
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
|
65 |
|
48083 | 66 |
// Revisit: can we get rid of this? |
67 |
static enum State {INITIAL, READING_HEADERS, READING_BODY, DONE} |
|
68 |
private volatile State readProgress = State.INITIAL; |
|
69 |
static final boolean DEBUG = Utils.DEBUG; // Revisit: temporary dev flag. |
|
56165
8a6065d830b9
http-client-branch: Add a test for BodySubscribers throwing in getBody() or returning exceptionally completed CFs
dfuchs
parents:
56126
diff
changeset
|
70 |
final System.Logger debug = Utils.getDebugLogger(this::dbgString, DEBUG); |
8a6065d830b9
http-client-branch: Add a test for BodySubscribers throwing in getBody() or returning exceptionally completed CFs
dfuchs
parents:
56126
diff
changeset
|
71 |
final static AtomicLong responseCount = new AtomicLong(); |
8a6065d830b9
http-client-branch: Add a test for BodySubscribers throwing in getBody() or returning exceptionally completed CFs
dfuchs
parents:
56126
diff
changeset
|
72 |
final long id = responseCount.incrementAndGet(); |
48083 | 73 |
|
74 |
Http1Response(HttpConnection conn, |
|
75 |
Http1Exchange<T> exchange, |
|
76 |
Http1AsyncReceiver asyncReceiver) { |
|
77 |
this.readProgress = State.INITIAL; |
|
42460
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
78 |
this.request = exchange.request(); |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
79 |
this.exchange = exchange; |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
80 |
this.connection = conn; |
48083 | 81 |
this.asyncReceiver = asyncReceiver; |
82 |
headersReader = new HeadersReader(this::advance); |
|
83 |
bodyReader = new BodyReader(this::advance); |
|
42460
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
84 |
} |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
85 |
|
56165
8a6065d830b9
http-client-branch: Add a test for BodySubscribers throwing in getBody() or returning exceptionally completed CFs
dfuchs
parents:
56126
diff
changeset
|
86 |
String dbgTag; |
8a6065d830b9
http-client-branch: Add a test for BodySubscribers throwing in getBody() or returning exceptionally completed CFs
dfuchs
parents:
56126
diff
changeset
|
87 |
private String dbgString() { |
8a6065d830b9
http-client-branch: Add a test for BodySubscribers throwing in getBody() or returning exceptionally completed CFs
dfuchs
parents:
56126
diff
changeset
|
88 |
String dbg = dbgTag; |
8a6065d830b9
http-client-branch: Add a test for BodySubscribers throwing in getBody() or returning exceptionally completed CFs
dfuchs
parents:
56126
diff
changeset
|
89 |
if (dbg == null) { |
8a6065d830b9
http-client-branch: Add a test for BodySubscribers throwing in getBody() or returning exceptionally completed CFs
dfuchs
parents:
56126
diff
changeset
|
90 |
String cdbg = connection.dbgTag; |
8a6065d830b9
http-client-branch: Add a test for BodySubscribers throwing in getBody() or returning exceptionally completed CFs
dfuchs
parents:
56126
diff
changeset
|
91 |
if (cdbg != null) { |
8a6065d830b9
http-client-branch: Add a test for BodySubscribers throwing in getBody() or returning exceptionally completed CFs
dfuchs
parents:
56126
diff
changeset
|
92 |
dbgTag = dbg = "Http1Response(id=" + id + ", " + cdbg + ")"; |
8a6065d830b9
http-client-branch: Add a test for BodySubscribers throwing in getBody() or returning exceptionally completed CFs
dfuchs
parents:
56126
diff
changeset
|
93 |
} else { |
8a6065d830b9
http-client-branch: Add a test for BodySubscribers throwing in getBody() or returning exceptionally completed CFs
dfuchs
parents:
56126
diff
changeset
|
94 |
dbg = "Http1Response(id=" + id + ")"; |
8a6065d830b9
http-client-branch: Add a test for BodySubscribers throwing in getBody() or returning exceptionally completed CFs
dfuchs
parents:
56126
diff
changeset
|
95 |
} |
8a6065d830b9
http-client-branch: Add a test for BodySubscribers throwing in getBody() or returning exceptionally completed CFs
dfuchs
parents:
56126
diff
changeset
|
96 |
} |
8a6065d830b9
http-client-branch: Add a test for BodySubscribers throwing in getBody() or returning exceptionally completed CFs
dfuchs
parents:
56126
diff
changeset
|
97 |
return dbg; |
8a6065d830b9
http-client-branch: Add a test for BodySubscribers throwing in getBody() or returning exceptionally completed CFs
dfuchs
parents:
56126
diff
changeset
|
98 |
} |
8a6065d830b9
http-client-branch: Add a test for BodySubscribers throwing in getBody() or returning exceptionally completed CFs
dfuchs
parents:
56126
diff
changeset
|
99 |
|
8a6065d830b9
http-client-branch: Add a test for BodySubscribers throwing in getBody() or returning exceptionally completed CFs
dfuchs
parents:
56126
diff
changeset
|
100 |
// The ClientRefCountTracker is used to track the state |
8a6065d830b9
http-client-branch: Add a test for BodySubscribers throwing in getBody() or returning exceptionally completed CFs
dfuchs
parents:
56126
diff
changeset
|
101 |
// of a pending operation. Altough there usually is a single |
8a6065d830b9
http-client-branch: Add a test for BodySubscribers throwing in getBody() or returning exceptionally completed CFs
dfuchs
parents:
56126
diff
changeset
|
102 |
// point where the operation starts, it may terminate at |
8a6065d830b9
http-client-branch: Add a test for BodySubscribers throwing in getBody() or returning exceptionally completed CFs
dfuchs
parents:
56126
diff
changeset
|
103 |
// different places. |
8a6065d830b9
http-client-branch: Add a test for BodySubscribers throwing in getBody() or returning exceptionally completed CFs
dfuchs
parents:
56126
diff
changeset
|
104 |
private final class ClientRefCountTracker { |
8a6065d830b9
http-client-branch: Add a test for BodySubscribers throwing in getBody() or returning exceptionally completed CFs
dfuchs
parents:
56126
diff
changeset
|
105 |
final HttpClientImpl client = connection.client(); |
8a6065d830b9
http-client-branch: Add a test for BodySubscribers throwing in getBody() or returning exceptionally completed CFs
dfuchs
parents:
56126
diff
changeset
|
106 |
// state & 0x01 != 0 => acquire called |
8a6065d830b9
http-client-branch: Add a test for BodySubscribers throwing in getBody() or returning exceptionally completed CFs
dfuchs
parents:
56126
diff
changeset
|
107 |
// state & 0x02 != 0 => tryRelease called |
8a6065d830b9
http-client-branch: Add a test for BodySubscribers throwing in getBody() or returning exceptionally completed CFs
dfuchs
parents:
56126
diff
changeset
|
108 |
byte state; |
8a6065d830b9
http-client-branch: Add a test for BodySubscribers throwing in getBody() or returning exceptionally completed CFs
dfuchs
parents:
56126
diff
changeset
|
109 |
|
8a6065d830b9
http-client-branch: Add a test for BodySubscribers throwing in getBody() or returning exceptionally completed CFs
dfuchs
parents:
56126
diff
changeset
|
110 |
public synchronized void acquire() { |
8a6065d830b9
http-client-branch: Add a test for BodySubscribers throwing in getBody() or returning exceptionally completed CFs
dfuchs
parents:
56126
diff
changeset
|
111 |
if (state == 0) { |
8a6065d830b9
http-client-branch: Add a test for BodySubscribers throwing in getBody() or returning exceptionally completed CFs
dfuchs
parents:
56126
diff
changeset
|
112 |
// increment the reference count on the HttpClientImpl |
8a6065d830b9
http-client-branch: Add a test for BodySubscribers throwing in getBody() or returning exceptionally completed CFs
dfuchs
parents:
56126
diff
changeset
|
113 |
// to prevent the SelectorManager thread from exiting |
8a6065d830b9
http-client-branch: Add a test for BodySubscribers throwing in getBody() or returning exceptionally completed CFs
dfuchs
parents:
56126
diff
changeset
|
114 |
// until our operation is complete. |
8a6065d830b9
http-client-branch: Add a test for BodySubscribers throwing in getBody() or returning exceptionally completed CFs
dfuchs
parents:
56126
diff
changeset
|
115 |
debug.log(Level.DEBUG, "incrementing ref count for %s", client); |
8a6065d830b9
http-client-branch: Add a test for BodySubscribers throwing in getBody() or returning exceptionally completed CFs
dfuchs
parents:
56126
diff
changeset
|
116 |
client.reference(); |
8a6065d830b9
http-client-branch: Add a test for BodySubscribers throwing in getBody() or returning exceptionally completed CFs
dfuchs
parents:
56126
diff
changeset
|
117 |
state = 0x01; |
8a6065d830b9
http-client-branch: Add a test for BodySubscribers throwing in getBody() or returning exceptionally completed CFs
dfuchs
parents:
56126
diff
changeset
|
118 |
} else { |
8a6065d830b9
http-client-branch: Add a test for BodySubscribers throwing in getBody() or returning exceptionally completed CFs
dfuchs
parents:
56126
diff
changeset
|
119 |
assert (state & 0x01) == 0 : "reference count already incremented"; |
8a6065d830b9
http-client-branch: Add a test for BodySubscribers throwing in getBody() or returning exceptionally completed CFs
dfuchs
parents:
56126
diff
changeset
|
120 |
} |
8a6065d830b9
http-client-branch: Add a test for BodySubscribers throwing in getBody() or returning exceptionally completed CFs
dfuchs
parents:
56126
diff
changeset
|
121 |
} |
8a6065d830b9
http-client-branch: Add a test for BodySubscribers throwing in getBody() or returning exceptionally completed CFs
dfuchs
parents:
56126
diff
changeset
|
122 |
|
8a6065d830b9
http-client-branch: Add a test for BodySubscribers throwing in getBody() or returning exceptionally completed CFs
dfuchs
parents:
56126
diff
changeset
|
123 |
public synchronized void tryRelease() { |
8a6065d830b9
http-client-branch: Add a test for BodySubscribers throwing in getBody() or returning exceptionally completed CFs
dfuchs
parents:
56126
diff
changeset
|
124 |
if (state == 0x01) { |
8a6065d830b9
http-client-branch: Add a test for BodySubscribers throwing in getBody() or returning exceptionally completed CFs
dfuchs
parents:
56126
diff
changeset
|
125 |
// decrement the reference count on the HttpClientImpl |
8a6065d830b9
http-client-branch: Add a test for BodySubscribers throwing in getBody() or returning exceptionally completed CFs
dfuchs
parents:
56126
diff
changeset
|
126 |
// to allow the SelectorManager thread to exit if no |
8a6065d830b9
http-client-branch: Add a test for BodySubscribers throwing in getBody() or returning exceptionally completed CFs
dfuchs
parents:
56126
diff
changeset
|
127 |
// other operation is pending and the facade is no |
8a6065d830b9
http-client-branch: Add a test for BodySubscribers throwing in getBody() or returning exceptionally completed CFs
dfuchs
parents:
56126
diff
changeset
|
128 |
// longer referenced. |
8a6065d830b9
http-client-branch: Add a test for BodySubscribers throwing in getBody() or returning exceptionally completed CFs
dfuchs
parents:
56126
diff
changeset
|
129 |
debug.log(Level.DEBUG, "decrementing ref count for %s", client); |
8a6065d830b9
http-client-branch: Add a test for BodySubscribers throwing in getBody() or returning exceptionally completed CFs
dfuchs
parents:
56126
diff
changeset
|
130 |
client.unreference(); |
8a6065d830b9
http-client-branch: Add a test for BodySubscribers throwing in getBody() or returning exceptionally completed CFs
dfuchs
parents:
56126
diff
changeset
|
131 |
state |= 0x02; |
8a6065d830b9
http-client-branch: Add a test for BodySubscribers throwing in getBody() or returning exceptionally completed CFs
dfuchs
parents:
56126
diff
changeset
|
132 |
} |
8a6065d830b9
http-client-branch: Add a test for BodySubscribers throwing in getBody() or returning exceptionally completed CFs
dfuchs
parents:
56126
diff
changeset
|
133 |
} |
8a6065d830b9
http-client-branch: Add a test for BodySubscribers throwing in getBody() or returning exceptionally completed CFs
dfuchs
parents:
56126
diff
changeset
|
134 |
} |
8a6065d830b9
http-client-branch: Add a test for BodySubscribers throwing in getBody() or returning exceptionally completed CFs
dfuchs
parents:
56126
diff
changeset
|
135 |
|
48083 | 136 |
public CompletableFuture<Response> readHeadersAsync(Executor executor) { |
137 |
debug.log(Level.DEBUG, () -> "Reading Headers: (remaining: " |
|
138 |
+ asyncReceiver.remaining() +") " + readProgress); |
|
139 |
// with expect continue we will resume reading headers + body. |
|
140 |
asyncReceiver.unsubscribe(bodyReader); |
|
141 |
bodyReader.reset(); |
|
142 |
Http1HeaderParser hd = new Http1HeaderParser(); |
|
143 |
readProgress = State.READING_HEADERS; |
|
144 |
headersReader.start(hd); |
|
145 |
asyncReceiver.subscribe(headersReader); |
|
146 |
CompletableFuture<State> cf = headersReader.completion(); |
|
147 |
assert cf != null : "parsing not started"; |
|
148 |
||
149 |
Function<State, Response> lambda = (State completed) -> { |
|
150 |
assert completed == State.READING_HEADERS; |
|
151 |
debug.log(Level.DEBUG, () -> |
|
152 |
"Reading Headers: creating Response object;" |
|
153 |
+ " state is now " + readProgress); |
|
154 |
asyncReceiver.unsubscribe(headersReader); |
|
155 |
responseCode = hd.responseCode(); |
|
156 |
headers = hd.headers(); |
|
157 |
||
158 |
response = new Response(request, |
|
159 |
exchange.getExchange(), |
|
160 |
headers, |
|
56126
86e628130926
http-client-branch: fixed TLS hostname checking issue, SSL session reuse, and changed HttpResponse to return SSLSession
michaelm
parents:
56092
diff
changeset
|
161 |
connection, |
48083 | 162 |
responseCode, |
163 |
HTTP_1_1); |
|
56253
875dbf6234f2
http-client-branch: set correct request returned by response when redirecting, and add additional test coverage
chegar
parents:
56167
diff
changeset
|
164 |
|
875dbf6234f2
http-client-branch: set correct request returned by response when redirecting, and add additional test coverage
chegar
parents:
56167
diff
changeset
|
165 |
if (Log.headers()) { |
875dbf6234f2
http-client-branch: set correct request returned by response when redirecting, and add additional test coverage
chegar
parents:
56167
diff
changeset
|
166 |
StringBuilder sb = new StringBuilder("RESPONSE HEADERS:\n"); |
875dbf6234f2
http-client-branch: set correct request returned by response when redirecting, and add additional test coverage
chegar
parents:
56167
diff
changeset
|
167 |
Log.dumpHeaders(sb, " ", headers); |
875dbf6234f2
http-client-branch: set correct request returned by response when redirecting, and add additional test coverage
chegar
parents:
56167
diff
changeset
|
168 |
Log.logHeaders(sb.toString()); |
875dbf6234f2
http-client-branch: set correct request returned by response when redirecting, and add additional test coverage
chegar
parents:
56167
diff
changeset
|
169 |
} |
875dbf6234f2
http-client-branch: set correct request returned by response when redirecting, and add additional test coverage
chegar
parents:
56167
diff
changeset
|
170 |
|
48083 | 171 |
return response; |
172 |
}; |
|
173 |
||
174 |
if (executor != null) { |
|
175 |
return cf.thenApplyAsync(lambda, executor); |
|
176 |
} else { |
|
177 |
return cf.thenApply(lambda); |
|
42460
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
178 |
} |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
179 |
} |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
180 |
|
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
181 |
private boolean finished; |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
182 |
|
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
183 |
synchronized void completed() { |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
184 |
finished = true; |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
185 |
} |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
186 |
|
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
187 |
synchronized boolean finished() { |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
188 |
return finished; |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
189 |
} |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
190 |
|
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
191 |
int fixupContentLen(int clen) { |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
192 |
if (request.method().equalsIgnoreCase("HEAD")) { |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
193 |
return 0; |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
194 |
} |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
195 |
if (clen == -1) { |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
196 |
if (headers.firstValue("Transfer-encoding").orElse("") |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
197 |
.equalsIgnoreCase("chunked")) { |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
198 |
return -1; |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
199 |
} |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
200 |
return 0; |
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 |
return clen; |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
203 |
} |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
204 |
|
48083 | 205 |
/** |
206 |
* Read up to MAX_IGNORE bytes discarding |
|
207 |
*/ |
|
208 |
public CompletableFuture<Void> ignoreBody(Executor executor) { |
|
209 |
int clen = (int)headers.firstValueAsLong("Content-Length").orElse(-1); |
|
210 |
if (clen == -1 || clen > MAX_IGNORE) { |
|
211 |
connection.close(); |
|
212 |
return MinimalFuture.completedFuture(null); // not treating as error |
|
213 |
} else { |
|
56167
96fa4f49a9ff
http-client-branch: CSR review commet - outboard pre-defined BP/BH/BS
chegar
parents:
56165
diff
changeset
|
214 |
return readBody(discarding(), true, executor); |
48083 | 215 |
} |
42460
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 |
|
56165
8a6065d830b9
http-client-branch: Add a test for BodySubscribers throwing in getBody() or returning exceptionally completed CFs
dfuchs
parents:
56126
diff
changeset
|
218 |
|
48083 | 219 |
public <U> CompletableFuture<U> readBody(HttpResponse.BodySubscriber<U> p, |
220 |
boolean return2Cache, |
|
221 |
Executor executor) { |
|
42460
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
222 |
this.return2Cache = return2Cache; |
48083 | 223 |
final HttpResponse.BodySubscriber<U> pusher = p; |
56082
1da51fab3032
http-client-branch: added mapping subscriber, miscellaneous bug fixes and change to discard()/replace() subscribers
michaelm
parents:
56079
diff
changeset
|
224 |
|
1da51fab3032
http-client-branch: added mapping subscriber, miscellaneous bug fixes and change to discard()/replace() subscribers
michaelm
parents:
56079
diff
changeset
|
225 |
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
|
226 |
|
48083 | 227 |
int clen0 = (int)headers.firstValueAsLong("Content-Length").orElse(-1); |
228 |
||
42460
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
229 |
final int clen = fixupContentLen(clen0); |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
230 |
|
48083 | 231 |
// expect-continue reads headers and body twice. |
232 |
// if we reach here, we must reset the headersReader state. |
|
233 |
asyncReceiver.unsubscribe(headersReader); |
|
234 |
headersReader.reset(); |
|
56165
8a6065d830b9
http-client-branch: Add a test for BodySubscribers throwing in getBody() or returning exceptionally completed CFs
dfuchs
parents:
56126
diff
changeset
|
235 |
ClientRefCountTracker refCountTracker = new ClientRefCountTracker(); |
48083 | 236 |
|
42460
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
237 |
executor.execute(() -> { |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
238 |
try { |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
239 |
content = new ResponseContent( |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
240 |
connection, clen, headers, pusher, |
48083 | 241 |
this::onFinished |
42460
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
242 |
); |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
243 |
if (cf.isCompletedExceptionally()) { |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
244 |
// if an error occurs during subscription |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
245 |
connection.close(); |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
246 |
return; |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
247 |
} |
48083 | 248 |
// increment the reference count on the HttpClientImpl |
249 |
// to prevent the SelectorManager thread from exiting until |
|
250 |
// the body is fully read. |
|
56165
8a6065d830b9
http-client-branch: Add a test for BodySubscribers throwing in getBody() or returning exceptionally completed CFs
dfuchs
parents:
56126
diff
changeset
|
251 |
refCountTracker.acquire(); |
48083 | 252 |
bodyReader.start(content.getBodyParser( |
253 |
(t) -> { |
|
254 |
try { |
|
255 |
if (t != null) { |
|
256 |
pusher.onError(t); |
|
257 |
connection.close(); |
|
258 |
if (!cf.isDone()) |
|
259 |
cf.completeExceptionally(t); |
|
260 |
} |
|
261 |
} finally { |
|
262 |
bodyReader.onComplete(t); |
|
263 |
} |
|
264 |
})); |
|
265 |
CompletableFuture<State> bodyReaderCF = bodyReader.completion(); |
|
266 |
asyncReceiver.subscribe(bodyReader); |
|
267 |
assert bodyReaderCF != null : "parsing not started"; |
|
268 |
// Make sure to keep a reference to asyncReceiver from |
|
269 |
// within this |
|
270 |
CompletableFuture<?> trailingOp = bodyReaderCF.whenComplete((s,t) -> { |
|
271 |
t = Utils.getCompletionCause(t); |
|
272 |
try { |
|
56165
8a6065d830b9
http-client-branch: Add a test for BodySubscribers throwing in getBody() or returning exceptionally completed CFs
dfuchs
parents:
56126
diff
changeset
|
273 |
if (t == null) { |
48083 | 274 |
debug.log(Level.DEBUG, () -> |
275 |
"Finished reading body: " + s); |
|
276 |
assert s == State.READING_BODY; |
|
277 |
} |
|
278 |
if (t != null && !cf.isDone()) { |
|
279 |
pusher.onError(t); |
|
280 |
cf.completeExceptionally(t); |
|
281 |
} |
|
282 |
} catch (Throwable x) { |
|
283 |
// not supposed to happen |
|
284 |
asyncReceiver.onReadError(x); |
|
56165
8a6065d830b9
http-client-branch: Add a test for BodySubscribers throwing in getBody() or returning exceptionally completed CFs
dfuchs
parents:
56126
diff
changeset
|
285 |
} finally { |
8a6065d830b9
http-client-branch: Add a test for BodySubscribers throwing in getBody() or returning exceptionally completed CFs
dfuchs
parents:
56126
diff
changeset
|
286 |
// we're done: release the ref count for |
8a6065d830b9
http-client-branch: Add a test for BodySubscribers throwing in getBody() or returning exceptionally completed CFs
dfuchs
parents:
56126
diff
changeset
|
287 |
// the current operation. |
8a6065d830b9
http-client-branch: Add a test for BodySubscribers throwing in getBody() or returning exceptionally completed CFs
dfuchs
parents:
56126
diff
changeset
|
288 |
refCountTracker.tryRelease(); |
48083 | 289 |
} |
290 |
}); |
|
291 |
connection.addTrailingOperation(trailingOp); |
|
42460
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
292 |
} catch (Throwable t) { |
48083 | 293 |
debug.log(Level.DEBUG, () -> "Failed reading body: " + t); |
294 |
try { |
|
295 |
if (!cf.isDone()) { |
|
296 |
pusher.onError(t); |
|
297 |
cf.completeExceptionally(t); |
|
298 |
} |
|
299 |
} finally { |
|
300 |
asyncReceiver.onReadError(t); |
|
301 |
} |
|
42460
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
302 |
} |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
303 |
}); |
56165
8a6065d830b9
http-client-branch: Add a test for BodySubscribers throwing in getBody() or returning exceptionally completed CFs
dfuchs
parents:
56126
diff
changeset
|
304 |
try { |
8a6065d830b9
http-client-branch: Add a test for BodySubscribers throwing in getBody() or returning exceptionally completed CFs
dfuchs
parents:
56126
diff
changeset
|
305 |
p.getBody().whenComplete((U u, Throwable t) -> { |
8a6065d830b9
http-client-branch: Add a test for BodySubscribers throwing in getBody() or returning exceptionally completed CFs
dfuchs
parents:
56126
diff
changeset
|
306 |
if (t == null) |
8a6065d830b9
http-client-branch: Add a test for BodySubscribers throwing in getBody() or returning exceptionally completed CFs
dfuchs
parents:
56126
diff
changeset
|
307 |
cf.complete(u); |
8a6065d830b9
http-client-branch: Add a test for BodySubscribers throwing in getBody() or returning exceptionally completed CFs
dfuchs
parents:
56126
diff
changeset
|
308 |
else |
8a6065d830b9
http-client-branch: Add a test for BodySubscribers throwing in getBody() or returning exceptionally completed CFs
dfuchs
parents:
56126
diff
changeset
|
309 |
cf.completeExceptionally(t); |
8a6065d830b9
http-client-branch: Add a test for BodySubscribers throwing in getBody() or returning exceptionally completed CFs
dfuchs
parents:
56126
diff
changeset
|
310 |
}); |
8a6065d830b9
http-client-branch: Add a test for BodySubscribers throwing in getBody() or returning exceptionally completed CFs
dfuchs
parents:
56126
diff
changeset
|
311 |
} catch (Throwable t) { |
8a6065d830b9
http-client-branch: Add a test for BodySubscribers throwing in getBody() or returning exceptionally completed CFs
dfuchs
parents:
56126
diff
changeset
|
312 |
cf.completeExceptionally(t); |
8a6065d830b9
http-client-branch: Add a test for BodySubscribers throwing in getBody() or returning exceptionally completed CFs
dfuchs
parents:
56126
diff
changeset
|
313 |
asyncReceiver.setRetryOnError(false); |
8a6065d830b9
http-client-branch: Add a test for BodySubscribers throwing in getBody() or returning exceptionally completed CFs
dfuchs
parents:
56126
diff
changeset
|
314 |
asyncReceiver.onReadError(t); |
8a6065d830b9
http-client-branch: Add a test for BodySubscribers throwing in getBody() or returning exceptionally completed CFs
dfuchs
parents:
56126
diff
changeset
|
315 |
} |
8a6065d830b9
http-client-branch: Add a test for BodySubscribers throwing in getBody() or returning exceptionally completed CFs
dfuchs
parents:
56126
diff
changeset
|
316 |
|
8a6065d830b9
http-client-branch: Add a test for BodySubscribers throwing in getBody() or returning exceptionally completed CFs
dfuchs
parents:
56126
diff
changeset
|
317 |
return cf.whenComplete((s,t) -> { |
8a6065d830b9
http-client-branch: Add a test for BodySubscribers throwing in getBody() or returning exceptionally completed CFs
dfuchs
parents:
56126
diff
changeset
|
318 |
if (t != null) { |
8a6065d830b9
http-client-branch: Add a test for BodySubscribers throwing in getBody() or returning exceptionally completed CFs
dfuchs
parents:
56126
diff
changeset
|
319 |
// If an exception occurred, release the |
8a6065d830b9
http-client-branch: Add a test for BodySubscribers throwing in getBody() or returning exceptionally completed CFs
dfuchs
parents:
56126
diff
changeset
|
320 |
// ref count for the current operation, as |
8a6065d830b9
http-client-branch: Add a test for BodySubscribers throwing in getBody() or returning exceptionally completed CFs
dfuchs
parents:
56126
diff
changeset
|
321 |
// it may never be triggered otherwise |
8a6065d830b9
http-client-branch: Add a test for BodySubscribers throwing in getBody() or returning exceptionally completed CFs
dfuchs
parents:
56126
diff
changeset
|
322 |
// (BodySubscriber ofInputStream) |
8a6065d830b9
http-client-branch: Add a test for BodySubscribers throwing in getBody() or returning exceptionally completed CFs
dfuchs
parents:
56126
diff
changeset
|
323 |
// If there was no exception then the |
8a6065d830b9
http-client-branch: Add a test for BodySubscribers throwing in getBody() or returning exceptionally completed CFs
dfuchs
parents:
56126
diff
changeset
|
324 |
// ref count will be/have been released when |
8a6065d830b9
http-client-branch: Add a test for BodySubscribers throwing in getBody() or returning exceptionally completed CFs
dfuchs
parents:
56126
diff
changeset
|
325 |
// the last byte of the response is/was received |
8a6065d830b9
http-client-branch: Add a test for BodySubscribers throwing in getBody() or returning exceptionally completed CFs
dfuchs
parents:
56126
diff
changeset
|
326 |
refCountTracker.tryRelease(); |
8a6065d830b9
http-client-branch: Add a test for BodySubscribers throwing in getBody() or returning exceptionally completed CFs
dfuchs
parents:
56126
diff
changeset
|
327 |
} |
56082
1da51fab3032
http-client-branch: added mapping subscriber, miscellaneous bug fixes and change to discard()/replace() subscribers
michaelm
parents:
56079
diff
changeset
|
328 |
}); |
42460
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
329 |
} |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
330 |
|
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
331 |
|
48083 | 332 |
private void onFinished() { |
333 |
asyncReceiver.clear(); |
|
334 |
if (return2Cache) { |
|
335 |
Log.logTrace("Attempting to return connection to the pool: {0}", connection); |
|
336 |
// TODO: need to do something here? |
|
337 |
// connection.setAsyncCallbacks(null, null, null); |
|
42460
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
338 |
|
48083 | 339 |
// don't return the connection to the cache if EOF happened. |
340 |
debug.log(Level.DEBUG, () -> connection.getConnectionFlow() |
|
341 |
+ ": return to HTTP/1.1 pool"); |
|
342 |
connection.closeOrReturnToCache(eof == null ? headers : null); |
|
343 |
} |
|
42460
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
344 |
} |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
345 |
|
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
346 |
HttpHeaders responseHeaders() { |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
347 |
return headers; |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
348 |
} |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
349 |
|
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
350 |
int responseCode() { |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
351 |
return responseCode; |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
352 |
} |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
353 |
|
48083 | 354 |
// ================ Support for plugging into Http1Receiver ================= |
355 |
// ============================================================================ |
|
42460
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
356 |
|
48083 | 357 |
// Callback: Error receiver: Consumer of Throwable. |
358 |
void onReadError(Throwable t) { |
|
359 |
Log.logError(t); |
|
360 |
Receiver<?> receiver = receiver(readProgress); |
|
361 |
if (t instanceof EOFException) { |
|
362 |
debug.log(Level.DEBUG, "onReadError: received EOF"); |
|
363 |
eof = (EOFException) t; |
|
364 |
} |
|
365 |
CompletableFuture<?> cf = receiver == null ? null : receiver.completion(); |
|
366 |
debug.log(Level.DEBUG, () -> "onReadError: cf is " |
|
367 |
+ (cf == null ? "null" |
|
368 |
: (cf.isDone() ? "already completed" |
|
369 |
: "not yet completed"))); |
|
370 |
if (cf != null && !cf.isDone()) cf.completeExceptionally(t); |
|
371 |
else { debug.log(Level.DEBUG, "onReadError", t); } |
|
372 |
debug.log(Level.DEBUG, () -> "closing connection: cause is " + t); |
|
373 |
connection.close(); |
|
374 |
} |
|
375 |
||
376 |
// ======================================================================== |
|
377 |
||
378 |
private State advance(State previous) { |
|
379 |
assert readProgress == previous; |
|
380 |
switch(previous) { |
|
381 |
case READING_HEADERS: |
|
382 |
asyncReceiver.unsubscribe(headersReader); |
|
383 |
return readProgress = State.READING_BODY; |
|
384 |
case READING_BODY: |
|
385 |
asyncReceiver.unsubscribe(bodyReader); |
|
386 |
return readProgress = State.DONE; |
|
387 |
default: |
|
388 |
throw new InternalError("can't advance from " + previous); |
|
389 |
} |
|
390 |
} |
|
42460
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
391 |
|
48083 | 392 |
Receiver<?> receiver(State state) { |
393 |
switch(state) { |
|
394 |
case READING_HEADERS: return headersReader; |
|
395 |
case READING_BODY: return bodyReader; |
|
396 |
default: return null; |
|
42460
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
397 |
} |
48083 | 398 |
|
399 |
} |
|
400 |
||
401 |
static abstract class Receiver<T> |
|
402 |
implements Http1AsyncReceiver.Http1AsyncDelegate { |
|
403 |
abstract void start(T parser); |
|
404 |
abstract CompletableFuture<State> completion(); |
|
405 |
// accepts a buffer from upstream. |
|
406 |
// this should be implemented as a simple call to |
|
407 |
// accept(ref, parser, cf) |
|
408 |
public abstract boolean tryAsyncReceive(ByteBuffer buffer); |
|
409 |
public abstract void onReadError(Throwable t); |
|
410 |
// handle a byte buffer received from upstream. |
|
411 |
// this method should set the value of Http1Response.buffer |
|
412 |
// to ref.get() before beginning parsing. |
|
413 |
abstract void handle(ByteBuffer buf, T parser, |
|
414 |
CompletableFuture<State> cf); |
|
415 |
// resets this objects state so that it can be reused later on |
|
416 |
// typically puts the reference to parser and completion to null |
|
417 |
abstract void reset(); |
|
418 |
||
419 |
// accepts a byte buffer received from upstream |
|
420 |
// returns true if the buffer is fully parsed and more data can |
|
421 |
// be accepted, false otherwise. |
|
422 |
final boolean accept(ByteBuffer buf, T parser, |
|
423 |
CompletableFuture<State> cf) { |
|
424 |
if (cf == null || parser == null || cf.isDone()) return false; |
|
425 |
handle(buf, parser, cf); |
|
426 |
return !cf.isDone(); |
|
427 |
} |
|
428 |
public abstract void onSubscribe(AbstractSubscription s); |
|
429 |
public abstract AbstractSubscription subscription(); |
|
430 |
||
42460
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
431 |
} |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
432 |
|
48083 | 433 |
// Invoked with each new ByteBuffer when reading headers... |
434 |
final class HeadersReader extends Receiver<Http1HeaderParser> { |
|
435 |
final Consumer<State> onComplete; |
|
436 |
volatile Http1HeaderParser parser; |
|
437 |
volatile CompletableFuture<State> cf; |
|
438 |
volatile long count; // bytes parsed (for debug) |
|
439 |
volatile AbstractSubscription subscription; |
|
440 |
||
441 |
HeadersReader(Consumer<State> onComplete) { |
|
442 |
this.onComplete = onComplete; |
|
443 |
} |
|
444 |
||
445 |
@Override |
|
446 |
public AbstractSubscription subscription() { |
|
447 |
return subscription; |
|
448 |
} |
|
449 |
||
450 |
@Override |
|
451 |
public void onSubscribe(AbstractSubscription s) { |
|
452 |
this.subscription = s; |
|
453 |
s.request(1); |
|
454 |
} |
|
42460
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
455 |
|
48083 | 456 |
@Override |
457 |
void reset() { |
|
458 |
cf = null; |
|
459 |
parser = null; |
|
460 |
count = 0; |
|
461 |
subscription = null; |
|
462 |
} |
|
463 |
||
464 |
// Revisit: do we need to support restarting? |
|
465 |
@Override |
|
466 |
final void start(Http1HeaderParser hp) { |
|
467 |
count = 0; |
|
468 |
cf = new MinimalFuture<>(); |
|
469 |
parser = hp; |
|
470 |
} |
|
471 |
||
472 |
@Override |
|
473 |
CompletableFuture<State> completion() { |
|
474 |
return cf; |
|
475 |
} |
|
476 |
||
477 |
@Override |
|
478 |
public final boolean tryAsyncReceive(ByteBuffer ref) { |
|
479 |
boolean hasDemand = subscription.demand().tryDecrement(); |
|
480 |
assert hasDemand; |
|
481 |
boolean needsMore = accept(ref, parser, cf); |
|
482 |
if (needsMore) subscription.request(1); |
|
483 |
return needsMore; |
|
484 |
} |
|
485 |
||
486 |
@Override |
|
487 |
public final void onReadError(Throwable t) { |
|
488 |
Http1Response.this.onReadError(t); |
|
489 |
} |
|
42460
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
490 |
|
48083 | 491 |
@Override |
492 |
final void handle(ByteBuffer b, |
|
493 |
Http1HeaderParser parser, |
|
494 |
CompletableFuture<State> cf) { |
|
495 |
assert cf != null : "parsing not started"; |
|
496 |
assert parser != null : "no parser"; |
|
497 |
try { |
|
498 |
count += b.remaining(); |
|
499 |
debug.log(Level.DEBUG, () -> "Sending " + b.remaining() |
|
500 |
+ "/" + b.capacity() + " bytes to header parser"); |
|
501 |
if (parser.parse(b)) { |
|
502 |
count -= b.remaining(); |
|
503 |
debug.log(Level.DEBUG, () -> |
|
504 |
"Parsing headers completed. bytes=" + count); |
|
505 |
onComplete.accept(State.READING_HEADERS); |
|
506 |
cf.complete(State.READING_HEADERS); |
|
42460
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
507 |
} |
48083 | 508 |
} catch (Throwable t) { |
509 |
debug.log(Level.DEBUG, |
|
510 |
() -> "Header parser failed to handle buffer: " + t); |
|
511 |
cf.completeExceptionally(t); |
|
512 |
} |
|
513 |
} |
|
514 |
} |
|
515 |
||
516 |
// Invoked with each new ByteBuffer when reading bodies... |
|
517 |
final class BodyReader extends Receiver<BodyParser> { |
|
518 |
final Consumer<State> onComplete; |
|
519 |
volatile BodyParser parser; |
|
520 |
volatile CompletableFuture<State> cf; |
|
521 |
volatile AbstractSubscription subscription; |
|
522 |
BodyReader(Consumer<State> onComplete) { |
|
523 |
this.onComplete = onComplete; |
|
524 |
} |
|
525 |
||
526 |
@Override |
|
527 |
void reset() { |
|
528 |
parser = null; |
|
529 |
cf = null; |
|
530 |
subscription = null; |
|
531 |
} |
|
532 |
||
533 |
// Revisit: do we need to support restarting? |
|
534 |
@Override |
|
535 |
final void start(BodyParser parser) { |
|
536 |
cf = new MinimalFuture<>(); |
|
537 |
this.parser = parser; |
|
538 |
} |
|
539 |
||
540 |
@Override |
|
541 |
CompletableFuture<State> completion() { |
|
542 |
return cf; |
|
543 |
} |
|
544 |
||
545 |
@Override |
|
546 |
public final boolean tryAsyncReceive(ByteBuffer b) { |
|
547 |
return accept(b, parser, cf); |
|
548 |
} |
|
549 |
||
550 |
@Override |
|
551 |
public final void onReadError(Throwable t) { |
|
552 |
Http1Response.this.onReadError(t); |
|
553 |
} |
|
554 |
||
555 |
@Override |
|
556 |
public AbstractSubscription subscription() { |
|
557 |
return subscription; |
|
558 |
} |
|
559 |
||
560 |
@Override |
|
561 |
public void onSubscribe(AbstractSubscription s) { |
|
562 |
this.subscription = s; |
|
563 |
parser.onSubscribe(s); |
|
564 |
} |
|
565 |
||
566 |
@Override |
|
567 |
final void handle(ByteBuffer b, |
|
568 |
BodyParser parser, |
|
569 |
CompletableFuture<State> cf) { |
|
570 |
assert cf != null : "parsing not started"; |
|
571 |
assert parser != null : "no parser"; |
|
572 |
try { |
|
573 |
debug.log(Level.DEBUG, () -> "Sending " + b.remaining() |
|
574 |
+ "/" + b.capacity() + " bytes to body parser"); |
|
575 |
parser.accept(b); |
|
576 |
} catch (Throwable t) { |
|
577 |
debug.log(Level.DEBUG, |
|
578 |
() -> "Body parser failed to handle buffer: " + t); |
|
579 |
if (!cf.isDone()) { |
|
580 |
cf.completeExceptionally(t); |
|
42460
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
581 |
} |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
582 |
} |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
583 |
} |
48083 | 584 |
|
585 |
final void onComplete(Throwable closedExceptionally) { |
|
586 |
if (cf.isDone()) return; |
|
587 |
if (closedExceptionally != null) { |
|
588 |
cf.completeExceptionally(closedExceptionally); |
|
589 |
} else { |
|
590 |
onComplete.accept(State.READING_BODY); |
|
591 |
cf.complete(State.READING_BODY); |
|
592 |
} |
|
593 |
} |
|
594 |
||
595 |
@Override |
|
596 |
public String toString() { |
|
597 |
return super.toString() + "/parser=" + String.valueOf(parser); |
|
598 |
} |
|
599 |
||
42460
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
600 |
} |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
601 |
} |