You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@knox.apache.org by km...@apache.org on 2014/10/31 18:10:53 UTC

git commit: KNOX-464: Location headers have wrong hostname when used behind load balancer

Repository: knox
Updated Branches:
  refs/heads/master ae64e7dfd -> f1f4fa223


KNOX-464: Location headers have wrong hostname when used behind load balancer


Project: http://git-wip-us.apache.org/repos/asf/knox/repo
Commit: http://git-wip-us.apache.org/repos/asf/knox/commit/f1f4fa22
Tree: http://git-wip-us.apache.org/repos/asf/knox/tree/f1f4fa22
Diff: http://git-wip-us.apache.org/repos/asf/knox/diff/f1f4fa22

Branch: refs/heads/master
Commit: f1f4fa2239c2218405e19d65bb12f89ee31305e8
Parents: ae64e7d
Author: Kevin Minder <ke...@hortonworks.com>
Authored: Fri Oct 31 13:10:47 2014 -0400
Committer: Kevin Minder <ke...@hortonworks.com>
Committed: Fri Oct 31 13:10:47 2014 -0400

----------------------------------------------------------------------
 .../filter/rewrite/impl/UrlRewriteResponse.java | 20 ++++++++++++++++++--
 1 file changed, 18 insertions(+), 2 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/knox/blob/f1f4fa22/gateway-provider-rewrite/src/main/java/org/apache/hadoop/gateway/filter/rewrite/impl/UrlRewriteResponse.java
----------------------------------------------------------------------
diff --git a/gateway-provider-rewrite/src/main/java/org/apache/hadoop/gateway/filter/rewrite/impl/UrlRewriteResponse.java b/gateway-provider-rewrite/src/main/java/org/apache/hadoop/gateway/filter/rewrite/impl/UrlRewriteResponse.java
index 05795cb..00eea7b 100644
--- a/gateway-provider-rewrite/src/main/java/org/apache/hadoop/gateway/filter/rewrite/impl/UrlRewriteResponse.java
+++ b/gateway-provider-rewrite/src/main/java/org/apache/hadoop/gateway/filter/rewrite/impl/UrlRewriteResponse.java
@@ -40,7 +40,9 @@ import javax.servlet.http.HttpServletResponse;
 import java.io.IOException;
 import java.io.InputStream;
 import java.io.OutputStream;
+import java.net.InetAddress;
 import java.net.URISyntaxException;
+import java.net.UnknownHostException;
 import java.util.Arrays;
 import java.util.Collections;
 import java.util.HashSet;
@@ -175,11 +177,25 @@ public class UrlRewriteResponse extends GatewayResponseWrapper implements Params
     }
   }
 
+  // KNOX-464: Doing this because Jetty only returns the string version of the IP address for request.getLocalName().
+  // Hopefully the local hostname will be cached so this will not be a significant performance hit.
+  // Previously this was an inline request.getServerName() but this ended up mixing the hostname from the Host header
+  // and the local port which was making load balancer configuration difficult if not impossible.
+  private String getRequestLocalHostName() {
+    String hostName = request.getLocalName();
+    try {
+      hostName = InetAddress.getByName( hostName ).getHostName();
+    } catch( UnknownHostException e ) {
+      // Ignore it and use the original hostname.
+    }
+    return hostName;
+  }
+
   private String getGatewayParam( String name ) {
     if( "url".equals( name ) ) {
-      return request.getScheme() + "://" + request.getServerName() + ":" + request.getLocalPort() + request.getContextPath();
+      return request.getScheme() + "://" + getRequestLocalHostName() + ":" + request.getLocalPort() + request.getContextPath();
     } else if( "address".equals( name ) ) {
-      return request.getServerName() + ":" + request.getLocalPort();
+      return getRequestLocalHostName() + ":" + request.getLocalPort();
     } else if( "path".equals( name ) ) {
       return request.getContextPath();
     } else {