-
Notifications
You must be signed in to change notification settings - Fork 259
Expand file tree
/
Copy pathOkHttpNetworkHandler.cs
More file actions
274 lines (222 loc) · 11.1 KB
/
OkHttpNetworkHandler.cs
File metadata and controls
274 lines (222 loc) · 11.1 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
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
using System;
using System.Threading;
using System.Threading.Tasks;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Net.Http;
using OkHttp;
using Javax.Net.Ssl;
using System.Text.RegularExpressions;
using Java.IO;
using System.Security.Cryptography.X509Certificates;
using System.Globalization;
using Android.OS;
using Java.Net;
namespace ModernHttpClient
{
public class NativeMessageHandler : HttpClientHandler
{
readonly OkHttpClient client = new OkHttpClient();
readonly bool throwOnCaptiveNetwork;
readonly Dictionary<HttpRequestMessage, WeakReference> registeredProgressCallbacks =
new Dictionary<HttpRequestMessage, WeakReference>();
readonly Dictionary<string, string> headerSeparators =
new Dictionary<string,string>(){
{"User-Agent", " "}
};
public NativeMessageHandler() : this(false, false) {}
public NativeMessageHandler(bool throwOnCaptiveNetwork, bool customSSLVerification)
{
this.throwOnCaptiveNetwork = throwOnCaptiveNetwork;
if (customSSLVerification) client.SetHostnameVerifier(new HostnameVerifier());
}
public void RegisterForProgress(HttpRequestMessage request, ProgressDelegate callback)
{
if (callback == null && registeredProgressCallbacks.ContainsKey(request)) {
registeredProgressCallbacks.Remove(request);
return;
}
registeredProgressCallbacks[request] = new WeakReference(callback);
}
ProgressDelegate getAndRemoveCallbackFromRegister(HttpRequestMessage request)
{
ProgressDelegate emptyDelegate = delegate { };
lock (registeredProgressCallbacks) {
if (!registeredProgressCallbacks.ContainsKey(request)) return emptyDelegate;
var weakRef = registeredProgressCallbacks[request];
if (weakRef == null) return emptyDelegate;
var callback = weakRef.Target as ProgressDelegate;
if (callback == null) return emptyDelegate;
registeredProgressCallbacks.Remove(request);
return callback;
}
}
private string GetHeaderSeparator(string name)
{
if (headerSeparators.ContainsKey(name))
return headerSeparators[name];
return ",";
}
protected override async Task<HttpResponseMessage> SendAsync(HttpRequestMessage request, CancellationToken cancellationToken)
{
var java_uri = request.RequestUri.GetComponents(UriComponents.AbsoluteUri, UriFormat.UriEscaped);
var url = new Java.Net.URL(java_uri);
var body = default(RequestBody);
if (request.Content != null) {
var bytes = await request.Content.ReadAsByteArrayAsync().ConfigureAwait(false);
var contentType = String.Join (" ", request.Content.Headers.GetValues ("Content-Type"));
body = RequestBody.Create(MediaType.Parse(contentType), bytes);
}
var builder = new Request.Builder()
.Method(request.Method.Method.ToUpperInvariant(), body)
.Url(url);
var keyValuePairs = request.Headers
.Union(request.Content != null ?
(IEnumerable<KeyValuePair<string, IEnumerable<string>>>)request.Content.Headers :
Enumerable.Empty<KeyValuePair<string, IEnumerable<string>>>());
foreach (var kvp in keyValuePairs) builder.AddHeader(kvp.Key, String.Join(GetHeaderSeparator(kvp.Key), kvp.Value));
cancellationToken.ThrowIfCancellationRequested();
var rq = builder.Build();
var call = client.NewCall(rq);
// NB: Even closing a socket must be done off the UI thread. Cray!
cancellationToken.Register(() => Task.Run(() => call.Cancel()));
var resp = default(Response);
try {
resp = await call.EnqueueAsync().ConfigureAwait(false);
var newReq = resp.Request();
var newUri = newReq == null ? null : newReq.Uri();
if (throwOnCaptiveNetwork && newUri != null) {
if (url.Host != newUri.Host) {
throw new CaptiveNetworkException(new Uri(java_uri), new Uri(newUri.ToString()));
}
}
} catch (UnknownHostException ex) {
throw new HttpRequestException("Unknown host", ex);
} catch (IOException ex) {
if (ex.Message.ToLowerInvariant().Contains("canceled")) {
throw new OperationCanceledException(ex.Message, ex);
}
throw new HttpRequestException(
ex.Message,
ex);
}
var respBody = resp.Body();
cancellationToken.ThrowIfCancellationRequested();
var ret = new HttpResponseMessage((HttpStatusCode)resp.Code());
ret.RequestMessage = request;
ret.ReasonPhrase = resp.Message();
if (respBody != null) {
var content = new ProgressStreamContent(respBody.ByteStream(), cancellationToken);
content.Progress = getAndRemoveCallbackFromRegister(request);
ret.Content = content;
} else {
ret.Content = new ByteArrayContent(new byte[0]);
}
var respHeaders = resp.Headers();
foreach (var k in respHeaders.Names()) {
ret.Headers.TryAddWithoutValidation(k, respHeaders.Get(k));
ret.Content.Headers.TryAddWithoutValidation(k, respHeaders.Get(k));
}
return ret;
}
}
public static class AwaitableOkHttp
{
public static Task<Response> EnqueueAsync(this Call This)
{
var cb = new OkTaskCallback();
This.Enqueue(cb);
return cb.Task;
}
class OkTaskCallback : Java.Lang.Object, ICallback
{
readonly TaskCompletionSource<Response> tcs = new TaskCompletionSource<Response>();
public Task<Response> Task { get { return tcs.Task; } }
public void OnFailure(Request p0, Java.IO.IOException p1)
{
// Kind of a hack, but the simplest way to find out that server cert. validation failed
if (p1.Message == String.Format("Hostname '{0}' was not verified", p0.Url().Host)) {
tcs.TrySetException(new WebException(p1.LocalizedMessage, WebExceptionStatus.TrustFailure));
} else {
tcs.TrySetException(p1);
}
}
public void OnResponse(Response p0)
{
tcs.TrySetResult(p0);
}
}
}
class HostnameVerifier : Java.Lang.Object, IHostnameVerifier
{
static readonly Regex cnRegex = new Regex(@"CN\s*=\s*([^,]*)", RegexOptions.Compiled | RegexOptions.CultureInvariant | RegexOptions.Singleline);
public bool Verify(string hostname, ISSLSession session)
{
return verifyServerCertificate(hostname, session) & verifyClientCiphers(hostname, session);
}
/// <summary>
/// Verifies the server certificate by calling into ServicePointManager.ServerCertificateValidationCallback or,
/// if the is no delegate attached to it by using the default hostname verifier.
/// </summary>
/// <returns><c>true</c>, if server certificate was verifyed, <c>false</c> otherwise.</returns>
/// <param name="hostname"></param>
/// <param name="session"></param>
bool verifyServerCertificate(string hostname, ISSLSession session)
{
var defaultVerifier = HttpsURLConnection.DefaultHostnameVerifier;
if (ServicePointManager.ServerCertificateValidationCallback == null) return defaultVerifier.Verify(hostname, session);
// Convert java certificates to .NET certificates and build cert chain from root certificate
var certificates = session.GetPeerCertificateChain();
var chain = new System.Security.Cryptography.X509Certificates.X509Chain();
System.Security.Cryptography.X509Certificates.X509Certificate2 root = null;
var errors = System.Net.Security.SslPolicyErrors.None;
// Build certificate chain and check for errors
if (certificates == null || certificates.Length == 0) {//no cert at all
errors = System.Net.Security.SslPolicyErrors.RemoteCertificateNotAvailable;
goto bail;
}
if (certificates.Length == 1) {//no root?
errors = System.Net.Security.SslPolicyErrors.RemoteCertificateChainErrors;
goto bail;
}
var netCerts = certificates.Select(x => new System.Security.Cryptography.X509Certificates.X509Certificate2(x.GetEncoded())).ToArray();
for (int i = 1; i < netCerts.Length; i++) {
chain.ChainPolicy.ExtraStore.Add(netCerts[i]);
}
root = netCerts[0];
chain.ChainPolicy.RevocationFlag = System.Security.Cryptography.X509Certificates.X509RevocationFlag.EntireChain;
chain.ChainPolicy.RevocationMode = System.Security.Cryptography.X509Certificates.X509RevocationMode.NoCheck;
chain.ChainPolicy.UrlRetrievalTimeout = new TimeSpan(0, 1, 0);
chain.ChainPolicy.VerificationFlags =
System.Security.Cryptography.X509Certificates.X509VerificationFlags.AllowUnknownCertificateAuthority;
if (!chain.Build(root)) {
errors = System.Net.Security.SslPolicyErrors.RemoteCertificateChainErrors;
goto bail;
}
var subject = root.Subject;
var subjectCn = cnRegex.Match(subject).Groups[1].Value;
if (String.IsNullOrWhiteSpace(subjectCn) || !Utility.MatchHostnameToPattern(hostname, subjectCn)) {
errors = System.Net.Security.SslPolicyErrors.RemoteCertificateNameMismatch;
goto bail;
}
bail:
// Call the delegate to validate
return ServicePointManager.ServerCertificateValidationCallback(this, root, chain, errors);
}
/// <summary>
/// Verifies client ciphers and is only available in Mono and Xamarin products.
/// </summary>
/// <returns><c>true</c>, if client ciphers was verifyed, <c>false</c> otherwise.</returns>
/// <param name="hostname"></param>
/// <param name="session"></param>
bool verifyClientCiphers(string hostname, ISSLSession session)
{
var callback = ServicePointManager.ClientCipherSuitesCallback;
if (callback == null) return true;
var protocol = session.Protocol.StartsWith("SSL", StringComparison.InvariantCulture) ? SecurityProtocolType.Ssl3 : SecurityProtocolType.Tls;
var acceptedCiphers = callback(protocol, new[] { session.CipherSuite });
return acceptedCiphers.Contains(session.CipherSuite);
}
}
}