mirror of
https://github.com/Prowlarr/Prowlarr.git
synced 2025-09-09 12:47:56 +02:00
@ -16,6 +16,8 @@ namespace NzbDrone.Common.Test.Http
|
||||
[TestCase("Readarr/1.0.0.2300 (ubuntu 20.04)", "Readarr")]
|
||||
[TestCase("Sonarr/3.0.6.9999 (ubuntu 20.04)", "Sonarr")]
|
||||
[TestCase("Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/81.0.4044.138 Safari/537.36", "Other")]
|
||||
[TestCase("appbrr", "appbrr")]
|
||||
[TestCase(" appbrr ", "appbrr")]
|
||||
public void should_parse_user_agent(string userAgent, string parsedAgent)
|
||||
{
|
||||
UserAgentParser.ParseSource(userAgent).Should().Be(parsedAgent);
|
||||
|
@ -1,15 +1,16 @@
|
||||
using System;
|
||||
using System.Text.RegularExpressions;
|
||||
|
||||
namespace NzbDrone.Common.Http
|
||||
{
|
||||
public static class UserAgentParser
|
||||
{
|
||||
private static readonly Regex AppSourceRegex = new Regex(@"(?<agent>[a-z0-9]*)\/.*(?:\(.*\))?",
|
||||
private static readonly Regex AppSourceRegex = new (@"^(?<agent>[a-z0-9]+)(?:\/.+(?:\(.*\))?|$)",
|
||||
RegexOptions.IgnoreCase | RegexOptions.Compiled);
|
||||
|
||||
public static string SimplifyUserAgent(string userAgent)
|
||||
{
|
||||
if (userAgent == null || userAgent.StartsWith("Mozilla/5.0"))
|
||||
if (userAgent == null || userAgent.StartsWith("Mozilla/5.0", StringComparison.Ordinal))
|
||||
{
|
||||
return null;
|
||||
}
|
||||
@ -19,14 +20,9 @@ namespace NzbDrone.Common.Http
|
||||
|
||||
public static string ParseSource(string userAgent)
|
||||
{
|
||||
var match = AppSourceRegex.Match(SimplifyUserAgent(userAgent) ?? string.Empty);
|
||||
var match = AppSourceRegex.Match(SimplifyUserAgent(userAgent?.Trim()) ?? string.Empty);
|
||||
|
||||
if (match.Groups["agent"].Success)
|
||||
{
|
||||
return match.Groups["agent"].Value;
|
||||
}
|
||||
|
||||
return "Other";
|
||||
return match.Groups["agent"].Success ? match.Groups["agent"].Value : "Other";
|
||||
}
|
||||
}
|
||||
}
|
||||
|
Reference in New Issue
Block a user