Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Allow access to underlying IClient instance through IHttpContext.Client #31

Open
wants to merge 6 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -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))
Expand Down
3 changes: 2 additions & 1 deletion uhttpsharp/Clients/IClient.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
using System.IO;
using System.Net;
using System.Threading.Tasks;

namespace uhttpsharp.Clients
{
Expand All @@ -14,7 +15,7 @@ public interface IClient

EndPoint RemoteEndPoint { get; }


Task InitializeStream();

}
}
5 changes: 5 additions & 0 deletions uhttpsharp/Clients/TcpClientAdapter.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
using System.IO;
using System.Net;
using System.Net.Sockets;
using System.Threading.Tasks;

namespace uhttpsharp.Clients
{
Expand Down Expand Up @@ -45,5 +46,9 @@ public EndPoint RemoteEndPoint
{
get { return _client.Client.RemoteEndPoint; }
}

public Task InitializeStream() {
return Task.Factory.GetCompleted();
}
}
}
16 changes: 4 additions & 12 deletions uhttpsharp/HttpClient.cs
Original file line number Diff line number Diff line change
Expand Up @@ -60,21 +60,13 @@ public HttpClientHandler(IClient client, Func<IHttpContext, Task> 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)
{
Expand All @@ -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);

Expand Down
15 changes: 10 additions & 5 deletions uhttpsharp/HttpContext.cs
Original file line number Diff line number Diff line change
@@ -1,19 +1,20 @@
using System.Dynamic;
using System.Net;
using uhttpsharp.Clients;
using uhttpsharp.Headers;

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));
}

Expand All @@ -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; }
}
}
}
3 changes: 3 additions & 0 deletions uhttpsharp/IHttpContext.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
using System.Collections.Generic;
using System.Net;
using System.Text;
using uhttpsharp.Clients;
using uhttpsharp.Headers;

namespace uhttpsharp
Expand All @@ -16,6 +17,8 @@ public interface IHttpContext

dynamic State { get; }

IClient Client { get; }

EndPoint RemoteEndPoint { get; }
}

Expand Down
4 changes: 2 additions & 2 deletions uhttpsharp/uhttpsharp.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@
<Compile Include="App_Packages\LibLog.3.1\LibLog.cs" />
<Compile Include="Attributes\IModelBinding.cs" />
<Compile Include="Attributes\HttpMethodAttribute.cs" />
<Compile Include="Clients\ClientSslDecoerator.cs" />
<Compile Include="Clients\ClientSslDecorator.cs" />
<Compile Include="Clients\IClient.cs" />
<Compile Include="Controllers\ErrorContainer.cs" />
<Compile Include="Controllers\IErrorContainer.cs" />
Expand Down Expand Up @@ -102,7 +102,7 @@
<Compile Include="IHttpMethodProvider.cs" />
<Compile Include="LimitedStream.cs" />
<Compile Include="Listeners\IHttpListener.cs" />
<Compile Include="Listeners\SslListenerDecoerator.cs" />
<Compile Include="Listeners\SslListenerDecorator.cs" />
<Compile Include="Listeners\TcpListenerAdapter.cs" />
<Compile Include="ModelBinders\IModelBinder.cs" />
<Compile Include="ModelBinders\JsonModelBinder.cs" />
Expand Down