cloudstack-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From ko...@apache.org
Subject [05/11] git commit: updated refs/heads/master to 31db58f
Date Sun, 23 Nov 2014 19:49:13 GMT
Some simplification on getClientAddress in ApiServlet

Signed-off-by: Laszlo Hornyak <laszlo.hornyak@gmail.com>


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

Branch: refs/heads/master
Commit: 2a6d6891eb734cb0d229d650689786697dcae46d
Parents: fa52ffa
Author: Laszlo Hornyak <laszlo.hornyak@gmail.com>
Authored: Sat Nov 22 20:57:23 2014 +0100
Committer: Laszlo Hornyak <laszlo.hornyak@gmail.com>
Committed: Sun Nov 23 19:48:58 2014 +0100

----------------------------------------------------------------------
 server/src/com/cloud/api/ApiServlet.java | 39 +++++++++------------------
 1 file changed, 13 insertions(+), 26 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/cloudstack/blob/2a6d6891/server/src/com/cloud/api/ApiServlet.java
----------------------------------------------------------------------
diff --git a/server/src/com/cloud/api/ApiServlet.java b/server/src/com/cloud/api/ApiServlet.java
index 75f7ed2..e1f7927 100644
--- a/server/src/com/cloud/api/ApiServlet.java
+++ b/server/src/com/cloud/api/ApiServlet.java
@@ -18,7 +18,10 @@ package com.cloud.api;
 
 import java.io.UnsupportedEncodingException;
 import java.net.URLDecoder;
+import java.util.Arrays;
+import java.util.Collections;
 import java.util.HashMap;
+import java.util.List;
 import java.util.Map;
 
 import javax.inject.Inject;
@@ -54,6 +57,9 @@ import com.cloud.utils.net.NetUtils;
 public class ApiServlet extends HttpServlet {
     public static final Logger s_logger = Logger.getLogger(ApiServlet.class.getName());
     private static final Logger s_accessLogger = Logger.getLogger("apiserver." + ApiServer.class.getName());
+    private final static List<String> s_clientAddressHeaders = Collections
+            .unmodifiableList(Arrays.asList("X-Forwarded-For",
+                    "HTTP_CLIENT_IP", "HTTP_X_FORWARDED_FOR", "Remote_Addr"));
 
     @Inject
     ApiServerService _apiServer;
@@ -309,34 +315,15 @@ public class ApiServlet extends HttpServlet {
     }
 
     //This method will try to get login IP of user even if servlet is behind reverseProxy
or loadBalancer
-    static String getClientAddress(HttpServletRequest request) {
-        String ip = null;
-        ip = request.getHeader("X-Forwarded-For");
-        ip = getCorrectIPAddress(ip);
-        if (ip != null) {
-            return ip;
-        }
-
-        ip = request.getHeader("HTTP_CLIENT_IP");
-        ip = getCorrectIPAddress(ip);
-        if (ip != null) {
-            return ip;
-        }
-
-        ip = request.getHeader("HTTP_X_FORWARDED_FOR");
-        ip = getCorrectIPAddress(ip);
-        if (ip != null) {
-            return ip;
-        }
-
-        ip = request.getHeader("Remote_Addr");
-        ip = getCorrectIPAddress(ip);
-        if (ip != null) {
-            return ip;
+    static String getClientAddress(final HttpServletRequest request) {
+        for(final String header : s_clientAddressHeaders) {
+            final String ip = getCorrectIPAddress(request.getHeader(header));
+            if (ip != null) {
+                return ip;
+            }
         }
 
-        ip = request.getRemoteAddr();
-        return ip;
+        return request.getRemoteAddr();
     }
 
     private static String getCorrectIPAddress(String ip) {


Mime
View raw message