forked from I2P_Developers/i2p.i2p
Utils: Consolidate user-agent detection code in a new utility class
Add some new checks for mobile
This commit is contained in:
@ -33,6 +33,7 @@ import net.i2p.data.Base32;
|
|||||||
import net.i2p.data.Base64;
|
import net.i2p.data.Base64;
|
||||||
import net.i2p.data.DataHelper;
|
import net.i2p.data.DataHelper;
|
||||||
import net.i2p.data.Hash;
|
import net.i2p.data.Hash;
|
||||||
|
import net.i2p.servlet.util.ServletUtil;
|
||||||
import net.i2p.util.Log;
|
import net.i2p.util.Log;
|
||||||
import net.i2p.util.SecureFile;
|
import net.i2p.util.SecureFile;
|
||||||
import net.i2p.util.SystemVersion;
|
import net.i2p.util.SystemVersion;
|
||||||
@ -450,9 +451,7 @@ public class I2PSnarkServlet extends BasicServlet {
|
|||||||
// Opera and text-mode browsers: no   and no input type=image values submitted
|
// Opera and text-mode browsers: no   and no input type=image values submitted
|
||||||
// Using a unique name fixes Opera, except for the buttons with js confirms, see below
|
// Using a unique name fixes Opera, except for the buttons with js confirms, see below
|
||||||
String ua = req.getHeader("User-Agent");
|
String ua = req.getHeader("User-Agent");
|
||||||
boolean isDegraded = ua != null && (ua.startsWith("Lynx") || ua.startsWith("w3m") ||
|
boolean isDegraded = ua != null && ServletUtil.isTextBrowser(ua);
|
||||||
ua.startsWith("ELinks") || ua.startsWith("Links") ||
|
|
||||||
ua.startsWith("Dillo") || ua.startsWith("Emacs-w3m"));
|
|
||||||
boolean noThinsp = isDegraded || (ua != null && ua.startsWith("Opera"));
|
boolean noThinsp = isDegraded || (ua != null && ua.startsWith("Opera"));
|
||||||
|
|
||||||
// pages
|
// pages
|
||||||
|
64
apps/jetty/java/src/net/i2p/servlet/util/ServletUtil.java
Normal file
64
apps/jetty/java/src/net/i2p/servlet/util/ServletUtil.java
Normal file
@ -0,0 +1,64 @@
|
|||||||
|
package net.i2p.servlet.util;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Simple utilities for servlets.
|
||||||
|
* Consolidated from i2psnark, susimail, and routerconsole
|
||||||
|
* @since 0.9.33
|
||||||
|
*/
|
||||||
|
public class ServletUtil {
|
||||||
|
|
||||||
|
private ServletUtil() {};
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @param ua User-Agent string, non-null
|
||||||
|
* @return true if a text-mode or mobile browser
|
||||||
|
*/
|
||||||
|
public static boolean isSmallBrowser(String ua) {
|
||||||
|
return isTextBrowser(ua) || isMobileBrowser(ua);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @param ua User-Agent string, non-null
|
||||||
|
* @return true if a text-mode browser
|
||||||
|
*/
|
||||||
|
public static boolean isTextBrowser(String ua) {
|
||||||
|
return
|
||||||
|
ua.startsWith("Lynx") || ua.startsWith("w3m") ||
|
||||||
|
ua.startsWith("ELinks") || ua.startsWith("Links") ||
|
||||||
|
ua.startsWith("Dillo") || ua.startsWith("Emacs-w3m");
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
/**
|
||||||
|
* The intent here is to return true for phones but
|
||||||
|
* false for big tablets? But not consistent.
|
||||||
|
*
|
||||||
|
* @param ua User-Agent string, non-null
|
||||||
|
* @return true if a mobile browser
|
||||||
|
*/
|
||||||
|
public static boolean isMobileBrowser(String ua) {
|
||||||
|
return
|
||||||
|
// http://www.zytrax.com/tech/web/mobile_ids.html
|
||||||
|
// Android tablet UAs don't have "Mobile" in them
|
||||||
|
(ua.contains("Android") && ua.contains("Mobile")) ||
|
||||||
|
ua.contains("BlackBerry") ||
|
||||||
|
ua.contains("iPhone") ||
|
||||||
|
ua.contains("iPod") || ua.contains("iPad") ||
|
||||||
|
ua.contains("Kindle") || ua.contains("Mobile") ||
|
||||||
|
ua.contains("Nintendo") ||
|
||||||
|
ua.contains("Opera Mini") || ua.contains("Opera Mobi") ||
|
||||||
|
ua.contains("Palm") ||
|
||||||
|
ua.contains("PLAYSTATION") || ua.contains("Playstation") ||
|
||||||
|
ua.contains("Profile/MIDP-") || ua.contains("SymbianOS") ||
|
||||||
|
ua.contains("Windows CE") || ua.contains("Windows Phone") ||
|
||||||
|
ua.startsWith("DoCoMo") ||
|
||||||
|
ua.startsWith("Cricket") || ua.startsWith("HTC") ||
|
||||||
|
ua.startsWith("J-PHONE") || ua.startsWith("KDDI-") ||
|
||||||
|
ua.startsWith("LG-") || ua.startsWith("LGE-") ||
|
||||||
|
ua.startsWith("Nokia") || ua.startsWith("OPWV-SDK") ||
|
||||||
|
ua.startsWith("MOT-") || ua.startsWith("SAMSUNG-") ||
|
||||||
|
ua.startsWith("nook") || ua.startsWith("SCH-") ||
|
||||||
|
ua.startsWith("SEC-") || ua.startsWith("SonyEricsson") ||
|
||||||
|
ua.startsWith("Vodafone");
|
||||||
|
}
|
||||||
|
}
|
9
apps/jetty/java/src/net/i2p/servlet/util/package.html
Normal file
9
apps/jetty/java/src/net/i2p/servlet/util/package.html
Normal file
@ -0,0 +1,9 @@
|
|||||||
|
<html>
|
||||||
|
<body>
|
||||||
|
<p>
|
||||||
|
Small utilities for servlets.
|
||||||
|
Consolidated from i2psnark, susimail, and router console.
|
||||||
|
As of 0.9.33.
|
||||||
|
</p>
|
||||||
|
</body>
|
||||||
|
</html>
|
@ -5,6 +5,7 @@ import java.util.Locale;
|
|||||||
import java.util.Map;
|
import java.util.Map;
|
||||||
import java.util.concurrent.ConcurrentHashMap;
|
import java.util.concurrent.ConcurrentHashMap;
|
||||||
|
|
||||||
|
import net.i2p.servlet.util.ServletUtil;
|
||||||
import net.i2p.util.RandomSource;
|
import net.i2p.util.RandomSource;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@ -182,29 +183,6 @@ public class CSSHelper extends HelperBase {
|
|||||||
}
|
}
|
||||||
|
|
||||||
private static boolean shouldAllowIFrame(String ua) {
|
private static boolean shouldAllowIFrame(String ua) {
|
||||||
return
|
return !ServletUtil.isSmallBrowser(ua);
|
||||||
// text
|
|
||||||
!(ua.startsWith("Lynx") || ua.startsWith("w3m") ||
|
|
||||||
ua.startsWith("ELinks") || ua.startsWith("Links") ||
|
|
||||||
ua.startsWith("Dillo") || ua.startsWith("Emacs-w3m") ||
|
|
||||||
// mobile
|
|
||||||
// http://www.zytrax.com/tech/web/mobile_ids.html
|
|
||||||
// Android tablet UAs don't have "Mobile" in them
|
|
||||||
(ua.contains("Android") && ua.contains("Mobile")) ||
|
|
||||||
ua.contains("iPhone") ||
|
|
||||||
ua.contains("iPod") || ua.contains("iPad") ||
|
|
||||||
ua.contains("Kindle") || ua.contains("Mobile") ||
|
|
||||||
ua.contains("Nintendo Wii") ||
|
|
||||||
ua.contains("Opera Mini") || ua.contains("Opera Mobi") ||
|
|
||||||
ua.contains("Palm") ||
|
|
||||||
ua.contains("PLAYSTATION") || ua.contains("Playstation") ||
|
|
||||||
ua.contains("Profile/MIDP-") || ua.contains("SymbianOS") ||
|
|
||||||
ua.contains("Windows CE") || ua.contains("Windows Phone") ||
|
|
||||||
ua.startsWith("BlackBerry") || ua.startsWith("DoCoMo") ||
|
|
||||||
ua.startsWith("Nokia") || ua.startsWith("OPWV-SDK") ||
|
|
||||||
ua.startsWith("MOT-") || ua.startsWith("SAMSUNG-") ||
|
|
||||||
ua.startsWith("nook") || ua.startsWith("SCH-") ||
|
|
||||||
ua.startsWith("SEC-") || ua.startsWith("SonyEricsson") ||
|
|
||||||
ua.startsWith("Vodafone"));
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -71,6 +71,7 @@ import net.i2p.CoreVersion;
|
|||||||
import net.i2p.I2PAppContext;
|
import net.i2p.I2PAppContext;
|
||||||
import net.i2p.data.DataHelper;
|
import net.i2p.data.DataHelper;
|
||||||
import net.i2p.servlet.RequestWrapper;
|
import net.i2p.servlet.RequestWrapper;
|
||||||
|
import net.i2p.servlet.util.ServletUtil;
|
||||||
import net.i2p.util.Translate;
|
import net.i2p.util.Translate;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@ -1567,6 +1568,7 @@ public class WebMail extends HttpServlet
|
|||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
* Either mobile or text browser
|
||||||
* Copied from net.i2p.router.web.CSSHelper
|
* Copied from net.i2p.router.web.CSSHelper
|
||||||
* @param ua null ok
|
* @param ua null ok
|
||||||
* @since 0.9.7
|
* @since 0.9.7
|
||||||
@ -1574,31 +1576,9 @@ public class WebMail extends HttpServlet
|
|||||||
private static boolean isMobile(String ua) {
|
private static boolean isMobile(String ua) {
|
||||||
if (ua == null)
|
if (ua == null)
|
||||||
return false;
|
return false;
|
||||||
return
|
return ServletUtil.isSmallBrowser(ua);
|
||||||
// text
|
|
||||||
(ua.startsWith("Lynx") || ua.startsWith("w3m") ||
|
|
||||||
ua.startsWith("ELinks") || ua.startsWith("Links") ||
|
|
||||||
ua.startsWith("Dillo") || ua.startsWith("Emacs-w3m") ||
|
|
||||||
// mobile
|
|
||||||
// http://www.zytrax.com/tech/web/mobile_ids.html
|
|
||||||
// Android tablet UAs don't have "Mobile" in them
|
|
||||||
(ua.contains("Android") && ua.contains("Mobile")) ||
|
|
||||||
ua.contains("iPhone") ||
|
|
||||||
ua.contains("iPod") || ua.contains("iPad") ||
|
|
||||||
ua.contains("Kindle") || ua.contains("Mobile") ||
|
|
||||||
ua.contains("Nintendo Wii") ||
|
|
||||||
ua.contains("Opera Mini") || ua.contains("Opera Mobi") ||
|
|
||||||
ua.contains("Palm") ||
|
|
||||||
ua.contains("PLAYSTATION") || ua.contains("Playstation") ||
|
|
||||||
ua.contains("Profile/MIDP-") || ua.contains("SymbianOS") ||
|
|
||||||
ua.contains("Windows CE") || ua.contains("Windows Phone") ||
|
|
||||||
ua.startsWith("BlackBerry") || ua.startsWith("DoCoMo") ||
|
|
||||||
ua.startsWith("Nokia") || ua.startsWith("OPWV-SDK") ||
|
|
||||||
ua.startsWith("MOT-") || ua.startsWith("SAMSUNG-") ||
|
|
||||||
ua.startsWith("nook") || ua.startsWith("SCH-") ||
|
|
||||||
ua.startsWith("SEC-") || ua.startsWith("SonyEricsson") ||
|
|
||||||
ua.startsWith("Vodafone"));
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* The entry point for all web page loads
|
* The entry point for all web page loads
|
||||||
*
|
*
|
||||||
|
Reference in New Issue
Block a user