This repository was archived by the owner on Jul 19, 2018. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 495
/
Copy pathUAParser.cs
438 lines (374 loc) · 16.7 KB
/
UAParser.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
#region Apache License, Version 2.0
//
// Copyright 2014 Atif Aziz
// Portions Copyright 2012 Søren Enemærke
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
//
#endregion
namespace UAParser
{
#region Imports
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text.RegularExpressions;
#endregion
public sealed class Device
{
public Device(string family, bool isSpider)
{
Family = family;
IsSpider = isSpider;
}
public string Family { get; private set; }
public bool IsSpider { get; private set; }
public override string ToString() { return Family; }
}
// ReSharper disable once InconsistentNaming
public sealed class OS
{
public OS(string family, string major, string minor, string patch, string patchMinor)
{
Family = family;
Major = major;
Minor = minor;
Patch = patch;
PatchMinor = patchMinor;
}
public string Family { get; private set; }
public string Major { get; private set; }
public string Minor { get; private set; }
public string Patch { get; private set; }
public string PatchMinor { get; private set; }
public override string ToString()
{
var version = VersionString.Format(Major, Minor, Patch, PatchMinor);
return Family + (!string.IsNullOrEmpty(version) ? " " + version : null);
}
}
public sealed class UserAgent
{
public UserAgent(string family, string major, string minor, string patch)
{
Family = family;
Major = major;
Minor = minor;
Patch = patch;
}
public string Family { get; private set; }
public string Major { get; private set; }
public string Minor { get; private set; }
public string Patch { get; private set; }
public override string ToString()
{
var version = VersionString.Format(Major, Minor, Patch);
return Family + (!string.IsNullOrEmpty(version) ? " " + version : null);
}
}
static class VersionString
{
public static string Format(params string[] parts)
{
return string.Join(".", parts.Where(v => !String.IsNullOrEmpty(v)).ToArray());
}
}
public class ClientInfo
{
// ReSharper disable once InconsistentNaming
public OS OS { get; private set; }
public Device Device { get; private set; }
public UserAgent UserAgent { get; private set; }
public ClientInfo(OS os, Device device, UserAgent userAgent)
{
OS = os;
Device = device;
UserAgent = userAgent;
}
public override string ToString()
{
return string.Format("{0} {1} {2}", OS, Device, UserAgent);
}
}
public sealed class Parser
{
readonly Func<string, OS> _osParser;
readonly Func<string, Device> _deviceParser;
readonly Func<string, UserAgent> _userAgentParser;
Parser(MinimalYamlParser yamlParser)
{
const string other = "Other";
var defaultDevice = new Device(other, isSpider: false);
_userAgentParser = CreateParser(Read(yamlParser.ReadMapping("user_agent_parsers"), Config.UserAgent), new UserAgent(other, null, null, null));
_osParser = CreateParser(Read(yamlParser.ReadMapping("os_parsers"), Config.OS), new OS(other, null, null, null, null));
_deviceParser = CreateParser(Read(yamlParser.ReadMapping("device_parsers"), Config.Device), defaultDevice.Family, f => defaultDevice.Family == f ? defaultDevice : new Device(f, "Spider".Equals(f, StringComparison.InvariantCultureIgnoreCase)));
}
static IEnumerable<T> Read<T>(IEnumerable<Dictionary<string, string>> entries, Func<Func<string, string>, T> selector)
{
return from cm in entries select selector(cm.Find);
}
public static Parser FromYaml(string yaml) { return new Parser(new MinimalYamlParser(yaml)); }
public static Parser FromYamlFile(string path) { return new Parser(new MinimalYamlParser(File.ReadAllText(path))); }
public static Parser GetDefault()
{
using (var stream = typeof(Parser).Assembly.GetManifestResourceStream("UAParser.regexes.yaml"))
// ReSharper disable once AssignNullToNotNullAttribute
using (var reader = new StreamReader(stream))
return new Parser(new MinimalYamlParser(reader.ReadToEnd()));
}
public ClientInfo Parse(string uaString)
{
var os = ParseOS(uaString);
var device = ParseDevice(uaString);
var ua = ParseUserAgent(uaString);
return new ClientInfo(os, device, ua);
}
public OS ParseOS(string uaString) { return _osParser(uaString); }
public Device ParseDevice(string uaString) { return _deviceParser(uaString); }
public UserAgent ParseUserAgent(string uaString) { return _userAgentParser(uaString); }
static Func<string, T> CreateParser<T>(IEnumerable<Func<string, T>> parsers, T defaultValue) where T : class
{
return CreateParser(parsers, defaultValue, t => t);
}
static Func<string, TResult> CreateParser<T, TResult>(IEnumerable<Func<string, T>> parsers, T defaultValue, Func<T, TResult> selector) where T : class
{
parsers = parsers != null ? parsers.ToArray() : Enumerable.Empty<Func<string, T>>();
return ua => selector(parsers.Select(p => p(ua)).FirstOrDefault(m => m != null) ?? defaultValue);
}
static class Config
{
// ReSharper disable once InconsistentNaming
public static Func<string, OS> OS(Func<string, string> indexer)
{
var regex = Regex(indexer, "OS");
var os = indexer("os_replacement");
var v1 = indexer("os_v1_replacement");
var v2 = indexer("os_v2_replacement");
return Parsers.OS(regex, os, v1, v2);
}
public static Func<string, UserAgent> UserAgent(Func<string, string> indexer)
{
var regex = Regex(indexer, "User agent");
var family = indexer("family_replacement");
var v1 = indexer("v1_replacement");
var v2 = indexer("v2_replacement");
return Parsers.UserAgent(regex, family, v1, v2);
}
public static Func<string, string> Device(Func<string, string> indexer)
{
return Parsers.Device(Regex(indexer, "Device"), indexer("device_replacement"));
}
static Regex Regex(Func<string, string> indexer, string key)
{
var pattern = indexer("regex");
if (pattern == null)
throw new Exception(String.Format("{0} is missing regular expression specification.", key));
// Some expressions in the regex.yaml file causes parsing errors
// in .NET such as the \_ token so need to alter them before
// proceeding.
if (pattern.IndexOf(@"\_", StringComparison.Ordinal) >= 0)
pattern = pattern.Replace(@"\_", "_");
// TODO: potentially allow parser to specify e.g. to use
// compiled regular expressions which are faster but increase
// startup time
return new Regex(pattern);
}
}
static class Parsers
{
// ReSharper disable once InconsistentNaming
public static Func<string, OS> OS(Regex regex, string osReplacement, string v1Replacement, string v2Replacement)
{
return Create(regex, from family in Replace(osReplacement, "$1")
from v1 in Replace(v1Replacement)
from v2 in Replace(v2Replacement)
from v3 in Select(v => v)
from v4 in Select(v => v)
select new OS(family, v1, v2, v3, v4));
}
public static Func<string, string> Device(Regex regex, string familyReplacement)
{
return Create(regex, Replace(familyReplacement, "$1"));
}
public static Func<string, UserAgent> UserAgent(Regex regex, string familyReplacement, string majorReplacement, string minorReplacement)
{
return Create(regex, from family in Replace(familyReplacement, "$1")
from v1 in Replace(majorReplacement)
from v2 in Replace(minorReplacement)
from v3 in Select()
select new UserAgent(family, v1, v2, v3));
}
static Func<Match, IEnumerator<int>, string> Replace(string replacement)
{
return replacement != null ? Select(_ => replacement) : Select();
}
static Func<Match, IEnumerator<int>, string> Replace(
string replacement, string token)
{
return replacement != null && replacement.Contains(token)
? Select(s => s != null ? replacement.ReplaceFirstOccurence(token, s) : replacement)
: Replace(replacement);
}
static Func<Match, IEnumerator<int>, string> Select() { return Select(v => v); }
static Func<Match, IEnumerator<int>, T> Select<T>(Func<string, T> selector)
{
return (m, num) =>
{
if (!num.MoveNext()) throw new InvalidOperationException();
var groups = m.Groups; Group group;
return selector(num.Current <= groups.Count && (group = groups[num.Current]).Success
? group.Value : null);
};
}
static Func<string, T> Create<T>(Regex regex, Func<Match, IEnumerator<int>, T> binder)
{
return input =>
{
var m = regex.Match(input);
var num = Generate(1, n => n + 1);
return m.Success ? binder(m, num) : default(T);
};
}
static IEnumerator<T> Generate<T>(T initial, Func<T, T> next)
{
for (var state = initial; ; state = next(state))
yield return state;
// ReSharper disable once FunctionNeverReturns
}
}
}
static class RegexBinderBuilder
{
public static Func<Match, IEnumerator<int>, TResult> SelectMany<T1, T2, TResult>(
this Func<Match, IEnumerator<int>, T1> binder,
Func<T1, Func<Match, IEnumerator<int>, T2>> continuation,
Func<T1, T2, TResult> projection)
{
return (m, num) => { T1 f; return projection(f = binder(m, num), continuation(f)(m, num)); };
}
}
static class StringExtensions
{
public static string ReplaceFirstOccurence(this string input, string search, string replacement)
{
if (input == null) throw new ArgumentNullException("input");
var index = input.IndexOf(search, StringComparison.Ordinal);
return index >= 0
? input.Substring(0, index) + replacement + input.Substring(index + search.Length)
: input;
}
}
static class DictionaryExtensions
{
public static TValue Find<TKey, TValue>(this IDictionary<TKey, TValue> dictionary, TKey key)
{
if (dictionary == null) throw new ArgumentNullException("dictionary");
TValue result;
return dictionary.TryGetValue(key, out result) ? result : default(TValue);
}
}
/// <summary>
/// Just enough string parsing to recognize the regexes.yaml file format. Introduced to remove
/// dependency on large Yaml parsing lib. Note that a unittest ensures compatibility
/// by ensuring regexes and properties are read similar to using the full yaml lib
/// </summary>
internal class MinimalYamlParser
{
internal class Mapping
{
private Dictionary<string, string> m_lastEntry;
public Mapping()
{
Sequences = new List<Dictionary<string, string>>();
}
public List<Dictionary<string, string>> Sequences { get; private set; }
public void BeginSequence()
{
m_lastEntry = new Dictionary<string, string>();
Sequences.Add(m_lastEntry);
}
public void AddToSequence(string key, string value)
{
m_lastEntry[key] = value;
}
}
private readonly Dictionary<string, Mapping> m_mappings = new Dictionary<string, Mapping>();
public MinimalYamlParser(string yamlString)
{
ReadIntoMappingModel(yamlString);
}
internal IDictionary<string, Mapping> Mappings { get { return m_mappings; } }
private void ReadIntoMappingModel(string yamlInputString)
{
// line splitting using various splitting characters
string[] lines = yamlInputString.Split(new[] { Environment.NewLine, "\r", "\n", "\r\n" }, StringSplitOptions.RemoveEmptyEntries);
int lineCount = 0;
Mapping activeMapping = null;
foreach (var line in lines)
{
lineCount++;
if (line.Trim().StartsWith("#")) //skipping comments
continue;
if (line.Trim().Length == 0)
continue;
//is this a new mapping entity
if (line[0] != ' ')
{
int indexOfMappingColon = line.IndexOf(':');
if (indexOfMappingColon == -1)
throw new ArgumentException("YamlParsing: Expecting mapping entry to contain a ':', at line " + lineCount);
string name = line.Substring(0, indexOfMappingColon).Trim();
activeMapping = new Mapping();
m_mappings.Add(name, activeMapping);
continue;
}
//reading scalar entries into the active mapping
if (activeMapping == null)
throw new ArgumentException("YamlParsing: Expecting mapping entry to contain a ':', at line " + lineCount);
var seqLine = line.Trim();
if (seqLine[0] == '-')
{
activeMapping.BeginSequence();
seqLine = seqLine.Substring(1);
}
int indexOfColon = seqLine.IndexOf(':');
if (indexOfColon == -1)
throw new ArgumentException("YamlParsing: Expecting scalar mapping entry to contain a ':', at line " + lineCount);
string key = seqLine.Substring(0, indexOfColon).Trim();
string value = ReadQuotedValue(seqLine.Substring(indexOfColon + 1).Trim());
activeMapping.AddToSequence(key, value);
}
}
private static string ReadQuotedValue(string value)
{
if (value.StartsWith("'") && value.EndsWith("'"))
return value.Substring(1, value.Length - 2);
if (value.StartsWith("\"") && value.EndsWith("\""))
return value.Substring(1, value.Length - 2);
return value;
}
public IEnumerable<Dictionary<string, string>> ReadMapping(string mappingName)
{
Mapping mapping;
if (m_mappings.TryGetValue(mappingName, out mapping))
{
foreach (var s in mapping.Sequences)
{
var temp = s;
yield return temp;
}
}
}
}
}