Skip to content

Commit 02edbb4

Browse files
committed
add docs
1 parent d2c4a82 commit 02edbb4

7 files changed

+198
-14
lines changed

src/MyCSharp.HttpUserAgentParser/HttpUserAgentInformation.cs

Lines changed: 45 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -2,16 +2,46 @@
22

33
namespace MyCSharp.HttpUserAgentParser
44
{
5+
/// <summary>
6+
/// Analyzed user agent
7+
/// </summary>
58
public readonly struct HttpUserAgentInformation
69
{
10+
/// <summary>
11+
/// Full User Agent string
12+
/// </summary>
713
public string UserAgent { get; }
14+
15+
/// <summary>
16+
/// Type of user agent, see <see cref="HttpUserAgentType"/>
17+
/// </summary>
818
public HttpUserAgentType Type { get; }
919

20+
/// <summary>
21+
/// Platform of user agent, see <see cref="HttpUserAgentPlatformInformation"/>
22+
/// </summary>
1023
public HttpUserAgentPlatformInformation? Platform { get; }
24+
25+
/// <summary>
26+
/// Browser or Bot Name of user agent e.g. "Chrome", "Edge"..
27+
/// </summary>
1128
public string? Name { get; }
29+
30+
31+
/// <summary>
32+
/// Version of Browser or Bot Name of user agent e.g. "79.0", "83.0.125.4"
33+
/// </summary>
1234
public string? Version { get; }
35+
36+
37+
/// <summary>
38+
/// Device Type of user agent, e.g. "Android", "Apple iPhone"
39+
/// </summary>
1340
public string? MobileDeviceType { get; }
1441

42+
/// <summary>
43+
/// Creates a new instance of <see cref="HttpUserAgentInformation"/>
44+
/// </summary>
1545
private HttpUserAgentInformation(string userAgent, HttpUserAgentPlatformInformation? platform, HttpUserAgentType type, string? name, string? version, string? deviceName)
1646
{
1747
UserAgent = userAgent;
@@ -22,19 +52,27 @@ private HttpUserAgentInformation(string userAgent, HttpUserAgentPlatformInformat
2252
MobileDeviceType = deviceName;
2353
}
2454

25-
// parse
26-
55+
/// <summary>
56+
/// Parses given <param name="userAgent">User Agent</param>
57+
/// </summary>
2758
public static HttpUserAgentInformation Parse(string userAgent) => HttpUserAgentParser.Parse(userAgent);
2859

29-
// create factories
30-
31-
public static HttpUserAgentInformation CreateForRobot(string userAgent, string robotName)
60+
/// <summary>
61+
/// Creates <see cref="HttpUserAgentInformation"/> for a robot
62+
/// </summary>
63+
internal static HttpUserAgentInformation CreateForRobot(string userAgent, string robotName)
3264
=> new(userAgent, null, HttpUserAgentType.Robot, robotName, null, null);
3365

34-
public static HttpUserAgentInformation CreateForBrowser(string userAgent, HttpUserAgentPlatformInformation? platform, string? browserName, string? browserVersion, string? deviceName)
66+
/// <summary>
67+
/// Creates <see cref="HttpUserAgentInformation"/> for a browser
68+
/// </summary>
69+
internal static HttpUserAgentInformation CreateForBrowser(string userAgent, HttpUserAgentPlatformInformation? platform, string? browserName, string? browserVersion, string? deviceName)
3570
=> new(userAgent, platform, HttpUserAgentType.Browser, browserName, browserVersion, deviceName);
3671

37-
public static HttpUserAgentInformation CreateForUnknown(string userAgent, HttpUserAgentPlatformInformation? platform, string? deviceName)
72+
/// <summary>
73+
/// Creates <see cref="HttpUserAgentInformation"/> for an unknown agent type
74+
/// </summary>
75+
internal static HttpUserAgentInformation CreateForUnknown(string userAgent, HttpUserAgentPlatformInformation? platform, string? deviceName)
3876
=> new(userAgent, platform, HttpUserAgentType.Unknown, null, null, deviceName);
3977
}
4078
}
Lines changed: 20 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,31 @@
1-
// Copyright © myCSharp 2020-2021, all rights reserved
1+
// Copyright © myCSharp 2020-2021, all rights reserved
22

33
namespace MyCSharp.HttpUserAgentParser
44
{
5+
/// <summary>
6+
/// Extensions for <see cref="HttpUserAgentInformation"/>
7+
/// </summary>
58
public static class HttpUserAgentInformationExtensions
69
{
10+
/// <summary>
11+
/// returns true <typeparam name="userAgent.Type"></typeparam> is of <param name="type">type</param>
12+
/// </summary>
713
public static bool IsType(this in HttpUserAgentInformation userAgent, HttpUserAgentType type) => userAgent.Type == type;
14+
15+
/// <summary>
16+
/// returns true <see cref="HttpUserAgentInformation.Type"/> is a robot
17+
/// </summary>
818
public static bool IsRobot(this in HttpUserAgentInformation userAgent) => IsType(userAgent, HttpUserAgentType.Robot);
19+
20+
/// <summary>
21+
/// returns true <see cref="HttpUserAgentInformation.Type"/> is a browser
22+
/// </summary>
923
public static bool IsBrowser(this in HttpUserAgentInformation userAgent) => IsType(userAgent, HttpUserAgentType.Browser);
24+
25+
/// <summary>
26+
/// returns true if agent is a mobile device
27+
/// </summary>
28+
/// <remarks>checks if <seealso cref="HttpUserAgentInformation.MobileDeviceType"/> is null</remarks>
1029
public static bool IsMobile(this in HttpUserAgentInformation userAgent) => userAgent.MobileDeviceType is not null;
1130
}
1231
}

src/MyCSharp.HttpUserAgentParser/HttpUserAgentParser.cs

Lines changed: 34 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,19 @@
1-
// Copyright © myCSharp 2020-2021, all rights reserved
1+
// Copyright © myCSharp 2020-2021, all rights reserved
22

33
using System;
44
using System.Diagnostics.CodeAnalysis;
55
using System.Text.RegularExpressions;
66

77
namespace MyCSharp.HttpUserAgentParser
88
{
9+
/// <summary>
10+
/// Parser logic for user agents
11+
/// </summary>
912
public static class HttpUserAgentParser
1013
{
14+
/// <summary>
15+
/// Parses given <param name="userAgent">user agent</param>
16+
/// </summary>
1117
public static HttpUserAgentInformation Parse(string userAgent)
1218
{
1319
// prepare
@@ -30,8 +36,14 @@ public static HttpUserAgentInformation Parse(string userAgent)
3036
return HttpUserAgentInformation.CreateForUnknown(userAgent, platform, mobileDeviceType);
3137
}
3238

39+
/// <summary>
40+
/// pre-cleanup of <param name="userAgent">user agent</param>
41+
/// </summary>
3342
public static string Cleanup(string userAgent) => userAgent.Trim();
3443

44+
/// <summary>
45+
/// returns the platform or null
46+
/// </summary>
3547
public static HttpUserAgentPlatformInformation? GetPlatform(string userAgent)
3648
{
3749
foreach (HttpUserAgentPlatformInformation item in HttpUserAgentStatics.Platforms)
@@ -45,12 +57,18 @@ public static HttpUserAgentInformation Parse(string userAgent)
4557
return null;
4658
}
4759

60+
/// <summary>
61+
/// returns true if platform was found
62+
/// </summary>
4863
public static bool TryGetPlatform(string userAgent, [NotNullWhen(true)] out HttpUserAgentPlatformInformation? platform)
4964
{
5065
platform = GetPlatform(userAgent);
5166
return platform is not null;
5267
}
5368

69+
/// <summary>
70+
/// returns the browser or null
71+
/// </summary>
5472
public static (string Name, string? Version)? GetBrowser(string userAgent)
5573
{
5674
foreach ((Regex key, string? value) in HttpUserAgentStatics.Browsers)
@@ -65,12 +83,18 @@ public static (string Name, string? Version)? GetBrowser(string userAgent)
6583
return null;
6684
}
6785

86+
/// <summary>
87+
/// returns true if browser was found
88+
/// </summary>
6889
public static bool TryGetBrowser(string userAgent, [NotNullWhen(true)] out (string Name, string? Version)? browser)
6990
{
7091
browser = GetBrowser(userAgent);
7192
return browser is not null;
7293
}
7394

95+
/// <summary>
96+
/// returns the robot or null
97+
/// </summary>
7498
public static string? GetRobot(string userAgent)
7599
{
76100
foreach ((string key, string value) in HttpUserAgentStatics.Robots)
@@ -84,12 +108,18 @@ public static bool TryGetBrowser(string userAgent, [NotNullWhen(true)] out (stri
84108
return null;
85109
}
86110

111+
/// <summary>
112+
/// returns true if robot was found
113+
/// </summary>
87114
public static bool TryGetRobot(string userAgent, [NotNullWhen(true)] out string? robotName)
88115
{
89116
robotName = GetRobot(userAgent);
90117
return robotName is not null;
91118
}
92119

120+
/// <summary>
121+
/// returns the device or null
122+
/// </summary>
93123
public static string? GetMobileDevice(string userAgent)
94124
{
95125
foreach ((string key, string value) in HttpUserAgentStatics.Mobiles)
@@ -103,6 +133,9 @@ public static bool TryGetRobot(string userAgent, [NotNullWhen(true)] out string?
103133
return null;
104134
}
105135

136+
/// <summary>
137+
/// returns true if device was found
138+
/// </summary>
106139
public static bool TryGetMobileDevice(string userAgent, [NotNullWhen(true)] out string? device)
107140
{
108141
device = GetMobileDevice(userAgent);
Lines changed: 19 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,37 @@
1-
// Copyright © myCSharp 2020-2021, all rights reserved
1+
// Copyright © myCSharp 2020-2021, all rights reserved
22

33
using System.Text.RegularExpressions;
44

55
namespace MyCSharp.HttpUserAgentParser
66
{
7+
/// <summary>
8+
/// Information about the user agent platform
9+
/// </summary>
710
public readonly struct HttpUserAgentPlatformInformation
811
{
12+
/// <summary>
13+
/// Regex to match
14+
/// </summary>
915
public Regex Regex { get; }
16+
17+
/// <summary>
18+
/// Name of the platform
19+
/// </summary>
1020
public string Name { get; }
21+
22+
/// <summary>
23+
/// Specific platform type aka family
24+
/// </summary>
1125
public HttpUserAgentPlatformType PlatformType { get; }
1226

27+
/// <summary>
28+
/// Creates a new instance of <see cref="HttpUserAgentPlatformInformation"/>
29+
/// </summary>
1330
public HttpUserAgentPlatformInformation(Regex regex, string name, HttpUserAgentPlatformType platformType)
1431
{
1532
Regex = regex;
1633
Name = name;
1734
PlatformType = platformType;
1835
}
1936
}
20-
}
37+
}
Lines changed: 34 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,51 @@
1-
// Copyright © myCSharp 2020-2021, all rights reserved
1+
// Copyright © myCSharp 2020-2021, all rights reserved
22

33
namespace MyCSharp.HttpUserAgentParser
44
{
5+
/// <summary>
6+
/// Platform types
7+
/// </summary>
58
public enum HttpUserAgentPlatformType : byte
69
{
10+
/// <summary>
11+
/// Unknown / not mapped
12+
/// </summary>
713
Unknown = 0,
14+
/// <summary>
15+
/// Generic
16+
/// </summary>
817
Generic,
18+
/// <summary>
19+
/// Windows
20+
/// </summary>
921
Windows,
22+
/// <summary>
23+
/// Linux
24+
/// </summary>
1025
Linux,
26+
/// <summary>
27+
/// Unix
28+
/// </summary>
1129
Unix,
30+
/// <summary>
31+
/// Apple iOS
32+
/// </summary>
1233
IOS,
34+
/// <summary>
35+
/// MacOS
36+
/// </summary>
1337
MacOS,
38+
/// <summary>
39+
/// BlackBerry
40+
/// </summary>
1441
BlackBerry,
42+
/// <summary>
43+
/// Android
44+
/// </summary>
1545
Android,
46+
/// <summary>
47+
/// Symbian
48+
/// </summary>
1649
Symbian
1750
}
1851
}

src/MyCSharp.HttpUserAgentParser/HttpUserAgentStatics.cs

Lines changed: 33 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,11 +5,24 @@
55

66
namespace MyCSharp.HttpUserAgentParser
77
{
8+
/// <summary>
9+
/// Parser settings
10+
/// </summary>
811
public static class HttpUserAgentStatics
912
{
10-
13+
/// <summary>
14+
/// Regex defauls for platform mappings
15+
/// </summary>
1116
private const RegexOptions DefaultPlatformsRegexFlags = RegexOptions.IgnoreCase | RegexOptions.Compiled;
17+
18+
/// <summary>
19+
/// Creates default platform mapping regex
20+
/// </summary>
1221
private static Regex CreateDefaultPlatformRegex(string key) => new(Regex.Escape($"{key}"), DefaultPlatformsRegexFlags);
22+
23+
/// <summary>
24+
/// Platforms
25+
/// </summary>
1326
public static readonly HashSet<HttpUserAgentPlatformInformation> Platforms = new()
1427
{
1528
new(CreateDefaultPlatformRegex("windows nt 10.0"), "Windows 10", HttpUserAgentPlatformType.Windows),
@@ -56,8 +69,18 @@ public static class HttpUserAgentStatics
5669
new(CreateDefaultPlatformRegex("symbian"), "Symbian OS", HttpUserAgentPlatformType.Symbian),
5770
};
5871

72+
/// <summary>
73+
/// Regex defauls for browser mappings
74+
/// </summary>
5975
private const RegexOptions DefaultBrowserRegexFlags = RegexOptions.IgnoreCase | RegexOptions.Compiled;
76+
/// <summary>
77+
/// Creates default browser mapping regex
78+
/// </summary>
6079
private static Regex CreateDefaultBrowserRegex(string key) => new($@"{key}.*?([0-9\.]+)", DefaultBrowserRegexFlags);
80+
81+
/// <summary>
82+
/// Browsers
83+
/// </summary>
6184
public static Dictionary<Regex, string> Browsers = new()
6285
{
6386
{ CreateDefaultBrowserRegex("OPR"), "Opera" },
@@ -97,6 +120,9 @@ public static class HttpUserAgentStatics
97120
{ CreateDefaultBrowserRegex("Ubuntu"), "Ubuntu Web Browser" },
98121
};
99122

123+
/// <summary>
124+
/// Mobiles
125+
/// </summary>
100126
public static readonly Dictionary<string, string> Mobiles = new()
101127
{
102128
// Legacy
@@ -182,6 +208,9 @@ public static class HttpUserAgentStatics
182208
{ "cellphone", "Generic Mobile" },
183209
};
184210

211+
/// <summary>
212+
/// Robots
213+
/// </summary>
185214
public static readonly (string Key, string Value)[] Robots =
186215
{
187216
( "googlebot", "Googlebot" ),
@@ -228,6 +257,9 @@ public static readonly (string Key, string Value)[] Robots =
228257
( "Sistrix", "Sistrix" )
229258
};
230259

260+
/// <summary>
261+
/// Tools
262+
/// </summary>
231263
public static readonly Dictionary<string, string> Tools = new()
232264
{
233265
{ "curl", "curl" }

0 commit comments

Comments
 (0)