11/*
2- * Copyright (c) 2018-2021 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.
2121
2222import io .netty .channel .Channel ;
2323import io .netty .channel .socket .SocketChannel ;
24+ import io .netty .handler .codec .http .HttpHeaderNames ;
2425import io .netty .handler .codec .http .HttpRequest ;
2526import reactor .util .annotation .Nullable ;
2627
4142 * @see <a href="https://tools.ietf.org/html/rfc7239">rfc7239</a>
4243 */
4344public final class ConnectionInfo {
45+ static final int DEFAULT_HTTP_PORT = 80 ;
46+ static final int DEFAULT_HTTPS_PORT = 443 ;
47+ static final String DEFAULT_HOST_NAME = "localhost" ;
4448
4549 final InetSocketAddress hostAddress ;
4650
4751 final InetSocketAddress remoteAddress ;
4852
4953 final String scheme ;
5054
51- @ Nullable
55+ final String hostName ;
56+
57+ final int hostPort ;
58+
5259 static ConnectionInfo from (Channel channel , HttpRequest request , boolean secured , SocketAddress remoteAddress ,
5360 @ Nullable BiFunction <ConnectionInfo , HttpRequest , ConnectionInfo > forwardedHeaderHandler ) {
61+ String hostName = DEFAULT_HOST_NAME ;
62+ int hostPort = secured ? DEFAULT_HTTPS_PORT : DEFAULT_HTTP_PORT ;
63+ String scheme = secured ? "https" : "http" ;
64+
65+ String header = request .headers ().get (HttpHeaderNames .HOST );
66+ if (header != null ) {
67+ hostName = header ;
68+ int portIndex = header .charAt (0 ) == '[' ? header .indexOf (':' , header .indexOf (']' )) : header .indexOf (':' );
69+ if (portIndex != -1 ) {
70+ hostName = header .substring (0 , portIndex );
71+ hostPort = Integer .parseInt (header .substring (portIndex + 1 ));
72+ }
73+ }
74+
5475 if (!(remoteAddress instanceof InetSocketAddress )) {
55- return null ;
76+ return new ConnectionInfo ( hostName , hostPort , scheme ) ;
5677 }
5778 else {
58- ConnectionInfo connectionInfo = ConnectionInfo .newConnectionInfo (channel , secured , (InetSocketAddress ) remoteAddress );
79+ ConnectionInfo connectionInfo =
80+ new ConnectionInfo (((SocketChannel ) channel ).localAddress (), hostName , hostPort ,
81+ (InetSocketAddress ) remoteAddress , scheme );
5982 if (forwardedHeaderHandler != null ) {
6083 return forwardedHeaderHandler .apply (connectionInfo , request );
6184 }
6285 return connectionInfo ;
6386 }
6487 }
6588
66- /**
67- * Retrieve the connection information from the current connection directly
68- * @param c the current channel
69- * @param secured is transport secure (SSL)
70- * @return the connection information
71- */
72- static ConnectionInfo newConnectionInfo (Channel c , boolean secured , InetSocketAddress remoteAddress ) {
73- SocketChannel channel = (SocketChannel ) c ;
74- InetSocketAddress hostAddress = channel .localAddress ();
75- String scheme = secured ? "https" : "http" ;
76- return new ConnectionInfo (hostAddress , remoteAddress , scheme );
89+ ConnectionInfo (String hostName , int hostPort , String scheme ) {
90+ this (null , hostName , hostPort , null , scheme );
7791 }
7892
79- ConnectionInfo (InetSocketAddress hostAddress , InetSocketAddress remoteAddress , String scheme ) {
93+ ConnectionInfo (@ Nullable InetSocketAddress hostAddress , String hostName , int hostPort ,
94+ @ Nullable InetSocketAddress remoteAddress , String scheme ) {
8095 this .hostAddress = hostAddress ;
96+ this .hostName = hostName ;
97+ this .hostPort = hostPort ;
8198 this .remoteAddress = remoteAddress ;
8299 this .scheme = scheme ;
83100 }
@@ -86,6 +103,7 @@ static ConnectionInfo newConnectionInfo(Channel c, boolean secured, InetSocketAd
86103 * Return the host address of the connection.
87104 * @return the host address
88105 */
106+ @ Nullable
89107 public InetSocketAddress getHostAddress () {
90108 return hostAddress ;
91109 }
@@ -94,6 +112,7 @@ public InetSocketAddress getHostAddress() {
94112 * Return the remote address of the connection.
95113 * @return the remote address
96114 */
115+ @ Nullable
97116 public InetSocketAddress getRemoteAddress () {
98117 return remoteAddress ;
99118 }
@@ -113,7 +132,7 @@ public String getScheme() {
113132 */
114133 public ConnectionInfo withHostAddress (InetSocketAddress hostAddress ) {
115134 requireNonNull (hostAddress , "hostAddress" );
116- return new ConnectionInfo (hostAddress , this .remoteAddress , this .scheme );
135+ return new ConnectionInfo (hostAddress , hostAddress . getHostString (), hostAddress . getPort (), this .remoteAddress , this .scheme );
117136 }
118137
119138 /**
@@ -123,7 +142,7 @@ public ConnectionInfo withHostAddress(InetSocketAddress hostAddress) {
123142 */
124143 public ConnectionInfo withRemoteAddress (InetSocketAddress remoteAddress ) {
125144 requireNonNull (remoteAddress , "remoteAddress" );
126- return new ConnectionInfo (this .hostAddress , remoteAddress , this .scheme );
145+ return new ConnectionInfo (this .hostAddress , this . hostName , this . hostPort , remoteAddress , this .scheme );
127146 }
128147
129148 /**
@@ -133,7 +152,14 @@ public ConnectionInfo withRemoteAddress(InetSocketAddress remoteAddress) {
133152 */
134153 public ConnectionInfo withScheme (String scheme ) {
135154 requireNonNull (scheme , "scheme" );
136- return new ConnectionInfo (this .hostAddress , this .remoteAddress , scheme );
155+ return new ConnectionInfo (this .hostAddress , this .hostName , this .hostPort , this .remoteAddress , scheme );
156+ }
157+
158+ String getHostName () {
159+ return hostName ;
137160 }
138161
162+ int getHostPort () {
163+ return hostPort ;
164+ }
139165}
0 commit comments