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 .NCSARequestLog ;
@@ -185,6 +188,7 @@ public void start() throws Exception {
185188 httpConfig .setResponseHeaderSize (8192 );
186189 httpConfig .setSendServerVersion (false );
187190 httpConfig .setSendDateHeader (false );
191+ addForwordingCustomiser (httpConfig );
188192
189193 // HTTP Connector
190194 createHttpConnector (httpConfig );
@@ -207,6 +211,21 @@ public void start() throws Exception {
207211 server .join ();
208212 }
209213
214+ /**
215+ * Adds a ForwardedRequestCustomizer to the HTTP configuration to handle forwarded headers.
216+ * The header used for forwarding is determined by the ApiServer.listOfForwardHeaders property.
217+ * Only non empty headers are considdered and only the first of the comma-separated list is used.
218+ * @param httpConfig the HTTP configuration to which the customizer will be added
219+ */
220+ private static void addForwordingCustomiser (HttpConfiguration httpConfig ) {
221+ ForwardedRequestCustomizer customiser = new ForwardedRequestCustomizer ();
222+ String header = Arrays .stream (ApiServer .listOfForwardHeaders .value ().split ("," )).findFirst ().orElse (null );
223+ if (com .cloud .utils .StringUtils .isNotEmpty (header )) {
224+ customiser .setForwardedForHeader (header );
225+ }
226+ httpConfig .addCustomizer (customiser );
227+ }
228+
210229 @ Override
211230 public void stop () throws Exception {
212231 server .stop ();
0 commit comments