-
Notifications
You must be signed in to change notification settings - Fork 533
Expand file tree
/
Copy pathUserAgentContainer.cs
More file actions
93 lines (83 loc) · 3.58 KB
/
UserAgentContainer.cs
File metadata and controls
93 lines (83 loc) · 3.58 KB
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
// ------------------------------------------------------------
// Copyright (c) Microsoft Corporation. All rights reserved.
// ------------------------------------------------------------
namespace Microsoft.Azure.Cosmos
{
using System.Text.RegularExpressions;
/// <summary>
/// Contains information about the user environment and helps identify requests.
/// </summary>
internal class UserAgentContainer : Documents.UserAgentContainer
{
private const int MaxOperatingSystemString = 30;
private const int MaxClientId = 10;
private readonly string cosmosBaseUserAgent;
private readonly string clientId;
public UserAgentContainer(
int clientId,
string features = null,
string regionConfiguration = "NS",
string suffix = null)
: base()
{
this.clientId = System.Math.Min(clientId, UserAgentContainer.MaxClientId).ToString();
this.cosmosBaseUserAgent = this.CreateBaseUserAgentString(
features: features,
regionConfiguration: regionConfiguration);
this.Suffix = suffix ?? string.Empty;
}
public void AppendFeatures(
string features)
{
if (!string.IsNullOrEmpty(features))
{
this.Suffix = string.IsNullOrEmpty(this.Suffix)
? features
: $"{features}|{this.Suffix}";
}
}
internal override string BaseUserAgent => this.cosmosBaseUserAgent ?? string.Empty;
protected virtual void GetEnvironmentInformation(
out string clientVersion,
out string processArchitecture,
out string operatingSystem,
out string runtimeFramework)
{
EnvironmentInformation environmentInformation = new EnvironmentInformation();
clientVersion = environmentInformation.ClientVersion;
processArchitecture = environmentInformation.ProcessArchitecture;
operatingSystem = environmentInformation.OperatingSystem;
runtimeFramework = environmentInformation.RuntimeFramework;
}
private string CreateBaseUserAgentString(
string features = null,
string regionConfiguration = null)
{
this.GetEnvironmentInformation(
out string clientVersion,
out string processArchitecture,
out string operatingSystem,
out string runtimeFramework);
if (operatingSystem.Length > MaxOperatingSystemString)
{
operatingSystem = operatingSystem.Substring(0, MaxOperatingSystemString);
}
// Regex replaces all special characters with empty space except . - | since they do not cause format exception for the user agent string.
// Do not change the cosmos-netstandard-sdk as it is required for reporting
string previewFlag = string.Empty;
#if PREVIEW
previewFlag = "P";
#endif
string baseUserAgent = $"cosmos-netstandard-sdk/{clientVersion}" + previewFlag + Regex.Replace($"|{this.clientId}|{processArchitecture}|{operatingSystem}|{runtimeFramework}|", @"[^0-9a-zA-Z\.\|\-]+", " ");
if (!string.IsNullOrEmpty(regionConfiguration))
{
baseUserAgent += $"{regionConfiguration}|";
}
if (!string.IsNullOrEmpty(features))
{
baseUserAgent += $"F {features}|";
}
return baseUserAgent;
}
}
}