From 3b4767384df79d7e560f23d62abc3ae27422d6af Mon Sep 17 00:00:00 2001 From: ly <641921791@qq.com> Date: Fri, 23 Aug 2019 00:02:00 +0800 Subject: [PATCH 1/3] [ISSUE] #1671 Unified request header "Client-Version" --- .../client/config/impl/HttpSimpleClient.java | 4 ++- .../nacos/client/naming/net/NamingProxy.java | 7 +++-- .../common/constant/HttpHeaderConsts.java | 26 +++++++++++++++++++ .../alibaba/nacos/naming/acl/AuthChecker.java | 8 ++---- .../naming/controllers/ApiController.java | 5 ++-- .../controllers/InstanceController.java | 11 +++----- .../alibaba/nacos/naming/misc/HttpClient.java | 8 +++--- .../nacos/naming/misc/NamingProxy.java | 20 ++++++++------ .../nacos/naming/web/DistroFilter.java | 3 ++- .../nacos/naming/web/TrafficReviseFilter.java | 6 ++--- .../alibaba/nacos/test/naming/NamingBase.java | 3 ++- 11 files changed, 64 insertions(+), 37 deletions(-) create mode 100644 common/src/main/java/com/alibaba/nacos/common/constant/HttpHeaderConsts.java diff --git a/client/src/main/java/com/alibaba/nacos/client/config/impl/HttpSimpleClient.java b/client/src/main/java/com/alibaba/nacos/client/config/impl/HttpSimpleClient.java index b8d7b554838..67499d649b7 100644 --- a/client/src/main/java/com/alibaba/nacos/client/config/impl/HttpSimpleClient.java +++ b/client/src/main/java/com/alibaba/nacos/client/config/impl/HttpSimpleClient.java @@ -20,7 +20,9 @@ import com.alibaba.nacos.client.config.utils.IOUtils; import com.alibaba.nacos.client.config.utils.MD5; import com.alibaba.nacos.client.utils.ParamUtil; +import com.alibaba.nacos.common.constant.HttpHeaderConsts; import com.alibaba.nacos.common.util.UuidUtils; +import com.alibaba.nacos.common.util.VersionUtils; import java.io.IOException; import java.io.UnsupportedEncodingException; @@ -201,7 +203,7 @@ static private void setHeaders(HttpURLConnection conn, List headers, Str conn.addRequestProperty(iter.next(), iter.next()); } } - conn.addRequestProperty("Client-Version", ParamUtil.getClientVersion()); + conn.addRequestProperty(HttpHeaderConsts.CLIENT_VERSION_HEADER, VersionUtils.VERSION); conn.addRequestProperty("Content-Type", "application/x-www-form-urlencoded;charset=" + encoding); String ts = String.valueOf(System.currentTimeMillis()); diff --git a/client/src/main/java/com/alibaba/nacos/client/naming/net/NamingProxy.java b/client/src/main/java/com/alibaba/nacos/client/naming/net/NamingProxy.java index 92de72fa776..3eec5d752cf 100644 --- a/client/src/main/java/com/alibaba/nacos/client/naming/net/NamingProxy.java +++ b/client/src/main/java/com/alibaba/nacos/client/naming/net/NamingProxy.java @@ -35,8 +35,10 @@ import com.alibaba.nacos.client.utils.AppNameUtils; import com.alibaba.nacos.client.utils.StringUtils; import com.alibaba.nacos.client.utils.TemplateUtils; +import com.alibaba.nacos.common.constant.HttpHeaderConsts; import com.alibaba.nacos.common.util.HttpMethod; import com.alibaba.nacos.common.util.UuidUtils; +import com.alibaba.nacos.common.util.VersionUtils; import java.io.IOException; import java.io.StringReader; @@ -499,8 +501,9 @@ private void checkSignature(Map params) { } public List builderHeaders() { - List headers = Arrays.asList("Client-Version", UtilAndComs.VERSION, - "User-Agent", UtilAndComs.VERSION, + List headers = Arrays.asList( + HttpHeaderConsts.CLIENT_VERSION_HEADER, VersionUtils.VERSION, + HttpHeaderConsts.USER_AGENT_HEADER, UtilAndComs.VERSION, "Accept-Encoding", "gzip,deflate,sdch", "Connection", "Keep-Alive", "RequestId", UuidUtils.generateUuid(), "Request-Module", "Naming"); diff --git a/common/src/main/java/com/alibaba/nacos/common/constant/HttpHeaderConsts.java b/common/src/main/java/com/alibaba/nacos/common/constant/HttpHeaderConsts.java new file mode 100644 index 00000000000..d3239f182bb --- /dev/null +++ b/common/src/main/java/com/alibaba/nacos/common/constant/HttpHeaderConsts.java @@ -0,0 +1,26 @@ +/* + * Copyright 1999-2018 Alibaba Group Holding Ltd. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package com.alibaba.nacos.common.constant; + +/** + * Nacos header constants + */ +public interface HttpHeaderConsts { + + String CLIENT_VERSION_HEADER = "Client-Version"; + String USER_AGENT_HEADER = "User-Agent"; + +} diff --git a/naming/src/main/java/com/alibaba/nacos/naming/acl/AuthChecker.java b/naming/src/main/java/com/alibaba/nacos/naming/acl/AuthChecker.java index be2545c658f..9e075162903 100644 --- a/naming/src/main/java/com/alibaba/nacos/naming/acl/AuthChecker.java +++ b/naming/src/main/java/com/alibaba/nacos/naming/acl/AuthChecker.java @@ -17,6 +17,7 @@ import com.alibaba.nacos.api.common.Constants; import com.alibaba.nacos.api.naming.CommonParams; +import com.alibaba.nacos.common.constant.HttpHeaderConsts; import com.alibaba.nacos.core.utils.WebUtils; import com.alibaba.nacos.naming.core.Service; import com.alibaba.nacos.naming.core.ServiceManager; @@ -52,12 +53,7 @@ public void doRaftAuth(HttpServletRequest req) throws Exception { return; } - String agent = req.getHeader("Client-Version"); - if (StringUtils.startsWith(agent, UtilsAndCommons.NACOS_SERVER_HEADER)) { - return; - } - - agent = req.getHeader("User-Agent"); + String agent = req.getHeader(HttpHeaderConsts.USER_AGENT_HEADER); if (StringUtils.startsWith(agent, UtilsAndCommons.NACOS_SERVER_HEADER)) { return; } diff --git a/naming/src/main/java/com/alibaba/nacos/naming/controllers/ApiController.java b/naming/src/main/java/com/alibaba/nacos/naming/controllers/ApiController.java index a9da7b0bb03..30c5fb1e195 100644 --- a/naming/src/main/java/com/alibaba/nacos/naming/controllers/ApiController.java +++ b/naming/src/main/java/com/alibaba/nacos/naming/controllers/ApiController.java @@ -19,6 +19,7 @@ import com.alibaba.nacos.api.common.Constants; import com.alibaba.nacos.api.naming.CommonParams; import com.alibaba.nacos.api.naming.utils.NamingUtils; +import com.alibaba.nacos.common.constant.HttpHeaderConsts; import com.alibaba.nacos.core.utils.WebUtils; import com.alibaba.nacos.naming.core.DistroMapper; import com.alibaba.nacos.naming.core.ServiceManager; @@ -61,7 +62,7 @@ public JSONObject allDomNames(HttpServletRequest request) throws Exception { JSONObject result = new JSONObject(); // For old DNS-F client: String dnsfVersion = "1.0.1"; - String agent = request.getHeader("Client-Version"); + String agent = request.getHeader(HttpHeaderConsts.USER_AGENT_HEADER); ClientInfo clientInfo = new ClientInfo(agent); if (clientInfo.type == ClientInfo.ClientType.DNS && clientInfo.version.compareTo(VersionUtil.parseVersion(dnsfVersion)) <= 0) { @@ -122,7 +123,7 @@ public JSONObject srvIPXT(HttpServletRequest request) throws Exception { Constants.DEFAULT_NAMESPACE_ID); String dom = WebUtils.required(request, "dom"); - String agent = request.getHeader("Client-Version"); + String agent = request.getHeader(HttpHeaderConsts.USER_AGENT_HEADER); String clusters = WebUtils.optional(request, "clusters", StringUtils.EMPTY); String clientIP = WebUtils.optional(request, "clientIP", StringUtils.EMPTY); Integer udpPort = Integer.parseInt(WebUtils.optional(request, "udpPort", "0")); diff --git a/naming/src/main/java/com/alibaba/nacos/naming/controllers/InstanceController.java b/naming/src/main/java/com/alibaba/nacos/naming/controllers/InstanceController.java index c8b4cb8290b..e6746b28d9a 100644 --- a/naming/src/main/java/com/alibaba/nacos/naming/controllers/InstanceController.java +++ b/naming/src/main/java/com/alibaba/nacos/naming/controllers/InstanceController.java @@ -21,6 +21,7 @@ import com.alibaba.nacos.api.common.Constants; import com.alibaba.nacos.api.naming.CommonParams; import com.alibaba.nacos.api.naming.utils.NamingUtils; +import com.alibaba.nacos.common.constant.HttpHeaderConsts; import com.alibaba.nacos.core.utils.WebUtils; import com.alibaba.nacos.naming.core.DistroMapper; import com.alibaba.nacos.naming.core.Instance; @@ -126,10 +127,7 @@ public String update(HttpServletRequest request) throws Exception { String serviceName = WebUtils.required(request, CommonParams.SERVICE_NAME); String namespaceId = WebUtils.optional(request, CommonParams.NAMESPACE_ID, Constants.DEFAULT_NAMESPACE_ID); - String agent = request.getHeader("Client-Version"); - if (StringUtils.isBlank(agent)) { - agent = request.getHeader("User-Agent"); - } + String agent = request.getHeader(HttpHeaderConsts.USER_AGENT_HEADER); ClientInfo clientInfo = new ClientInfo(agent); @@ -149,10 +147,7 @@ public JSONObject list(HttpServletRequest request) throws Exception { Constants.DEFAULT_NAMESPACE_ID); String serviceName = WebUtils.required(request, CommonParams.SERVICE_NAME); - String agent = request.getHeader("Client-Version"); - if (StringUtils.isBlank(agent)) { - agent = request.getHeader("User-Agent"); - } + String agent = request.getHeader(HttpHeaderConsts.USER_AGENT_HEADER); String clusters = WebUtils.optional(request, "clusters", StringUtils.EMPTY); String clientIP = WebUtils.optional(request, "clientIP", StringUtils.EMPTY); Integer udpPort = Integer.parseInt(WebUtils.optional(request, "udpPort", "0")); diff --git a/naming/src/main/java/com/alibaba/nacos/naming/misc/HttpClient.java b/naming/src/main/java/com/alibaba/nacos/naming/misc/HttpClient.java index 808d7016a6a..b3f4ab7f9b0 100644 --- a/naming/src/main/java/com/alibaba/nacos/naming/misc/HttpClient.java +++ b/naming/src/main/java/com/alibaba/nacos/naming/misc/HttpClient.java @@ -15,7 +15,9 @@ */ package com.alibaba.nacos.naming.misc; +import com.alibaba.nacos.common.constant.HttpHeaderConsts; import com.alibaba.nacos.common.util.HttpMethod; +import com.alibaba.nacos.common.util.VersionUtils; import com.ning.http.client.AsyncCompletionHandler; import com.ning.http.client.AsyncHttpClient; import com.ning.http.client.AsyncHttpClientConfig; @@ -102,8 +104,6 @@ public static HttpResult request(String url, List headers, Map headers, Str conn.addRequestProperty("Content-Type", "application/x-www-form-urlencoded;charset=" + encoding); conn.addRequestProperty("Accept-Charset", encoding); - conn.addRequestProperty("Client-Version", UtilsAndCommons.SERVER_VERSION); - conn.addRequestProperty("User-Agent", UtilsAndCommons.SERVER_VERSION); + conn.addRequestProperty(HttpHeaderConsts.CLIENT_VERSION_HEADER, VersionUtils.VERSION); + conn.addRequestProperty(HttpHeaderConsts.USER_AGENT_HEADER, UtilsAndCommons.SERVER_VERSION); } public static String encodingParams(Map params, String encoding) diff --git a/naming/src/main/java/com/alibaba/nacos/naming/misc/NamingProxy.java b/naming/src/main/java/com/alibaba/nacos/naming/misc/NamingProxy.java index 59347bb7d18..373dba6d96b 100644 --- a/naming/src/main/java/com/alibaba/nacos/naming/misc/NamingProxy.java +++ b/naming/src/main/java/com/alibaba/nacos/naming/misc/NamingProxy.java @@ -16,6 +16,8 @@ package com.alibaba.nacos.naming.misc; import com.alibaba.fastjson.JSON; +import com.alibaba.nacos.common.constant.HttpHeaderConsts; +import com.alibaba.nacos.common.util.VersionUtils; import com.alibaba.nacos.naming.boot.RunningConfig; import com.ning.http.client.AsyncCompletionHandler; import com.ning.http.client.Response; @@ -43,8 +45,8 @@ public static void syncCheckSums(Map checksumMap, String server) try { Map headers = new HashMap<>(128); - headers.put("Client-Version", UtilsAndCommons.SERVER_VERSION); - headers.put("User-Agent", UtilsAndCommons.SERVER_VERSION); + headers.put(HttpHeaderConsts.CLIENT_VERSION_HEADER, VersionUtils.VERSION); + headers.put(HttpHeaderConsts.USER_AGENT_HEADER, UtilsAndCommons.SERVER_VERSION); headers.put("Connection", "Keep-Alive"); HttpClient.asyncHttpPutLarge("http://" + server + RunningConfig.getContextPath() @@ -112,8 +114,8 @@ public static boolean syncData(byte[] data, String curServer) throws Exception { try { Map headers = new HashMap<>(128); - headers.put("Client-Version", UtilsAndCommons.SERVER_VERSION); - headers.put("User-Agent", UtilsAndCommons.SERVER_VERSION); + headers.put(HttpHeaderConsts.CLIENT_VERSION_HEADER, VersionUtils.VERSION); + headers.put(HttpHeaderConsts.USER_AGENT_HEADER, UtilsAndCommons.SERVER_VERSION); headers.put("Accept-Encoding", "gzip,deflate,sdch"); headers.put("Connection", "Keep-Alive"); headers.put("Content-Encoding", "gzip"); @@ -141,8 +143,9 @@ public static boolean syncData(byte[] data, String curServer) throws Exception { public static String reqAPI(String api, Map params, String curServer) throws Exception { try { - List headers = Arrays.asList("Client-Version", UtilsAndCommons.SERVER_VERSION, - "User-Agent", UtilsAndCommons.SERVER_VERSION, + List headers = Arrays.asList( + HttpHeaderConsts.CLIENT_VERSION_HEADER, VersionUtils.VERSION, + HttpHeaderConsts.USER_AGENT_HEADER, UtilsAndCommons.SERVER_VERSION, "Accept-Encoding", "gzip,deflate,sdch", "Connection", "Keep-Alive", "Content-Encoding", "gzip"); @@ -175,8 +178,9 @@ public static String reqAPI(String api, Map params, String curSe public static String reqAPI(String api, Map params, String curServer, boolean isPost) throws Exception { try { - List headers = Arrays.asList("Client-Version", UtilsAndCommons.SERVER_VERSION, - "User-Agent", UtilsAndCommons.SERVER_VERSION, + List headers = Arrays.asList( + HttpHeaderConsts.CLIENT_VERSION_HEADER, VersionUtils.VERSION, + HttpHeaderConsts.USER_AGENT_HEADER, UtilsAndCommons.SERVER_VERSION, "Accept-Encoding", "gzip,deflate,sdch", "Connection", "Keep-Alive", "Content-Encoding", "gzip"); diff --git a/naming/src/main/java/com/alibaba/nacos/naming/web/DistroFilter.java b/naming/src/main/java/com/alibaba/nacos/naming/web/DistroFilter.java index 78b51e138c3..85873163c55 100644 --- a/naming/src/main/java/com/alibaba/nacos/naming/web/DistroFilter.java +++ b/naming/src/main/java/com/alibaba/nacos/naming/web/DistroFilter.java @@ -17,6 +17,7 @@ import com.alibaba.nacos.api.common.Constants; import com.alibaba.nacos.api.naming.CommonParams; +import com.alibaba.nacos.common.constant.HttpHeaderConsts; import com.alibaba.nacos.naming.core.DistroMapper; import com.alibaba.nacos.naming.misc.HttpClient; import com.alibaba.nacos.naming.misc.Loggers; @@ -97,7 +98,7 @@ public void doFilter(ServletRequest servletRequest, ServletResponse servletRespo // proxy request to other server if necessary: if (method.isAnnotationPresent(CanDistro.class) && !distroMapper.responsible(groupedServiceName)) { - String userAgent = req.getHeader("User-Agent"); + String userAgent = req.getHeader(HttpHeaderConsts.USER_AGENT_HEADER); if (StringUtils.isNotBlank(userAgent) && userAgent.contains(UtilsAndCommons.NACOS_SERVER_HEADER)) { // This request is sent from peer server, should not be redirected again: diff --git a/naming/src/main/java/com/alibaba/nacos/naming/web/TrafficReviseFilter.java b/naming/src/main/java/com/alibaba/nacos/naming/web/TrafficReviseFilter.java index 8ba69b55141..59f8c65f722 100644 --- a/naming/src/main/java/com/alibaba/nacos/naming/web/TrafficReviseFilter.java +++ b/naming/src/main/java/com/alibaba/nacos/naming/web/TrafficReviseFilter.java @@ -15,6 +15,7 @@ */ package com.alibaba.nacos.naming.web; +import com.alibaba.nacos.common.constant.HttpHeaderConsts; import com.alibaba.nacos.common.util.HttpMethod; import com.alibaba.nacos.naming.cluster.ServerStatus; import com.alibaba.nacos.naming.cluster.ServerStatusManager; @@ -70,10 +71,7 @@ public void doFilter(ServletRequest request, ServletResponse response, FilterCha } // requests from peer server should be let pass: - String agent = req.getHeader("Client-Version"); - if (StringUtils.isBlank(agent)) { - agent = req.getHeader("User-Agent"); - } + String agent = req.getHeader(HttpHeaderConsts.USER_AGENT_HEADER); if (StringUtils.startsWith(agent, UtilsAndCommons.NACOS_SERVER_HEADER)) { filterChain.doFilter(req, resp); diff --git a/test/src/test/java/com/alibaba/nacos/test/naming/NamingBase.java b/test/src/test/java/com/alibaba/nacos/test/naming/NamingBase.java index a76fc785aa9..96b634e0080 100644 --- a/test/src/test/java/com/alibaba/nacos/test/naming/NamingBase.java +++ b/test/src/test/java/com/alibaba/nacos/test/naming/NamingBase.java @@ -17,6 +17,7 @@ import com.alibaba.nacos.api.naming.pojo.Instance; import com.alibaba.nacos.client.naming.net.HttpClient; +import com.alibaba.nacos.common.constant.HttpHeaderConsts; import org.apache.http.HttpStatus; import org.junit.Assert; @@ -173,7 +174,7 @@ public static void prepareServer(int localPort) { public static void prepareServer(int localPort, String status) { String url = "http://127.0.0.1:" + localPort + "/nacos/v1/ns/operator/switches?entry=overriddenServerStatus&value=" + status; List headers = new ArrayList(); - headers.add("User-Agent"); + headers.add(HttpHeaderConsts.USER_AGENT_HEADER); headers.add("Nacos-Server"); HttpClient.HttpResult result = HttpClient.request(url, headers, new HashMap(), "UTF-8", "PUT"); From 5d428a2366ca845b1b45a994b85ae75e38e52ae9 Mon Sep 17 00:00:00 2001 From: ly <641921791@qq.com> Date: Fri, 23 Aug 2019 00:50:56 +0800 Subject: [PATCH 2/3] [ISSUE] #1671 Unified request header "Client-Version" --- .../com/alibaba/nacos/common/constant/HttpHeaderConsts.java | 2 ++ 1 file changed, 2 insertions(+) diff --git a/common/src/main/java/com/alibaba/nacos/common/constant/HttpHeaderConsts.java b/common/src/main/java/com/alibaba/nacos/common/constant/HttpHeaderConsts.java index d3239f182bb..f6d0215e2c2 100644 --- a/common/src/main/java/com/alibaba/nacos/common/constant/HttpHeaderConsts.java +++ b/common/src/main/java/com/alibaba/nacos/common/constant/HttpHeaderConsts.java @@ -17,6 +17,8 @@ /** * Nacos header constants + * + * @author ly */ public interface HttpHeaderConsts { From 721c5a4a585b6ec47dd19c03a7d5b533076002f1 Mon Sep 17 00:00:00 2001 From: ly <641921791@qq.com> Date: Tue, 27 Aug 2019 23:56:45 +0800 Subject: [PATCH 3/3] Compatible with older versions --- .../com/alibaba/nacos/core/utils/WebUtils.java | 17 +++++++++++++++++ .../alibaba/nacos/naming/acl/AuthChecker.java | 3 +-- .../nacos/naming/controllers/ApiController.java | 5 ++--- .../naming/controllers/InstanceController.java | 5 ++--- .../nacos/naming/web/TrafficReviseFilter.java | 4 ++-- 5 files changed, 24 insertions(+), 10 deletions(-) diff --git a/core/src/main/java/com/alibaba/nacos/core/utils/WebUtils.java b/core/src/main/java/com/alibaba/nacos/core/utils/WebUtils.java index 4f5d2d2b6b8..5fd037340c0 100644 --- a/core/src/main/java/com/alibaba/nacos/core/utils/WebUtils.java +++ b/core/src/main/java/com/alibaba/nacos/core/utils/WebUtils.java @@ -15,6 +15,7 @@ */ package com.alibaba.nacos.core.utils; +import com.alibaba.nacos.common.constant.HttpHeaderConsts; import org.apache.commons.lang3.StringUtils; import javax.servlet.http.HttpServletRequest; @@ -71,4 +72,20 @@ public static String getAcceptEncoding(HttpServletRequest req) { encode = encode.contains(",") ? encode.substring(0, encode.indexOf(",")) : encode; return encode.contains(";") ? encode.substring(0, encode.indexOf(";")) : encode; } + + /** + * Returns the value of the request header "user-agent" as a String. + * + * @param request HttpServletRequest + * @return the value of the request header "user-agent", or the value of the + * request header "client-version" if the request does not have a + * header of "user-agent" + */ + public static String getUserAgent(HttpServletRequest request) { + String userAgent = request.getHeader(HttpHeaderConsts.USER_AGENT_HEADER); + if (StringUtils.isEmpty(userAgent)) { + userAgent = StringUtils.defaultIfEmpty(request.getHeader(HttpHeaderConsts.CLIENT_VERSION_HEADER), StringUtils.EMPTY); + } + return userAgent; + } } diff --git a/naming/src/main/java/com/alibaba/nacos/naming/acl/AuthChecker.java b/naming/src/main/java/com/alibaba/nacos/naming/acl/AuthChecker.java index 9e075162903..aa863732b98 100644 --- a/naming/src/main/java/com/alibaba/nacos/naming/acl/AuthChecker.java +++ b/naming/src/main/java/com/alibaba/nacos/naming/acl/AuthChecker.java @@ -17,7 +17,6 @@ import com.alibaba.nacos.api.common.Constants; import com.alibaba.nacos.api.naming.CommonParams; -import com.alibaba.nacos.common.constant.HttpHeaderConsts; import com.alibaba.nacos.core.utils.WebUtils; import com.alibaba.nacos.naming.core.Service; import com.alibaba.nacos.naming.core.ServiceManager; @@ -53,7 +52,7 @@ public void doRaftAuth(HttpServletRequest req) throws Exception { return; } - String agent = req.getHeader(HttpHeaderConsts.USER_AGENT_HEADER); + String agent = WebUtils.getUserAgent(req); if (StringUtils.startsWith(agent, UtilsAndCommons.NACOS_SERVER_HEADER)) { return; } diff --git a/naming/src/main/java/com/alibaba/nacos/naming/controllers/ApiController.java b/naming/src/main/java/com/alibaba/nacos/naming/controllers/ApiController.java index 30c5fb1e195..98ea7724f17 100644 --- a/naming/src/main/java/com/alibaba/nacos/naming/controllers/ApiController.java +++ b/naming/src/main/java/com/alibaba/nacos/naming/controllers/ApiController.java @@ -19,7 +19,6 @@ import com.alibaba.nacos.api.common.Constants; import com.alibaba.nacos.api.naming.CommonParams; import com.alibaba.nacos.api.naming.utils.NamingUtils; -import com.alibaba.nacos.common.constant.HttpHeaderConsts; import com.alibaba.nacos.core.utils.WebUtils; import com.alibaba.nacos.naming.core.DistroMapper; import com.alibaba.nacos.naming.core.ServiceManager; @@ -62,7 +61,7 @@ public JSONObject allDomNames(HttpServletRequest request) throws Exception { JSONObject result = new JSONObject(); // For old DNS-F client: String dnsfVersion = "1.0.1"; - String agent = request.getHeader(HttpHeaderConsts.USER_AGENT_HEADER); + String agent = WebUtils.getUserAgent(request); ClientInfo clientInfo = new ClientInfo(agent); if (clientInfo.type == ClientInfo.ClientType.DNS && clientInfo.version.compareTo(VersionUtil.parseVersion(dnsfVersion)) <= 0) { @@ -123,7 +122,7 @@ public JSONObject srvIPXT(HttpServletRequest request) throws Exception { Constants.DEFAULT_NAMESPACE_ID); String dom = WebUtils.required(request, "dom"); - String agent = request.getHeader(HttpHeaderConsts.USER_AGENT_HEADER); + String agent = WebUtils.getUserAgent(request); String clusters = WebUtils.optional(request, "clusters", StringUtils.EMPTY); String clientIP = WebUtils.optional(request, "clientIP", StringUtils.EMPTY); Integer udpPort = Integer.parseInt(WebUtils.optional(request, "udpPort", "0")); diff --git a/naming/src/main/java/com/alibaba/nacos/naming/controllers/InstanceController.java b/naming/src/main/java/com/alibaba/nacos/naming/controllers/InstanceController.java index e6746b28d9a..f3f1ebb2188 100644 --- a/naming/src/main/java/com/alibaba/nacos/naming/controllers/InstanceController.java +++ b/naming/src/main/java/com/alibaba/nacos/naming/controllers/InstanceController.java @@ -21,7 +21,6 @@ import com.alibaba.nacos.api.common.Constants; import com.alibaba.nacos.api.naming.CommonParams; import com.alibaba.nacos.api.naming.utils.NamingUtils; -import com.alibaba.nacos.common.constant.HttpHeaderConsts; import com.alibaba.nacos.core.utils.WebUtils; import com.alibaba.nacos.naming.core.DistroMapper; import com.alibaba.nacos.naming.core.Instance; @@ -127,7 +126,7 @@ public String update(HttpServletRequest request) throws Exception { String serviceName = WebUtils.required(request, CommonParams.SERVICE_NAME); String namespaceId = WebUtils.optional(request, CommonParams.NAMESPACE_ID, Constants.DEFAULT_NAMESPACE_ID); - String agent = request.getHeader(HttpHeaderConsts.USER_AGENT_HEADER); + String agent = WebUtils.getUserAgent(request); ClientInfo clientInfo = new ClientInfo(agent); @@ -147,7 +146,7 @@ public JSONObject list(HttpServletRequest request) throws Exception { Constants.DEFAULT_NAMESPACE_ID); String serviceName = WebUtils.required(request, CommonParams.SERVICE_NAME); - String agent = request.getHeader(HttpHeaderConsts.USER_AGENT_HEADER); + String agent = WebUtils.getUserAgent(request); String clusters = WebUtils.optional(request, "clusters", StringUtils.EMPTY); String clientIP = WebUtils.optional(request, "clientIP", StringUtils.EMPTY); Integer udpPort = Integer.parseInt(WebUtils.optional(request, "udpPort", "0")); diff --git a/naming/src/main/java/com/alibaba/nacos/naming/web/TrafficReviseFilter.java b/naming/src/main/java/com/alibaba/nacos/naming/web/TrafficReviseFilter.java index 59f8c65f722..9240dbf0d1a 100644 --- a/naming/src/main/java/com/alibaba/nacos/naming/web/TrafficReviseFilter.java +++ b/naming/src/main/java/com/alibaba/nacos/naming/web/TrafficReviseFilter.java @@ -15,8 +15,8 @@ */ package com.alibaba.nacos.naming.web; -import com.alibaba.nacos.common.constant.HttpHeaderConsts; import com.alibaba.nacos.common.util.HttpMethod; +import com.alibaba.nacos.core.utils.WebUtils; import com.alibaba.nacos.naming.cluster.ServerStatus; import com.alibaba.nacos.naming.cluster.ServerStatusManager; import com.alibaba.nacos.naming.misc.SwitchDomain; @@ -71,7 +71,7 @@ public void doFilter(ServletRequest request, ServletResponse response, FilterCha } // requests from peer server should be let pass: - String agent = req.getHeader(HttpHeaderConsts.USER_AGENT_HEADER); + String agent = WebUtils.getUserAgent(req); if (StringUtils.startsWith(agent, UtilsAndCommons.NACOS_SERVER_HEADER)) { filterChain.doFilter(req, resp);