added support for the Forwarded header (RFC7239)
Project: http://git-wip-us.apache.org/repos/asf/struts-extras/repo Commit: http://git-wip-us.apache.org/repos/asf/struts-extras/commit/ba12972c Tree: http://git-wip-us.apache.org/repos/asf/struts-extras/tree/ba12972c Diff: http://git-wip-us.apache.org/repos/asf/struts-extras/diff/ba12972c Branch: refs/heads/master Commit: ba12972cbc0e030a20dec11cf6a0b4dd2360aafd Parents: 785a77f Author: Stefaan Dutry <stefaan.du...@gmail.com> Authored: Tue Mar 28 13:24:52 2017 +0200 Committer: Stefaan Dutry <stefaan.du...@gmail.com> Committed: Tue Mar 28 13:24:52 2017 +0200 ---------------------------------------------------------------------- .../HttpsOffloadAwareServletRedirectResult.java | 29 +++++++++++++++++++- 1 file changed, 28 insertions(+), 1 deletion(-) ---------------------------------------------------------------------- http://git-wip-us.apache.org/repos/asf/struts-extras/blob/ba12972c/struts2-custom-results-plugin/src/main/java/org/apache/struts2/result/HttpsOffloadAwareServletRedirectResult.java ---------------------------------------------------------------------- diff --git a/struts2-custom-results-plugin/src/main/java/org/apache/struts2/result/HttpsOffloadAwareServletRedirectResult.java b/struts2-custom-results-plugin/src/main/java/org/apache/struts2/result/HttpsOffloadAwareServletRedirectResult.java index 5c70339..3431467 100644 --- a/struts2-custom-results-plugin/src/main/java/org/apache/struts2/result/HttpsOffloadAwareServletRedirectResult.java +++ b/struts2-custom-results-plugin/src/main/java/org/apache/struts2/result/HttpsOffloadAwareServletRedirectResult.java @@ -22,8 +22,11 @@ package org.apache.struts2.result; import java.util.Collection; +import java.util.Enumeration; import java.util.List; import java.util.Map; +import java.util.regex.Pattern; +import java.util.regex.Matcher; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; @@ -44,6 +47,7 @@ import com.opensymphony.xwork2.inject.Inject; public class HttpsOffloadAwareServletRedirectResult extends ServletRedirectResult { private static final long serialVersionUID = -5384946213381645549L; private static final Logger LOG = LogManager.getLogger(HttpsOffloadAwareServletRedirectResult.class); + private static final Pattern FORWARDED_PROTO_PARAM_HTTPS = Pattern.compile("[^;]proto=https[$;]"); private UrlHelper urlHelper; @@ -155,7 +159,30 @@ public class HttpsOffloadAwareServletRedirectResult extends ServletRedirectResul } protected boolean shouldFixScheme(HttpServletRequest request) { - return "https".equals(request.getHeader("X-Forwarded-Proto")); + return "https".equals(request.getHeader("X-Forwarded-Proto")) || hasForwardedHeaderWithProtoParamHttps(request); + } + + private boolean hasForwardedHeaderWithProtoParamHttps(HttpServletRequest request) { + Enumeration<String> forwardedHeaders = request.getHeaders("Forwarded"); + + if (forwardedHeaders == null) { + return false; + } + + while (forwardedHeaders.hasMoreElements()) { + String forwardedHeader = forwardedHeaders.nextElement(); + String[] forwardedHeaderElements = forwardedHeader.split(","); + + for (String forwardedHeaderElement : forwardedHeaderElements) { + Matcher matcher = FORWARDED_PROTO_PARAM_HTTPS.matcher(forwardedHeaderElement.trim()); + + if (matcher.matches()) { + return true; + } + } + } + + return false; } }