Skip to content

Commit

Permalink
Compatible with older versions
Browse files Browse the repository at this point in the history
  • Loading branch information
ly641921791 committed Aug 27, 2019
1 parent 5d428a2 commit 721c5a4
Show file tree
Hide file tree
Showing 5 changed files with 24 additions and 10 deletions.
17 changes: 17 additions & 0 deletions core/src/main/java/com/alibaba/nacos/core/utils/WebUtils.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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 <code>String</code>.
*
* @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;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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) {
Expand Down Expand Up @@ -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"));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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);

Expand All @@ -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"));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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);
Expand Down

0 comments on commit 721c5a4

Please sign in to comment.