Skip to content

Commit

Permalink
NetFx: Add workaround for unsupported HttpClientHandler.SslProtocols (
Browse files Browse the repository at this point in the history
  • Loading branch information
flobernd authored Jul 5, 2023
1 parent 169a5a0 commit 90fa59a
Showing 1 changed file with 28 additions and 3 deletions.
31 changes: 28 additions & 3 deletions src/Elastic.Stack.ArtifactsApi/Resolvers/ApiResolver.cs
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,19 @@
using System.Collections.Generic;
using System.IO;
using System.Linq;

#if NETFRAMEWORK
using System.Net;
#endif

using System.Net.Http;

#if !NETFRAMEWORK

using System.Security.Authentication;

#endif

using System.Text.Json;
using System.Text.Json.Serialization;
using System.Text.RegularExpressions;
Expand All @@ -22,7 +33,12 @@ public static class ApiResolver
private static readonly ConcurrentDictionary<string, bool> Releases = new ConcurrentDictionary<string, bool>();

private static HttpClient HttpClient { get; } =
new HttpClient(new HttpClientHandler {SslProtocols = SslProtocols.Tls12})
#if NETFRAMEWORK
new HttpClient
#else
// SslProtocols is only available in .NET Framework 4.7.2 and above
new HttpClient(new HttpClientHandler { SslProtocols = SslProtocols.Tls12 })
#endif
{
BaseAddress = new Uri(ArtifactsApiUrl)
};
Expand All @@ -39,9 +55,10 @@ public static string FetchJson(string path)

public static bool IsReleasedVersion(string version)
{
if (Releases.TryGetValue(version, out var released)) return released;
if (Releases.TryGetValue(version, out var released))
return released;
var versionPath = "https://github.com/elastic/elasticsearch/releases/tag/v" + version;
var message = new HttpRequestMessage {Method = HttpMethod.Head, RequestUri = new Uri(versionPath)};
var message = new HttpRequestMessage { Method = HttpMethod.Head, RequestUri = new Uri(versionPath) };

using (var response = HttpClient.SendAsync(message).GetAwaiter().GetResult())
{
Expand Down Expand Up @@ -76,6 +93,14 @@ public static string GetBuildHash(string url)

return tokens[1];
}

#if NETFRAMEWORK
static ApiResolver() =>
ServicePointManager.SecurityProtocol = ServicePointManager.SecurityProtocol
& ~SecurityProtocolType.Ssl3
& ~SecurityProtocolType.Tls
& ~SecurityProtocolType.Tls11;
#endif
}

internal class ArtifactsVersionsResponse
Expand Down

0 comments on commit 90fa59a

Please sign in to comment.