Skip to content

Commit

Permalink
Merge #2606 into 1.1.1
Browse files Browse the repository at this point in the history
  • Loading branch information
pderop committed Dec 12, 2022
2 parents 26623fb + 76e1fcf commit a74c684
Show file tree
Hide file tree
Showing 12 changed files with 208 additions and 32 deletions.
4 changes: 4 additions & 0 deletions reactor-netty-http/build.gradle
Expand Up @@ -240,6 +240,10 @@ task japicmp(type: JapicmpTask) {
includeSynthetic = true

compatibilityChangeExcludes = [ "METHOD_NEW_DEFAULT" ]
methodExcludes = [
'reactor.netty.http.server.logging.AccessLogArgProvider#connectionInformation()',
'reactor.netty.http.server.HttpServerRequest#scheme()'
]
}

tasks.japicmp.dependsOn(downloadBaseline)
Expand Down
@@ -0,0 +1,77 @@
/*
* Copyright (c) 2022 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 reactor.util.annotation.Nullable;

import java.net.SocketAddress;

/**
* Resolve informations about the connection from which an http request is received.
*
* @since 1.0.26
*/
public interface ConnectionInformation {

/**
* Returns the address of the host which received the request, possibly {@code null} in case of Unix Domain Sockets.
* The returned address is the merged informations from all proxies.
*
* @return the address merged from all proxies of the host which received the request
*/
@Nullable
SocketAddress hostAddress();

/**
* Returns the address of the host which received the request, possibly {@code null} in case of Unix Domain Sockets.
*
* @return the address of the host which received the request
*/
@Nullable
SocketAddress connectionHostAddress();

/**
* Returns the address of the client that initiated the request, possibly {@code null} in case of Unix Domain Sockets.
* The returned address is the merged informations from all proxies.
*
* @return the address merged from all proxies of the client that initiated the request
*/
@Nullable
SocketAddress remoteAddress();

/**
* Returns the address of the client that initiated the request, possibly {@code null} in case of Unix Domain Sockets.
*
* @return the address of the client that initiated the request
*/
@Nullable
SocketAddress connectionRemoteAddress();

/**
* Returns the current protocol scheme.
* The returned address is the merged informations from all proxies.
*
* @return the protocol scheme merged from all proxies
*/
String scheme();

/**
* Returns the current protocol scheme.
*
* @return the protocol scheme
*/
String connectionScheme();
}
@@ -1,5 +1,5 @@
/*
* Copyright (c) 2021 VMware, Inc. or its affiliates, All Rights Reserved.
* Copyright (c) 2021-2022 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.
Expand Down Expand Up @@ -28,7 +28,7 @@
* @author Violeta Georgieva
* @since 1.0.8
*/
public interface HttpServerInfos extends HttpInfos {
public interface HttpServerInfos extends HttpInfos, ConnectionInformation {

/**
* Returns resolved HTTP cookies. As opposed to {@link #cookies()}, this
Expand Down
Expand Up @@ -17,6 +17,7 @@

import java.io.IOException;
import java.net.InetSocketAddress;
import java.net.SocketAddress;
import java.nio.file.Files;
import java.nio.file.Path;
import java.util.HashSet;
Expand Down Expand Up @@ -415,6 +416,12 @@ public InetSocketAddress hostAddress() {
}
}

@Override
@Nullable
public SocketAddress connectionHostAddress() {
return channel().localAddress();
}

@Override
@Nullable
public InetSocketAddress remoteAddress() {
Expand All @@ -426,6 +433,12 @@ public InetSocketAddress remoteAddress() {
}
}

@Override
@Nullable
public SocketAddress connectionRemoteAddress() {
return channel().remoteAddress();
}

@Override
public HttpHeaders requestHeaders() {
if (nettyRequest != null) {
Expand All @@ -444,6 +457,11 @@ public String scheme() {
}
}

@Override
public String connectionScheme() {
return scheme;
}

@Override
public HttpHeaders responseHeaders() {
return responseHeaders;
Expand Down
@@ -1,5 +1,5 @@
/*
* Copyright (c) 2011-2021 VMware, Inc. or its affiliates, All Rights Reserved.
* Copyright (c) 2011-2022 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.
Expand Down Expand Up @@ -127,20 +127,12 @@ default Flux<HttpContent> receiveContent() {
*/
Flux<HttpData> receiveForm(Consumer<HttpServerFormDecoderProvider.Builder> formDecoderBuilder);

/**
* Returns the address of the host peer or {@code null} in case of Unix Domain Sockets.
*
* @return the host's address
*/
@Nullable
@Override
InetSocketAddress hostAddress();

/**
* Returns the address of the remote peer or {@code null} in case of Unix Domain Sockets.
*
* @return the peer's address
*/
@Nullable
@Override
InetSocketAddress remoteAddress();

/**
Expand All @@ -150,11 +142,4 @@ default Flux<HttpContent> receiveContent() {
*/
HttpHeaders requestHeaders();

/**
* Returns the current protocol scheme
*
* @return the protocol scheme
*/
String scheme();

}
@@ -1,5 +1,5 @@
/*
* Copyright (c) 2020-2021 VMware, Inc. or its affiliates, All Rights Reserved.
* Copyright (c) 2020-2022 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.
Expand All @@ -18,6 +18,7 @@
import io.netty.handler.codec.http.HttpMethod;
import io.netty.handler.codec.http.cookie.Cookie;
import reactor.netty.ReactorNetty;
import reactor.netty.http.server.ConnectionInformation;
import reactor.util.annotation.Nullable;

import java.net.SocketAddress;
Expand All @@ -39,6 +40,7 @@ abstract class AbstractAccessLogArgProvider<SELF extends AbstractAccessLogArgPro

final SocketAddress remoteAddress;
final String user = MISSING;
ConnectionInformation connectionInfo;
String zonedDateTime;
ZonedDateTime accessDateTime;
CharSequence method;
Expand Down Expand Up @@ -68,10 +70,17 @@ public ZonedDateTime accessDateTime() {

@Override
@Nullable
@SuppressWarnings("deprecation")
public SocketAddress remoteAddress() {
return remoteAddress;
}

@Override
@Nullable
public ConnectionInformation connectionInformation() {
return connectionInfo;
}

@Override
@Nullable
public CharSequence method() {
Expand Down Expand Up @@ -135,6 +144,7 @@ void clear() {
this.contentLength = -1;
this.startTime = 0;
this.cookies = null;
this.connectionInfo = null;
}

SELF cookies(Map<CharSequence, Set<Cookie>> cookies) {
Expand All @@ -157,4 +167,9 @@ SELF increaseContentLength(long contentLength) {
return get();
}

SELF connectionInformation(ConnectionInformation connectionInfo) {
this.connectionInfo = connectionInfo;
return get();
}

}
@@ -1,5 +1,5 @@
/*
* Copyright (c) 2020-2021 VMware, Inc. or its affiliates, All Rights Reserved.
* Copyright (c) 2020-2022 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.
Expand All @@ -16,12 +16,14 @@
package reactor.netty.http.server.logging;

import io.netty.handler.codec.http.cookie.Cookie;
import reactor.netty.http.server.ConnectionInformation;
import reactor.util.annotation.Nullable;

import java.net.SocketAddress;
import java.time.ZonedDateTime;
import java.util.Map;
import java.util.Set;
import java.util.function.BiFunction;

/**
* A provider of the args required for access log.
Expand Down Expand Up @@ -54,10 +56,25 @@ public interface AccessLogArgProvider {
* Returns the address of the remote peer or {@code null} in case of Unix Domain Sockets.
*
* @return the peer's address
* @deprecated as of 1.0.26. Use {@link ConnectionInformation#connectionRemoteAddress()}
*
*/
@Nullable
@Deprecated
SocketAddress remoteAddress();

/**
* Returns the information about the current connection.
* <p> Note that the {@link ConnectionInformation#remoteAddress()} will return the forwarded
* remote client address if the server is configured in forwarded mode.
*
* @since 1.0.26
* @return the connection info
* @see reactor.netty.http.server.HttpServer#forwarded(BiFunction)
*/
@Nullable
ConnectionInformation connectionInformation();

/**
* Returns the name of this method, (e.g. "GET").
*
Expand Down
@@ -1,5 +1,5 @@
/*
* Copyright (c) 2018-2021 VMware, Inc. or its affiliates, All Rights Reserved.
* Copyright (c) 2018-2022 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.
Expand All @@ -26,7 +26,7 @@
import io.netty.handler.codec.http.HttpUtil;
import io.netty.handler.codec.http.LastHttpContent;
import reactor.netty.channel.ChannelOperations;
import reactor.netty.http.HttpInfos;
import reactor.netty.http.server.HttpServerInfos;
import reactor.util.annotation.Nullable;

import java.util.function.Function;
Expand Down Expand Up @@ -79,8 +79,8 @@ public void write(ChannelHandlerContext ctx, Object msg, ChannelPromise promise)
}

ChannelOperations<?, ?> ops = ChannelOperations.get(ctx.channel());
if (ops instanceof HttpInfos) {
accessLogArgProvider.cookies(((HttpInfos) ops).cookies());
if (ops instanceof HttpServerInfos) {
super.applyServerInfos(accessLogArgProvider, (HttpServerInfos) ops);
}
}
if (msg instanceof LastHttpContent) {
Expand Down
@@ -1,5 +1,5 @@
/*
* Copyright (c) 2018-2021 VMware, Inc. or its affiliates, All Rights Reserved.
* Copyright (c) 2018-2022 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.
Expand All @@ -21,7 +21,7 @@
import io.netty.handler.codec.http2.Http2DataFrame;
import io.netty.handler.codec.http2.Http2HeadersFrame;
import reactor.netty.channel.ChannelOperations;
import reactor.netty.http.HttpInfos;
import reactor.netty.http.server.HttpServerInfos;
import reactor.util.annotation.Nullable;

import java.util.function.Function;
Expand Down Expand Up @@ -65,8 +65,8 @@ public void write(ChannelHandlerContext ctx, Object msg, ChannelPromise promise)
.chunked(true);

ChannelOperations<?, ?> ops = ChannelOperations.get(ctx.channel());
if (ops instanceof HttpInfos) {
accessLogArgProvider.cookies(((HttpInfos) ops).cookies());
if (ops instanceof HttpServerInfos) {
super.applyServerInfos(accessLogArgProvider, (HttpServerInfos) ops);
}
}
if (msg instanceof Http2DataFrame) {
Expand Down
@@ -1,5 +1,5 @@
/*
* Copyright (c) 2020-2021 VMware, Inc. or its affiliates, All Rights Reserved.
* Copyright (c) 2020-2022 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.
Expand All @@ -16,6 +16,7 @@
package reactor.netty.http.server.logging;

import io.netty.channel.ChannelDuplexHandler;
import reactor.netty.http.server.HttpServerInfos;
import reactor.util.annotation.Nullable;

import java.net.InetSocketAddress;
Expand Down Expand Up @@ -48,4 +49,9 @@ static String applyAddress(@Nullable SocketAddress socketAddress) {
return socketAddress instanceof InetSocketAddress ? ((InetSocketAddress) socketAddress).getHostString() : MISSING;
}

final <T extends AbstractAccessLogArgProvider<T>> void applyServerInfos(AbstractAccessLogArgProvider<T> accessLogArgs, HttpServerInfos serverInfos) {
accessLogArgs.cookies(serverInfos.cookies());
accessLogArgs.connectionInformation(serverInfos);
}

}

0 comments on commit a74c684

Please sign in to comment.