Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix GetClientUserAgentString() #1151

Merged
merged 1 commit into from
Apr 6, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -33,11 +33,5 @@ public class when_building_the_client
{
var clientData = JToken.Parse(request.Headers.GetValues("X-Stripe-Client-User-Agent").FirstOrDefault());
};

It should_have_the_net45_for_uname = () =>
{
var clientData = JToken.Parse(request.Headers.GetValues("X-Stripe-Client-User-Agent").FirstOrDefault());
clientData.SelectToken("uname").ToString().ShouldContain("net45.");
};
}
}
105 changes: 22 additions & 83 deletions src/Stripe.net/Infrastructure/Client.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@
using Newtonsoft.Json;
#if NET45
using Microsoft.Win32;
#else
using System.Runtime.InteropServices;
#endif

namespace Stripe.Infrastructure
Expand All @@ -31,105 +33,42 @@ public void ApplyClientData()

private string GetClientUserAgentString()
{
var langVersion = "4.5";

#if NET45
langVersion = typeof(object).GetTypeInfo().Assembly.ImageRuntimeVersion;
#endif

var mono = testForMono();
if (!string.IsNullOrEmpty(mono)) langVersion = mono;

var values = new Dictionary<string, string>
{
{ "bindings_version", StripeConfiguration.StripeNetVersion },
{ "lang", ".net" },
{ "publisher", "Jayme Davis" },
{ "lang_version", WebUtility.HtmlEncode(langVersion) },
{ "uname", WebUtility.HtmlEncode(getSystemInformation()) }
{ "publisher", "stripe" },
};

return JsonConvert.SerializeObject(values, Formatting.None);
}

private string testForMono()
{
var type = Type.GetType("Mono.Runtime");
var getDisplayName = type?.GetTypeInfo().GetDeclaredMethod("GetDisplayName");

return getDisplayName?.Invoke(null, null).ToString();
}

private string getSystemInformation()
{
var result = string.Empty;

#if NET45
result += $"net45.platform: { Environment.OSVersion.VersionString }";
result += $", {getOperatingSystemInfo()}";
result += $", framework: {getFrameworkFromRegistry()}";
#else
result += "portable.platform: ";
values.Add("lang_version", ".NET Framework 4.5+");
values.Add("os_version", Environment.OSVersion.ToString());

try
{
result += typeof(object).GetTypeInfo().Assembly.GetCustomAttribute<AssemblyProductAttribute>().Product;
}
catch
{
result += "unknown";
string monoVersion = Client.GetMonoVersion();
if (monoVersion != null) {
values.Add("mono_version", monoVersion);
}
#else
values.Add("lang_version", RuntimeInformation.FrameworkDescription);
values.Add("os_version", RuntimeInformation.OSDescription);
#endif

return result;
return JsonConvert.SerializeObject(values, Formatting.None);
}

#if NET45
private static string getFrameworkFromRegistry()
{
using (var key = RegistryKey.OpenBaseKey(RegistryHive.LocalMachine, RegistryView.Registry32).OpenSubKey("SOFTWARE\\Microsoft\\NET Framework Setup\\NDP\\v4\\Full\\"))
{
var releaseKey = Convert.ToInt32(key.GetValue("Release"));
return get45Version(releaseKey);
}
}

private static string get45Version(int releaseKey)
{
if (releaseKey >= 393273)
return "4.6 RC or later";

if (releaseKey >= 379893)
return "4.5.2";

if (releaseKey >= 378675)
return "4.5.1";

if (releaseKey >= 378389)
return "4.5";

return "4.5 not detected! wat?";
}

private string getOperatingSystemInfo()
private static string GetMonoVersion()
{
var os = Environment.OSVersion;
var pid = os.Platform;

switch (pid)
{
case PlatformID.Win32NT:
case PlatformID.Win32S:
case PlatformID.Win32Windows:
case PlatformID.WinCE:
return "OS: Windows";
case PlatformID.Unix:
return "OS: Unix";
default:
return "OS: Unknown";
Type monoRuntimeType;
MethodInfo getDisplayNameMethod;
if ((monoRuntimeType = typeof(object).Assembly.GetType("Mono.Runtime")) != null &&
(getDisplayNameMethod = monoRuntimeType.GetMethod("GetDisplayName",
BindingFlags.NonPublic | BindingFlags.Static | BindingFlags.DeclaredOnly | BindingFlags.ExactBinding, null,
Type.EmptyTypes, null)) != null) {
return (string)getDisplayNameMethod.Invoke(null, null);
}
return null;
}
#endif

}
}
}