Skip to content

Commit b0458de

Browse files
committed
fix(User-Agent): Updated method of getting OS and OS version
Getting OS and OS Version for user-agent header was causing errors in different operating systems. This functionality was updated to clean up the strings and get a generalized OS
1 parent a035159 commit b0458de

File tree

3 files changed

+137
-10
lines changed

3 files changed

+137
-10
lines changed

src/IBM.WatsonDeveloperCloud/Http/HttpFactory.cs

Lines changed: 24 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -15,16 +15,22 @@
1515
*
1616
*/
1717

18+
using IBM.WatsonDeveloperCloud.Util;
1819
using System;
1920
using System.Linq;
2021
using System.Net.Http;
2122
using System.Net.Http.Formatting;
2223
using System.Net.Http.Headers;
24+
using System.Runtime.InteropServices;
2325

2426
namespace IBM.WatsonDeveloperCloud.Http
2527
{
2628
internal static class HttpFactory
2729
{
30+
private static string os;
31+
private static string osVersion;
32+
private static string frameworkDescription;
33+
2834
public static MediaTypeFormatter GetFormatter(MediaTypeFormatterCollection formatters, MediaTypeHeaderValue contentType = null)
2935
{
3036
if (!formatters.Any())
@@ -45,17 +51,25 @@ public static HttpRequestMessage GetRequestMessage(HttpMethod method, Uri resour
4551

4652
// add default headers
4753
request.Headers.Add("accept", formatters.SelectMany(p => p.SupportedMediaTypes).Select(p => p.MediaType));
48-
string osInfo = System.Runtime.InteropServices.RuntimeInformation.OSDescription;
49-
int versionIndex = osInfo.IndexOfAny("0123456789".ToCharArray());
50-
string os = osInfo.Substring(0, versionIndex).Replace(" ", "");
51-
string osVersion = osInfo.Substring(versionIndex).Replace(" ", "");
52-
request.Headers.Add("User-Agent",
54+
55+
if (string.IsNullOrEmpty(os) || string.IsNullOrEmpty(osVersion))
56+
{
57+
string osInfo = RuntimeInformation.OSDescription;
58+
os = Utility.GetOs(osInfo);
59+
osVersion = Utility.GetVersion(osInfo);
60+
}
61+
if (string.IsNullOrEmpty(frameworkDescription))
62+
{
63+
frameworkDescription = RuntimeInformation.FrameworkDescription.Trim();
64+
}
65+
66+
request.Headers.Add("User-Agent",
5367
string.Format(
54-
"{0} {1} {2} {3}",
55-
Constants.SDK_VERSION,
56-
os,
57-
osVersion,
58-
System.Runtime.InteropServices.RuntimeInformation.FrameworkDescription.Replace(" ", "")
68+
"{0} {1} {2} {3}",
69+
Constants.SDK_VERSION,
70+
Utility.CleanupUserAgentString(os),
71+
Utility.CleanupUserAgentString(osVersion),
72+
Utility.CleanupUserAgentString(frameworkDescription)
5973
));
6074

6175
return request;

src/IBM.WatsonDeveloperCloud/Util/Utility.cs

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,8 @@
2020
using System.IO;
2121
using System.Net;
2222
using System.Net.Http;
23+
using System.Runtime.InteropServices;
24+
using System.Text.RegularExpressions;
2325
using System.Threading.Tasks;
2426

2527
namespace IBM.WatsonDeveloperCloud.Util
@@ -168,5 +170,36 @@ public static List<string> GetCredentialsPaths()
168170

169171
return filePathsToLoad;
170172
}
173+
174+
public static string GetVersion(string input)
175+
{
176+
Regex pattern = new Regex("\\d+(\\.\\d+)+");
177+
Match m = pattern.Match(input);
178+
return m.Value;
179+
}
180+
181+
public static string GetOs(string input)
182+
{
183+
if (RuntimeInformation.IsOSPlatform(OSPlatform.OSX))
184+
{
185+
return "MacOS";
186+
}
187+
if (RuntimeInformation.IsOSPlatform(OSPlatform.Windows))
188+
{
189+
return "Windows";
190+
}
191+
if (RuntimeInformation.IsOSPlatform(OSPlatform.Linux))
192+
{
193+
return "Linux";
194+
}
195+
196+
return input;
197+
}
198+
199+
public static string CleanupUserAgentString(string input)
200+
{
201+
Regex pattern = new Regex("[;:#()~/ ]");
202+
return pattern.Replace(input, "-");
203+
}
171204
}
172205
}
Lines changed: 80 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,80 @@
1+
/**
2+
* Copyright 2019 IBM Corp. All Rights Reserved.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*
16+
*/
17+
18+
using IBM.WatsonDeveloperCloud.Util;
19+
using Microsoft.VisualStudio.TestTools.UnitTesting;
20+
using System.Collections.Generic;
21+
using System.Runtime.InteropServices;
22+
23+
namespace IBM.WatsonDeveloperCloud.Core.IntegrationTests
24+
{
25+
[TestClass]
26+
public class UserAgentParsingTests
27+
{
28+
[TestMethod]
29+
public void TestGetDefaultHeaders()
30+
{
31+
Dictionary<string, string> sdkHeaders = new Dictionary<string, string>();
32+
string osInfo = RuntimeInformation.OSDescription;
33+
string os = Utility.GetOs(osInfo);
34+
string osVersion = Utility.GetVersion(osInfo);
35+
string frameworkDescription = RuntimeInformation.FrameworkDescription.Trim();
36+
37+
sdkHeaders.Add("User-Agent", string.Format(
38+
"{0} {1} {2} {3}",
39+
Constants.SDK_VERSION,
40+
Utility.CleanupUserAgentString(os),
41+
Utility.CleanupUserAgentString(osVersion),
42+
Utility.CleanupUserAgentString(frameworkDescription)
43+
));
44+
Assert.IsTrue(sdkHeaders.Count == 1);
45+
Assert.IsTrue(sdkHeaders.ContainsKey("User-Agent"));
46+
Assert.IsTrue(sdkHeaders["User-Agent"].Contains(Constants.SDK_VERSION));
47+
Assert.IsFalse(sdkHeaders["User-Agent"].Contains("("));
48+
Assert.IsFalse(sdkHeaders["User-Agent"].Contains(")"));
49+
Assert.IsFalse(sdkHeaders["User-Agent"].Contains(":"));
50+
Assert.IsFalse(sdkHeaders["User-Agent"].Contains(";"));
51+
Assert.IsFalse(sdkHeaders["User-Agent"].Contains("#"));
52+
Assert.IsFalse(sdkHeaders["User-Agent"].Contains("~"));
53+
Assert.IsTrue(sdkHeaders["User-Agent"].Split().Length == 4);
54+
}
55+
56+
[TestMethod]
57+
public void GetVersionWindows()
58+
{
59+
string osDescription = "Microsoft Windows 10.0.17134 ";
60+
string osVersion = Utility.GetVersion(osDescription);
61+
Assert.IsTrue(osVersion == "10.0.17134");
62+
}
63+
64+
[TestMethod]
65+
public void GetVersionOSX()
66+
{
67+
string osDescription = "Darwin 17.7.0 Darwin Kernel Version 17.7.0: Fri Nov 2 20:43:16 PDT 2018; root:xnu-4570.71.17~1/RELEASE_X86_64";
68+
string osVersion = Utility.GetVersion(osDescription);
69+
Assert.IsTrue(osVersion == "17.7.0");
70+
}
71+
72+
[TestMethod]
73+
public void GetVersionLinux()
74+
{
75+
string osDescription = "Linux 4.19.28-1-MANJARO #1 SMP PREEMPT Sun Mar 10 08:32:42 UTC 2019";
76+
string osVersion = Utility.GetVersion(osDescription);
77+
Assert.IsTrue(osVersion == "4.19.28");
78+
}
79+
}
80+
}

0 commit comments

Comments
 (0)