From d111d19071bc482426057558ec2a467b67e23fa4 Mon Sep 17 00:00:00 2001 From: "b.debeaubien" Date: Mon, 30 Mar 2015 17:23:50 -0400 Subject: [PATCH] #138 - if x-forwarded-proto is set, use that --- .../server/ApacheProxyAddressStrategy.java | 18 +++++++++++------- 1 file changed, 11 insertions(+), 7 deletions(-) diff --git a/hapi-fhir-base/src/main/java/ca/uhn/fhir/rest/server/ApacheProxyAddressStrategy.java b/hapi-fhir-base/src/main/java/ca/uhn/fhir/rest/server/ApacheProxyAddressStrategy.java index 987008f79f1..33cf0d9ac53 100644 --- a/hapi-fhir-base/src/main/java/ca/uhn/fhir/rest/server/ApacheProxyAddressStrategy.java +++ b/hapi-fhir-base/src/main/java/ca/uhn/fhir/rest/server/ApacheProxyAddressStrategy.java @@ -20,7 +20,6 @@ package ca.uhn.fhir.rest.server; * #L% */ -import ca.uhn.fhir.rest.server.IncomingRequestAddressStrategy; import org.apache.commons.lang3.StringUtils; import javax.servlet.ServletContext; @@ -30,11 +29,12 @@ import javax.servlet.http.HttpServletRequest; * Works like the normal {@link ca.uhn.fhir.rest.server.IncomingRequestAddressStrategy} unless there's an * x-forwarded-host present, in which case that's used in place of the server's address. * - * Because Apache Http Server's mod_proxy doesn't supply x-forwarded-proto, you have to tell it whether to use http or https - * by using the appropriate factory method. + * If the Apache Http Server mod_proxy isn't configured to supply x-forwarded-proto, the factory method that you use + * to create the address strategy will determine the default. Note that mod_proxy doesn't set this by default, but it can be + * configured via RequestHeader set X-Forwarded-Proto http (or https) * - * If you want to make that determination based on something other than the constructor argument, you should be able to do so - * by overriding prefix. + * If you want to set the protocol based on something other than the constructor argument, you should be able to do so + * by overriding protocol. * * Note that while this strategy was designed to work with Apache Http Server, and has been tested against it, it should work with * any proxy server that sets x-forwarded-host @@ -65,13 +65,17 @@ public class ApacheProxyAddressStrategy extends IncomingRequestAddressStrategy { forwardedHost = forwardedHost.substring(0, commaPos - 1); } String requestFullPath = StringUtils.defaultString(theRequest.getRequestURI()); - String serverBase = prefix(theRequest) + forwardedHost + requestFullPath; + String serverBase = protocol(theRequest) + forwardedHost + requestFullPath; return serverBase; } return super.determineServerBase(theServletContext, theRequest); } - protected String prefix(HttpServletRequest theRequest) { + protected String protocol(HttpServletRequest theRequest) { + String protocol = theRequest.getHeader("x-forwarded-proto"); + if (protocol != null) { + return protocol + "://"; + } return myUseHttps ? "https://" : "http://"; } }