Skip to content

Commit bb6853d

Browse files
author
Pierre
committed
Added a webView flag in Browser. Supported for IE10+ for now.
Also a few more bot detections.
1 parent 3d9cc84 commit bb6853d

File tree

5 files changed

+32
-10
lines changed

5 files changed

+32
-10
lines changed

src/main/java/net/pieroxy/ua/detection/BotsHelper.java

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -34,10 +34,9 @@ public static String[] getGroups(String regexp, String ua, int ... groups) {
3434
static private Map<String, Bot> genericBotsLiteral;
3535
static private Bot genericBotBase = new Bot(Brand.OTHER, BotFamily.ROBOT, "", "");
3636
static private GenericBot[]genericBotsPatterns = new GenericBot[] {
37-
new GenericBot("Mozilla/5\\.0 \\(compatible; ?([^\\);/]+)/([0-9\\.]+); ?(MirrorDetector; )?(\\+? ?https?://[^\\)]+)\\)", new int[]{1,2,4}, true),
37+
new GenericBot("Mozilla/5\\.0 \\(compatible; ?([^\\);/]+)/([0-9\\.]+[ab]?); ?(MirrorDetector; )?(\\+? ?https?://[^\\)]+)\\)", new int[]{1,2,4}, true),
3838
new GenericBot("Mozilla/5\\.0 \\(compatible; ([^\\);/]+)\\-([0-9\\.]+); (\\+? ?https?://[^\\)]+)\\)", new int[]{1,2,3}, true),
39-
new GenericBot("Mozilla/5\\.0 \\(compatible; ([^\\);/]+); (\\+? ?https?://[^\\)]+)\\)", new int[]{1,0,2}, true),
40-
new GenericBot("Mozilla/5\\.0 \\(compatible; ([^\\);/ ]+) (\\+? ?https?://[^\\)]+)\\)", new int[]{1,0,2}, true),
39+
new GenericBot("Mozilla/5\\.0 \\(compatible; ([^\\);/]+);? (\\+? ?https?://[^\\)]+)\\)", new int[]{1,0,2}, true),
4140
new GenericBot("([^\\(\\);/]+)/([0-9RC\\.]+) \\((\\+?https?://[^\\);]+)\\)( .*)?", new int[]{1,2,3}, true),
4241
new GenericBot("([^\\(\\);]+) \\((\\+?https?://[^\\);]+)\\)( .*)?", new int[]{1,0,2}, true),
4342
new GenericBot("([^\\(\\);/]+)/([0-9RC\\.]+) \\(([A-Za-z0-9._%+-]+@[a-zA-Z0-9.-]+\\.[a-zA-Z]{2,})\\)( .*)?", new int[]{1,2,0}, true),
@@ -274,7 +273,7 @@ else if ((ver=context.getcVersionAfterPattern("Diffbot/", MatchingType.BEGINS,Ma
274273
} else if ((ver = context.getcVersionAfterPattern("Mail.RU_Bot/", MatchingType.BEGINS, MatchingRegion.PARENTHESIS))!=null) {
275274
return new Bot(Brand.MAILRU,BotFamily.CRAWLER,"Mail.ru crawler", ver, consumeUrlAndMozilla(context, "http://go.mail.ru"));
276275
} else if ((ver = context.getcVersionAfterPattern("MJ12bot/", MatchingType.BEGINS, MatchingRegion.PARENTHESIS))!=null) {
277-
return new Bot(Brand.MAJESTIC12,BotFamily.CRAWLER,"Majestic 12", ver, consumeUrlAndMozilla(context, "http://www.majestic12"));
276+
return new Bot(Brand.MAJESTIC12,BotFamily.CRAWLER,"Majestic 12", ver, consumeUrlAndMozilla(context, "http://"));
278277
} else if ((ver = context.getcVersionAfterPattern("GigablastOpenSource/", MatchingType.BEGINS, MatchingRegion.REGULAR))!=null) {
279278
return new Bot(Brand.OTHER,BotFamily.CRAWLER,"GigaBlast Crawler", ver);
280279
} else if (context.getUA().equals("NetLyzer FastProbe")) {

src/main/java/net/pieroxy/ua/detection/Browser.java

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ public class Browser extends VersionedObject {
99
BrowserFamily family;
1010
String description;
1111
RenderingEngine renderingEngine;
12+
boolean inWebView;
1213

1314
/**
1415
* This is the most detailed constructor of the Browser object, where everything can be specified.
@@ -131,5 +132,13 @@ public String getDescription() {
131132
public RenderingEngine getRenderingEngine() {
132133
return renderingEngine;
133134
}
135+
/** @return true if the browser is a webview, false if not or unknown */
136+
public boolean isInWebView() {
137+
return inWebView;
138+
}
139+
/** @param value true if the browser is a webview, false if not or unknown */
140+
public void setInWebView(boolean value) {
141+
inWebView = value;
142+
}
134143

135144
}

src/main/java/net/pieroxy/ua/detection/UserAgentDetector.java

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -887,6 +887,12 @@ static float tryParseVersionNumber(String s) {
887887
}
888888
}
889889

890+
static void setInternetExplorerWebview(UserAgentContext context, Browser b) {
891+
if (context.consume("MSAppHost/", MatchingType.BEGINS, MatchingRegion.PARENTHESIS)) {
892+
b.setInWebView(true);
893+
}
894+
}
895+
890896
static Browser tryGetIE(UserAgentContext context, String possibleVersions, OS os) {
891897

892898
Browser res=null;
@@ -922,6 +928,10 @@ static Browser tryGetIE(UserAgentContext context, String possibleVersions, OS os
922928
res = new Browser(Brand.MICROSOFT,BrowserFamily.IE,"IE",new RenderingEngine(Brand.MICROSOFT, RenderingEngineFamily.TRIDENT, "for IE " + iever, 2), verie);
923929
}
924930

931+
if (iever >= 10) {
932+
setInternetExplorerWebview(context, res);
933+
}
934+
925935
if (possibleVersions.indexOf(String.valueOf((int)Math.floor(iever))+",")==-1) res = null;
926936

927937
if (res != null) {
@@ -990,6 +1000,7 @@ static Browser tryGetIE(UserAgentContext context, String possibleVersions, OS os
9901000
new Matcher("Gecko", MatchingType.REGEXP)
9911001
},
9921002
MatchingRegion.REGULAR);
1003+
setInternetExplorerWebview(context, res);
9931004
}
9941005
}
9951006
if (res!=null) return res;

src/main/java/net/pieroxy/ua/tooling/UserAgentTester.java

Lines changed: 9 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -136,7 +136,9 @@ private static String compare(UserAgentDetectionResult a,
136136
addErrorReport(result, "browser version", a.getBrowser().getVersion(),
137137
b.getBrowser().getVersion());
138138
addErrorReport(result, "browser fullVersion", a.getBrowser().getFullVersion(),
139-
b.getBrowser().getFullVersion());
139+
b.getBrowser().getFullVersion());
140+
addErrorReport(result, "browser inWebView", a.getBrowser().isInWebView(),
141+
b.getBrowser().isInWebView());
140142

141143
addErrorReport(result, "device", a.getDevice(), b.getDevice());
142144
addErrorReport(result, "device architecture", a.getDevice().getArchitecture(),
@@ -197,11 +199,10 @@ private static void addErrorReport(StringBuilder errors, String name,
197199
.append("'\n");
198200
} else {
199201
if (expected != null && actual != null) {
200-
if (expected instanceof String || expected instanceof Enum) {
202+
if (expected instanceof String || expected instanceof Enum || expected instanceof Boolean) {
201203
errors.append("\texpected ").append(name).append(" to be '")
202204
.append(expected.toString()).append("' but was '")
203205
.append(actual.toString()).append("'\n");
204-
205206
}
206207
}
207208
}
@@ -222,7 +223,7 @@ private static class UserAgentDetection {
222223
comment, ignored_tokens, unknown_tokens, device_arch, browser_vendor,
223224
os_vendor, bot_family, bot_vendor, bot_description, bot_version,
224225
bot_url, browser_version, browser_fullVersion, re_brand, re_family,
225-
re_version, re_fullversion;
226+
re_version, re_fullversion, browser_inwebview;
226227

227228

228229
public UserAgentDetection(String line) {
@@ -257,6 +258,7 @@ public UserAgentDetection(String line) {
257258
re_family = getStringOrNull(elements[30]);
258259
re_version = getStringOrNull(elements[31]);
259260
re_fullversion = getStringOrNull(elements[32]);
261+
browser_inwebview = getStringOrNull(elements[33]);
260262
}
261263

262264
private String getStringOrNull(String s) {
@@ -274,7 +276,7 @@ public UserAgentDetectionResult getDetectionResult() {
274276
bot_description, bot_version, bot_url);
275277
}
276278

277-
return new UserAgentDetectionResult(
279+
UserAgentDetectionResult res = new UserAgentDetectionResult(
278280
new Device(device_arch, Enum.valueOf(DeviceType.class, device_type),
279281
Enum.valueOf(Brand.class, device_brand), Enum.valueOf(
280282
Brand.class, device_manufacturer), device),
@@ -291,7 +293,8 @@ public UserAgentDetectionResult getDetectionResult() {
291293
OSFamily.class, os_family), os_description, os_version),
292294
new Locale(lang, country), comment, ignored_tokens, unknown_tokens,
293295
bot);
294-
296+
res.getBrowser().setInWebView(browser_inwebview.equals("1"));
297+
return res;
295298
}
296299
}
297300
}

test-data/database.gz

2.51 KB
Binary file not shown.

0 commit comments

Comments
 (0)