From c636bd0e1c9358668cbefc562ac89b4c82a88ca4 Mon Sep 17 00:00:00 2001 From: Andrew Galante Date: Fri, 8 Nov 2019 16:48:01 -0800 Subject: [PATCH 1/6] Decoerator -> Decorator --- .../Clients/{ClientSslDecoerator.cs => ClientSslDecorator.cs} | 0 .../{SslListenerDecoerator.cs => SslListenerDecorator.cs} | 0 uhttpsharp/uhttpsharp.csproj | 4 ++-- 3 files changed, 2 insertions(+), 2 deletions(-) rename uhttpsharp/Clients/{ClientSslDecoerator.cs => ClientSslDecorator.cs} (100%) rename uhttpsharp/Listeners/{SslListenerDecoerator.cs => SslListenerDecorator.cs} (100%) diff --git a/uhttpsharp/Clients/ClientSslDecoerator.cs b/uhttpsharp/Clients/ClientSslDecorator.cs similarity index 100% rename from uhttpsharp/Clients/ClientSslDecoerator.cs rename to uhttpsharp/Clients/ClientSslDecorator.cs diff --git a/uhttpsharp/Listeners/SslListenerDecoerator.cs b/uhttpsharp/Listeners/SslListenerDecorator.cs similarity index 100% rename from uhttpsharp/Listeners/SslListenerDecoerator.cs rename to uhttpsharp/Listeners/SslListenerDecorator.cs diff --git a/uhttpsharp/uhttpsharp.csproj b/uhttpsharp/uhttpsharp.csproj index 424e233..3fa151f 100644 --- a/uhttpsharp/uhttpsharp.csproj +++ b/uhttpsharp/uhttpsharp.csproj @@ -56,7 +56,7 @@ - + @@ -102,7 +102,7 @@ - + From f3a6808c70bbbfad1f869402e4bcb0a3b7608ae9 Mon Sep 17 00:00:00 2001 From: Andrew Galante Date: Fri, 8 Nov 2019 17:02:32 -0800 Subject: [PATCH 2/6] Use polymorphism to initialize ClientSslDecorator --- uhttpsharp/Clients/ClientSslDecorator.cs | 2 +- uhttpsharp/Clients/IClient.cs | 3 ++- uhttpsharp/Clients/TcpClientAdapter.cs | 5 +++++ uhttpsharp/HttpClient.cs | 14 +++----------- 4 files changed, 11 insertions(+), 13 deletions(-) diff --git a/uhttpsharp/Clients/ClientSslDecorator.cs b/uhttpsharp/Clients/ClientSslDecorator.cs index 7f3962a..216b569 100644 --- a/uhttpsharp/Clients/ClientSslDecorator.cs +++ b/uhttpsharp/Clients/ClientSslDecorator.cs @@ -21,7 +21,7 @@ public ClientSslDecorator(IClient child, X509Certificate certificate) _sslStream = new SslStream(_child.Stream); } - public async Task AuthenticateAsServer() + public async Task InitializeStream() { Task timeout = Task.Delay(TimeSpan.FromSeconds(10)); if (timeout == await Task.WhenAny(_sslStream.AuthenticateAsServerAsync(_certificate, false, SslProtocols.Tls, true), timeout).ConfigureAwait(false)) diff --git a/uhttpsharp/Clients/IClient.cs b/uhttpsharp/Clients/IClient.cs index e231a52..cfb2f4b 100644 --- a/uhttpsharp/Clients/IClient.cs +++ b/uhttpsharp/Clients/IClient.cs @@ -1,5 +1,6 @@ using System.IO; using System.Net; +using System.Threading.Tasks; namespace uhttpsharp.Clients { @@ -14,7 +15,7 @@ public interface IClient EndPoint RemoteEndPoint { get; } - + Task InitializeStream(); } } diff --git a/uhttpsharp/Clients/TcpClientAdapter.cs b/uhttpsharp/Clients/TcpClientAdapter.cs index 7f3b444..c86fee0 100644 --- a/uhttpsharp/Clients/TcpClientAdapter.cs +++ b/uhttpsharp/Clients/TcpClientAdapter.cs @@ -1,6 +1,7 @@ using System.IO; using System.Net; using System.Net.Sockets; +using System.Threading.Tasks; namespace uhttpsharp.Clients { @@ -45,5 +46,9 @@ public EndPoint RemoteEndPoint { get { return _client.Client.RemoteEndPoint; } } + + public Task InitializeStream() { + return Task.Factory.GetCompleted(); + } } } \ No newline at end of file diff --git a/uhttpsharp/HttpClient.cs b/uhttpsharp/HttpClient.cs index 9adadb0..c35e688 100644 --- a/uhttpsharp/HttpClient.cs +++ b/uhttpsharp/HttpClient.cs @@ -60,21 +60,13 @@ public HttpClientHandler(IClient client, Func requestHandler UpdateLastOperationTime(); } - private async Task InitializeStream() - { - if (Client is ClientSslDecorator) - { - await ((ClientSslDecorator)Client).AuthenticateAsServer().ConfigureAwait(false); - } - - _stream = new BufferedStream(_client.Stream, 8096); - } - private async void Process() { try { - await InitializeStream(); + await _client.InitializeStream().ConfigureAwait(false); + + _stream = new BufferedStream(_client.Stream, 8096); while (_client.Connected) { From e86c47a4b13ed2317077c534328cddf63bf6477a Mon Sep 17 00:00:00 2001 From: Andrew Galante Date: Thu, 14 Nov 2019 15:04:50 -0800 Subject: [PATCH 3/6] Pass connected Client into Context.State --- uhttpsharp/HttpClient.cs | 2 +- uhttpsharp/HttpContext.cs | 6 ++++-- 2 files changed, 5 insertions(+), 3 deletions(-) diff --git a/uhttpsharp/HttpClient.cs b/uhttpsharp/HttpClient.cs index c35e688..3bc75c5 100644 --- a/uhttpsharp/HttpClient.cs +++ b/uhttpsharp/HttpClient.cs @@ -81,7 +81,7 @@ private async void Process() { UpdateLastOperationTime(); - var context = new HttpContext(request, _client.RemoteEndPoint); + var context = new HttpContext(request, _client); Logger.InfoFormat("{1} : Got request {0}", request.Uri, _client.RemoteEndPoint); diff --git a/uhttpsharp/HttpContext.cs b/uhttpsharp/HttpContext.cs index 84094b2..601fdc8 100644 --- a/uhttpsharp/HttpContext.cs +++ b/uhttpsharp/HttpContext.cs @@ -1,5 +1,6 @@ using System.Dynamic; using System.Net; +using uhttpsharp.Clients; using uhttpsharp.Headers; namespace uhttpsharp @@ -10,11 +11,12 @@ internal class HttpContext : IHttpContext private readonly EndPoint _remoteEndPoint; private readonly ICookiesStorage _cookies; private readonly ExpandoObject _state = new ExpandoObject(); - public HttpContext(IHttpRequest request, EndPoint remoteEndPoint) + public HttpContext(IHttpRequest request, IClient client) { _request = request; - _remoteEndPoint = remoteEndPoint; + _remoteEndPoint = client.RemoteEndPoint; _cookies = new CookiesStorage(_request.Headers.GetByNameOrDefault("cookie", string.Empty)); + State.Client = client; } public IHttpRequest Request From 1a2f4325626bef0da44cac7102489113b6d4ac1b Mon Sep 17 00:00:00 2001 From: Andrew Galante Date: Thu, 14 Nov 2019 15:39:33 -0800 Subject: [PATCH 4/6] Don't overload use of 'State' property --- uhttpsharp/HttpContext.cs | 13 ++++++++----- 1 file changed, 8 insertions(+), 5 deletions(-) diff --git a/uhttpsharp/HttpContext.cs b/uhttpsharp/HttpContext.cs index 601fdc8..6d2b59e 100644 --- a/uhttpsharp/HttpContext.cs +++ b/uhttpsharp/HttpContext.cs @@ -8,15 +8,14 @@ namespace uhttpsharp internal class HttpContext : IHttpContext { private readonly IHttpRequest _request; - private readonly EndPoint _remoteEndPoint; + private readonly IClient _client; private readonly ICookiesStorage _cookies; private readonly ExpandoObject _state = new ExpandoObject(); public HttpContext(IHttpRequest request, IClient client) { _request = request; - _remoteEndPoint = client.RemoteEndPoint; + _client = client; _cookies = new CookiesStorage(_request.Headers.GetByNameOrDefault("cookie", string.Empty)); - State.Client = client; } public IHttpRequest Request @@ -31,14 +30,18 @@ public ICookiesStorage Cookies get { return _cookies; } } - public dynamic State { get { return _state; } } + + public IClient Client { + get { return _client; } + } + public EndPoint RemoteEndPoint { - get { return _remoteEndPoint; } + get { return _client.RemoteEndPoint; } } } } \ No newline at end of file From 7a3fee85dc4d7f864f329e81969ca918093c09ee Mon Sep 17 00:00:00 2001 From: Andrew Galante Date: Thu, 14 Nov 2019 15:45:53 -0800 Subject: [PATCH 5/6] Add Client to IHttpContext --- uhttpsharp/IHttpContext.cs | 2 ++ 1 file changed, 2 insertions(+) diff --git a/uhttpsharp/IHttpContext.cs b/uhttpsharp/IHttpContext.cs index 45f9d02..1f3759d 100644 --- a/uhttpsharp/IHttpContext.cs +++ b/uhttpsharp/IHttpContext.cs @@ -16,6 +16,8 @@ public interface IHttpContext dynamic State { get; } + IClient Client { get; } + EndPoint RemoteEndPoint { get; } } From 403831ca26c490466b470445de2abcec32f2036e Mon Sep 17 00:00:00 2001 From: Andrew Galante Date: Thu, 14 Nov 2019 15:49:07 -0800 Subject: [PATCH 6/6] use the right namespace --- uhttpsharp/IHttpContext.cs | 1 + 1 file changed, 1 insertion(+) diff --git a/uhttpsharp/IHttpContext.cs b/uhttpsharp/IHttpContext.cs index 1f3759d..1dfff80 100644 --- a/uhttpsharp/IHttpContext.cs +++ b/uhttpsharp/IHttpContext.cs @@ -2,6 +2,7 @@ using System.Collections.Generic; using System.Net; using System.Text; +using uhttpsharp.Clients; using uhttpsharp.Headers; namespace uhttpsharp