diff --git a/scm-core/src/main/java/sonia/scm/util/HttpUtil.java b/scm-core/src/main/java/sonia/scm/util/HttpUtil.java index c8717c1cb7..244ac98719 100644 --- a/scm-core/src/main/java/sonia/scm/util/HttpUtil.java +++ b/scm-core/src/main/java/sonia/scm/util/HttpUtil.java @@ -53,6 +53,7 @@ import java.io.UnsupportedEncodingException; import java.net.URLDecoder; import java.net.URLEncoder; +import java.util.Locale; import java.util.regex.Matcher; import java.util.regex.Pattern; @@ -79,6 +80,9 @@ public final class HttpUtil */ public static final String HEADER_SCM_CLIENT = "X-SCM-Client"; + /** Field description */ + public static final String HEADER_USERAGENT = "User-Agent"; + /** authentication header */ public static final String HEADER_WWW_AUTHENTICATE = "WWW-Authenticate"; @@ -522,6 +526,26 @@ public final class HttpUtil STATUS_UNAUTHORIZED_MESSAGE); } + /** + * Returns true if the User-Agent header of the current request starts with + * the given string. + * + * + * @param request http request + * @param userAgent string to test against the header + * + * @return true if the header starts with the given string + * + * @since 1.37 + */ + public static boolean userAgentStartsWith(HttpServletRequest request, + String userAgent) + { + return Strings.nullToEmpty(request.getHeader(HEADER_USERAGENT)).toLowerCase( + Locale.ENGLISH).startsWith( + Strings.nullToEmpty(userAgent).toLowerCase(Locale.ENGLISH)); + } + //~--- get methods ---------------------------------------------------------- /** diff --git a/scm-core/src/test/java/sonia/scm/util/HttpUtilTest.java b/scm-core/src/test/java/sonia/scm/util/HttpUtilTest.java index 4f88616662..585bc0e005 100644 --- a/scm-core/src/test/java/sonia/scm/util/HttpUtilTest.java +++ b/scm-core/src/test/java/sonia/scm/util/HttpUtilTest.java @@ -184,6 +184,23 @@ public class HttpUtilTest } + /** + * Method description + * + */ + @Test + public void userAgentStartsWithTest() + { + HttpServletRequest request = mock(HttpServletRequest.class); + + when(request.getHeader(HttpUtil.HEADER_USERAGENT)).thenReturn( + "git/1.7.10.5997.gaa4aa"); + assertTrue(HttpUtil.userAgentStartsWith(request, "git/")); + assertTrue(HttpUtil.userAgentStartsWith(request, "GIT/")); + assertFalse(HttpUtil.userAgentStartsWith(request, "git/a")); + assertFalse(HttpUtil.userAgentStartsWith(request, "sobbo/")); + } + //~--- get methods ---------------------------------------------------------- /**