예제 #1
0
        public static ConnectionSettings CreateConnectionSettings <TConfig>(this IEphemeralCluster <TConfig> cluster)
            where TConfig : EphemeralClusterConfiguration
        {
            var clusterNodes = cluster.NodesUris(TestConnectionSettings.LocalOrProxyHost);

            //we ignore the uri's that TestConnection provides and seed with the nodes the cluster dictates.
            return(new TestConnectionSettings(uris => new StaticConnectionPool(clusterNodes)));
        }
예제 #2
0
        private HttpResponseMessage Call(
            IEphemeralCluster <EphemeralClusterConfiguration> cluster,
            string path,
            string query,
            Func <HttpClient, Uri, CancellationToken, Task <HttpResponseMessage> > verb)
        {
            var statusUrl = new UriBuilder(cluster.NodesUris().First())
            {
                Path = path, Query = query
            }.Uri;

            var tokenSource = new CancellationTokenSource(TimeSpan.FromSeconds(20));
            var handler     = new HttpClientHandler
            {
                AutomaticDecompression = DecompressionMethods.Deflate | DecompressionMethods.GZip | DecompressionMethods.None,
            };

            if (cluster.ClusterConfiguration.EnableSsl)
            {
                handler.ServerCertificateCustomValidationCallback += (m, c, cn, p) => true;
            }
            using (var client = new HttpClient(handler)
            {
                Timeout = TimeSpan.FromSeconds(20)
            })
            {
                if (cluster.ClusterConfiguration.EnableSecurity)
                {
                    var byteArray = Encoding.ASCII.GetBytes($"{ClusterAuthentication.Admin.Username}:{ClusterAuthentication.Admin.Password}");
                    client.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue("Basic", Convert.ToBase64String(byteArray));
                }

                try
                {
                    var response = verb(client, statusUrl, tokenSource.Token).ConfigureAwait(false).GetAwaiter().GetResult();
                    if (response.StatusCode == HttpStatusCode.OK)
                    {
                        return(response);
                    }
                }
                catch
                {
                    // ignored
                }
            }
            return(null);
        }
예제 #3
0
        private HttpResponseMessage Call(
            IEphemeralCluster <EphemeralClusterConfiguration> cluster,
            string path,
            string query,
            Func <HttpClient, Uri, CancellationToken, Task <HttpResponseMessage> > verb)
        {
            var q         = string.IsNullOrEmpty(query) ? "pretty=true" : (query + "&pretty=true");
            var statusUrl = new UriBuilder(cluster.NodesUris().First())
            {
                Path = path, Query = q
            }.Uri;

            var tokenSource = new CancellationTokenSource(TimeSpan.FromSeconds(20));
            var handler     = new HttpClientHandler
            {
                AutomaticDecompression =
                    DecompressionMethods.Deflate | DecompressionMethods.GZip | DecompressionMethods.None,
            };

            cluster.Writer.WriteDiagnostic($"{{{nameof(Call)}}} [{statusUrl}] SSL: {cluster.ClusterConfiguration.EnableSsl} Security: {cluster.ClusterConfiguration.EnableSecurity}");
            if (cluster.ClusterConfiguration.EnableSsl)
            {
#if !NETSTANDARD
                ServicePointManager.ServerCertificateValidationCallback += ServerCertificateValidationCallback;
#else
                handler.ServerCertificateCustomValidationCallback += (m, c, cn, p) => true;
#endif
            }

            using (var client = new HttpClient(handler)
            {
                Timeout = TimeSpan.FromSeconds(20)
            })
            {
                if (cluster.ClusterConfiguration.EnableSecurity)
                {
                    var byteArray =
                        Encoding.ASCII.GetBytes(
                            $"{ClusterAuthentication.Admin.Username}:{ClusterAuthentication.Admin.Password}");
                    client.DefaultRequestHeaders.Authorization =
                        new AuthenticationHeaderValue("Basic", Convert.ToBase64String(byteArray));
                }

                try
                {
                    var response = verb(client, statusUrl, tokenSource.Token).ConfigureAwait(false).GetAwaiter()
                                   .GetResult();
                    if (response.StatusCode == HttpStatusCode.OK)
                    {
                        return(response);
                    }
                    cluster.Writer.WriteDiagnostic(
                        $"{{{nameof(Call)}}} [{statusUrl}] Bad status code: [{(int) response.StatusCode}]");
                    var body = response.Content.ReadAsStringAsync().GetAwaiter().GetResult();
                    foreach (var l in (body ?? string.Empty).Split('\n', '\r'))
                    {
                        cluster.Writer.WriteDiagnostic($"{{{nameof(Call)}}} [{statusUrl}] returned [{l}]");
                    }
                }
                catch (Exception e)
                {
                    cluster.Writer.WriteError($"{{{nameof(Call)}}} [{statusUrl}] exception: {e}");
                    // ignored
                }
                finally
                {
#if !NETSTANDARD
                    ServicePointManager.ServerCertificateValidationCallback -= ServerCertificateValidationCallback;
#endif
                }
            }

            return(null);
        }