Skip to content

NetFx: Add workaround for unsupported HttpClientHandler.SslProtocols #58

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

Merged
merged 1 commit into from
Jul 5, 2023
Merged
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
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