@@ -98,25 +98,31 @@ private ConnectionInfo parseXForwardedInfo(ConnectionInfo connectionInfo, HttpRe
9898 }
9999 String hostHeader = request .headers ().get (X_FORWARDED_HOST_HEADER );
100100 if (hostHeader != null ) {
101- String scheme = connectionInfo .getScheme ();
102- int port = scheme .equalsIgnoreCase ("https" ) || scheme .equalsIgnoreCase ("wss" ) ?
103- DEFAULT_HTTPS_PORT : DEFAULT_HTTP_PORT ;
104101 connectionInfo = connectionInfo .withHostAddress (
105- AddressUtils .parseAddress (hostHeader .split ("," , 2 )[0 ].trim (), port , DEFAULT_FORWARDED_HEADER_VALIDATION ));
106- String portHeader = request .headers ().get (X_FORWARDED_PORT_HEADER );
107- if (portHeader != null && !portHeader .isEmpty ()) {
108- String portStr = portHeader .split ("," , 2 )[0 ].trim ();
109- if (portStr .chars ().allMatch (Character ::isDigit )) {
110- port = Integer .parseInt (portStr );
111- }
112- else if (DEFAULT_FORWARDED_HEADER_VALIDATION ) {
113- throw new IllegalArgumentException ("Failed to parse a port from " + portHeader );
114- }
115- connectionInfo = connectionInfo .withHostAddress (
116- AddressUtils .createUnresolved (connectionInfo .getHostAddress ().getHostString (), port ));
102+ AddressUtils .parseAddress (hostHeader .split ("," , 2 )[0 ].trim (),
103+ getDefaultHostPort (connectionInfo ), DEFAULT_FORWARDED_HEADER_VALIDATION ));
104+ }
105+
106+ String portHeader = request .headers ().get (X_FORWARDED_PORT_HEADER );
107+ if (portHeader != null && !portHeader .isEmpty ()) {
108+ String portStr = portHeader .split ("," , 2 )[0 ].trim ();
109+ if (portStr .chars ().allMatch (Character ::isDigit )) {
110+ int port = Integer .parseInt (portStr );
111+ connectionInfo = new ConnectionInfo (
112+ AddressUtils .createUnresolved (connectionInfo .getHostAddress ().getHostString (), port ),
113+ connectionInfo .getHostName (), port , connectionInfo .getRemoteAddress (), connectionInfo .getScheme ());
114+ }
115+ else if (DEFAULT_FORWARDED_HEADER_VALIDATION ) {
116+ throw new IllegalArgumentException ("Failed to parse a port from " + portHeader );
117117 }
118118 }
119119 return connectionInfo ;
120120 }
121121
122+ private int getDefaultHostPort (ConnectionInfo connectionInfo ) {
123+ String scheme = connectionInfo .getScheme ();
124+ return scheme .equalsIgnoreCase ("https" ) || scheme .equalsIgnoreCase ("wss" ) ?
125+ DEFAULT_HTTPS_PORT : DEFAULT_HTTP_PORT ;
126+ }
127+
122128}
0 commit comments