Skip to content

Commit

Permalink
Устанавливаем отдельный специальный User-Agent при запросах из COM SDK (
Browse files Browse the repository at this point in the history
#1097)

DDCORE-8019

Попутно Заменил netcoreapp2.2 на net5.0 в тестах.

Мотивация: netcoreapp2.2 уже нет в образе Visual Studio 2019 в appveyor [0].

[0]: https://www.appveyor.com/docs/windows-images-software/
  • Loading branch information
tkirill authored Dec 12, 2023
1 parent 16fc5ee commit 32b6906
Show file tree
Hide file tree
Showing 4 changed files with 57 additions and 31 deletions.
5 changes: 4 additions & 1 deletion src/ComDiadocApi.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
using System.Security;
using Diadoc.Api.Com;
using Diadoc.Api.Cryptography;
using Diadoc.Api.Http;
using Diadoc.Api.Proto;
using Diadoc.Api.Proto.Certificates;
using Diadoc.Api.Proto.Departments;
Expand Down Expand Up @@ -583,7 +584,9 @@ public class ComDiadocApi : SafeComObject, IComDiadocApi

public void Initialize(string apiClientId, string serverUrl)
{
diadoc = new DiadocApi(apiClientId, serverUrl, new WinApiCrypt());
var httpClient = new HttpClient(serverUrl);
httpClient.SetUserAgent(UserAgentBuilder.Build("COM"));
diadoc = new DiadocApi(new DiadocHttpApi(apiClientId, httpClient, new WinApiCrypt()));
DocflowApi = new ComDocflowApi(diadoc.Docflow);
}

Expand Down
37 changes: 8 additions & 29 deletions src/Http/HttpClient.cs
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@ public class HttpClient : IHttpClient
private readonly string baseUrl;
private NetworkCredential proxyCredential;
private Uri proxyUri;
private string userAgentString;

public HttpClient([NotNull] string baseUrl)
{
Expand All @@ -53,6 +54,7 @@ public HttpClient([NotNull] string baseUrl)

this.baseUrl = baseUrl.EndsWith("/") ? baseUrl.Substring(0, baseUrl.Length - 1) : baseUrl;
UseSystemProxy = true;
userAgentString = UserAgentBuilder.Build("C#");
}

/// <summary>
Expand Down Expand Up @@ -93,6 +95,11 @@ public void SetProxyCredentials([NotNull] string user, [NotNull] SecureString pa
proxyCredential = new NetworkCredential(userDomain.User, SecureStringToString(password), userDomain.Domain);
}

internal void SetUserAgent([NotNull] string userAgent)
{
userAgentString = userAgent ?? throw new ArgumentNullException(nameof(userAgent));
}

[NotNull]
private static string SecureStringToString([NotNull] SecureString secureString)
{
Expand Down Expand Up @@ -289,7 +296,7 @@ private WebRequest PrepareWebRequest([NotNull] HttpRequest request)
webRequest.ContentLength = 0;
}

webRequest.UserAgent = UserAgentString;
webRequest.UserAgent = userAgentString;
return webRequest;
}

Expand All @@ -304,33 +311,5 @@ private void ProxifyWebRequest([NotNull] WebRequest request)
request.Proxy.Credentials = proxyCredential;
}
}

private static string UserAgentString { get; set; }

static HttpClient()
{
string sdkVersion = typeof(HttpClient).Assembly.GetName().Version.ToString();
string netFxVersion = Environment.Version.ToString();
try
{
sdkVersion = FileVersionInfo.GetVersionInfo(typeof(HttpClient).Assembly.Location).FileVersion;
}
catch
{
}

try
{
netFxVersion = FileVersionInfo.GetVersionInfo(typeof(int).Assembly.Location).FileVersion;
}
catch
{
}

UserAgentString = string.Format("Diadoc C# SDK={0};OS={1};NETFX={2}",
sdkVersion,
Environment.OSVersion.VersionString,
netFxVersion);
}
}
}
44 changes: 44 additions & 0 deletions src/Http/UserAgentBuilder.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
using System;
using System.Diagnostics;
using JetBrains.Annotations;

namespace Diadoc.Api.Http
{
internal static class UserAgentBuilder
{
private static readonly string SdkVersion;
private static readonly string NetFxVersion;

static UserAgentBuilder()
{
SdkVersion = typeof(HttpClient).Assembly.GetName().Version.ToString();
NetFxVersion = Environment.Version.ToString();
try
{
SdkVersion = FileVersionInfo.GetVersionInfo(typeof(HttpClient).Assembly.Location).FileVersion;
}
catch
{
}

try
{
NetFxVersion = FileVersionInfo.GetVersionInfo(typeof(int).Assembly.Location).FileVersion;
}
catch
{
}
}

public static string Build([NotNull] string sdkName)
{
if (sdkName == null)
throw new ArgumentNullException(nameof(sdkName));
return string.Format("Diadoc {0} SDK={1};OS={2};NETFX={3}",
sdkName,
SdkVersion,
Environment.OSVersion.VersionString,
NetFxVersion);
}
}
}
2 changes: 1 addition & 1 deletion tests/DiadocApi-Tests.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
<PropertyGroup>
<RootNamespace>Diadoc.Api.Tests</RootNamespace>
<AssemblyName>DiadocApi.Tests</AssemblyName>
<TargetFrameworks>net35;net40;net45;net461;net472;netcoreapp2.2</TargetFrameworks>
<TargetFrameworks>net35;net40;net45;net461;net472;net5.0</TargetFrameworks>
<GenerateAssemblyInfo>false</GenerateAssemblyInfo>
<OutputPath>$(SolutionDir)\bin\$(Configuration)\$(AssemblyName)\</OutputPath>
</PropertyGroup>
Expand Down

0 comments on commit 32b6906

Please sign in to comment.