diff --git a/src/Ocelot/Configuration/Creator/HeaderFindAndReplaceCreator.cs b/src/Ocelot/Configuration/Creator/HeaderFindAndReplaceCreator.cs index 1a5f1b6a1..53f629af6 100644 --- a/src/Ocelot/Configuration/Creator/HeaderFindAndReplaceCreator.cs +++ b/src/Ocelot/Configuration/Creator/HeaderFindAndReplaceCreator.cs @@ -2,6 +2,7 @@ using System.Collections.Generic; using Ocelot.Configuration.File; using Ocelot.Infrastructure; +using Ocelot.Infrastructure.Extensions; using Ocelot.Logging; using Ocelot.Middleware; using Ocelot.Responses; diff --git a/src/Ocelot/Configuration/File/FileAuthenticationOptions.cs b/src/Ocelot/Configuration/File/FileAuthenticationOptions.cs index 70e376906..873d1b5bb 100644 --- a/src/Ocelot/Configuration/File/FileAuthenticationOptions.cs +++ b/src/Ocelot/Configuration/File/FileAuthenticationOptions.cs @@ -1,5 +1,6 @@ using System.Collections.Generic; using System.Text; +using Ocelot.Infrastructure.Extensions; namespace Ocelot.Configuration.File { diff --git a/src/Ocelot/Configuration/File/FileRateLimitRule.cs b/src/Ocelot/Configuration/File/FileRateLimitRule.cs index 5e1616e65..7d1ca1ef4 100644 --- a/src/Ocelot/Configuration/File/FileRateLimitRule.cs +++ b/src/Ocelot/Configuration/File/FileRateLimitRule.cs @@ -3,6 +3,7 @@ using System.Linq; using System.Text; using System.Threading.Tasks; +using Ocelot.Infrastructure.Extensions; namespace Ocelot.Configuration.File { diff --git a/src/Ocelot/Configuration/FileConfigurationController.cs b/src/Ocelot/Configuration/FileConfigurationController.cs index 3b88096f6..1ea7fa7d3 100644 --- a/src/Ocelot/Configuration/FileConfigurationController.cs +++ b/src/Ocelot/Configuration/FileConfigurationController.cs @@ -49,7 +49,7 @@ public async Task Post([FromBody]FileConfiguration fileConfigurat if (test != null) { var node = (INode)test; - var result = node.Accept(new UpdateFileConfiguration(fileConfiguration)); + var result = await node.Accept(new UpdateFileConfiguration(fileConfiguration)); if (result.GetType() == typeof(Rafty.Concensus.ErrorResponse)) { return new BadRequestObjectResult("There was a problem. This error message sucks raise an issue in GitHub."); diff --git a/src/Ocelot/Headers/AddHeadersToResponse.cs b/src/Ocelot/Headers/AddHeadersToResponse.cs index 6cdf9a221..7c440d6af 100644 --- a/src/Ocelot/Headers/AddHeadersToResponse.cs +++ b/src/Ocelot/Headers/AddHeadersToResponse.cs @@ -3,6 +3,7 @@ namespace Ocelot.Headers using System.Collections.Generic; using Ocelot.Configuration.Creator; using Ocelot.Infrastructure; + using Ocelot.Infrastructure.Extensions; using Ocelot.Logging; using Ocelot.Middleware; diff --git a/src/Ocelot/Infrastructure/Extensions/NetCoreSupportExtensions.cs b/src/Ocelot/Infrastructure/Extensions/NetCoreSupportExtensions.cs new file mode 100644 index 000000000..3dee5caf2 --- /dev/null +++ b/src/Ocelot/Infrastructure/Extensions/NetCoreSupportExtensions.cs @@ -0,0 +1,32 @@ +using System; +using System.Collections.Generic; +using System.Text; + +namespace Ocelot.Infrastructure.Extensions +{ + /// + /// Trivial implementations of methods present in .NET Core 2 but not supported on .NET Standard 2.0. + /// + internal static class NetCoreSupportExtensions + { + internal static void AppendJoin(this StringBuilder builder, char separator, IEnumerable values) + { + builder.Append(string.Join(separator.ToString(), values)); + } + + internal static string[] Split(this string input, string separator, StringSplitOptions options = StringSplitOptions.None) + { + return input.Split(new[] { separator }, options); + } + + internal static bool StartsWith(this string input, char value) + { + return input.StartsWith(value.ToString()); + } + + internal static bool EndsWith(this string input, char value) + { + return input.EndsWith(value.ToString()); + } + } +} diff --git a/src/Ocelot/LoadBalancer/LoadBalancers/CookieStickySessions.cs b/src/Ocelot/LoadBalancer/LoadBalancers/CookieStickySessions.cs index b64c4b435..357ea84e7 100644 --- a/src/Ocelot/LoadBalancer/LoadBalancers/CookieStickySessions.cs +++ b/src/Ocelot/LoadBalancer/LoadBalancers/CookieStickySessions.cs @@ -35,7 +35,7 @@ public CookieStickySessions(ILoadBalancer loadBalancer, string key, int keyExpir { if (stickySession.Expiry < DateTime.UtcNow) { - _stored.Remove(stickySession.Key, out _); + _stored.TryRemove(stickySession.Key, out _); _loadBalancer.Release(stickySession.HostAndPort); } } diff --git a/src/Ocelot/Ocelot.csproj b/src/Ocelot/Ocelot.csproj index 833887a2e..c7dfcfdae 100644 --- a/src/Ocelot/Ocelot.csproj +++ b/src/Ocelot/Ocelot.csproj @@ -1,6 +1,6 @@ - netcoreapp2.0 + netstandard2.0 2.0.0 2.0.0 true @@ -25,14 +25,22 @@ True - + + + NU1701 + - + + + + - + + NU1701 + @@ -48,6 +56,6 @@ - + diff --git a/src/Ocelot/Raft/FileFsm.cs b/src/Ocelot/Raft/FileFsm.cs index 13c41c9d7..afa47d26e 100644 --- a/src/Ocelot/Raft/FileFsm.cs +++ b/src/Ocelot/Raft/FileFsm.cs @@ -1,5 +1,6 @@ using System; using System.IO; +using System.Threading.Tasks; using Newtonsoft.Json; using Rafty.FiniteStateMachine; using Rafty.Infrastructure; @@ -17,7 +18,7 @@ public FileFsm(NodeId nodeId) _id = nodeId.Id.Replace("/","").Replace(":",""); } - public void Handle(LogEntry log) + public Task Handle(LogEntry log) { try { @@ -28,6 +29,8 @@ public void Handle(LogEntry log) { Console.WriteLine(exception); } + + return Task.CompletedTask; } } } diff --git a/src/Ocelot/Raft/HttpPeer.cs b/src/Ocelot/Raft/HttpPeer.cs index b89c884c5..135e1d021 100644 --- a/src/Ocelot/Raft/HttpPeer.cs +++ b/src/Ocelot/Raft/HttpPeer.cs @@ -1,6 +1,7 @@ using System; using System.Collections.Generic; using System.Net.Http; +using System.Threading.Tasks; using Newtonsoft.Json; using Ocelot.Configuration; using Ocelot.Middleware; @@ -35,7 +36,7 @@ public HttpPeer(string hostAndPort, HttpClient httpClient, IBaseUrlFinder finder public string Id {get; private set;} - public RequestVoteResponse Request(RequestVote requestVote) + public async Task Request(RequestVote requestVote) { if(_token == null) { @@ -48,7 +49,7 @@ public RequestVoteResponse Request(RequestVote requestVote) var response = _httpClient.PostAsync($"{_hostAndPort}/administration/raft/requestvote", content).GetAwaiter().GetResult(); if(response.IsSuccessStatusCode) { - return JsonConvert.DeserializeObject(response.Content.ReadAsStringAsync().GetAwaiter().GetResult(), _jsonSerializerSettings); + return JsonConvert.DeserializeObject(await response.Content.ReadAsStringAsync(), _jsonSerializerSettings); } else { @@ -56,7 +57,7 @@ public RequestVoteResponse Request(RequestVote requestVote) } } - public AppendEntriesResponse Request(AppendEntries appendEntries) + public async Task Request(AppendEntries appendEntries) { try { @@ -71,7 +72,7 @@ public AppendEntriesResponse Request(AppendEntries appendEntries) var response = _httpClient.PostAsync($"{_hostAndPort}/administration/raft/appendEntries", content).GetAwaiter().GetResult(); if(response.IsSuccessStatusCode) { - return JsonConvert.DeserializeObject(response.Content.ReadAsStringAsync().GetAwaiter().GetResult(),_jsonSerializerSettings); + return JsonConvert.DeserializeObject(await response.Content.ReadAsStringAsync(), _jsonSerializerSettings); } else { @@ -85,7 +86,7 @@ public AppendEntriesResponse Request(AppendEntries appendEntries) } } - public Response Request(T command) + public async Task> Request(T command) where T : ICommand { Console.WriteLine("SENDING REQUEST...."); @@ -107,7 +108,7 @@ public Response Request(T command) else { Console.WriteLine("REQUEST NOT OK...."); - return new ErrorResponse(response.Content.ReadAsStringAsync().GetAwaiter().GetResult(), command); + return new ErrorResponse(await response.Content.ReadAsStringAsync(), command); } } diff --git a/src/Ocelot/Raft/OcelotFiniteStateMachine.cs b/src/Ocelot/Raft/OcelotFiniteStateMachine.cs index feecd7d7c..b3d7720fb 100644 --- a/src/Ocelot/Raft/OcelotFiniteStateMachine.cs +++ b/src/Ocelot/Raft/OcelotFiniteStateMachine.cs @@ -1,3 +1,4 @@ +using System.Threading.Tasks; using Ocelot.Configuration.Setter; using Rafty.FiniteStateMachine; using Rafty.Log; @@ -14,12 +15,12 @@ public OcelotFiniteStateMachine(IFileConfigurationSetter setter) _setter = setter; } - public void Handle(LogEntry log) + public async Task Handle(LogEntry log) { //todo - handle an error //hack it to just cast as at the moment we know this is the only command :P var hack = (UpdateFileConfiguration)log.CommandData; - _setter.Set(hack.Configuration).GetAwaiter().GetResult(); + await _setter.Set(hack.Configuration); } } } diff --git a/src/Ocelot/Raft/SqlLiteLog.cs b/src/Ocelot/Raft/SqlLiteLog.cs index aff04bf55..4e74e97ad 100644 --- a/src/Ocelot/Raft/SqlLiteLog.cs +++ b/src/Ocelot/Raft/SqlLiteLog.cs @@ -5,6 +5,7 @@ using System; using Rafty.Infrastructure; using System.Collections.Generic; +using System.Threading.Tasks; namespace Ocelot.Raft { @@ -40,89 +41,80 @@ data text not null } } - public int LastLogIndex + public Task LastLogIndex() { - get + lock(_lock) { - lock(_lock) + var result = 1; + using(var connection = new SqliteConnection($"Data Source={_path};")) { - var result = 1; - using(var connection = new SqliteConnection($"Data Source={_path};")) + connection.Open(); + var sql = @"select id from logs order by id desc limit 1"; + using(var command = new SqliteCommand(sql, connection)) { - connection.Open(); - var sql = @"select id from logs order by id desc limit 1"; - using(var command = new SqliteCommand(sql, connection)) + var index = Convert.ToInt32(command.ExecuteScalar()); + if(index > result) { - var index = Convert.ToInt32(command.ExecuteScalar()); - if(index > result) - { - result = index; - } + result = index; } } - - return result; } + + return Task.FromResult(result); } } - public long LastLogTerm + public Task LastLogTerm () { - get + lock(_lock) { - lock(_lock) + long result = 0; + using(var connection = new SqliteConnection($"Data Source={_path};")) { - long result = 0; - using(var connection = new SqliteConnection($"Data Source={_path};")) + connection.Open(); + var sql = @"select data from logs order by id desc limit 1"; + using(var command = new SqliteCommand(sql, connection)) { - connection.Open(); - var sql = @"select data from logs order by id desc limit 1"; - using(var command = new SqliteCommand(sql, connection)) + var data = Convert.ToString(command.ExecuteScalar()); + var jsonSerializerSettings = new JsonSerializerSettings() { + TypeNameHandling = TypeNameHandling.All + }; + var log = JsonConvert.DeserializeObject(data, jsonSerializerSettings); + if(log != null && log.Term > result) { - var data = Convert.ToString(command.ExecuteScalar()); - var jsonSerializerSettings = new JsonSerializerSettings() { - TypeNameHandling = TypeNameHandling.All - }; - var log = JsonConvert.DeserializeObject(data, jsonSerializerSettings); - if(log != null && log.Term > result) - { - result = log.Term; - } + result = log.Term; } } - - return result; } + + return Task.FromResult(result); } } - public int Count + public Task Count () { - get + lock(_lock) { - lock(_lock) + var result = 0; + using(var connection = new SqliteConnection($"Data Source={_path};")) { - var result = 0; - using(var connection = new SqliteConnection($"Data Source={_path};")) + connection.Open(); + var sql = @"select count(id) from logs"; + using(var command = new SqliteCommand(sql, connection)) { - connection.Open(); - var sql = @"select count(id) from logs"; - using(var command = new SqliteCommand(sql, connection)) + var index = Convert.ToInt32(command.ExecuteScalar()); + if(index > result) { - var index = Convert.ToInt32(command.ExecuteScalar()); - if(index > result) - { - result = index; - } + result = index; } } - - return result; } + + return Task.FromResult(result); } } - public int Apply(LogEntry log) + public Task Apply(LogEntry log) { lock(_lock) { @@ -145,13 +137,13 @@ public int Apply(LogEntry log) using(var command = new SqliteCommand(sql, connection)) { var result = command.ExecuteScalar(); - return Convert.ToInt32(result); + return Task.FromResult(Convert.ToInt32(result)); } } } } - public void DeleteConflictsFromThisLog(int index, LogEntry logEntry) + public Task DeleteConflictsFromThisLog(int index, LogEntry logEntry) { lock(_lock) { @@ -174,15 +166,17 @@ public void DeleteConflictsFromThisLog(int index, LogEntry logEntry) var deleteSql = $"delete from logs where id >= {index};"; using(var deleteCommand = new SqliteCommand(deleteSql, connection)) { - var result = deleteCommand.ExecuteNonQuery(); + var result = deleteCommand.ExecuteNonQuery(); } } } } } + + return Task.CompletedTask; } - public LogEntry Get(int index) + public Task Get(int index) { lock(_lock) { @@ -199,13 +193,13 @@ public LogEntry Get(int index) TypeNameHandling = TypeNameHandling.All }; var log = JsonConvert.DeserializeObject(data, jsonSerializerSettings); - return log; + return Task.FromResult(log); } } } } - public System.Collections.Generic.List<(int index, LogEntry logEntry)> GetFrom(int index) + public Task> GetFrom(int index) { lock(_lock) { @@ -235,11 +229,11 @@ public LogEntry Get(int index) } } - return logsToReturn; + return Task.FromResult(logsToReturn); } } - public long GetTermAtIndex(int index) + public Task GetTermAtIndex(int index) { lock(_lock) { @@ -264,11 +258,11 @@ public long GetTermAtIndex(int index) } } - return result; + return Task.FromResult(result); } } - public void Remove(int indexOfCommand) + public Task Remove(int indexOfCommand) { lock(_lock) { @@ -284,6 +278,8 @@ public void Remove(int indexOfCommand) } } } + + return Task.CompletedTask; } } } diff --git a/src/Ocelot/Requester/HttpClientBuilder.cs b/src/Ocelot/Requester/HttpClientBuilder.cs index 6d3ffcb0b..18160e02b 100644 --- a/src/Ocelot/Requester/HttpClientBuilder.cs +++ b/src/Ocelot/Requester/HttpClientBuilder.cs @@ -52,7 +52,7 @@ public IHttpClient Create(DownstreamContext context) if(context.DownstreamReRoute.DangerousAcceptAnyServerCertificateValidator) { - httpclientHandler.ServerCertificateCustomValidationCallback = HttpClientHandler.DangerousAcceptAnyServerCertificateValidator; + httpclientHandler.ServerCertificateCustomValidationCallback = (request, certificate, chain, errors) => true; _logger .LogWarning($"You have ignored all SSL warnings by using DangerousAcceptAnyServerCertificateValidator for this DownstreamReRoute, UpstreamPathTemplate: {context.DownstreamReRoute.UpstreamPathTemplate}, DownstreamPathTemplate: {context.DownstreamReRoute.DownstreamPathTemplate}"); diff --git a/test/Ocelot.AcceptanceTests/Ocelot.AcceptanceTests.csproj b/test/Ocelot.AcceptanceTests/Ocelot.AcceptanceTests.csproj index 9edbfd745..b8ec7eb6b 100644 --- a/test/Ocelot.AcceptanceTests/Ocelot.AcceptanceTests.csproj +++ b/test/Ocelot.AcceptanceTests/Ocelot.AcceptanceTests.csproj @@ -54,6 +54,7 @@ - + + diff --git a/test/Ocelot.IntegrationTests/Ocelot.IntegrationTests.csproj b/test/Ocelot.IntegrationTests/Ocelot.IntegrationTests.csproj index 97c8782fe..32cf393a0 100644 --- a/test/Ocelot.IntegrationTests/Ocelot.IntegrationTests.csproj +++ b/test/Ocelot.IntegrationTests/Ocelot.IntegrationTests.csproj @@ -44,7 +44,7 @@ - + \ No newline at end of file diff --git a/test/Ocelot.UnitTests/Controllers/FileConfigurationControllerTests.cs b/test/Ocelot.UnitTests/Controllers/FileConfigurationControllerTests.cs index 520d82740..41cc35732 100644 --- a/test/Ocelot.UnitTests/Controllers/FileConfigurationControllerTests.cs +++ b/test/Ocelot.UnitTests/Controllers/FileConfigurationControllerTests.cs @@ -126,14 +126,14 @@ private void GivenTheNodeReturnsOK() { _node .Setup(x => x.Accept(It.IsAny())) - .Returns(new Rafty.Concensus.OkResponse(new UpdateFileConfiguration(new FileConfiguration()))); + .ReturnsAsync(new Rafty.Concensus.OkResponse(new UpdateFileConfiguration(new FileConfiguration()))); } private void GivenTheNodeReturnsError() { _node .Setup(x => x.Accept(It.IsAny())) - .Returns(new Rafty.Concensus.ErrorResponse("error", new UpdateFileConfiguration(new FileConfiguration()))); + .ReturnsAsync(new Rafty.Concensus.ErrorResponse("error", new UpdateFileConfiguration(new FileConfiguration()))); } private void GivenTheConfigSetterReturns(Response response) diff --git a/test/Ocelot.UnitTests/Ocelot.UnitTests.csproj b/test/Ocelot.UnitTests/Ocelot.UnitTests.csproj index 395dde67c..6f219ad35 100644 --- a/test/Ocelot.UnitTests/Ocelot.UnitTests.csproj +++ b/test/Ocelot.UnitTests/Ocelot.UnitTests.csproj @@ -49,10 +49,10 @@ + -