-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathRetryWithCurlOnSecureChannelFailureHandler.cs
More file actions
123 lines (79 loc) · 3.99 KB
/
RetryWithCurlOnSecureChannelFailureHandler.cs
File metadata and controls
123 lines (79 loc) · 3.99 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
using Gsemac.IO.Logging;
using Gsemac.Net.Http;
using Gsemac.Net.Http.Extensions;
using System;
using System.Collections.Generic;
using System.Net;
using System.Threading;
namespace Gsemac.Net.Curl {
// Automatically retry requests made with .NET's native HTTP engine with Curl when we get a "SecureChannelFailure" error.
// This allows requests using protocols not supported on older versions of .NET (e.g. TLS 1.3) to succeed.
public sealed class RetryWithCurlOnSecureChannelFailureHandler :
HttpWebRequestHandler {
// Public members
public RetryWithCurlOnSecureChannelFailureHandler() :
this(Logger.Null) {
}
public RetryWithCurlOnSecureChannelFailureHandler(ICurlHttpWebRequestFactory webRequestFactory) :
this(webRequestFactory, Logger.Null) {
}
public RetryWithCurlOnSecureChannelFailureHandler(ILogger logger) :
this(new CurlHttpWebRequestFactory(), logger) {
}
public RetryWithCurlOnSecureChannelFailureHandler(ICurlHttpWebRequestFactory webRequestFactory, ILogger logger) :
base() {
if (webRequestFactory is null)
throw new ArgumentNullException(nameof(webRequestFactory));
if (logger is null)
throw new ArgumentNullException(nameof(logger));
this.webRequestFactory = webRequestFactory;
this.logger = new NamedLogger(logger, nameof(RetryWithCurlOnSecureChannelFailureHandler));
}
// Protected members
protected override IHttpWebResponse GetResponse(IHttpWebRequest request, CancellationToken cancellationToken) {
if (request is null)
throw new ArgumentNullException(nameof(request));
bool shouldHandleRequestWithCurl = false;
lock (hostnameCache)
shouldHandleRequestWithCurl = hostnameCache.Contains(request.Host);
if (shouldHandleRequestWithCurl)
return GetResponseWithCurl(request, cancellationToken);
try {
return base.GetResponse(request, cancellationToken);
}
catch (WebException ex) {
bool isFailedToCreateSecureChannelError = ex.Status == WebExceptionStatus.SecureChannelFailure;
if (isFailedToCreateSecureChannelError) {
if (HttpWebRequestUtilities.GetInnermostWebRequest((WebRequest)request) is CurlHttpWebRequest)
throw;
try {
logger.Error(ex);
logger.Warning($"Failed to create secure channel. Retrying request with Curl.");
IHttpWebResponse response = GetResponseWithCurl(request, cancellationToken);
// If the request was successful, use Curl for subsequent requests to this host.
lock (hostnameCache)
hostnameCache.Add(request.Host);
return response;
}
finally {
// Make sure to close the response before returning, because it will not be accessible to the caller.
if (ex.Response is object)
ex.Response.Close();
}
}
else {
throw;
}
}
}
// Private members
private readonly ILogger logger;
private readonly ICurlHttpWebRequestFactory webRequestFactory;
private static readonly HashSet<string> hostnameCache = new HashSet<string>();
private IHttpWebResponse GetResponseWithCurl(IHttpWebRequest request, CancellationToken cancellationToken) {
IHttpWebRequest curlWebRequest = webRequestFactory.Create(request.RequestUri)
.WithOptions(HttpWebRequestOptions.FromHttpWebRequest(request));
return base.GetResponse(curlWebRequest, cancellationToken);
}
}
}