-
Notifications
You must be signed in to change notification settings - Fork 14
/
Copy pathRestClient.cs
63 lines (53 loc) · 1.86 KB
/
RestClient.cs
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
// Copyright (c) Microsoft. All rights reserved.
// Licensed under the MIT license. See LICENSE file in the project root for full license information.
using UnityEngine;
using System.Collections;
using System.Collections.Generic;
using System;
using System.Text.RegularExpressions;
#if !NETFX_CORE || UNITY_ANDROID
using System.Net;
using System.Security.Cryptography.X509Certificates;
using System.Net.Security;
#endif
namespace RESTClient {
public class RestClient {
public string Url { get; private set; }
/// <summary>
/// Creates a new REST Client
/// </summary>
public RestClient(string url, bool forceHttps = false) {
Url = forceHttps ? HttpsUri(url) : url;
// required for running in Windows and Android
#if !NETFX_CORE || UNITY_ANDROID
ServicePointManager.ServerCertificateValidationCallback = RemoteCertificateValidationCallback;
#endif
}
public override string ToString() {
return this.Url;
}
/// <summary>
/// Changes 'http' to be 'https' instead
/// </summary>
private static string HttpsUri(string appUrl) {
return Regex.Replace(appUrl, "(?si)^http://", "https://").TrimEnd('/');
}
private static string DomainName(string url) {
var match = Regex.Match(url, @"^(https:\/\/|http:\/\/)(www\.)?([a-z0-9-_]+\.[a-z]+)", RegexOptions.IgnoreCase);
if (match.Groups.Count == 4 && match.Groups[3].Value.Length > 0) {
return match.Groups[3].Value;
}
return url;
}
#if !NETFX_CORE || UNITY_ANDROID
private bool RemoteCertificateValidationCallback(System.Object sender, X509Certificate certificate, X509Chain chain, SslPolicyErrors sslPolicyErrors) {
// Check the certificate to see if it was issued from host
if (certificate.Subject.Contains(DomainName(Url))) {
return true;
} else {
return false;
}
}
#endif
}
}