2424import java .io .InputStream ;
2525import java .lang .management .ManagementFactory ;
2626import java .net .URL ;
27+ import java .util .Arrays ;
2728import java .util .Properties ;
2829
30+ import com .cloud .api .ApiServer ;
2931import org .apache .commons .daemon .Daemon ;
3032import org .apache .commons .daemon .DaemonContext ;
3133import org .apache .commons .lang3 .StringUtils ;
3234import org .eclipse .jetty .jmx .MBeanContainer ;
35+ import org .eclipse .jetty .server .ForwardedRequestCustomizer ;
3336import org .eclipse .jetty .server .HttpConfiguration ;
3437import org .eclipse .jetty .server .HttpConnectionFactory ;
3538import org .eclipse .jetty .server .RequestLog ;
@@ -184,6 +187,7 @@ public void start() throws Exception {
184187 httpConfig .setResponseHeaderSize (8192 );
185188 httpConfig .setSendServerVersion (false );
186189 httpConfig .setSendDateHeader (false );
190+ addForwardingCustomiser (httpConfig );
187191
188192 // HTTP Connector
189193 createHttpConnector (httpConfig );
@@ -206,6 +210,21 @@ public void start() throws Exception {
206210 server .join ();
207211 }
208212
213+ /**
214+ * Adds a ForwardedRequestCustomizer to the HTTP configuration to handle forwarded headers.
215+ * The header used for forwarding is determined by the ApiServer.listOfForwardHeaders property.
216+ * Only non empty headers are considered and only the first of the comma-separated list is used.
217+ * @param httpConfig the HTTP configuration to which the customizer will be added
218+ */
219+ private static void addForwardingCustomiser (HttpConfiguration httpConfig ) {
220+ ForwardedRequestCustomizer customiser = new ForwardedRequestCustomizer ();
221+ String header = Arrays .stream (ApiServer .listOfForwardHeaders .value ().split ("," )).findFirst ().orElse (null );
222+ if (com .cloud .utils .StringUtils .isNotEmpty (header )) {
223+ customiser .setForwardedForHeader (header );
224+ }
225+ httpConfig .addCustomizer (customiser );
226+ }
227+
209228 @ Override
210229 public void stop () throws Exception {
211230 server .stop ();
0 commit comments