author | dfuchs |
Wed, 28 Feb 2018 15:48:46 +0000 | |
branch | http-client-branch |
changeset 56209 | 43d5ad612710 |
parent 56204 | e5d0c20217a3 |
child 56410 | 1b37529eaf3a |
permissions | -rw-r--r-- |
42460
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
1 |
/* |
56040 | 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 |
|
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
28 |
import java.io.IOException; |
48083 | 29 |
import java.lang.System.Logger.Level; |
42460
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
30 |
import java.net.InetSocketAddress; |
56089
42208b2f224e
http-client-branch: move to standard package and module name
chegar
parents:
56079
diff
changeset
|
31 |
import java.net.http.HttpResponse.BodyHandler; |
42208b2f224e
http-client-branch: move to standard package and module name
chegar
parents:
56079
diff
changeset
|
32 |
import java.net.http.HttpResponse.BodySubscriber; |
42460
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
33 |
import java.nio.ByteBuffer; |
48083 | 34 |
import java.util.Objects; |
42460
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
35 |
import java.util.concurrent.CompletableFuture; |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
36 |
import java.util.LinkedList; |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
37 |
import java.util.List; |
48083 | 38 |
import java.util.concurrent.ConcurrentLinkedDeque; |
42460
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
39 |
import java.util.concurrent.Executor; |
48083 | 40 |
import java.util.concurrent.Flow; |
56092
fd85b2bf2b0d
http-client-branch: move implementation to jdk.internal.net.http
chegar
parents:
56089
diff
changeset
|
41 |
import jdk.internal.net.http.common.Demand; |
fd85b2bf2b0d
http-client-branch: move implementation to jdk.internal.net.http
chegar
parents:
56089
diff
changeset
|
42 |
import jdk.internal.net.http.common.Log; |
fd85b2bf2b0d
http-client-branch: move implementation to jdk.internal.net.http
chegar
parents:
56089
diff
changeset
|
43 |
import jdk.internal.net.http.common.FlowTube; |
fd85b2bf2b0d
http-client-branch: move implementation to jdk.internal.net.http
chegar
parents:
56089
diff
changeset
|
44 |
import jdk.internal.net.http.common.SequentialScheduler; |
fd85b2bf2b0d
http-client-branch: move implementation to jdk.internal.net.http
chegar
parents:
56089
diff
changeset
|
45 |
import jdk.internal.net.http.common.MinimalFuture; |
fd85b2bf2b0d
http-client-branch: move implementation to jdk.internal.net.http
chegar
parents:
56089
diff
changeset
|
46 |
import jdk.internal.net.http.common.Utils; |
56089
42208b2f224e
http-client-branch: move to standard package and module name
chegar
parents:
56079
diff
changeset
|
47 |
import static java.net.http.HttpClient.Version.HTTP_1_1; |
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 |
/** |
48083 | 50 |
* Encapsulates one HTTP/1.1 request/response exchange. |
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 |
class Http1Exchange<T> extends ExchangeImpl<T> { |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
53 |
|
48083 | 54 |
static final boolean DEBUG = Utils.DEBUG; // Revisit: temporary dev flag. |
55 |
final System.Logger debug = Utils.getDebugLogger(this::dbgString, DEBUG); |
|
56 |
final HttpRequestImpl request; // main request |
|
42460
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
57 |
final Http1Request requestAction; |
44639
5c2838d882a5
8178147: Race conditions in timeout handling code in http/2 incubator client
dfuchs
parents:
43999
diff
changeset
|
58 |
private volatile Http1Response<T> response; |
42460
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
59 |
final HttpConnection connection; |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
60 |
final HttpClientImpl client; |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
61 |
final Executor executor; |
48083 | 62 |
private final Http1AsyncReceiver asyncReceiver; |
63 |
||
64 |
/** Records a possible cancellation raised before any operation |
|
65 |
* has been initiated, or an error received while sending the request. */ |
|
66 |
private Throwable failed; |
|
67 |
private final List<CompletableFuture<?>> operations; // used for cancel |
|
68 |
||
69 |
/** Must be held when operating on any internal state or data. */ |
|
70 |
private final Object lock = new Object(); |
|
71 |
||
72 |
/** Holds the outgoing data, either the headers or a request body part. Or |
|
73 |
* an error from the request body publisher. At most there can be ~2 pieces |
|
74 |
* of outgoing data ( onComplete|onError can be invoked without demand ).*/ |
|
75 |
final ConcurrentLinkedDeque<DataPair> outgoing = new ConcurrentLinkedDeque<>(); |
|
76 |
||
77 |
/** The write publisher, responsible for writing the complete request ( both |
|
78 |
* headers and body ( if any ). */ |
|
79 |
private final Http1Publisher writePublisher = new Http1Publisher(); |
|
80 |
||
81 |
/** Completed when the header have been published, or there is an error */ |
|
56041
b4b5e09ef3cc
http-client-branch: make it possible to supply proxy-authorization headers
dfuchs
parents:
56040
diff
changeset
|
82 |
private final CompletableFuture<ExchangeImpl<T>> headersSentCF = new MinimalFuture<>(); |
48083 | 83 |
/** Completed when the body has been published, or there is an error */ |
56041
b4b5e09ef3cc
http-client-branch: make it possible to supply proxy-authorization headers
dfuchs
parents:
56040
diff
changeset
|
84 |
private final CompletableFuture<ExchangeImpl<T>> bodySentCF = new MinimalFuture<>(); |
48083 | 85 |
|
86 |
/** The subscriber to the request's body published. Maybe null. */ |
|
87 |
private volatile Http1BodySubscriber bodySubscriber; |
|
88 |
||
89 |
enum State { INITIAL, |
|
90 |
HEADERS, |
|
91 |
BODY, |
|
92 |
ERROR, // terminal state |
|
93 |
COMPLETING, |
|
94 |
COMPLETED } // terminal state |
|
95 |
||
96 |
private State state = State.INITIAL; |
|
97 |
||
98 |
/** A carrier for either data or an error. Used to carry data, and communicate |
|
99 |
* errors from the request ( both headers and body ) to the exchange. */ |
|
100 |
static class DataPair { |
|
101 |
Throwable throwable; |
|
102 |
List<ByteBuffer> data; |
|
103 |
DataPair(List<ByteBuffer> data, Throwable throwable){ |
|
104 |
this.data = data; |
|
105 |
this.throwable = throwable; |
|
106 |
} |
|
107 |
@Override |
|
108 |
public String toString() { |
|
109 |
return "DataPair [data=" + data + ", throwable=" + throwable + "]"; |
|
110 |
} |
|
111 |
} |
|
112 |
||
113 |
/** An abstract supertype for HTTP/1.1 body subscribers. There are two |
|
114 |
* concrete implementations: {@link Http1Request.StreamSubscriber}, and |
|
115 |
* {@link Http1Request.FixedContentSubscriber}, for receiving chunked and |
|
116 |
* fixed length bodies, respectively. */ |
|
117 |
static abstract class Http1BodySubscriber implements Flow.Subscriber<ByteBuffer> { |
|
56204
e5d0c20217a3
http-client-branch: Fix a race condition between Http1BodySubscriber::request and BodyPublisher::subscribe in Http1BodySubscriber and Http1Exchange.
dfuchs
parents:
56165
diff
changeset
|
118 |
final MinimalFuture<Flow.Subscription> whenSubscribed = new MinimalFuture<>(); |
e5d0c20217a3
http-client-branch: Fix a race condition between Http1BodySubscriber::request and BodyPublisher::subscribe in Http1BodySubscriber and Http1Exchange.
dfuchs
parents:
56165
diff
changeset
|
119 |
private volatile Flow.Subscription subscription; |
e5d0c20217a3
http-client-branch: Fix a race condition between Http1BodySubscriber::request and BodyPublisher::subscribe in Http1BodySubscriber and Http1Exchange.
dfuchs
parents:
56165
diff
changeset
|
120 |
volatile boolean complete; |
56209
43d5ad612710
http-client-branch: more ThrowingPublishers tests and fixes
dfuchs
parents:
56204
diff
changeset
|
121 |
private final System.Logger debug; |
43d5ad612710
http-client-branch: more ThrowingPublishers tests and fixes
dfuchs
parents:
56204
diff
changeset
|
122 |
Http1BodySubscriber(System.Logger debug) { |
43d5ad612710
http-client-branch: more ThrowingPublishers tests and fixes
dfuchs
parents:
56204
diff
changeset
|
123 |
assert debug != null; |
43d5ad612710
http-client-branch: more ThrowingPublishers tests and fixes
dfuchs
parents:
56204
diff
changeset
|
124 |
this.debug = debug; |
43d5ad612710
http-client-branch: more ThrowingPublishers tests and fixes
dfuchs
parents:
56204
diff
changeset
|
125 |
} |
48083 | 126 |
|
127 |
/** Final sentinel in the stream of request body. */ |
|
128 |
static final List<ByteBuffer> COMPLETED = List.of(ByteBuffer.allocate(0)); |
|
129 |
||
56204
e5d0c20217a3
http-client-branch: Fix a race condition between Http1BodySubscriber::request and BodyPublisher::subscribe in Http1BodySubscriber and Http1Exchange.
dfuchs
parents:
56165
diff
changeset
|
130 |
final void request(long n) { |
56209
43d5ad612710
http-client-branch: more ThrowingPublishers tests and fixes
dfuchs
parents:
56204
diff
changeset
|
131 |
debug.log(Level.DEBUG, () -> |
48083 | 132 |
"Http1BodySubscriber requesting " + n + ", from " + subscription); |
133 |
subscription.request(n); |
|
134 |
} |
|
135 |
||
56204
e5d0c20217a3
http-client-branch: Fix a race condition between Http1BodySubscriber::request and BodyPublisher::subscribe in Http1BodySubscriber and Http1Exchange.
dfuchs
parents:
56165
diff
changeset
|
136 |
final boolean isSubscribed() { |
e5d0c20217a3
http-client-branch: Fix a race condition between Http1BodySubscriber::request and BodyPublisher::subscribe in Http1BodySubscriber and Http1Exchange.
dfuchs
parents:
56165
diff
changeset
|
137 |
return subscription != null; |
e5d0c20217a3
http-client-branch: Fix a race condition between Http1BodySubscriber::request and BodyPublisher::subscribe in Http1BodySubscriber and Http1Exchange.
dfuchs
parents:
56165
diff
changeset
|
138 |
} |
e5d0c20217a3
http-client-branch: Fix a race condition between Http1BodySubscriber::request and BodyPublisher::subscribe in Http1BodySubscriber and Http1Exchange.
dfuchs
parents:
56165
diff
changeset
|
139 |
|
e5d0c20217a3
http-client-branch: Fix a race condition between Http1BodySubscriber::request and BodyPublisher::subscribe in Http1BodySubscriber and Http1Exchange.
dfuchs
parents:
56165
diff
changeset
|
140 |
final void setSubscription(Flow.Subscription subscription) { |
e5d0c20217a3
http-client-branch: Fix a race condition between Http1BodySubscriber::request and BodyPublisher::subscribe in Http1BodySubscriber and Http1Exchange.
dfuchs
parents:
56165
diff
changeset
|
141 |
this.subscription = subscription; |
e5d0c20217a3
http-client-branch: Fix a race condition between Http1BodySubscriber::request and BodyPublisher::subscribe in Http1BodySubscriber and Http1Exchange.
dfuchs
parents:
56165
diff
changeset
|
142 |
whenSubscribed.complete(subscription); |
e5d0c20217a3
http-client-branch: Fix a race condition between Http1BodySubscriber::request and BodyPublisher::subscribe in Http1BodySubscriber and Http1Exchange.
dfuchs
parents:
56165
diff
changeset
|
143 |
} |
e5d0c20217a3
http-client-branch: Fix a race condition between Http1BodySubscriber::request and BodyPublisher::subscribe in Http1BodySubscriber and Http1Exchange.
dfuchs
parents:
56165
diff
changeset
|
144 |
|
e5d0c20217a3
http-client-branch: Fix a race condition between Http1BodySubscriber::request and BodyPublisher::subscribe in Http1BodySubscriber and Http1Exchange.
dfuchs
parents:
56165
diff
changeset
|
145 |
final void cancelSubscription() { |
56209
43d5ad612710
http-client-branch: more ThrowingPublishers tests and fixes
dfuchs
parents:
56204
diff
changeset
|
146 |
try { |
43d5ad612710
http-client-branch: more ThrowingPublishers tests and fixes
dfuchs
parents:
56204
diff
changeset
|
147 |
subscription.cancel(); |
43d5ad612710
http-client-branch: more ThrowingPublishers tests and fixes
dfuchs
parents:
56204
diff
changeset
|
148 |
} catch(Throwable t) { |
43d5ad612710
http-client-branch: more ThrowingPublishers tests and fixes
dfuchs
parents:
56204
diff
changeset
|
149 |
String msg = "Ignoring exception raised when canceling BodyPublisher subscription"; |
43d5ad612710
http-client-branch: more ThrowingPublishers tests and fixes
dfuchs
parents:
56204
diff
changeset
|
150 |
debug.log(Level.DEBUG, "%s: %s", msg, t); |
43d5ad612710
http-client-branch: more ThrowingPublishers tests and fixes
dfuchs
parents:
56204
diff
changeset
|
151 |
Log.logError("{0}: {1}", msg, (Object)t); |
43d5ad612710
http-client-branch: more ThrowingPublishers tests and fixes
dfuchs
parents:
56204
diff
changeset
|
152 |
} |
56204
e5d0c20217a3
http-client-branch: Fix a race condition between Http1BodySubscriber::request and BodyPublisher::subscribe in Http1BodySubscriber and Http1Exchange.
dfuchs
parents:
56165
diff
changeset
|
153 |
} |
e5d0c20217a3
http-client-branch: Fix a race condition between Http1BodySubscriber::request and BodyPublisher::subscribe in Http1BodySubscriber and Http1Exchange.
dfuchs
parents:
56165
diff
changeset
|
154 |
|
56209
43d5ad612710
http-client-branch: more ThrowingPublishers tests and fixes
dfuchs
parents:
56204
diff
changeset
|
155 |
static Http1BodySubscriber completeSubscriber(System.Logger debug) { |
43d5ad612710
http-client-branch: more ThrowingPublishers tests and fixes
dfuchs
parents:
56204
diff
changeset
|
156 |
return new Http1BodySubscriber(debug) { |
48083 | 157 |
@Override public void onSubscribe(Flow.Subscription subscription) { error(); } |
158 |
@Override public void onNext(ByteBuffer item) { error(); } |
|
159 |
@Override public void onError(Throwable throwable) { error(); } |
|
160 |
@Override public void onComplete() { error(); } |
|
161 |
private void error() { |
|
162 |
throw new InternalError("should not reach here"); |
|
163 |
} |
|
164 |
}; |
|
165 |
} |
|
166 |
} |
|
42460
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
167 |
|
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
168 |
@Override |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
169 |
public String toString() { |
48083 | 170 |
return "HTTP/1.1 " + request.toString(); |
42460
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
171 |
} |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
172 |
|
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
173 |
HttpRequestImpl request() { |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
174 |
return request; |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
175 |
} |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
176 |
|
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
177 |
Http1Exchange(Exchange<T> exchange, HttpConnection connection) |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
178 |
throws IOException |
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 |
super(exchange); |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
181 |
this.request = exchange.request(); |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
182 |
this.client = exchange.client(); |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
183 |
this.executor = exchange.executor(); |
44639
5c2838d882a5
8178147: Race conditions in timeout handling code in http/2 incubator client
dfuchs
parents:
43999
diff
changeset
|
184 |
this.operations = new LinkedList<>(); |
48083 | 185 |
operations.add(headersSentCF); |
186 |
operations.add(bodySentCF); |
|
42460
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
187 |
if (connection != null) { |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
188 |
this.connection = connection; |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
189 |
} else { |
48083 | 190 |
InetSocketAddress addr = request.getAddress(); |
191 |
this.connection = HttpConnection.getConnection(addr, client, request, HTTP_1_1); |
|
42460
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
192 |
} |
48083 | 193 |
this.requestAction = new Http1Request(request, this); |
194 |
this.asyncReceiver = new Http1AsyncReceiver(executor, this); |
|
195 |
asyncReceiver.subscribe(new InitialErrorReceiver()); |
|
42460
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
196 |
} |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
197 |
|
48083 | 198 |
/** An initial receiver that handles no data, but cancels the request if |
199 |
* it receives an error. Will be replaced when reading response body. */ |
|
200 |
final class InitialErrorReceiver implements Http1AsyncReceiver.Http1AsyncDelegate { |
|
201 |
volatile AbstractSubscription s; |
|
202 |
@Override |
|
203 |
public boolean tryAsyncReceive(ByteBuffer ref) { |
|
204 |
return false; // no data has been processed, leave it in the queue |
|
205 |
} |
|
42460
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
206 |
|
48083 | 207 |
@Override |
208 |
public void onReadError(Throwable ex) { |
|
209 |
cancelImpl(ex); |
|
210 |
} |
|
211 |
||
212 |
@Override |
|
213 |
public void onSubscribe(AbstractSubscription s) { |
|
214 |
this.s = s; |
|
215 |
} |
|
216 |
||
217 |
public AbstractSubscription subscription() { |
|
218 |
return s; |
|
219 |
} |
|
220 |
} |
|
221 |
||
222 |
@Override |
|
42460
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
223 |
HttpConnection connection() { |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
224 |
return connection; |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
225 |
} |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
226 |
|
48083 | 227 |
private void connectFlows(HttpConnection connection) { |
228 |
FlowTube tube = connection.getConnectionFlow(); |
|
229 |
debug.log(Level.DEBUG, "%s connecting flows", tube); |
|
230 |
||
231 |
// Connect the flow to our Http1TubeSubscriber: |
|
232 |
// asyncReceiver.subscriber(). |
|
233 |
tube.connectFlows(writePublisher, |
|
234 |
asyncReceiver.subscriber()); |
|
235 |
} |
|
42460
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 |
@Override |
48083 | 238 |
CompletableFuture<ExchangeImpl<T>> sendHeadersAsync() { |
239 |
// create the response before sending the request headers, so that |
|
240 |
// the response can set the appropriate receivers. |
|
241 |
debug.log(Level.DEBUG, "Sending headers only"); |
|
242 |
if (response == null) { |
|
243 |
response = new Http1Response<>(connection, this, asyncReceiver); |
|
42460
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
244 |
} |
48083 | 245 |
|
246 |
debug.log(Level.DEBUG, "response created in advance"); |
|
247 |
// If the first attempt to read something triggers EOF, or |
|
248 |
// IOException("channel reset by peer"), we're going to retry. |
|
249 |
// Instruct the asyncReceiver to throw ConnectionExpiredException |
|
250 |
// to force a retry. |
|
251 |
asyncReceiver.setRetryOnError(true); |
|
252 |
||
253 |
CompletableFuture<Void> connectCF; |
|
254 |
if (!connection.connected()) { |
|
255 |
debug.log(Level.DEBUG, "initiating connect async"); |
|
256 |
connectCF = connection.connectAsync(); |
|
257 |
synchronized (lock) { |
|
258 |
operations.add(connectCF); |
|
259 |
} |
|
260 |
} else { |
|
261 |
connectCF = new MinimalFuture<>(); |
|
262 |
connectCF.complete(null); |
|
263 |
} |
|
264 |
||
265 |
return connectCF |
|
266 |
.thenCompose(unused -> { |
|
267 |
CompletableFuture<Void> cf = new MinimalFuture<>(); |
|
268 |
try { |
|
269 |
connectFlows(connection); |
|
270 |
||
271 |
debug.log(Level.DEBUG, "requestAction.headers"); |
|
272 |
List<ByteBuffer> data = requestAction.headers(); |
|
273 |
synchronized (lock) { |
|
274 |
state = State.HEADERS; |
|
275 |
} |
|
276 |
debug.log(Level.DEBUG, "setting outgoing with headers"); |
|
277 |
assert outgoing.isEmpty() : "Unexpected outgoing:" + outgoing; |
|
278 |
appendToOutgoing(data); |
|
279 |
cf.complete(null); |
|
280 |
return cf; |
|
281 |
} catch (Throwable t) { |
|
282 |
debug.log(Level.DEBUG, "Failed to send headers: %s", t); |
|
283 |
connection.close(); |
|
284 |
cf.completeExceptionally(t); |
|
285 |
return cf; |
|
286 |
} }) |
|
287 |
.thenCompose(unused -> headersSentCF); |
|
42460
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
288 |
} |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
289 |
|
48083 | 290 |
@Override |
291 |
CompletableFuture<ExchangeImpl<T>> sendBodyAsync() { |
|
292 |
assert headersSentCF.isDone(); |
|
293 |
try { |
|
294 |
bodySubscriber = requestAction.continueRequest(); |
|
295 |
if (bodySubscriber == null) { |
|
56209
43d5ad612710
http-client-branch: more ThrowingPublishers tests and fixes
dfuchs
parents:
56204
diff
changeset
|
296 |
bodySubscriber = Http1BodySubscriber.completeSubscriber(debug); |
48083 | 297 |
appendToOutgoing(Http1BodySubscriber.COMPLETED); |
298 |
} else { |
|
56204
e5d0c20217a3
http-client-branch: Fix a race condition between Http1BodySubscriber::request and BodyPublisher::subscribe in Http1BodySubscriber and Http1Exchange.
dfuchs
parents:
56165
diff
changeset
|
299 |
// start |
56209
43d5ad612710
http-client-branch: more ThrowingPublishers tests and fixes
dfuchs
parents:
56204
diff
changeset
|
300 |
bodySubscriber.whenSubscribed |
43d5ad612710
http-client-branch: more ThrowingPublishers tests and fixes
dfuchs
parents:
56204
diff
changeset
|
301 |
.thenAccept((s) -> requestMoreBody()); |
48083 | 302 |
} |
303 |
} catch (Throwable t) { |
|
56209
43d5ad612710
http-client-branch: more ThrowingPublishers tests and fixes
dfuchs
parents:
56204
diff
changeset
|
304 |
cancelImpl(t); |
48083 | 305 |
bodySentCF.completeExceptionally(t); |
306 |
} |
|
307 |
return bodySentCF; |
|
42460
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
308 |
} |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
309 |
|
48083 | 310 |
@Override |
311 |
CompletableFuture<Response> getResponseAsync(Executor executor) { |
|
312 |
CompletableFuture<Response> cf = response.readHeadersAsync(executor); |
|
313 |
Throwable cause; |
|
314 |
synchronized (lock) { |
|
315 |
operations.add(cf); |
|
316 |
cause = failed; |
|
317 |
failed = null; |
|
318 |
} |
|
319 |
||
320 |
if (cause != null) { |
|
321 |
Log.logTrace("Http1Exchange: request [{0}/timeout={1}ms]" |
|
322 |
+ "\n\tCompleting exceptionally with {2}\n", |
|
323 |
request.uri(), |
|
324 |
request.timeout().isPresent() ? |
|
325 |
// calling duration.toMillis() can throw an exception. |
|
326 |
// this is just debugging, we don't care if it overflows. |
|
327 |
(request.timeout().get().getSeconds() * 1000 |
|
328 |
+ request.timeout().get().getNano() / 1000000) : -1, |
|
329 |
cause); |
|
330 |
boolean acknowledged = cf.completeExceptionally(cause); |
|
331 |
debug.log(Level.DEBUG, |
|
332 |
() -> acknowledged |
|
333 |
? ("completed response with " + cause) |
|
334 |
: ("response already completed, ignoring " + cause)); |
|
335 |
} |
|
336 |
return cf; |
|
42460
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
337 |
} |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
338 |
|
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
339 |
@Override |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
340 |
CompletableFuture<T> readBodyAsync(BodyHandler<T> handler, |
43999
4cc44dd9f14f
8164625: Pooled HttpConnection should be removed during close
prappo
parents:
43984
diff
changeset
|
341 |
boolean returnConnectionToPool, |
42460
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
342 |
Executor executor) |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
343 |
{ |
48083 | 344 |
BodySubscriber<T> bs = handler.apply(response.responseCode(), |
345 |
response.responseHeaders()); |
|
346 |
CompletableFuture<T> bodyCF = response.readBody(bs, |
|
43999
4cc44dd9f14f
8164625: Pooled HttpConnection should be removed during close
prappo
parents:
43984
diff
changeset
|
347 |
returnConnectionToPool, |
42460
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
348 |
executor); |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
349 |
return bodyCF; |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
350 |
} |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
351 |
|
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
352 |
@Override |
48083 | 353 |
CompletableFuture<Void> ignoreBody() { |
354 |
return response.ignoreBody(executor); |
|
355 |
} |
|
356 |
||
357 |
ByteBuffer drainLeftOverBytes() { |
|
358 |
synchronized (lock) { |
|
359 |
asyncReceiver.stop(); |
|
360 |
return asyncReceiver.drain(Utils.EMPTY_BYTEBUFFER); |
|
42460
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
361 |
} |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
362 |
} |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
363 |
|
48083 | 364 |
void released() { |
365 |
Http1Response<T> resp = this.response; |
|
366 |
if (resp != null) resp.completed(); |
|
367 |
asyncReceiver.clear(); |
|
42460
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
368 |
} |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
369 |
|
48083 | 370 |
void completed() { |
371 |
Http1Response<T> resp = this.response; |
|
372 |
if (resp != null) resp.completed(); |
|
42460
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
373 |
} |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
374 |
|
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
375 |
/** |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
376 |
* Cancel checks to see if request and responseAsync finished already. |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
377 |
* If not it closes the connection and completes all pending operations |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
378 |
*/ |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
379 |
@Override |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
380 |
void cancel() { |
48083 | 381 |
cancelImpl(new IOException("Request cancelled")); |
42460
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
382 |
} |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
383 |
|
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
384 |
/** |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
385 |
* Cancel checks to see if request and responseAsync finished already. |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
386 |
* If not it closes the connection and completes all pending operations |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
387 |
*/ |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
388 |
@Override |
48083 | 389 |
void cancel(IOException cause) { |
390 |
cancelImpl(cause); |
|
391 |
} |
|
392 |
||
393 |
private void cancelImpl(Throwable cause) { |
|
394 |
LinkedList<CompletableFuture<?>> toComplete = null; |
|
42460
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
395 |
int count = 0; |
56209
43d5ad612710
http-client-branch: more ThrowingPublishers tests and fixes
dfuchs
parents:
56204
diff
changeset
|
396 |
Throwable error; |
48083 | 397 |
synchronized (lock) { |
56209
43d5ad612710
http-client-branch: more ThrowingPublishers tests and fixes
dfuchs
parents:
56204
diff
changeset
|
398 |
if ((error = failed) == null) { |
43d5ad612710
http-client-branch: more ThrowingPublishers tests and fixes
dfuchs
parents:
56204
diff
changeset
|
399 |
failed = error = cause; |
43d5ad612710
http-client-branch: more ThrowingPublishers tests and fixes
dfuchs
parents:
56204
diff
changeset
|
400 |
} |
48083 | 401 |
if (requestAction != null && requestAction.finished() |
402 |
&& response != null && response.finished()) { |
|
403 |
return; |
|
404 |
} |
|
405 |
connection.close(); // TODO: ensure non-blocking if holding the lock |
|
406 |
writePublisher.writeScheduler.stop(); |
|
407 |
if (operations.isEmpty()) { |
|
408 |
Log.logTrace("Http1Exchange: request [{0}/timeout={1}ms] no pending operation." |
|
409 |
+ "\n\tCan''t cancel yet with {2}", |
|
410 |
request.uri(), |
|
411 |
request.timeout().isPresent() ? |
|
412 |
// calling duration.toMillis() can throw an exception. |
|
413 |
// this is just debugging, we don't care if it overflows. |
|
414 |
(request.timeout().get().getSeconds() * 1000 |
|
415 |
+ request.timeout().get().getNano() / 1000000) : -1, |
|
416 |
cause); |
|
417 |
} else { |
|
418 |
for (CompletableFuture<?> cf : operations) { |
|
419 |
if (!cf.isDone()) { |
|
420 |
if (toComplete == null) toComplete = new LinkedList<>(); |
|
421 |
toComplete.add(cf); |
|
422 |
count++; |
|
423 |
} |
|
424 |
} |
|
425 |
operations.clear(); |
|
44639
5c2838d882a5
8178147: Race conditions in timeout handling code in http/2 incubator client
dfuchs
parents:
43999
diff
changeset
|
426 |
} |
42460
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
427 |
} |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
428 |
Log.logError("Http1Exchange.cancel: count=" + count); |
48083 | 429 |
if (toComplete != null) { |
430 |
// We might be in the selector thread in case of timeout, when |
|
431 |
// the SelectorManager calls purgeTimeoutsAndReturnNextDeadline() |
|
432 |
// There may or may not be other places that reach here |
|
433 |
// from the SelectorManager thread, so just make sure we |
|
434 |
// don't complete any CF from within the selector manager |
|
435 |
// thread. |
|
436 |
Executor exec = client.isSelectorThread() |
|
437 |
? executor |
|
438 |
: this::runInline; |
|
56209
43d5ad612710
http-client-branch: more ThrowingPublishers tests and fixes
dfuchs
parents:
56204
diff
changeset
|
439 |
Throwable x = error; |
48083 | 440 |
while (!toComplete.isEmpty()) { |
441 |
CompletableFuture<?> cf = toComplete.poll(); |
|
442 |
exec.execute(() -> { |
|
56209
43d5ad612710
http-client-branch: more ThrowingPublishers tests and fixes
dfuchs
parents:
56204
diff
changeset
|
443 |
if (cf.completeExceptionally(x)) { |
48083 | 444 |
debug.log(Level.DEBUG, "completed cf with %s", |
56209
43d5ad612710
http-client-branch: more ThrowingPublishers tests and fixes
dfuchs
parents:
56204
diff
changeset
|
445 |
(Object) x); |
48083 | 446 |
} |
447 |
}); |
|
448 |
} |
|
449 |
} |
|
450 |
} |
|
451 |
||
452 |
private void runInline(Runnable run) { |
|
453 |
assert !client.isSelectorThread(); |
|
454 |
run.run(); |
|
42460
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
455 |
} |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
456 |
|
48083 | 457 |
/** Returns true if this exchange was canceled. */ |
458 |
boolean isCanceled() { |
|
459 |
synchronized (lock) { |
|
460 |
return failed != null; |
|
461 |
} |
|
462 |
} |
|
463 |
||
464 |
/** Returns the cause for which this exchange was canceled, if available. */ |
|
465 |
Throwable getCancelCause() { |
|
466 |
synchronized (lock) { |
|
467 |
return failed; |
|
468 |
} |
|
469 |
} |
|
470 |
||
471 |
/** Convenience for {@link #appendToOutgoing(DataPair)}, with just a Throwable. */ |
|
472 |
void appendToOutgoing(Throwable throwable) { |
|
473 |
appendToOutgoing(new DataPair(null, throwable)); |
|
474 |
} |
|
475 |
||
476 |
/** Convenience for {@link #appendToOutgoing(DataPair)}, with just data. */ |
|
477 |
void appendToOutgoing(List<ByteBuffer> item) { |
|
478 |
appendToOutgoing(new DataPair(item, null)); |
|
479 |
} |
|
480 |
||
481 |
private void appendToOutgoing(DataPair dp) { |
|
482 |
debug.log(Level.DEBUG, "appending to outgoing " + dp); |
|
483 |
outgoing.add(dp); |
|
484 |
writePublisher.writeScheduler.runOrSchedule(); |
|
485 |
} |
|
486 |
||
487 |
/** Tells whether, or not, there is any outgoing data that can be published, |
|
488 |
* or if there is an error. */ |
|
489 |
private boolean hasOutgoing() { |
|
490 |
return !outgoing.isEmpty(); |
|
42460
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
491 |
} |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
492 |
|
56209
43d5ad612710
http-client-branch: more ThrowingPublishers tests and fixes
dfuchs
parents:
56204
diff
changeset
|
493 |
private void requestMoreBody() { |
43d5ad612710
http-client-branch: more ThrowingPublishers tests and fixes
dfuchs
parents:
56204
diff
changeset
|
494 |
try { |
43d5ad612710
http-client-branch: more ThrowingPublishers tests and fixes
dfuchs
parents:
56204
diff
changeset
|
495 |
debug.log(Level.DEBUG, "requesting more body from the subscriber"); |
43d5ad612710
http-client-branch: more ThrowingPublishers tests and fixes
dfuchs
parents:
56204
diff
changeset
|
496 |
bodySubscriber.request(1); |
43d5ad612710
http-client-branch: more ThrowingPublishers tests and fixes
dfuchs
parents:
56204
diff
changeset
|
497 |
} catch (Throwable t) { |
43d5ad612710
http-client-branch: more ThrowingPublishers tests and fixes
dfuchs
parents:
56204
diff
changeset
|
498 |
debug.log(Level.DEBUG, "Subscription::request failed", t); |
43d5ad612710
http-client-branch: more ThrowingPublishers tests and fixes
dfuchs
parents:
56204
diff
changeset
|
499 |
cancelImpl(t); |
43d5ad612710
http-client-branch: more ThrowingPublishers tests and fixes
dfuchs
parents:
56204
diff
changeset
|
500 |
bodySentCF.completeExceptionally(t); |
43d5ad612710
http-client-branch: more ThrowingPublishers tests and fixes
dfuchs
parents:
56204
diff
changeset
|
501 |
} |
43d5ad612710
http-client-branch: more ThrowingPublishers tests and fixes
dfuchs
parents:
56204
diff
changeset
|
502 |
} |
43d5ad612710
http-client-branch: more ThrowingPublishers tests and fixes
dfuchs
parents:
56204
diff
changeset
|
503 |
|
48083 | 504 |
// Invoked only by the publisher |
505 |
// ALL tasks should execute off the Selector-Manager thread |
|
506 |
/** Returns the next portion of the HTTP request, or the error. */ |
|
507 |
private DataPair getOutgoing() { |
|
508 |
final Executor exec = client.theExecutor(); |
|
509 |
final DataPair dp = outgoing.pollFirst(); |
|
510 |
||
511 |
if (dp == null) // publisher has not published anything yet |
|
512 |
return null; |
|
513 |
||
514 |
synchronized (lock) { |
|
515 |
if (dp.throwable != null) { |
|
516 |
state = State.ERROR; |
|
517 |
exec.execute(() -> { |
|
518 |
headersSentCF.completeExceptionally(dp.throwable); |
|
519 |
bodySentCF.completeExceptionally(dp.throwable); |
|
56165
8a6065d830b9
http-client-branch: Add a test for BodySubscribers throwing in getBody() or returning exceptionally completed CFs
dfuchs
parents:
56092
diff
changeset
|
520 |
connection.close(); |
48083 | 521 |
}); |
522 |
return dp; |
|
523 |
} |
|
524 |
||
525 |
switch (state) { |
|
526 |
case HEADERS: |
|
527 |
state = State.BODY; |
|
528 |
// completeAsync, since dependent tasks should run in another thread |
|
529 |
debug.log(Level.DEBUG, "initiating completion of headersSentCF"); |
|
530 |
headersSentCF.completeAsync(() -> this, exec); |
|
531 |
break; |
|
532 |
case BODY: |
|
533 |
if (dp.data == Http1BodySubscriber.COMPLETED) { |
|
534 |
state = State.COMPLETING; |
|
535 |
debug.log(Level.DEBUG, "initiating completion of bodySentCF"); |
|
536 |
bodySentCF.completeAsync(() -> this, exec); |
|
537 |
} else { |
|
56209
43d5ad612710
http-client-branch: more ThrowingPublishers tests and fixes
dfuchs
parents:
56204
diff
changeset
|
538 |
exec.execute(this::requestMoreBody); |
48083 | 539 |
} |
540 |
break; |
|
541 |
case INITIAL: |
|
542 |
case ERROR: |
|
543 |
case COMPLETING: |
|
544 |
case COMPLETED: |
|
545 |
default: |
|
546 |
assert false : "Unexpected state:" + state; |
|
547 |
} |
|
548 |
||
549 |
return dp; |
|
550 |
} |
|
551 |
} |
|
552 |
||
553 |
/** A Publisher of HTTP/1.1 headers and request body. */ |
|
554 |
final class Http1Publisher implements FlowTube.TubePublisher { |
|
555 |
||
556 |
final System.Logger debug = Utils.getDebugLogger(this::dbgString); |
|
557 |
volatile Flow.Subscriber<? super List<ByteBuffer>> subscriber; |
|
558 |
volatile boolean cancelled; |
|
559 |
final Http1WriteSubscription subscription = new Http1WriteSubscription(); |
|
560 |
final Demand demand = new Demand(); |
|
561 |
final SequentialScheduler writeScheduler = |
|
562 |
SequentialScheduler.synchronizedScheduler(new WriteTask()); |
|
563 |
||
564 |
@Override |
|
565 |
public void subscribe(Flow.Subscriber<? super List<ByteBuffer>> s) { |
|
566 |
assert state == State.INITIAL; |
|
567 |
Objects.requireNonNull(s); |
|
568 |
assert subscriber == null; |
|
569 |
||
570 |
subscriber = s; |
|
571 |
debug.log(Level.DEBUG, "got subscriber: %s", s); |
|
572 |
s.onSubscribe(subscription); |
|
44639
5c2838d882a5
8178147: Race conditions in timeout handling code in http/2 incubator client
dfuchs
parents:
43999
diff
changeset
|
573 |
} |
48083 | 574 |
|
575 |
volatile String dbgTag; |
|
576 |
String dbgString() { |
|
577 |
String tag = dbgTag; |
|
578 |
Object flow = connection.getConnectionFlow(); |
|
579 |
if (tag == null && flow != null) { |
|
580 |
dbgTag = tag = "Http1Publisher(" + flow + ")"; |
|
581 |
} else if (tag == null) { |
|
582 |
tag = "Http1Publisher(?)"; |
|
583 |
} |
|
584 |
return tag; |
|
44639
5c2838d882a5
8178147: Race conditions in timeout handling code in http/2 incubator client
dfuchs
parents:
43999
diff
changeset
|
585 |
} |
48083 | 586 |
|
587 |
final class WriteTask implements Runnable { |
|
588 |
@Override |
|
589 |
public void run() { |
|
590 |
assert state != State.COMPLETED : "Unexpected state:" + state; |
|
591 |
debug.log(Level.DEBUG, "WriteTask"); |
|
592 |
if (subscriber == null) { |
|
593 |
debug.log(Level.DEBUG, "no subscriber yet"); |
|
594 |
return; |
|
595 |
} |
|
596 |
debug.log(Level.DEBUG, () -> "hasOutgoing = " + hasOutgoing()); |
|
597 |
while (hasOutgoing() && demand.tryDecrement()) { |
|
598 |
DataPair dp = getOutgoing(); |
|
599 |
||
600 |
if (dp.throwable != null) { |
|
601 |
debug.log(Level.DEBUG, "onError"); |
|
602 |
// Do not call the subscriber's onError, it is not required. |
|
603 |
writeScheduler.stop(); |
|
604 |
} else { |
|
605 |
List<ByteBuffer> data = dp.data; |
|
606 |
if (data == Http1BodySubscriber.COMPLETED) { |
|
607 |
synchronized (lock) { |
|
608 |
assert state == State.COMPLETING : "Unexpected state:" + state; |
|
609 |
state = State.COMPLETED; |
|
610 |
} |
|
611 |
debug.log(Level.DEBUG, |
|
612 |
"completed, stopping %s", writeScheduler); |
|
613 |
writeScheduler.stop(); |
|
614 |
// Do nothing more. Just do not publish anything further. |
|
615 |
// The next Subscriber will eventually take over. |
|
616 |
||
617 |
} else { |
|
618 |
debug.log(Level.DEBUG, () -> |
|
619 |
"onNext with " + Utils.remaining(data) + " bytes"); |
|
620 |
subscriber.onNext(data); |
|
621 |
} |
|
622 |
} |
|
623 |
} |
|
624 |
} |
|
625 |
} |
|
626 |
||
627 |
final class Http1WriteSubscription implements Flow.Subscription { |
|
628 |
||
629 |
@Override |
|
630 |
public void request(long n) { |
|
631 |
if (cancelled) |
|
632 |
return; //no-op |
|
633 |
demand.increase(n); |
|
634 |
debug.log(Level.DEBUG, |
|
635 |
"subscription request(%d), demand=%s", n, demand); |
|
56040 | 636 |
writeScheduler.runOrSchedule(client.theExecutor()); |
48083 | 637 |
} |
638 |
||
639 |
@Override |
|
640 |
public void cancel() { |
|
641 |
debug.log(Level.DEBUG, "subscription cancelled"); |
|
642 |
if (cancelled) |
|
643 |
return; //no-op |
|
644 |
cancelled = true; |
|
645 |
writeScheduler.stop(); |
|
646 |
} |
|
647 |
} |
|
648 |
} |
|
649 |
||
650 |
String dbgString() { |
|
651 |
return "Http1Exchange"; |
|
42460
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
652 |
} |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
653 |
} |