mirror of
https://github.com/scm-manager/scm-manager.git
synced 2025-11-01 19:15:52 +01:00
added jgit detection to GitUserAgentProvider, to fix integration tests
This commit is contained in:
@@ -35,6 +35,8 @@ package sonia.scm.web;
|
|||||||
|
|
||||||
import com.google.common.annotations.VisibleForTesting;
|
import com.google.common.annotations.VisibleForTesting;
|
||||||
import com.google.common.base.Charsets;
|
import com.google.common.base.Charsets;
|
||||||
|
import com.google.common.base.Strings;
|
||||||
|
import java.util.Locale;
|
||||||
|
|
||||||
import sonia.scm.plugin.ext.Extension;
|
import sonia.scm.plugin.ext.Extension;
|
||||||
|
|
||||||
@@ -46,57 +48,75 @@ import sonia.scm.plugin.ext.Extension;
|
|||||||
@Extension
|
@Extension
|
||||||
public class GitUserAgentProvider implements UserAgentProvider
|
public class GitUserAgentProvider implements UserAgentProvider
|
||||||
{
|
{
|
||||||
|
|
||||||
|
private static final String PREFIX_JGIT = "jgit/";
|
||||||
|
private static final String PREFIX_REGULAR = "git/";
|
||||||
|
private static final String PREFIX_LFS = "git-lfs/";
|
||||||
|
private static final String SUFFIX_MSYSGIT = "msysgit";
|
||||||
|
|
||||||
/** Field description */
|
|
||||||
@VisibleForTesting
|
@VisibleForTesting
|
||||||
static final UserAgent GIT = UserAgent.builder("Git").browser(
|
static final UserAgent JGIT = UserAgent.builder("JGit")
|
||||||
false).basicAuthenticationCharset(
|
.browser(false)
|
||||||
Charsets.UTF_8).build();
|
.basicAuthenticationCharset(Charsets.UTF_8)
|
||||||
|
.build();
|
||||||
|
|
||||||
|
@VisibleForTesting
|
||||||
|
static final UserAgent GIT = UserAgent.builder("Git")
|
||||||
|
.browser(false)
|
||||||
|
.basicAuthenticationCharset(Charsets.UTF_8)
|
||||||
|
.build();
|
||||||
|
|
||||||
@VisibleForTesting
|
@VisibleForTesting
|
||||||
static final UserAgent GIT_LFS = UserAgent.builder("Git Lfs")
|
static final UserAgent GIT_LFS = UserAgent.builder("Git Lfs")
|
||||||
.browser(false)
|
.browser(false)
|
||||||
.basicAuthenticationCharset(Charsets.UTF_8)
|
.basicAuthenticationCharset(Charsets.UTF_8)
|
||||||
.build();
|
.build();
|
||||||
|
|
||||||
/** Field description */
|
|
||||||
@VisibleForTesting
|
@VisibleForTesting
|
||||||
static final UserAgent MSYSGIT = UserAgent.builder("msysGit").browser(
|
static final UserAgent MSYSGIT = UserAgent.builder("msysGit")
|
||||||
false).basicAuthenticationCharset(
|
.browser(false)
|
||||||
Charsets.UTF_8).build();
|
.basicAuthenticationCharset(Charsets.UTF_8)
|
||||||
|
.build();
|
||||||
|
|
||||||
/** Field description */
|
|
||||||
private static final String PREFIX_REGULAR = "git/";
|
|
||||||
private static final String PREFIX_LFS = "git-lfs/";
|
|
||||||
|
|
||||||
/** Field description */
|
|
||||||
private static final String SUFFIX_MSYSGIT = "msysgit";
|
|
||||||
|
|
||||||
//~--- methods --------------------------------------------------------------
|
//~--- methods --------------------------------------------------------------
|
||||||
|
|
||||||
/**
|
|
||||||
* Method description
|
|
||||||
*
|
|
||||||
*
|
|
||||||
* @param userAgentString
|
|
||||||
*
|
|
||||||
* @return
|
|
||||||
*/
|
|
||||||
@Override
|
@Override
|
||||||
public UserAgent parseUserAgent(String userAgentString)
|
public UserAgent parseUserAgent(String userAgentString)
|
||||||
{
|
{
|
||||||
UserAgent ua = null;
|
String lowerUserAgent = toLower(userAgentString);
|
||||||
|
|
||||||
if (userAgentString.startsWith(PREFIX_REGULAR)) {
|
if (isJGit(lowerUserAgent)) {
|
||||||
if (userAgentString.contains(SUFFIX_MSYSGIT)) {
|
return JGIT;
|
||||||
ua = MSYSGIT;
|
} else if (isMsysGit(lowerUserAgent)) {
|
||||||
} else {
|
return MSYSGIT;
|
||||||
ua = GIT;
|
} else if (isGitLFS(lowerUserAgent)) {
|
||||||
}
|
return GIT_LFS;
|
||||||
} else if (userAgentString.startsWith(PREFIX_LFS)) {
|
} else if (isGit(lowerUserAgent)) {
|
||||||
ua = GIT_LFS;
|
return GIT;
|
||||||
|
} else {
|
||||||
|
return null;
|
||||||
}
|
}
|
||||||
|
}
|
||||||
return ua;
|
|
||||||
|
private String toLower(String value) {
|
||||||
|
return Strings.nullToEmpty(value).toLowerCase(Locale.ENGLISH);
|
||||||
|
}
|
||||||
|
|
||||||
|
private boolean isJGit(String userAgent) {
|
||||||
|
return userAgent.startsWith(PREFIX_JGIT);
|
||||||
|
}
|
||||||
|
|
||||||
|
private boolean isMsysGit(String userAgent) {
|
||||||
|
return userAgent.startsWith(PREFIX_REGULAR) && userAgent.contains(SUFFIX_MSYSGIT);
|
||||||
|
}
|
||||||
|
|
||||||
|
private boolean isGitLFS(String userAgent) {
|
||||||
|
return userAgent.startsWith(PREFIX_LFS);
|
||||||
|
}
|
||||||
|
|
||||||
|
private boolean isGit(String userAgent) {
|
||||||
|
return userAgent.startsWith(PREFIX_REGULAR);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -58,6 +58,7 @@ public class GitUserAgentProviderTest
|
|||||||
public void testParseUserAgent()
|
public void testParseUserAgent()
|
||||||
{
|
{
|
||||||
assertEquals(GitUserAgentProvider.GIT, parse("git/1.7.9.5"));
|
assertEquals(GitUserAgentProvider.GIT, parse("git/1.7.9.5"));
|
||||||
|
assertEquals(GitUserAgentProvider.JGIT, parse("jgit/4.5.2"));
|
||||||
assertEquals(GitUserAgentProvider.GIT_LFS, parse("git-lfs/2.0.1 (GitHub; windows amd64; go 1.8; git 678cdbd4)"));
|
assertEquals(GitUserAgentProvider.GIT_LFS, parse("git-lfs/2.0.1 (GitHub; windows amd64; go 1.8; git 678cdbd4)"));
|
||||||
assertEquals(GitUserAgentProvider.MSYSGIT, parse("git/1.8.3.msysgit.0"));
|
assertEquals(GitUserAgentProvider.MSYSGIT, parse("git/1.8.3.msysgit.0"));
|
||||||
assertNull(parse("Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36"));
|
assertNull(parse("Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36"));
|
||||||
|
|||||||
Reference in New Issue
Block a user