Skip to content

Commit 981d2ae

Browse files
committed
Merge #2667 into 1.1.3
2 parents 9488e71 + 7ad753f commit 981d2ae

File tree

14 files changed

+310
-207
lines changed

14 files changed

+310
-207
lines changed

reactor-netty-core/src/testFixtures/java/reactor/netty/LogTracker.java

Lines changed: 24 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,8 @@
2020
import ch.qos.logback.core.AppenderBase;
2121
import org.slf4j.LoggerFactory;
2222

23+
import java.util.ArrayList;
24+
import java.util.List;
2325
import java.util.concurrent.CountDownLatch;
2426
import java.util.stream.Stream;
2527

@@ -30,39 +32,52 @@
3032
*/
3133
public final class LogTracker extends AppenderBase<ILoggingEvent> implements AutoCloseable {
3234

35+
public final List<ILoggingEvent> actualMessages = new ArrayList<>();
3336
public final CountDownLatch latch;
3437

3538
private final Logger logger;
36-
private final String[] messages;
39+
private final String[] expectedMessages;
3740

3841
/**
3942
* Creates a new {@link LogTracker}.
4043
*
4144
* @param className the logger name
42-
* @param messages the expected messages
45+
* @param expectedMessages the expected messages
4346
*/
44-
public LogTracker(Class<?> className, String... messages) {
45-
this(className.getName(), messages);
47+
public LogTracker(Class<?> className, String... expectedMessages) {
48+
this(className.getName(), expectedMessages);
4649
}
4750

4851
/**
4952
* Creates a new {@link LogTracker}.
5053
*
5154
* @param loggerName the logger name
52-
* @param messages the expected messages
55+
* @param expectedMessages the expected messages
5356
*/
54-
public LogTracker(String loggerName, String... messages) {
57+
public LogTracker(String loggerName, String... expectedMessages) {
58+
this(loggerName, expectedMessages.length, expectedMessages);
59+
}
60+
61+
/**
62+
* Creates a new {@link LogTracker}.
63+
*
64+
* @param loggerName the logger name
65+
* @param expectedCount the number of times the expected messages will appear in the logs
66+
* @param expectedMessages the expected messages
67+
*/
68+
public LogTracker(String loggerName, int expectedCount, String... expectedMessages) {
5569
this.logger = (Logger) LoggerFactory.getLogger(loggerName);
56-
this.messages = messages;
57-
this.latch = new CountDownLatch(messages.length);
70+
this.expectedMessages = expectedMessages;
71+
this.latch = new CountDownLatch(expectedCount);
5872

5973
start();
6074
this.logger.addAppender(this);
6175
}
6276

6377
@Override
6478
protected void append(ILoggingEvent eventObject) {
65-
if (Stream.of(messages).anyMatch(msg -> eventObject.getFormattedMessage().contains(msg))) {
79+
if (Stream.of(expectedMessages).anyMatch(msg -> eventObject.getFormattedMessage().contains(msg))) {
80+
actualMessages.add(eventObject);
6681
latch.countDown();
6782
}
6883
}

reactor-netty-http/build.gradle

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright (c) 2020-2022 VMware, Inc. or its affiliates, All Rights Reserved.
2+
* Copyright (c) 2020-2023 VMware, Inc. or its affiliates, All Rights Reserved.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -241,6 +241,8 @@ task japicmp(type: JapicmpTask) {
241241

242242
compatibilityChangeExcludes = [ "METHOD_NEW_DEFAULT" ]
243243
methodExcludes = [
244+
'reactor.netty.http.server.HttpServerRequest#protocol()',
245+
'reactor.netty.http.server.HttpServerRequest#timestamp()'
244246
]
245247
}
246248

reactor-netty-http/src/main/java/reactor/netty/http/server/Http2StreamBridgeServerHandler.java

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright (c) 2018-2022 VMware, Inc. or its affiliates, All Rights Reserved.
2+
* Copyright (c) 2018-2023 VMware, Inc. or its affiliates, All Rights Reserved.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -16,6 +16,7 @@
1616
package reactor.netty.http.server;
1717

1818
import java.net.SocketAddress;
19+
import java.time.ZonedDateTime;
1920
import java.util.Optional;
2021
import java.util.function.BiFunction;
2122
import java.util.function.BiPredicate;
@@ -39,6 +40,7 @@
3940
import reactor.core.publisher.Mono;
4041
import reactor.netty.Connection;
4142
import reactor.netty.ConnectionObserver;
43+
import reactor.netty.ReactorNetty;
4244
import reactor.netty.http.logging.HttpMessageArgProviderFactory;
4345
import reactor.netty.http.logging.HttpMessageLogFactory;
4446
import reactor.util.annotation.Nullable;
@@ -113,6 +115,7 @@ public void channelRead(ChannelHandlerContext ctx, Object msg) {
113115
if (msg instanceof HttpRequest) {
114116
HttpRequest request = (HttpRequest) msg;
115117
HttpServerOperations ops;
118+
ZonedDateTime timestamp = ZonedDateTime.now(ReactorNetty.ZONE_ID_SYSTEM);
116119
try {
117120
pendingResponse = true;
118121
ops = new HttpServerOperations(Connection.from(ctx.channel()),
@@ -129,12 +132,13 @@ public void channelRead(ChannelHandlerContext ctx, Object msg) {
129132
formDecoderProvider,
130133
httpMessageLogFactory,
131134
mapHandle,
132-
secured);
135+
secured,
136+
timestamp);
133137
}
134138
catch (RuntimeException e) {
135139
pendingResponse = false;
136140
request.setDecoderResult(DecoderResult.failure(e.getCause() != null ? e.getCause() : e));
137-
HttpServerOperations.sendDecodingFailures(ctx, listener, secured, e, msg, httpMessageLogFactory);
141+
HttpServerOperations.sendDecodingFailures(ctx, listener, secured, e, msg, httpMessageLogFactory, timestamp);
138142
return;
139143
}
140144
ops.bind();

reactor-netty-http/src/main/java/reactor/netty/http/server/HttpServerConfig.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -605,7 +605,7 @@ static void configureHttp11OrH2CleartextPipeline(ChannelPipeline p,
605605
forwardedHeaderHandler, httpMessageLogFactory, idleTimeout, listener, mapHandle, maxKeepAliveRequests));
606606

607607
if (accessLogEnabled) {
608-
p.addBefore(NettyPipeline.HttpTrafficHandler, NettyPipeline.AccessLogHandler, AccessLogHandlerFactory.H1.create(accessLog));
608+
p.addAfter(NettyPipeline.HttpTrafficHandler, NettyPipeline.AccessLogHandler, AccessLogHandlerFactory.H1.create(accessLog));
609609
}
610610

611611
boolean alwaysCompress = compressPredicate == null && minCompressionSize == 0;
@@ -665,7 +665,7 @@ static void configureHttp11Pipeline(ChannelPipeline p,
665665
forwardedHeaderHandler, httpMessageLogFactory, idleTimeout, listener, mapHandle, maxKeepAliveRequests));
666666

667667
if (accessLogEnabled) {
668-
p.addAfter(NettyPipeline.HttpCodec, NettyPipeline.AccessLogHandler, AccessLogHandlerFactory.H1.create(accessLog));
668+
p.addAfter(NettyPipeline.HttpTrafficHandler, NettyPipeline.AccessLogHandler, AccessLogHandlerFactory.H1.create(accessLog));
669669
}
670670

671671
boolean alwaysCompress = compressPredicate == null && minCompressionSize == 0;

reactor-netty-http/src/main/java/reactor/netty/http/server/HttpServerOperations.java

Lines changed: 27 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@
2020
import java.net.SocketAddress;
2121
import java.nio.file.Files;
2222
import java.nio.file.Path;
23+
import java.time.ZonedDateTime;
2324
import java.util.HashSet;
2425
import java.util.List;
2526
import java.util.Locale;
@@ -80,6 +81,7 @@
8081
import reactor.netty.FutureMono;
8182
import reactor.netty.NettyOutbound;
8283
import reactor.netty.NettyPipeline;
84+
import reactor.netty.ReactorNetty;
8385
import reactor.netty.channel.AbortedException;
8486
import reactor.netty.channel.ChannelOperations;
8587
import reactor.netty.http.HttpOperations;
@@ -117,6 +119,7 @@ class HttpServerOperations extends HttpOperations<HttpServerRequest, HttpServerR
117119
final HttpResponse nettyResponse;
118120
final HttpHeaders responseHeaders;
119121
final String scheme;
122+
final ZonedDateTime timestamp;
120123

121124
BiPredicate<HttpServerRequest, HttpServerResponse> compressionPredicate;
122125
Function<? super String, Map<String, String>> paramsResolver;
@@ -142,6 +145,7 @@ class HttpServerOperations extends HttpOperations<HttpServerRequest, HttpServerR
142145
this.path = replaced.path;
143146
this.responseHeaders = replaced.responseHeaders;
144147
this.scheme = replaced.scheme;
148+
this.timestamp = replaced.timestamp;
145149
this.trailerHeadersConsumer = replaced.trailerHeadersConsumer;
146150
}
147151

@@ -153,9 +157,10 @@ class HttpServerOperations extends HttpOperations<HttpServerRequest, HttpServerR
153157
HttpServerFormDecoderProvider formDecoderProvider,
154158
HttpMessageLogFactory httpMessageLogFactory,
155159
@Nullable BiFunction<? super Mono<Void>, ? super Connection, ? extends Mono<Void>> mapHandle,
156-
boolean secured) {
160+
boolean secured,
161+
ZonedDateTime timestamp) {
157162
this(c, listener, nettyRequest, compressionPredicate, connectionInfo, decoder, encoder, formDecoderProvider,
158-
httpMessageLogFactory, mapHandle, true, secured);
163+
httpMessageLogFactory, mapHandle, true, secured, timestamp);
159164
}
160165

161166
HttpServerOperations(Connection c, ConnectionObserver listener, HttpRequest nettyRequest,
@@ -167,7 +172,8 @@ class HttpServerOperations extends HttpOperations<HttpServerRequest, HttpServerR
167172
HttpMessageLogFactory httpMessageLogFactory,
168173
@Nullable BiFunction<? super Mono<Void>, ? super Connection, ? extends Mono<Void>> mapHandle,
169174
boolean resolvePath,
170-
boolean secured) {
175+
boolean secured,
176+
ZonedDateTime timestamp) {
171177
super(c, listener, httpMessageLogFactory);
172178
this.compressionPredicate = compressionPredicate;
173179
this.configuredCompressionPredicate = compressionPredicate;
@@ -189,6 +195,7 @@ class HttpServerOperations extends HttpOperations<HttpServerRequest, HttpServerR
189195
this.responseHeaders = nettyResponse.headers();
190196
this.responseHeaders.set(HttpHeaderNames.TRANSFER_ENCODING, HttpHeaderValues.CHUNKED);
191197
this.scheme = secured ? "https" : "http";
198+
this.timestamp = timestamp;
192199
}
193200

194201
@Override
@@ -472,6 +479,16 @@ public HttpHeaders responseHeaders() {
472479
return responseHeaders;
473480
}
474481

482+
@Override
483+
public String protocol() {
484+
return nettyRequest.protocolVersion().text();
485+
}
486+
487+
@Override
488+
public ZonedDateTime timestamp() {
489+
return timestamp;
490+
}
491+
475492
@Override
476493
public Mono<Void> send() {
477494
return FutureMono.deferFuture(() -> markSentHeaderAndBody() ?
@@ -767,7 +784,8 @@ static void sendDecodingFailures(
767784
boolean secure,
768785
Throwable t,
769786
Object msg,
770-
HttpMessageLogFactory httpMessageLogFactory) {
787+
HttpMessageLogFactory httpMessageLogFactory,
788+
@Nullable ZonedDateTime timestamp) {
771789

772790
Throwable cause = t.getCause() != null ? t.getCause() : t;
773791

@@ -798,7 +816,8 @@ else if (cause instanceof TooLongHttpHeaderException) {
798816
if (ops == null) {
799817
Connection conn = Connection.from(ctx.channel());
800818
if (msg instanceof HttpRequest) {
801-
ops = new FailedHttpServerRequest(conn, listener, (HttpRequest) msg, response, httpMessageLogFactory, secure);
819+
ops = new FailedHttpServerRequest(conn, listener, (HttpRequest) msg, response, httpMessageLogFactory, secure,
820+
timestamp == null ? ZonedDateTime.now(ReactorNetty.ZONE_ID_SYSTEM) : timestamp);
802821
ops.bind();
803822
}
804823
else {
@@ -967,9 +986,10 @@ static final class FailedHttpServerRequest extends HttpServerOperations {
967986
HttpRequest nettyRequest,
968987
HttpResponse nettyResponse,
969988
HttpMessageLogFactory httpMessageLogFactory,
970-
boolean secure) {
989+
boolean secure,
990+
ZonedDateTime timestamp) {
971991
super(c, listener, nettyRequest, null, null, ServerCookieDecoder.STRICT, ServerCookieEncoder.STRICT,
972-
DEFAULT_FORM_DECODER_SPEC, httpMessageLogFactory, null, false, secure);
992+
DEFAULT_FORM_DECODER_SPEC, httpMessageLogFactory, null, false, secure, timestamp);
973993
this.customResponse = nettyResponse;
974994
String tempPath = "";
975995
try {

reactor-netty-http/src/main/java/reactor/netty/http/server/HttpServerRequest.java

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright (c) 2011-2022 VMware, Inc. or its affiliates, All Rights Reserved.
2+
* Copyright (c) 2011-2023 VMware, Inc. or its affiliates, All Rights Reserved.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -16,6 +16,7 @@
1616
package reactor.netty.http.server;
1717

1818
import java.net.InetSocketAddress;
19+
import java.time.ZonedDateTime;
1920
import java.util.Map;
2021
import java.util.function.Consumer;
2122
import java.util.function.Function;
@@ -142,4 +143,19 @@ default Flux<HttpContent> receiveContent() {
142143
*/
143144
HttpHeaders requestHeaders();
144145

146+
/**
147+
* Returns the inbound protocol and version.
148+
*
149+
* @return the inbound protocol and version
150+
* @since 1.0.28
151+
*/
152+
String protocol();
153+
154+
/**
155+
* Returns the time when the request was received.
156+
*
157+
* @return the time when the request was received
158+
* @since 1.0.28
159+
*/
160+
ZonedDateTime timestamp();
145161
}

0 commit comments

Comments
 (0)