/
HttpConnection.java
458 lines (384 loc) · 14.9 KB
/
HttpConnection.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
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
/*
* Copyright (c) 2015, 2019 Oracle and/or its affiliates. All rights reserved.
*
* This program and the accompanying materials are made available under the
* terms of the Eclipse Public License v. 2.0, which is available at
* http://www.eclipse.org/legal/epl-2.0.
*
* This Source Code may also be made available under the following Secondary
* Licenses when the conditions for such availability set forth in the
* Eclipse Public License v. 2.0 are satisfied: GNU General Public License,
* version 2 with the GNU Classpath Exception, which is available at
* https://www.gnu.org/software/classpath/license.html.
*
* SPDX-License-Identifier: EPL-2.0 OR GPL-2.0 WITH Classpath-exception-2.0
*/
package org.glassfish.jersey.jdk.connector.internal;
import java.io.IOException;
import java.net.CookieManager;
import java.net.InetSocketAddress;
import java.net.URI;
import java.nio.ByteBuffer;
import java.util.List;
import java.util.Map;
import java.util.concurrent.Future;
import java.util.concurrent.ScheduledExecutorService;
import java.util.concurrent.TimeUnit;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.net.ssl.SSLContext;
import org.glassfish.jersey.SslConfigurator;
/**
* @author Petr Janouch
*/
class HttpConnection {
/**
* Input buffer that is used by {@link TransportFilter} when SSL is turned on.
* The size cannot be smaller than a maximal size of a SSL packet, which is 16kB for payload + header, because
* {@link SslFilter} does not have its own buffer for buffering incoming
* data and therefore the entire SSL packet must fit into {@link SslFilter}
* input buffer.
* <p/>
*/
private static final int SSL_INPUT_BUFFER_SIZE = 17_000;
/**
* Input buffer that is used by {@link TransportFilter} when SSL is not turned on.
*/
private static final int INPUT_BUFFER_SIZE = 2048;
private static final Logger LOGGER = Logger.getLogger(HttpConnection.class.getName());
private final Filter<HttpRequest, HttpResponse, HttpRequest, HttpResponse> filterChain;
private final CookieManager cookieManager;
// we are interested only in host-port pair, but URI is a convenient holder for it
private final URI uri;
private final StateChangeListener stateListener;
private final ScheduledExecutorService scheduler;
private final ConnectorConfiguration configuration;
private HttpRequest httpRequest;
private HttpResponse httResponse;
private Throwable error;
volatile State state = State.CREATED;
// by default we treat all connection as persistent
// this flag will change to false if we receive "Connection: Close" header
private boolean persistentConnection = true;
private Future<?> responseTimeout;
private Future<?> idleTimeout;
private Future<?> connectTimeout;
HttpConnection(URI uri,
CookieManager cookieManager,
ConnectorConfiguration configuration,
ScheduledExecutorService scheduler,
StateChangeListener stateListener) {
this.uri = uri;
this.cookieManager = cookieManager;
this.stateListener = stateListener;
this.configuration = configuration;
this.scheduler = scheduler;
filterChain = createFilterChain(uri, configuration);
}
synchronized void connect() {
if (state != State.CREATED) {
throw new IllegalStateException(LocalizationMessages.HTTP_CONNECTION_ESTABLISHING_ILLEGAL_STATE(state));
}
changeState(State.CONNECTING);
scheduleConnectTimeout();
filterChain.connect(new InetSocketAddress(uri.getHost(), Utils.getPort(uri)), null);
}
synchronized void send(final HttpRequest httpRequest) {
if (state != State.IDLE) {
throw new IllegalStateException(
"Http request cannot be sent over a connection that is in other state than IDLE. Current state: " + state);
}
cancelIdleTimeout();
this.httpRequest = httpRequest;
// clean state left by previous request
httResponse = null;
error = null;
persistentConnection = true;
changeState(State.SENDING_REQUEST);
addRequestHeaders();
filterChain.write(httpRequest, new CompletionHandler<HttpRequest>() {
@Override
public void failed(Throwable throwable) {
handleError(throwable);
}
@Override
public void completed(HttpRequest result) {
handleHeaderSent();
}
});
}
void close() {
if (state == State.CLOSED) {
return;
}
cancelAllTimeouts();
filterChain.close();
changeState(State.CLOSED);
}
private synchronized void handleHeaderSent() {
if (state != State.SENDING_REQUEST) {
return;
}
scheduleResponseTimeout();
if (httpRequest.getBodyMode() == HttpRequest.BodyMode.NONE
|| httpRequest.getBodyMode() == HttpRequest.BodyMode.BUFFERED) {
changeState(State.RECEIVING_HEADER);
} else {
ChunkedBodyOutputStream bodyStream = (ChunkedBodyOutputStream) httpRequest.getBodyStream();
bodyStream.setCloseListener(() -> {
synchronized (HttpConnection.this) {
if (state != State.SENDING_REQUEST) {
return;
}
}
changeState(State.RECEIVING_HEADER);
});
}
}
private void addRequestHeaders() {
Map<String, List<String>> cookies;
try {
cookies = cookieManager.get(httpRequest.getUri(), httpRequest.getHeaders());
} catch (IOException e) {
handleError(e);
return;
}
// unfortunately CookieManager returns ""Cookie" -> empty list" pair if the cookie is not set
cookies.entrySet().stream().filter(cookieHeader -> cookieHeader.getValue() != null && !cookieHeader.getValue().isEmpty())
.forEach(cookieHeader -> httpRequest.getHeaders().put(cookieHeader.getKey(), cookieHeader.getValue()));
}
private void processResponseHeaders(HttpResponse response) throws IOException {
cookieManager.put(httpRequest.getUri(), httResponse.getHeaders());
List<String> connectionValues = response.getHeader(Constants.CONNECTION);
if (connectionValues != null) {
connectionValues.stream().filter(connectionValue -> connectionValue.equalsIgnoreCase(Constants.CONNECTION_CLOSE))
.forEach(connectionValue -> persistentConnection = false);
}
}
protected Filter<HttpRequest, HttpResponse, HttpRequest, HttpResponse> createFilterChain(URI uri,
ConnectorConfiguration
configuration) {
boolean secure = Constants.HTTPS.equals(uri.getScheme());
Filter<ByteBuffer, ByteBuffer, ?, ?> socket;
if (secure) {
SSLContext sslContext = configuration.getSslContext();
TransportFilter transportFilter = new TransportFilter(SSL_INPUT_BUFFER_SIZE, configuration.getThreadPoolConfig(),
configuration.getContainerIdleTimeout());
if (sslContext == null) {
sslContext = SslConfigurator.getDefaultContext();
}
socket = new SslFilter(transportFilter, sslContext, uri.getHost(), configuration.getHostnameVerifier());
} else {
socket = new TransportFilter(INPUT_BUFFER_SIZE, configuration.getThreadPoolConfig(),
configuration.getContainerIdleTimeout());
}
int maxHeaderSize = configuration.getMaxHeaderSize();
HttpFilter httpFilter = new HttpFilter(socket, maxHeaderSize, maxHeaderSize + INPUT_BUFFER_SIZE);
ConnectorConfiguration.ProxyConfiguration proxyConfiguration = configuration.getProxyConfiguration();
if (proxyConfiguration.isConfigured()) {
ProxyFilter proxyFilter = new ProxyFilter(httpFilter, proxyConfiguration);
return new ConnectionFilter(proxyFilter);
}
return new ConnectionFilter(httpFilter);
}
private void changeState(State newState) {
if (state == State.CLOSED) {
return;
}
State old = state;
state = newState;
if (LOGGER.isLoggable(Level.FINEST)) {
LOGGER.finest(LocalizationMessages.CONNECTION_CHANGING_STATE(uri.getHost(), uri.getPort(), old, newState));
}
stateListener.onStateChanged(this, old, newState);
}
private void scheduleResponseTimeout() {
if (configuration.getResponseTimeout() == 0) {
return;
}
responseTimeout = scheduler.schedule(() -> {
synchronized (HttpConnection.this) {
if (state != State.RECEIVING_HEADER && state != State.RECEIVING_BODY) {
return;
}
responseTimeout = null;
changeState(State.RESPONSE_TIMEOUT);
close();
}
}, configuration.getResponseTimeout(), TimeUnit.MILLISECONDS);
}
private void cancelResponseTimeout() {
if (responseTimeout != null) {
responseTimeout.cancel(true);
responseTimeout = null;
}
}
private void scheduleConnectTimeout() {
if (configuration.getConnectTimeout() == 0) {
return;
}
connectTimeout = scheduler.schedule(() -> {
synchronized (HttpConnection.this) {
if (state != State.CONNECTING) {
return;
}
connectTimeout = null;
changeState(State.CONNECT_TIMEOUT);
close();
}
}, configuration.getConnectTimeout(), TimeUnit.MILLISECONDS);
}
private void cancelConnectTimeout() {
if (connectTimeout != null) {
connectTimeout.cancel(true);
connectTimeout = null;
}
}
private void scheduleIdleTimeout() {
if (configuration.getConnectionIdleTimeout() == 0) {
return;
}
idleTimeout = scheduler.schedule(() -> {
synchronized (HttpConnection.this) {
if (state != State.IDLE) {
return;
}
idleTimeout = null;
changeState(State.IDLE_TIMEOUT);
close();
}
}, configuration.getConnectionIdleTimeout(), TimeUnit.MILLISECONDS);
}
private void cancelIdleTimeout() {
if (idleTimeout != null) {
idleTimeout.cancel(true);
idleTimeout = null;
}
}
private void cancelAllTimeouts() {
cancelConnectTimeout();
cancelIdleTimeout();
cancelResponseTimeout();
}
private synchronized void handleError(Throwable t) {
cancelAllTimeouts();
error = t;
changeState(State.ERROR);
close();
}
private void changeStateToIdle() {
scheduleIdleTimeout();
changeState(State.IDLE);
}
Throwable getError() {
return error;
}
HttpResponse getHttResponse() {
return httResponse;
}
private synchronized void handleResponseRead() {
cancelResponseTimeout();
changeState(State.RECEIVED);
if (!persistentConnection) {
changeState(State.CLOSED);
return;
}
changeStateToIdle();
}
private class ConnectionFilter extends Filter<HttpRequest, HttpResponse, HttpRequest, HttpResponse> {
ConnectionFilter(Filter<HttpRequest, HttpResponse, ?, ?> downstreamFilter) {
super(downstreamFilter);
}
@Override
boolean processRead(HttpResponse response) {
synchronized (HttpConnection.this) {
if (state != State.RECEIVING_HEADER && state != State.SENDING_REQUEST) {
return false;
}
if (state == State.SENDING_REQUEST) {
// great we received response header so fast that we did not even switch into "receiving header" state,
// do it now to complete the formal lifecycle
// this happens when write completion listener is overtaken by "read event"
changeState(State.RECEIVING_HEADER);
}
httResponse = response;
try {
processResponseHeaders(response);
} catch (IOException e) {
handleError(e);
return false;
}
}
if (response.getHasContent()) {
AsynchronousBodyInputStream bodyStream = httResponse.getBodyStream();
changeState(State.RECEIVING_BODY);
bodyStream.setStateChangeLister(new AsynchronousBodyInputStream.StateChangeLister() {
@Override
public void onError(Throwable t) {
handleError(t);
}
@Override
public void onAllDataRead() {
handleResponseRead();
}
});
} else {
handleResponseRead();
}
return false;
}
@Override
void processConnect() {
synchronized (HttpConnection.this) {
if (state != State.CONNECTING) {
return;
}
downstreamFilter.startSsl();
}
}
@Override
void processSslHandshakeCompleted() {
synchronized (HttpConnection.this) {
if (state != State.CONNECTING) {
return;
}
cancelConnectTimeout();
changeStateToIdle();
}
}
@Override
void processConnectionClosed() {
synchronized (HttpConnection.this) {
cancelAllTimeouts();
changeState(State.CLOSED_BY_SERVER);
HttpConnection.this.close();
}
}
@Override
void processError(Throwable t) {
handleError(t);
}
@Override
void write(HttpRequest data, CompletionHandler<HttpRequest> completionHandler) {
downstreamFilter.write(data, completionHandler);
}
}
enum State {
CREATED,
CONNECTING,
CONNECT_TIMEOUT,
IDLE,
SENDING_REQUEST,
RECEIVING_HEADER,
RECEIVING_BODY,
RECEIVED,
RESPONSE_TIMEOUT,
CLOSED_BY_SERVER,
CLOSED,
ERROR,
IDLE_TIMEOUT
}
interface StateChangeListener {
void onStateChanged(HttpConnection connection, State oldState, State newState);
}
}