Exemplo n.º 1
0
        private static Platform GetPlatform(UserAgent agent)
        {
            // Unknown
            if (agent.IsNullOrEmpty())
            {
                return(Platform.Unknown);
            }
            // Google Android
            if (agent.Contains(Platform.Android))
            {
                return(Platform.Android);
            }
            // Microsoft Windows
            if (agent.Contains(Platform.Windows))
            {
                return(Platform.Windows);
            }
            // Apple iOS
            if (IsiOS(agent))
            {
                return(Platform.iOS);
            }
            // Apple Mac
            if (agent.Contains(Platform.Mac))
            {
                return(Platform.Mac);
            }
            // Linux Distribution
            if (agent.Contains(Platform.Linux))
            {
                return(Platform.Linux);
            }

            return(Platform.Others);
        }
Exemplo n.º 2
0
        public void ContainsString()
        {
            var agent = new UserAgent("abc");

            Assert.False(agent.Contains((string)null));
            Assert.False(agent.Contains(""));
            Assert.True(agent.Contains("abc"));
            Assert.True(agent.Contains("ABC"));
        }
Exemplo n.º 3
0
        public void ContainsGeneric()
        {
            var abc    = new UserAgent("abc");
            var google = new UserAgent("Mozilla/5.0 (compatible; Googlebot/2.1; +http://www.google.com/bot.html)");

            Assert.False(abc.Contains(Crawler.Google));
            Assert.True(google.Contains(Crawler.Google));
            Assert.False(google.Contains(Crawler.Yahoo));
            Assert.False(google.Contains(Crawler.Yahoo | Crawler.Bing));
            Assert.True(google.Contains(Crawler.Google | Crawler.Bing));
        }
Exemplo n.º 4
0
        public static bool Contains <T>(this UserAgent agent, T flags) where T : Enum
        {
            var strFlags = flags.ToString();

            return(strFlags.Contains(',')
                       ? flags.GetFlags().Any(agent.Contains)
                       : agent.Contains(strFlags));
        }
Exemplo n.º 5
0
        public void ContainsListString()
        {
            var agent = new UserAgent("Mozilla/5.0 (compatible; Googlebot/2.1; +http://www.google.com/bot.html)");

            Assert.False(agent.Contains(new List <string> {
                null
            }));
            Assert.False(agent.Contains(new List <string> {
                ""
            }));
            Assert.False(agent.Contains(new List <string> {
                null, ""
            }));
            Assert.False(agent.Contains(new List <string> {
                "abc"
            }));
            Assert.True(agent.Contains(new List <string> {
                "google"
            }));
            Assert.True(agent.Contains(new List <string> {
                "google", "abc"
            }));
            Assert.True(agent.Contains(new List <string> {
                "abc", "google"
            }));
        }
Exemplo n.º 6
0
        private static Version GetVersion(UserAgent agent, Browser browser)
        {
            if (agent.IsNullOrEmpty())
            {
                return(new Version());
            }

            if (agent.Contains("rv:11.0") || agent.Contains("ie 11.0"))
            {
                return(new Version(11, 0));
            }
            if (agent.Contains("msie 10"))
            {
                return(new Version(10, 0));
            }
            if (agent.Contains("msie 9"))
            {
                return(new Version(9, 0));
            }

            if (browser == Browser.Edge && !agent.Contains("edge"))
            {
                agent = agent.Replace("edg", "edge");
            }

            var    name  = browser.ToString();
            var    first = agent.IndexOf(browser);
            string cut;

            try
            {
                cut = agent.Substring(first + name.Length + 1);
            }
            catch
            {
                cut = agent.Substring(first + name.Length);
            }

            var version = cut.Contains(" ") ? cut.Substring(0, cut.IndexOf(' ')) : cut;

            return(version.ToVersion());
        }
Exemplo n.º 7
0
        private static Engine GetEngine(UserAgent agent, Platform os, Processor cpu)
        {
            // Null check
            if (agent.IsNullOrEmpty())
            {
                return(Engine.Unknown);
            }

            // Microsoft Edge
            if (IsEdge(agent, os))
            {
                return(Engine.Edge);
            }
            // Google Blink
            if (IsBlink(agent))
            {
                return(Engine.Blink);
            }
            // Apple WebKit
            if (agent.Contains(Engine.WebKit))
            {
                return(Engine.WebKit);
            }
            // Microsoft Trident
            if (agent.Contains(Engine.Trident))
            {
                return(Engine.Trident);
            }
            // Mozilla Gecko
            if (agent.Contains(Engine.Gecko))
            {
                return(Engine.Gecko);
            }
            // Sumsang Servo
            if (agent.Contains(Engine.Servo))
            {
                return(Engine.Servo);
            }

            return(Engine.Others);
        }
Exemplo n.º 8
0
 private static Device DeviceFromUserAgent(UserAgent agent)
 {
     // Tablet
     if (IsTablet(agent))
     {
         return(Device.Tablet);
     }
     // Tv
     if (IsTV(agent))
     {
         return(Device.Tv);
     }
     // Mobile
     if (IsMobile(agent))
     {
         return(Device.Mobile);
     }
     // Watch
     if (agent.Contains(Device.Watch))
     {
         return(Device.Watch);
     }
     // Console
     if (agent.Contains(Device.Console))
     {
         return(Device.Console);
     }
     // Car
     if (agent.Contains(Device.Car))
     {
         return(Device.Car);
     }
     // IoT
     if (agent.Contains(Device.IoT))
     {
         return(Device.IoT);
     }
     // Desktop
     return(Device.Desktop);
 }
Exemplo n.º 9
0
        private static Version GetVersion(UserAgent agent, Browser browser)
        {
            if (agent.IsNullOrEmpty())
            {
                return(new Version());
            }

            if (agent.Contains("rv:11.0") || agent.Contains("ie 11.0"))
            {
                return(new Version(11, 0));
            }
            if (agent.Contains("msie 10"))
            {
                return(new Version(10, 0));
            }
            if (agent.Contains("msie 9"))
            {
                return(new Version(9, 0));
            }

            if (browser == Browser.Edge && !agent.Contains("edge"))
            {
                return(GetVersionCommon(agent.Replace("edg", "edge"), browser));
            }

            if (browser == Browser.Safari && agent.Contains("version/"))
            {
                return(GetVersionSafari(agent, browser));
            }

            return(GetVersionCommon(agent, browser));
        }
Exemplo n.º 10
0
        private static Browser GetBrowser(UserAgent agent, Platform os, Engine engine)
        {
            // fail and return fast
            if (agent.IsNullOrEmpty())
            {
                return(Browser.Unknown);
            }
            // Microsoft Edge
            if (IsEdge(agent))
            {
                return(Browser.Edge);
            }
            // Google chrome
            if (agent.Contains(Browser.Chrome))
            {
                return(Browser.Chrome);
            }
            // Microsoft Internet Explorer
            if (IsInternetExplorer(agent, os, engine))
            {
                return(Browser.InternetExplorer);
            }
            // Apple Safari
            if (agent.Contains(Browser.Safari))
            {
                return(Browser.Safari);
            }
            // Firefox
            if (agent.Contains(Browser.Firefox))
            {
                return(Browser.Firefox);
            }
            // Opera
            if (agent.Contains(Browser.Opera))
            {
                return(Browser.Opera);
            }

            return(Browser.Others);
        }
Exemplo n.º 11
0
        public void ContainsArray()
        {
            var agent = new UserAgent("abc");

            Assert.False(agent.Contains(new[] { (string)null }));
            Assert.False(agent.Contains(new[] { "" }));
            Assert.False(agent.Contains(new[] { (string)null, "" }));
            Assert.True(agent.Contains(new[] { "ABC" }));
            Assert.True(agent.Contains(new[] { "ABC", "abc" }));
            Assert.False(agent.Contains(new[] { "ABCD" }));
        }
Exemplo n.º 12
0
        private static Crawler GetCrawler(UserAgent agent, IEnumerable <string> others)
        {
            if (agent.IsNullOrEmpty())
            {
                return(Crawler.Unknown);
            }

            foreach (var crawler in Crawlers)
            {
                if (agent.Contains(crawler))
                {
                    return(crawler);
                }
            }

            return(HasOthers(agent, others)
                       ? Crawler.Others
                       : Crawler.Unknown);
        }
Exemplo n.º 13
0
 private static bool IsMobile(UserAgent agent, HttpRequest request)
 => agent.Contains(MobileCollection.Keywords) ||
 agent.StartsWith(MobileCollection.Prefixes, 4) ||
 IsOperaMini(request) ||
 IsUserAgentWap(request) ||
 IsAcceptHeaderWap(request);
Exemplo n.º 14
0
 private static bool IsEdge(UserAgent agent)
 => agent.Contains(Browser.Edge) ||
 (agent.Contains("Win64") && agent.Contains("Edg"));
Exemplo n.º 15
0
 private static bool IsInternetExplorer(UserAgent agent, Platform os, Engine engine)
 => engine == Engine.Trident ||
 agent.Contains("MSIE") &&
 !agent.Contains(Browser.Opera);
Exemplo n.º 16
0
 private static bool HasOthers(UserAgent agent, IEnumerable <string> others)
 => agent.Contains(others) ||
 agent.Contains("bot");
Exemplo n.º 17
0
 private static bool IsTV(UserAgent agent)
 => agent.Contains(Device.Tv) || agent.Contains("BRAVIA");
Exemplo n.º 18
0
 private static bool IsMobile(UserAgent agent)
 => agent.Contains(MobileCollection.Keywords) ||
 agent.StartsWith(MobileCollection.Prefixes, 4);
Exemplo n.º 19
0
 private static bool IsTablet(UserAgent agent)
 => agent.Contains(TabletCollection.Keywords) ||
 agent.Contains(TabletCollection.Prefixes);
Exemplo n.º 20
0
 private static bool IsEdge(UserAgent agent, Platform os)
 => agent.Contains(Engine.Edge) ||
 agent.Contains("Edg") &&
 (Platform.Windows | Platform.Android).HasFlag(os);
Exemplo n.º 21
0
 private static bool IsBlink(UserAgent agent)
 => agent.Contains(Browser.Chrome) &&
 agent.Contains(Engine.WebKit);