diff --git a/uhttpsharp/Clients/ClientSslDecoerator.cs b/uhttpsharp/Clients/ClientSslDecorator.cs similarity index 96% rename from uhttpsharp/Clients/ClientSslDecoerator.cs rename to uhttpsharp/Clients/ClientSslDecorator.cs index 7f3962a..216b569 100644 --- a/uhttpsharp/Clients/ClientSslDecoerator.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..3bc75c5 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) { @@ -89,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..6d2b59e 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 @@ -7,13 +8,13 @@ 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, EndPoint remoteEndPoint) + public HttpContext(IHttpRequest request, IClient client) { _request = request; - _remoteEndPoint = remoteEndPoint; + _client = client; _cookies = new CookiesStorage(_request.Headers.GetByNameOrDefault("cookie", string.Empty)); } @@ -29,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 diff --git a/uhttpsharp/IHttpContext.cs b/uhttpsharp/IHttpContext.cs index 45f9d02..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 @@ -16,6 +17,8 @@ public interface IHttpContext dynamic State { get; } + IClient Client { get; } + EndPoint RemoteEndPoint { get; } } 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 @@ - +