From caafa42471d3fc8036544ef6b0eea95a10535ef4 Mon Sep 17 00:00:00 2001 From: "thiago.azevedo" Date: Tue, 13 Nov 2018 10:23:11 +0100 Subject: [PATCH 1/2] Replacing http:// and https:// from CONSUL_HTTP_ADDR from env Variable --- Consul/Client.cs | 127 +++++++++++++++++++++++++++++++++++++---------- 1 file changed, 100 insertions(+), 27 deletions(-) diff --git a/Consul/Client.cs b/Consul/Client.cs index 539ad84..8b2d34a 100644 --- a/Consul/Client.cs +++ b/Consul/Client.cs @@ -10,6 +10,7 @@ using System.Net.Http.Headers; using System.Linq; using System.Security.Cryptography.X509Certificates; + #if !(CORECLR || PORTABLE || PORTABLE40) using System.Security.Permissions; using System.Runtime.Serialization; @@ -23,12 +24,25 @@ namespace Consul #if !(CORECLR || PORTABLE || PORTABLE40) [Serializable] #endif + public class ConsulRequestException : Exception { public HttpStatusCode StatusCode { get; set; } - public ConsulRequestException() { } - public ConsulRequestException(string message, HttpStatusCode statusCode) : base(message) { StatusCode = statusCode; } - public ConsulRequestException(string message, HttpStatusCode statusCode, Exception inner) : base(message, inner) { StatusCode = statusCode; } + + public ConsulRequestException() + { + } + + public ConsulRequestException(string message, HttpStatusCode statusCode) : base(message) + { + StatusCode = statusCode; + } + + public ConsulRequestException(string message, HttpStatusCode statusCode, Exception inner) : base(message, inner) + { + StatusCode = statusCode; + } + #if !(CORECLR || PORTABLE || PORTABLE40) protected ConsulRequestException( SerializationInfo info, @@ -50,11 +64,21 @@ public override void GetObjectData(SerializationInfo info, StreamingContext cont #if !(CORECLR || PORTABLE || PORTABLE40) [Serializable] #endif + public class ConsulConfigurationException : Exception { - public ConsulConfigurationException() { } - public ConsulConfigurationException(string message) : base(message) { } - public ConsulConfigurationException(string message, Exception inner) : base(message, inner) { } + public ConsulConfigurationException() + { + } + + public ConsulConfigurationException(string message) : base(message) + { + } + + public ConsulConfigurationException(string message, Exception inner) : base(message, inner) + { + } + #if !(CORECLR || PORTABLE || PORTABLE40) protected ConsulConfigurationException( System.Runtime.Serialization.SerializationInfo info, @@ -78,6 +102,7 @@ public class ConsulClientConfiguration internal bool ClientCertificateSupported { get { return _clientCertSupport.Value; } } #if CORECLR + [Obsolete("Use of DisableServerCertificateValidation should be converted to setting the HttpHandler's ServerCertificateCustomValidationCallback in the ConsulClient constructor" + "This property will be removed when 0.8.0 is released.", false)] #else @@ -112,6 +137,7 @@ internal bool DisableServerCertificateValidation /// This is only needed if an authenticating service exists in front of Consul; Token is used for ACL authentication by Consul. /// #if CORECLR + [Obsolete("Use of HttpAuth should be converted to setting the HttpHandler's Credential property in the ConsulClient constructor" + "This property will be removed when 0.8.0 is released.", false)] #else @@ -139,6 +165,7 @@ internal get #if __MonoCS__ [Obsolete("Client Certificates are not implemented in Mono", true)] #elif CORECLR + [Obsolete("Use of ClientCertificate should be converted to adding to the HttpHandler's ClientCertificates list in the ConsulClient constructor." + "This property will be removed when 0.8.0 is released.", false)] #else @@ -191,6 +218,10 @@ public ConsulClientConfiguration() private void ConfigureFromEnvironment(UriBuilder consulAddress) { var envAddr = (Environment.GetEnvironmentVariable("CONSUL_HTTP_ADDR") ?? string.Empty).Trim().ToLowerInvariant(); + + if (envAddr.Contains("http")) + envAddr = envAddr.Replace("http://", "").Replace("https://", ""); + if (!string.IsNullOrEmpty(envAddr)) { var addrParts = envAddr.Split(':'); @@ -376,6 +407,7 @@ public class WriteOptions /// public string Token { get; set; } } + public abstract class ConsulResult { /// @@ -387,13 +419,18 @@ public abstract class ConsulResult /// Exposed so that the consumer can to check for a specific status code /// public HttpStatusCode StatusCode { get; set; } - public ConsulResult() { } + + public ConsulResult() + { + } + public ConsulResult(ConsulResult other) { RequestTime = other.RequestTime; StatusCode = other.StatusCode; } } + /// /// The result of a Consul API query /// @@ -419,7 +456,10 @@ public class QueryResult : ConsulResult /// public bool AddressTranslationEnabled { get; set; } - public QueryResult() { } + public QueryResult() + { + } + public QueryResult(QueryResult other) : base(other) { LastIndex = other.LastIndex; @@ -438,8 +478,15 @@ public class QueryResult : QueryResult /// The result of the query /// public T Response { get; set; } - public QueryResult() { } - public QueryResult(QueryResult other) : base(other) { } + + public QueryResult() + { + } + + public QueryResult(QueryResult other) : base(other) + { + } + public QueryResult(QueryResult other, T value) : base(other) { Response = value; @@ -451,9 +498,15 @@ public QueryResult(QueryResult other, T value) : base(other) /// public class WriteResult : ConsulResult { - public WriteResult() { } - public WriteResult(WriteResult other) : base(other) { } + public WriteResult() + { + } + + public WriteResult(WriteResult other) : base(other) + { + } } + /// /// The result of a Consul API write /// @@ -464,8 +517,15 @@ public class WriteResult : WriteResult /// The result of the write /// public T Response { get; set; } - public WriteResult() { } - public WriteResult(WriteResult other) : base(other) { } + + public WriteResult() + { + } + + public WriteResult(WriteResult other) : base(other) + { + } + public WriteResult(WriteResult other, T value) : base(other) { Response = value; @@ -477,14 +537,12 @@ public WriteResult(WriteResult other, T value) : base(other) /// public partial class ConsulClient : IDisposable { - /// /// This class is used to group all the configurable bits of a ConsulClient into a single pointer reference /// which is great for implementing reconfiguration later. /// private class ConsulClientConfigurationContainer { - internal readonly bool skipClientDispose; internal readonly HttpClient HttpClient; #if CORECLR @@ -509,6 +567,7 @@ public ConsulClientConfigurationContainer() } #region Old style config handling + public ConsulClientConfigurationContainer(ConsulClientConfiguration config, HttpClient client) { skipClientDispose = true; @@ -529,9 +588,11 @@ public ConsulClientConfigurationContainer(ConsulClientConfiguration config) HttpClient.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json")); HttpClient.DefaultRequestHeaders.Add("Keep-Alive", "true"); } - #endregion + + #endregion Old style config handling #region IDisposable Support + private bool disposedValue = false; // To detect redundant calls protected virtual void Dispose(bool disposing) @@ -575,7 +636,8 @@ public void CheckDisposed() throw new ObjectDisposedException(typeof(ConsulClientConfigurationContainer).FullName.ToString()); } } - #endregion + + #endregion IDisposable Support } private ConsulClientConfigurationContainer ConfigContainer; @@ -591,6 +653,7 @@ public void CheckDisposed() internal readonly JsonSerializer serializer = new JsonSerializer(); #region New style config with Actions + /// /// Initializes a new Consul client with a default configuration that connects to 127.0.0.1:8500. /// @@ -623,6 +686,7 @@ public ConsulClient(Action configOverride, Action configOverride, Action clientOverride, Action handlerOverride) #else + public ConsulClient(Action configOverride, Action clientOverride, Action handlerOverride) #endif { @@ -637,9 +701,11 @@ public ConsulClient(Action configOverride, Action /// Initializes a new Consul client with the configuration specified. /// @@ -651,7 +717,7 @@ public ConsulClient(ConsulClientConfiguration config) config.Updated += HandleConfigUpdateEvent; var ctr = new ConsulClientConfigurationContainer(config); ApplyConfig(ctr.Config, ctr.HttpHandler, ctr.HttpClient); - + ConfigContainer = ctr; InitializeEndpoints(); } @@ -674,7 +740,8 @@ public ConsulClient(ConsulClientConfiguration config, HttpClient client) ConfigContainer = ctr; InitializeEndpoints(); } - #endregion + + #endregion Old style config private void InitializeEndpoints() { @@ -694,6 +761,7 @@ private void InitializeEndpoints() } #region IDisposable Support + private bool disposedValue = false; // To detect redundant calls protected virtual void Dispose(bool disposing) @@ -734,18 +802,20 @@ public void CheckDisposed() throw new ObjectDisposedException(typeof(ConsulClient).FullName.ToString()); } } - #endregion - void HandleConfigUpdateEvent(object sender, EventArgs e) + #endregion IDisposable Support + + private void HandleConfigUpdateEvent(object sender, EventArgs e) { ApplyConfig(sender as ConsulClientConfiguration, HttpHandler, HttpClient); - } + #if !CORECLR void ApplyConfig(ConsulClientConfiguration config, WebRequestHandler handler, HttpClient client) #else - void ApplyConfig(ConsulClientConfiguration config, HttpClientHandler handler, HttpClient client) -#endif + + private void ApplyConfig(ConsulClientConfiguration config, HttpClientHandler handler, HttpClient client) +#endif { #pragma warning disable CS0618 // Type or member is obsolete if (config.HttpAuth != null) @@ -887,6 +957,7 @@ internal ConsulRequest(ConsulClient client, string url, HttpMethod method) } protected abstract void ApplyOptions(ConsulClientConfiguration clientConfig); + protected abstract void ApplyHeaders(HttpRequestMessage message, ConsulClientConfiguration clientConfig); protected Uri BuildConsulUri(string url, Dictionary p) @@ -1027,9 +1098,11 @@ protected override void ApplyOptions(ConsulClientConfiguration clientConfig) case ConsistencyMode.Consistent: Params["consistent"] = string.Empty; break; + case ConsistencyMode.Stale: Params["stale"] = string.Empty; break; + case ConsistencyMode.Default: break; } @@ -1847,4 +1920,4 @@ protected override void ApplyHeaders(HttpRequestMessage message, ConsulClientCon } } } -} +} \ No newline at end of file From 2aa1a6e26e571f5449403efc0c002b8644f5a0e0 Mon Sep 17 00:00:00 2001 From: "thiago.azevedo" Date: Tue, 13 Nov 2018 10:26:47 +0100 Subject: [PATCH 2/2] Reverted White space removal --- Consul/Client.cs | 121 ++++++++++------------------------------------- 1 file changed, 26 insertions(+), 95 deletions(-) diff --git a/Consul/Client.cs b/Consul/Client.cs index 8b2d34a..79ce00e 100644 --- a/Consul/Client.cs +++ b/Consul/Client.cs @@ -10,7 +10,6 @@ using System.Net.Http.Headers; using System.Linq; using System.Security.Cryptography.X509Certificates; - #if !(CORECLR || PORTABLE || PORTABLE40) using System.Security.Permissions; using System.Runtime.Serialization; @@ -24,25 +23,12 @@ namespace Consul #if !(CORECLR || PORTABLE || PORTABLE40) [Serializable] #endif - public class ConsulRequestException : Exception { public HttpStatusCode StatusCode { get; set; } - - public ConsulRequestException() - { - } - - public ConsulRequestException(string message, HttpStatusCode statusCode) : base(message) - { - StatusCode = statusCode; - } - - public ConsulRequestException(string message, HttpStatusCode statusCode, Exception inner) : base(message, inner) - { - StatusCode = statusCode; - } - + public ConsulRequestException() { } + public ConsulRequestException(string message, HttpStatusCode statusCode) : base(message) { StatusCode = statusCode; } + public ConsulRequestException(string message, HttpStatusCode statusCode, Exception inner) : base(message, inner) { StatusCode = statusCode; } #if !(CORECLR || PORTABLE || PORTABLE40) protected ConsulRequestException( SerializationInfo info, @@ -64,21 +50,11 @@ public override void GetObjectData(SerializationInfo info, StreamingContext cont #if !(CORECLR || PORTABLE || PORTABLE40) [Serializable] #endif - public class ConsulConfigurationException : Exception { - public ConsulConfigurationException() - { - } - - public ConsulConfigurationException(string message) : base(message) - { - } - - public ConsulConfigurationException(string message, Exception inner) : base(message, inner) - { - } - + public ConsulConfigurationException() { } + public ConsulConfigurationException(string message) : base(message) { } + public ConsulConfigurationException(string message, Exception inner) : base(message, inner) { } #if !(CORECLR || PORTABLE || PORTABLE40) protected ConsulConfigurationException( System.Runtime.Serialization.SerializationInfo info, @@ -102,7 +78,6 @@ public class ConsulClientConfiguration internal bool ClientCertificateSupported { get { return _clientCertSupport.Value; } } #if CORECLR - [Obsolete("Use of DisableServerCertificateValidation should be converted to setting the HttpHandler's ServerCertificateCustomValidationCallback in the ConsulClient constructor" + "This property will be removed when 0.8.0 is released.", false)] #else @@ -137,7 +112,6 @@ internal bool DisableServerCertificateValidation /// This is only needed if an authenticating service exists in front of Consul; Token is used for ACL authentication by Consul. /// #if CORECLR - [Obsolete("Use of HttpAuth should be converted to setting the HttpHandler's Credential property in the ConsulClient constructor" + "This property will be removed when 0.8.0 is released.", false)] #else @@ -165,7 +139,6 @@ internal get #if __MonoCS__ [Obsolete("Client Certificates are not implemented in Mono", true)] #elif CORECLR - [Obsolete("Use of ClientCertificate should be converted to adding to the HttpHandler's ClientCertificates list in the ConsulClient constructor." + "This property will be removed when 0.8.0 is released.", false)] #else @@ -407,7 +380,6 @@ public class WriteOptions /// public string Token { get; set; } } - public abstract class ConsulResult { /// @@ -419,18 +391,13 @@ public abstract class ConsulResult /// Exposed so that the consumer can to check for a specific status code /// public HttpStatusCode StatusCode { get; set; } - - public ConsulResult() - { - } - + public ConsulResult() { } public ConsulResult(ConsulResult other) { RequestTime = other.RequestTime; StatusCode = other.StatusCode; } } - /// /// The result of a Consul API query /// @@ -456,10 +423,7 @@ public class QueryResult : ConsulResult /// public bool AddressTranslationEnabled { get; set; } - public QueryResult() - { - } - + public QueryResult() { } public QueryResult(QueryResult other) : base(other) { LastIndex = other.LastIndex; @@ -478,15 +442,8 @@ public class QueryResult : QueryResult /// The result of the query /// public T Response { get; set; } - - public QueryResult() - { - } - - public QueryResult(QueryResult other) : base(other) - { - } - + public QueryResult() { } + public QueryResult(QueryResult other) : base(other) { } public QueryResult(QueryResult other, T value) : base(other) { Response = value; @@ -498,15 +455,9 @@ public QueryResult(QueryResult other, T value) : base(other) /// public class WriteResult : ConsulResult { - public WriteResult() - { - } - - public WriteResult(WriteResult other) : base(other) - { - } + public WriteResult() { } + public WriteResult(WriteResult other) : base(other) { } } - /// /// The result of a Consul API write /// @@ -517,15 +468,8 @@ public class WriteResult : WriteResult /// The result of the write /// public T Response { get; set; } - - public WriteResult() - { - } - - public WriteResult(WriteResult other) : base(other) - { - } - + public WriteResult() { } + public WriteResult(WriteResult other) : base(other) { } public WriteResult(WriteResult other, T value) : base(other) { Response = value; @@ -537,12 +481,14 @@ public WriteResult(WriteResult other, T value) : base(other) /// public partial class ConsulClient : IDisposable { + /// /// This class is used to group all the configurable bits of a ConsulClient into a single pointer reference /// which is great for implementing reconfiguration later. /// private class ConsulClientConfigurationContainer { + internal readonly bool skipClientDispose; internal readonly HttpClient HttpClient; #if CORECLR @@ -567,7 +513,6 @@ public ConsulClientConfigurationContainer() } #region Old style config handling - public ConsulClientConfigurationContainer(ConsulClientConfiguration config, HttpClient client) { skipClientDispose = true; @@ -588,11 +533,9 @@ public ConsulClientConfigurationContainer(ConsulClientConfiguration config) HttpClient.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json")); HttpClient.DefaultRequestHeaders.Add("Keep-Alive", "true"); } - - #endregion Old style config handling + #endregion #region IDisposable Support - private bool disposedValue = false; // To detect redundant calls protected virtual void Dispose(bool disposing) @@ -636,8 +579,7 @@ public void CheckDisposed() throw new ObjectDisposedException(typeof(ConsulClientConfigurationContainer).FullName.ToString()); } } - - #endregion IDisposable Support + #endregion } private ConsulClientConfigurationContainer ConfigContainer; @@ -653,7 +595,6 @@ public void CheckDisposed() internal readonly JsonSerializer serializer = new JsonSerializer(); #region New style config with Actions - /// /// Initializes a new Consul client with a default configuration that connects to 127.0.0.1:8500. /// @@ -686,7 +627,6 @@ public ConsulClient(Action configOverride, Action configOverride, Action clientOverride, Action handlerOverride) #else - public ConsulClient(Action configOverride, Action clientOverride, Action handlerOverride) #endif { @@ -701,11 +641,9 @@ public ConsulClient(Action configOverride, Action /// Initializes a new Consul client with the configuration specified. /// @@ -740,8 +678,7 @@ public ConsulClient(ConsulClientConfiguration config, HttpClient client) ConfigContainer = ctr; InitializeEndpoints(); } - - #endregion Old style config + #endregion private void InitializeEndpoints() { @@ -761,7 +698,6 @@ private void InitializeEndpoints() } #region IDisposable Support - private bool disposedValue = false; // To detect redundant calls protected virtual void Dispose(bool disposing) @@ -802,20 +738,18 @@ public void CheckDisposed() throw new ObjectDisposedException(typeof(ConsulClient).FullName.ToString()); } } + #endregion - #endregion IDisposable Support - - private void HandleConfigUpdateEvent(object sender, EventArgs e) + void HandleConfigUpdateEvent(object sender, EventArgs e) { ApplyConfig(sender as ConsulClientConfiguration, HttpHandler, HttpClient); - } + } #if !CORECLR void ApplyConfig(ConsulClientConfiguration config, WebRequestHandler handler, HttpClient client) #else - - private void ApplyConfig(ConsulClientConfiguration config, HttpClientHandler handler, HttpClient client) -#endif + void ApplyConfig(ConsulClientConfiguration config, HttpClientHandler handler, HttpClient client) +#endif { #pragma warning disable CS0618 // Type or member is obsolete if (config.HttpAuth != null) @@ -957,7 +891,6 @@ internal ConsulRequest(ConsulClient client, string url, HttpMethod method) } protected abstract void ApplyOptions(ConsulClientConfiguration clientConfig); - protected abstract void ApplyHeaders(HttpRequestMessage message, ConsulClientConfiguration clientConfig); protected Uri BuildConsulUri(string url, Dictionary p) @@ -1098,11 +1031,9 @@ protected override void ApplyOptions(ConsulClientConfiguration clientConfig) case ConsistencyMode.Consistent: Params["consistent"] = string.Empty; break; - case ConsistencyMode.Stale: Params["stale"] = string.Empty; break; - case ConsistencyMode.Default: break; } @@ -1920,4 +1851,4 @@ protected override void ApplyHeaders(HttpRequestMessage message, ConsulClientCon } } } -} \ No newline at end of file +}