Fixed: Parsing user agents without a version

Fixes #2392
This commit is contained in:
Bogdan
2025-05-11 00:05:29 +03:00
parent b0212dd780
commit a7d99f351c
2 changed files with 7 additions and 9 deletions

View File

@@ -16,6 +16,8 @@ namespace NzbDrone.Common.Test.Http
[TestCase("Readarr/1.0.0.2300 (ubuntu 20.04)", "Readarr")] [TestCase("Readarr/1.0.0.2300 (ubuntu 20.04)", "Readarr")]
[TestCase("Sonarr/3.0.6.9999 (ubuntu 20.04)", "Sonarr")] [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("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) public void should_parse_user_agent(string userAgent, string parsedAgent)
{ {
UserAgentParser.ParseSource(userAgent).Should().Be(parsedAgent); UserAgentParser.ParseSource(userAgent).Should().Be(parsedAgent);

View File

@@ -1,15 +1,16 @@
using System;
using System.Text.RegularExpressions; using System.Text.RegularExpressions;
namespace NzbDrone.Common.Http namespace NzbDrone.Common.Http
{ {
public static class UserAgentParser 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); RegexOptions.IgnoreCase | RegexOptions.Compiled);
public static string SimplifyUserAgent(string userAgent) 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; return null;
} }
@@ -19,14 +20,9 @@ namespace NzbDrone.Common.Http
public static string ParseSource(string userAgent) 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"].Success ? match.Groups["agent"].Value : "Other";
{
return match.Groups["agent"].Value;
}
return "Other";
} }
} }
} }