/
HttpExchange.java
322 lines (279 loc) · 9.51 KB
/
HttpExchange.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
//
// ========================================================================
// Copyright (c) 1995-2022 Mort Bay Consulting Pty Ltd and others.
//
// This program and the accompanying materials are made available under the
// terms of the Eclipse Public License v. 2.0 which is available at
// https://www.eclipse.org/legal/epl-2.0, or the Apache License, Version 2.0
// which is available at https://www.apache.org/licenses/LICENSE-2.0.
//
// SPDX-License-Identifier: EPL-2.0 OR Apache-2.0
// ========================================================================
//
package org.eclipse.jetty.client;
import java.util.List;
import org.eclipse.jetty.client.api.Request;
import org.eclipse.jetty.client.api.Response;
import org.eclipse.jetty.client.api.Result;
import org.eclipse.jetty.io.CyclicTimeouts;
import org.eclipse.jetty.util.thread.AutoLock;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
public class HttpExchange implements CyclicTimeouts.Expirable
{
private static final Logger LOG = LoggerFactory.getLogger(HttpExchange.class);
private final AutoLock lock = new AutoLock();
private final HttpDestination destination;
private final HttpRequest request;
private final List<Response.ResponseListener> listeners;
private final HttpResponse response;
private State requestState = State.PENDING;
private State responseState = State.PENDING;
private HttpChannel _channel;
private Throwable requestFailure;
private Throwable responseFailure;
public HttpExchange(HttpDestination destination, HttpRequest request, List<Response.ResponseListener> listeners)
{
this.destination = destination;
this.request = request;
this.listeners = listeners;
this.response = new HttpResponse(request, listeners);
HttpConversation conversation = request.getConversation();
conversation.getExchanges().offer(this);
conversation.updateResponseListeners(null);
}
public HttpDestination getHttpDestination()
{
return destination;
}
public HttpConversation getConversation()
{
return request.getConversation();
}
public HttpRequest getRequest()
{
return request;
}
public Throwable getRequestFailure()
{
try (AutoLock l = lock.lock())
{
return requestFailure;
}
}
public List<Response.ResponseListener> getResponseListeners()
{
return listeners;
}
public HttpResponse getResponse()
{
return response;
}
public Throwable getResponseFailure()
{
try (AutoLock l = lock.lock())
{
return responseFailure;
}
}
@Override
public long getExpireNanoTime()
{
return request.getTimeoutAt();
}
/**
* <p>Associates the given {@code channel} to this exchange.</p>
* <p>Works in strict collaboration with {@link HttpChannel#associate(HttpExchange)}.</p>
*
* @param channel the channel to associate to this exchange
* @return true if the channel could be associated, false otherwise
*/
boolean associate(HttpChannel channel)
{
boolean result = false;
boolean abort = false;
try (AutoLock l = lock.lock())
{
// Only associate if the exchange state is initial,
// as the exchange could be already failed.
if (requestState == State.PENDING && responseState == State.PENDING)
{
abort = _channel != null;
if (!abort)
{
_channel = channel;
result = true;
}
}
}
if (abort)
request.abort(new IllegalStateException(toString()));
return result;
}
void disassociate(HttpChannel channel)
{
boolean abort = false;
try (AutoLock l = lock.lock())
{
if (_channel != channel || requestState != State.TERMINATED || responseState != State.TERMINATED)
abort = true;
_channel = null;
}
if (abort)
request.abort(new IllegalStateException(toString()));
}
private HttpChannel getHttpChannel()
{
try (AutoLock l = lock.lock())
{
return _channel;
}
}
public boolean requestComplete(Throwable failure)
{
try (AutoLock l = lock.lock())
{
return completeRequest(failure);
}
}
private boolean completeRequest(Throwable failure)
{
if (requestState == State.PENDING)
{
requestState = State.COMPLETED;
requestFailure = failure;
return true;
}
return false;
}
public boolean responseComplete(Throwable failure)
{
try (AutoLock l = lock.lock())
{
return completeResponse(failure);
}
}
private boolean completeResponse(Throwable failure)
{
if (responseState == State.PENDING)
{
responseState = State.COMPLETED;
responseFailure = failure;
return true;
}
return false;
}
public Result terminateRequest()
{
Result result = null;
try (AutoLock l = lock.lock())
{
if (requestState == State.COMPLETED)
requestState = State.TERMINATED;
if (requestState == State.TERMINATED && responseState == State.TERMINATED)
result = new Result(getRequest(), requestFailure, getResponse(), responseFailure);
}
if (LOG.isDebugEnabled())
LOG.debug("Terminated request for {}, result: {}", this, result);
return result;
}
public Result terminateResponse()
{
Result result = null;
try (AutoLock l = lock.lock())
{
if (responseState == State.COMPLETED)
responseState = State.TERMINATED;
if (requestState == State.TERMINATED && responseState == State.TERMINATED)
result = new Result(getRequest(), requestFailure, getResponse(), responseFailure);
}
if (LOG.isDebugEnabled())
LOG.debug("Terminated response for {}, result: {}", this, result);
return result;
}
public boolean abort(Throwable failure)
{
// Atomically change the state of this exchange to be completed.
// This will avoid that this exchange can be associated to a channel.
boolean abortRequest;
boolean abortResponse;
try (AutoLock l = lock.lock())
{
abortRequest = completeRequest(failure);
abortResponse = completeResponse(failure);
}
if (LOG.isDebugEnabled())
LOG.debug("Failed {}: req={}/rsp={} {}", this, abortRequest, abortResponse, failure);
if (!abortRequest && !abortResponse)
return false;
// We failed this exchange, deal with it.
// Applications could be blocked providing
// request content, notify them of the failure.
Request.Content body = request.getBody();
if (abortRequest && body != null)
body.fail(failure);
// Case #1: exchange was in the destination queue.
if (destination.remove(this))
{
if (LOG.isDebugEnabled())
LOG.debug("Aborting while queued {}: {}", this, failure);
notifyFailureComplete(failure);
return true;
}
HttpChannel channel = getHttpChannel();
if (channel == null)
{
// Case #2: exchange was not yet associated.
// Because this exchange is failed, when associate() is called
// it will return false, and the caller will dispose the channel.
if (LOG.isDebugEnabled())
LOG.debug("Aborted before association {}: {}", this, failure);
notifyFailureComplete(failure);
return true;
}
// Case #3: exchange was already associated.
boolean aborted = channel.abort(this, abortRequest ? failure : null, abortResponse ? failure : null);
if (LOG.isDebugEnabled())
LOG.debug("Aborted ({}) while active {}: {}", aborted, this, failure);
return aborted;
}
private void notifyFailureComplete(Throwable failure)
{
destination.getRequestNotifier().notifyFailure(request, failure);
List<Response.ResponseListener> listeners = getConversation().getResponseListeners();
ResponseNotifier responseNotifier = destination.getResponseNotifier();
responseNotifier.notifyFailure(listeners, response, failure);
responseNotifier.notifyComplete(listeners, new Result(request, failure, response, failure));
}
public void resetResponse()
{
try (AutoLock l = lock.lock())
{
responseState = State.PENDING;
responseFailure = null;
response.clearHeaders();
}
}
public void proceed(Throwable failure)
{
HttpChannel channel = getHttpChannel();
if (channel != null)
channel.proceed(this, failure);
}
@Override
public String toString()
{
try (AutoLock l = lock.lock())
{
return String.format("%s@%x{req=%s[%s/%s] res=%s[%s/%s]}",
HttpExchange.class.getSimpleName(),
hashCode(),
request, requestState, requestFailure,
response, responseState, responseFailure);
}
}
private enum State
{
PENDING, COMPLETED, TERMINATED
}
}