Skip to content

Commit 90fa59a

Browse files
authored
NetFx: Add workaround for unsupported HttpClientHandler.SslProtocols (#58)
1 parent 169a5a0 commit 90fa59a

File tree

1 file changed

+28
-3
lines changed

1 file changed

+28
-3
lines changed

src/Elastic.Stack.ArtifactsApi/Resolvers/ApiResolver.cs

+28-3
Original file line numberDiff line numberDiff line change
@@ -7,8 +7,19 @@
77
using System.Collections.Generic;
88
using System.IO;
99
using System.Linq;
10+
11+
#if NETFRAMEWORK
12+
using System.Net;
13+
#endif
14+
1015
using System.Net.Http;
16+
17+
#if !NETFRAMEWORK
18+
1119
using System.Security.Authentication;
20+
21+
#endif
22+
1223
using System.Text.Json;
1324
using System.Text.Json.Serialization;
1425
using System.Text.RegularExpressions;
@@ -22,7 +33,12 @@ public static class ApiResolver
2233
private static readonly ConcurrentDictionary<string, bool> Releases = new ConcurrentDictionary<string, bool>();
2334

2435
private static HttpClient HttpClient { get; } =
25-
new HttpClient(new HttpClientHandler {SslProtocols = SslProtocols.Tls12})
36+
#if NETFRAMEWORK
37+
new HttpClient
38+
#else
39+
// SslProtocols is only available in .NET Framework 4.7.2 and above
40+
new HttpClient(new HttpClientHandler { SslProtocols = SslProtocols.Tls12 })
41+
#endif
2642
{
2743
BaseAddress = new Uri(ArtifactsApiUrl)
2844
};
@@ -39,9 +55,10 @@ public static string FetchJson(string path)
3955

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

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

7794
return tokens[1];
7895
}
96+
97+
#if NETFRAMEWORK
98+
static ApiResolver() =>
99+
ServicePointManager.SecurityProtocol = ServicePointManager.SecurityProtocol
100+
& ~SecurityProtocolType.Ssl3
101+
& ~SecurityProtocolType.Tls
102+
& ~SecurityProtocolType.Tls11;
103+
#endif
79104
}
80105

81106
internal class ArtifactsVersionsResponse

0 commit comments

Comments
 (0)