-
Notifications
You must be signed in to change notification settings - Fork 631
/
HttpTrafficHandler.java
536 lines (480 loc) · 17.5 KB
/
HttpTrafficHandler.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
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
/*
* Copyright (c) 2011-2024 VMware, Inc. or its affiliates, All Rights Reserved.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* https://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package reactor.netty.http.server;
import java.net.SocketAddress;
import java.time.Duration;
import java.time.ZonedDateTime;
import java.util.Optional;
import java.util.Queue;
import java.util.function.BiFunction;
import java.util.function.BiPredicate;
import io.netty.channel.ChannelDuplexHandler;
import io.netty.channel.ChannelFuture;
import io.netty.channel.ChannelFutureListener;
import io.netty.channel.ChannelHandlerContext;
import io.netty.channel.ChannelPromise;
import io.netty.handler.codec.DecoderResult;
import io.netty.handler.codec.DecoderResultProvider;
import io.netty.handler.codec.http.HttpHeaderNames;
import io.netty.handler.codec.http.HttpObject;
import io.netty.handler.codec.http.HttpRequest;
import io.netty.handler.codec.http.HttpResponse;
import io.netty.handler.codec.http.HttpResponseStatus;
import io.netty.handler.codec.http.HttpStatusClass;
import io.netty.handler.codec.http.HttpVersion;
import io.netty.handler.codec.http.LastHttpContent;
import io.netty.handler.codec.http.cookie.ServerCookieDecoder;
import io.netty.handler.codec.http.cookie.ServerCookieEncoder;
import io.netty.handler.ssl.SslHandler;
import io.netty.util.ReferenceCountUtil;
import reactor.core.Exceptions;
import reactor.core.publisher.Mono;
import reactor.netty.Connection;
import reactor.netty.ConnectionObserver;
import reactor.netty.ReactorNetty;
import reactor.netty.http.logging.HttpMessageArgProviderFactory;
import reactor.netty.http.logging.HttpMessageLogFactory;
import reactor.util.annotation.Nullable;
import reactor.util.concurrent.Queues;
import static io.netty.handler.codec.http.HttpUtil.isContentLengthSet;
import static io.netty.handler.codec.http.HttpUtil.isKeepAlive;
import static io.netty.handler.codec.http.HttpUtil.isTransferEncodingChunked;
import static io.netty.handler.codec.http.HttpUtil.setKeepAlive;
import static reactor.netty.ReactorNetty.format;
/**
* Replace {@link io.netty.handler.codec.http.HttpServerKeepAliveHandler} with extra
* handler management.
*/
final class HttpTrafficHandler extends ChannelDuplexHandler
implements Runnable, ChannelFutureListener {
static final String MULTIPART_PREFIX = "multipart";
static final HttpVersion H2 = HttpVersion.valueOf("HTTP/2.0");
final BiPredicate<HttpServerRequest, HttpServerResponse> compress;
final ServerCookieDecoder cookieDecoder;
final ServerCookieEncoder cookieEncoder;
final HttpServerFormDecoderProvider formDecoderProvider;
final BiFunction<ConnectionInfo, HttpRequest, ConnectionInfo> forwardedHeaderHandler;
final HttpMessageLogFactory httpMessageLogFactory;
final Duration idleTimeout;
final ConnectionObserver listener;
final BiFunction<? super Mono<Void>, ? super Connection, ? extends Mono<Void>>
mapHandle;
final int maxKeepAliveRequests;
ChannelHandlerContext ctx;
boolean nonInformationalResponse;
boolean overflow;
// Track pending responses to support client pipelining: https://tools.ietf.org/html/rfc7230#section-6.3.2
int pendingResponses;
boolean persistentConnection = true;
Queue<Object> pipelined;
SocketAddress remoteAddress;
Boolean secure;
HttpTrafficHandler(
@Nullable BiPredicate<HttpServerRequest, HttpServerResponse> compress,
ServerCookieDecoder decoder,
ServerCookieEncoder encoder,
HttpServerFormDecoderProvider formDecoderProvider,
@Nullable BiFunction<ConnectionInfo, HttpRequest, ConnectionInfo> forwardedHeaderHandler,
HttpMessageLogFactory httpMessageLogFactory,
@Nullable Duration idleTimeout,
ConnectionObserver listener,
@Nullable BiFunction<? super Mono<Void>, ? super Connection, ? extends Mono<Void>> mapHandle,
int maxKeepAliveRequests) {
this.listener = listener;
this.formDecoderProvider = formDecoderProvider;
this.forwardedHeaderHandler = forwardedHeaderHandler;
this.compress = compress;
this.cookieEncoder = encoder;
this.cookieDecoder = decoder;
this.httpMessageLogFactory = httpMessageLogFactory;
this.idleTimeout = idleTimeout;
this.mapHandle = mapHandle;
this.maxKeepAliveRequests = maxKeepAliveRequests;
}
@Override
public void handlerAdded(ChannelHandlerContext ctx) throws Exception {
super.handlerAdded(ctx);
this.ctx = ctx;
if (HttpServerOperations.log.isDebugEnabled()) {
HttpServerOperations.log.debug(format(ctx.channel(), "New http connection, requesting read"));
}
ctx.read();
}
@Override
public void channelActive(ChannelHandlerContext ctx) {
IdleTimeoutHandler.addIdleTimeoutHandler(ctx.pipeline(), idleTimeout);
ctx.fireChannelActive();
}
@Override
public void channelRead(ChannelHandlerContext ctx, Object msg) {
if (secure == null) {
secure = ctx.channel().pipeline().get(SslHandler.class) != null;
}
if (remoteAddress == null) {
remoteAddress =
Optional.ofNullable(HAProxyMessageReader.resolveRemoteAddressFromProxyProtocol(ctx.channel()))
.orElse(ctx.channel().remoteAddress());
}
// read message and track if it was keepAlive
if (msg instanceof HttpRequest) {
if (idleTimeout != null) {
IdleTimeoutHandler.removeIdleTimeoutHandler(ctx.pipeline());
}
final HttpRequest request = (HttpRequest) msg;
if (H2.equals(request.protocolVersion())) {
IllegalStateException e = new IllegalStateException(
"Unexpected request [" + request.method() + " " + request.uri() + " HTTP/2.0]");
request.setDecoderResult(DecoderResult.failure(e.getCause() != null ? e.getCause() : e));
sendDecodingFailures(e, msg);
return;
}
if (persistentConnection) {
pendingResponses += 1;
if (HttpServerOperations.log.isDebugEnabled()) {
HttpServerOperations.log.debug(format(ctx.channel(), "Increasing pending responses, now {}"),
pendingResponses);
}
persistentConnection = isKeepAlive(request);
}
else {
if (HttpServerOperations.log.isDebugEnabled()) {
HttpServerOperations.log.debug(format(ctx.channel(), "Dropping pipelined HTTP request, " +
"previous response requested connection close"));
}
ReferenceCountUtil.release(msg);
return;
}
if (pendingResponses > 1) {
if (HttpServerOperations.log.isDebugEnabled()) {
HttpServerOperations.log.debug(format(ctx.channel(), "Buffering pipelined HTTP request, " +
"pending response count: {}, queue: {}"),
pendingResponses,
pipelined != null ? pipelined.size() : 0);
}
overflow = true;
doPipeline(ctx, new HttpRequestHolder(request));
return;
}
else {
overflow = false;
DecoderResult decoderResult = request.decoderResult();
if (decoderResult.isFailure()) {
sendDecodingFailures(decoderResult.cause(), msg);
return;
}
HttpServerOperations ops;
ZonedDateTime timestamp = ZonedDateTime.now(ReactorNetty.ZONE_ID_SYSTEM);
ConnectionInfo connectionInfo = null;
try {
connectionInfo = ConnectionInfo.from(ctx.channel(),
request,
secure,
remoteAddress,
forwardedHeaderHandler);
ops = new HttpServerOperations(Connection.from(ctx.channel()),
listener,
request,
compress,
connectionInfo,
cookieDecoder,
cookieEncoder,
formDecoderProvider,
httpMessageLogFactory,
false,
mapHandle,
secure,
timestamp);
}
catch (RuntimeException e) {
request.setDecoderResult(DecoderResult.failure(e.getCause() != null ? e.getCause() : e));
sendDecodingFailures(e, msg, timestamp, connectionInfo);
return;
}
ops.bind();
listener.onStateChange(ops, ConnectionObserver.State.CONFIGURED);
ctx.fireChannelRead(msg);
return;
}
}
else if (persistentConnection && pendingResponses == 0) {
if (msg instanceof LastHttpContent) {
DecoderResult decoderResult = ((LastHttpContent) msg).decoderResult();
if (decoderResult.isFailure()) {
sendDecodingFailures(decoderResult.cause(), msg);
return;
}
ctx.fireChannelRead(msg);
}
else {
if (HttpServerOperations.log.isDebugEnabled()) {
HttpServerOperations.log.debug(
format(ctx.channel(), "Dropped HTTP content, since response has been sent already: {}"),
msg instanceof HttpObject ?
httpMessageLogFactory.debug(HttpMessageArgProviderFactory.create(msg)) : msg);
}
ReferenceCountUtil.release(msg);
}
ctx.read();
return;
}
else if (overflow) {
if (HttpServerOperations.log.isDebugEnabled()) {
HttpServerOperations.log.debug(format(ctx.channel(), "Buffering pipelined HTTP content, " +
"pending response count: {}, pending pipeline:{}"),
pendingResponses,
pipelined != null ? pipelined.size() : 0);
}
doPipeline(ctx, msg);
return;
}
if (msg instanceof DecoderResultProvider) {
DecoderResult decoderResult = ((DecoderResultProvider) msg).decoderResult();
if (decoderResult.isFailure()) {
sendDecodingFailures(decoderResult.cause(), msg);
return;
}
}
ctx.fireChannelRead(msg);
}
void sendDecodingFailures(Throwable t, Object msg) {
sendDecodingFailures(t, msg, null, null);
}
void sendDecodingFailures(Throwable t, Object msg, @Nullable ZonedDateTime timestamp, @Nullable ConnectionInfo connectionInfo) {
persistentConnection = false;
HttpServerOperations.sendDecodingFailures(ctx, listener, secure, t, msg, httpMessageLogFactory, timestamp, connectionInfo,
remoteAddress);
}
void doPipeline(ChannelHandlerContext ctx, Object msg) {
if (pipelined == null) {
pipelined = Queues.unbounded()
.get();
}
if (!pipelined.offer(msg)) {
ctx.fireExceptionCaught(Exceptions.failWithOverflow());
}
}
@Override
@SuppressWarnings("FutureReturnValueIgnored")
public void write(ChannelHandlerContext ctx, Object msg, ChannelPromise promise) {
// modify message on way out to add headers if needed
if (msg instanceof HttpResponse) {
final HttpResponse response = (HttpResponse) msg;
nonInformationalResponse = !isInformational(response);
// Assume the response writer knows if they can persist or not and sets isKeepAlive on the response
boolean maxKeepAliveRequestsReached = maxKeepAliveRequests != -1 && HttpServerOperations.requestsCounter(ctx.channel()) == maxKeepAliveRequests;
if (maxKeepAliveRequestsReached || !isKeepAlive(response) || !isSelfDefinedMessageLength(response)) {
// No longer keep alive as the client can't tell when the message is done unless we close connection
pendingResponses = 0;
persistentConnection = false;
}
// Server might think it can keep connection alive, but we should fix response header if we know better
if (!shouldKeepAlive()) {
setKeepAlive(response, false);
}
if (response.status().equals(HttpResponseStatus.CONTINUE)) {
//"FutureReturnValueIgnored" this is deliberate
ctx.write(msg, promise);
return;
}
}
if (msg instanceof LastHttpContent) {
if (!shouldKeepAlive()) {
if (HttpServerOperations.log.isDebugEnabled()) {
HttpServerOperations.log.debug(format(ctx.channel(), "Detected non persistent http " +
"connection, preparing to close"),
pendingResponses);
}
ctx.write(msg, promise.unvoid())
.addListener(this)
.addListener(ChannelFutureListener.CLOSE);
return;
}
ctx.write(msg, promise.unvoid())
.addListener(this);
if (!persistentConnection) {
return;
}
if (nonInformationalResponse) {
nonInformationalResponse = false;
pendingResponses -= 1;
if (HttpServerOperations.log.isDebugEnabled()) {
HttpServerOperations.log.debug(format(ctx.channel(), "Decreasing pending responses, now {}"),
pendingResponses);
}
}
if (pipelined != null && !pipelined.isEmpty()) {
if (HttpServerOperations.log.isDebugEnabled()) {
HttpServerOperations.log.debug(format(ctx.channel(), "Draining next pipelined " +
"request, pending response count: {}, queued: {}"),
pendingResponses, pipelined.size());
}
ctx.executor()
.execute(this);
}
else {
IdleTimeoutHandler.addIdleTimeoutHandler(ctx.pipeline(), idleTimeout);
ctx.read();
}
return;
}
if (persistentConnection && pendingResponses == 0) {
if (HttpServerOperations.log.isDebugEnabled()) {
HttpServerOperations.log.debug(
format(ctx.channel(), "Dropped HTTP content, since response has been sent already: {}"),
msg instanceof HttpObject ?
httpMessageLogFactory.debug(HttpMessageArgProviderFactory.create(msg)) : msg);
}
ReferenceCountUtil.release(msg);
promise.setSuccess();
return;
}
//"FutureReturnValueIgnored" this is deliberate
ctx.write(msg, promise);
}
@Override
public void run() {
Object next;
HttpRequest nextRequest = null;
while ((next = pipelined.peek()) != null) {
if (next instanceof HttpRequestHolder) {
if (nextRequest != null) {
return;
}
if (!persistentConnection) {
discard();
return;
}
HttpRequestHolder holder = (HttpRequestHolder) next;
nextRequest = holder.request;
DecoderResult decoderResult = nextRequest.decoderResult();
if (decoderResult.isFailure()) {
sendDecodingFailures(decoderResult.cause(), nextRequest, holder.timestamp, null);
discard();
return;
}
HttpServerOperations ops;
ConnectionInfo connectionInfo = null;
try {
connectionInfo = ConnectionInfo.from(ctx.channel(),
nextRequest,
secure,
remoteAddress,
forwardedHeaderHandler);
ops = new HttpServerOperations(Connection.from(ctx.channel()),
listener,
nextRequest,
compress,
connectionInfo,
cookieDecoder,
cookieEncoder,
formDecoderProvider,
httpMessageLogFactory,
false,
mapHandle,
secure,
holder.timestamp);
}
catch (RuntimeException e) {
holder.request.setDecoderResult(DecoderResult.failure(e.getCause() != null ? e.getCause() : e));
sendDecodingFailures(e, holder.request, holder.timestamp, connectionInfo);
return;
}
ops.bind();
listener.onStateChange(ops, ConnectionObserver.State.CONFIGURED);
pipelined.poll();
ctx.fireChannelRead(holder.request);
}
else {
ctx.fireChannelRead(pipelined.poll());
}
}
overflow = false;
}
@Override
public void operationComplete(ChannelFuture future) {
if (!future.isSuccess()) {
if (HttpServerOperations.log.isDebugEnabled()) {
HttpServerOperations.log.debug(format(future.channel(),
"Sending last HTTP packet was not successful, terminating the channel"),
future.cause());
}
}
else {
if (HttpServerOperations.log.isDebugEnabled()) {
HttpServerOperations.log.debug(format(future.channel(),
"Last HTTP packet was sent, terminating the channel"));
}
}
HttpServerOperations.cleanHandlerTerminate(future.channel());
}
@Override
public void handlerRemoved(ChannelHandlerContext ctx) {
discard();
}
final void discard() {
if (pipelined != null && !pipelined.isEmpty()) {
Object o;
while ((o = pipelined.poll()) != null) {
ReferenceCountUtil.release(o);
}
}
}
boolean shouldKeepAlive() {
return pendingResponses != 0 && persistentConnection;
}
/**
* Keep-alive only works if the client can detect when the message has ended without
* relying on the connection being closed.
* <p>
* <ul> <li>See <a href="https://tools.ietf.org/html/rfc7230#section-6.3"/></li>
* <li>See <a href="https://tools.ietf.org/html/rfc7230#section-3.3.3"/></li> </ul>
*
* @param response The HttpResponse to check
*
* @return true if the response has a self defined message length.
*/
static boolean isSelfDefinedMessageLength(HttpResponse response) {
return isContentLengthSet(response) || isTransferEncodingChunked(response) || isMultipart(
response) || isInformational(response) || isNotModified(response) || isNoContent(response);
}
static boolean isInformational(HttpResponse response) {
return response.status()
.codeClass() == HttpStatusClass.INFORMATIONAL;
}
static boolean isNoContent(HttpResponse response) {
return HttpResponseStatus.NO_CONTENT.code() == response.status().code();
}
static boolean isNotModified(HttpResponse response) {
return HttpResponseStatus.NOT_MODIFIED.code() == response.status().code();
}
static boolean isMultipart(HttpResponse response) {
String contentType = response.headers()
.get(HttpHeaderNames.CONTENT_TYPE);
return contentType != null && contentType.regionMatches(true,
0,
MULTIPART_PREFIX,
0,
MULTIPART_PREFIX.length());
}
static final class HttpRequestHolder {
final HttpRequest request;
final ZonedDateTime timestamp;
HttpRequestHolder(HttpRequest request) {
this.request = request;
this.timestamp = ZonedDateTime.now(ReactorNetty.ZONE_ID_SYSTEM);
}
}
}