Skip to content

Commit

Permalink
Merge pull request #1 from shanielh/ssl_sup
Browse files Browse the repository at this point in the history
Added SSL Support!
  • Loading branch information
shanielh committed Jan 9, 2014
2 parents dc8824e + 965b648 commit a7587af
Show file tree
Hide file tree
Showing 12 changed files with 207 additions and 29 deletions.
12 changes: 11 additions & 1 deletion uhttpsharp-demo/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,13 @@
*/

using System;
using System.IO;
using System.Net;
using System.Net.Sockets;
using System.Security.Cryptography.X509Certificates;
using uhttpsharp;
using uhttpsharp.Handlers;
using uhttpsharp.Listeners;
using uhttpsharpdemo.Handlers;

namespace uhttpsharpdemo
Expand All @@ -29,8 +34,13 @@ private static void Main()
{
log4net.Config.XmlConfigurator.Configure();

using (var httpServer = new HttpServer(8000, new HttpRequestProvider()))
var serverCertificate = X509Certificate.CreateFromCertFile(@"TempCert.cer");

using (var httpServer = new HttpServer(new HttpRequestProvider()))
{
httpServer.Use(new TcpListenerAdapter(new TcpListener(IPAddress.Loopback, 80)));
httpServer.Use(new ListenerSslDecorator(new TcpListenerAdapter(new TcpListener(IPAddress.Loopback, 443)), serverCertificate));

httpServer.Use(new TimingHandler());

httpServer.Use(new HttpRouter().With(string.Empty, new IndexHandler())
Expand Down
4 changes: 2 additions & 2 deletions uhttpsharp-demo/app.config
Original file line number Diff line number Diff line change
Expand Up @@ -31,8 +31,8 @@
</appender>

<root>
<level value="FATAL" />
<!--<appender-ref ref="ColoredConsoleAppender" />-->
<level value="WARN" />
<appender-ref ref="ColoredConsoleAppender" />
</root>

</log4net>
Expand Down
41 changes: 41 additions & 0 deletions uhttpsharp/Clients/ClientSslDecoerator.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
using System.IO;
using System.Net;
using System.Net.Security;
using System.Security.Authentication;
using System.Security.Cryptography.X509Certificates;

namespace uhttpsharp.Clients
{
public class ClientSslDecorator : IClient
{
private readonly IClient _child;
private readonly SslStream _sslStream;

public ClientSslDecorator(IClient child, X509Certificate certificate)
{
_child = child;
_sslStream = new SslStream(_child.Stream);
_sslStream.AuthenticateAsServer(certificate, false, SslProtocols.Tls, true);
}

public Stream Stream
{
get { return _sslStream; }
}

public bool Connected
{
get { return _child.Connected; }
}

public void Close()
{
_child.Close();
}

public EndPoint RemoteEndPoint
{
get { return _child.RemoteEndPoint; }
}
}
}
19 changes: 19 additions & 0 deletions uhttpsharp/Clients/IClient.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
using System.IO;
using System.Net;

namespace uhttpsharp.Clients
{
public interface IClient
{

Stream Stream { get; }

bool Connected { get; }
void Close();

EndPoint RemoteEndPoint { get; }



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

namespace uhttpsharp.Clients
{
public class TcpClientAdapter : IClient
{
private readonly TcpClient _client;

public TcpClientAdapter(TcpClient client)
{
_client = client;
}

public Stream Stream
{
get { return _client.GetStream(); }
}

public bool Connected
{
get { return _client.Connected; }
}

public void Close()
{
_client.Close();
}


public EndPoint RemoteEndPoint
{
get { return _client.Client.RemoteEndPoint; }
}
}
}
8 changes: 6 additions & 2 deletions uhttpsharp/Handlers/FileHandler.cs
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,6 @@

using System.Collections.Generic;
using System.IO;
using System.Runtime.Remoting.Contexts;
using System.Threading.Tasks;

namespace uhttpsharp.Handlers
Expand Down Expand Up @@ -58,10 +57,15 @@ public async Task Handle(IHttpContext context, System.Func<Task> next)

var httpRoot = Path.GetFullPath(HttpRootDirectory ?? ".");
var path = Path.GetFullPath(Path.Combine(httpRoot, requestPath));

if (!File.Exists(path))
{
await next();

return;
}


context.Response = new HttpResponse(GetContentType(path), File.OpenRead(path), context.Request.Headers.KeepAliveConnection());
}
}
Expand Down
18 changes: 10 additions & 8 deletions uhttpsharp/HttpClient.cs
Original file line number Diff line number Diff line change
Expand Up @@ -18,35 +18,37 @@

using System.Net;
using System.Reflection;
using System.Security.Cryptography.X509Certificates;
using log4net;
using System;
using System.Collections.Generic;
using System.IO;
using System.Net.Sockets;
using System.Threading.Tasks;
using uhttpsharp.Clients;

namespace uhttpsharp
{
internal sealed class HttpClient
{
private const string CrLf = "\r\n";
private static readonly ILog Logger = LogManager.GetLogger(MethodBase.GetCurrentMethod().DeclaringType);

private readonly TcpClient _client;
private readonly IClient _client;
private readonly StreamReader _inputStream;
private readonly StreamWriter _outputStream;
private readonly IList<IHttpRequestHandler> _requestHandlers;
private readonly IHttpRequestProvider _requestProvider;
private readonly EndPoint _remoteEndPoint;

public HttpClient(TcpClient client, IList<IHttpRequestHandler> requestHandlers, IHttpRequestProvider requestProvider)
public HttpClient(IClient client, IList<IHttpRequestHandler> requestHandlers, IHttpRequestProvider requestProvider)
{
_remoteEndPoint = client.Client.RemoteEndPoint;
_remoteEndPoint = client.RemoteEndPoint;
_client = client;
_requestHandlers = requestHandlers;
_requestProvider = requestProvider;
var bufferedStream = new BufferedStream(_client.GetStream());
_requestProvider = requestProvider;

var bufferedStream = new BufferedStream(_client.Stream);

_outputStream = new StreamWriter(bufferedStream) { NewLine = CrLf };
_inputStream = new StreamReader(bufferedStream);
Expand All @@ -69,7 +71,7 @@ private async void Process()

var context = new HttpContext(request);

Logger.InfoFormat("{1} : Got request {0}", request.Uri, _client.Client.RemoteEndPoint);
Logger.InfoFormat("{1} : Got request {0}", request.Uri, _client.RemoteEndPoint);

var getResponse = BuildHandlers(context)();

Expand Down
35 changes: 19 additions & 16 deletions uhttpsharp/HttpServer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -19,30 +19,26 @@
using log4net;
using System;
using System.Collections.Generic;
using System.Net;
using System.Net.Sockets;
using System.Reflection;
using System.Threading.Tasks;
using uhttpsharp.Listeners;

namespace uhttpsharp
{
public sealed class HttpServer : IDisposable
{
private static readonly ILog Logger = LogManager.GetLogger(MethodBase.GetCurrentMethod().DeclaringType);

private readonly int _port;

private TcpListener _listener;
private bool _isActive;

private readonly IList<IHttpRequestHandler> _handlers = new List<IHttpRequestHandler>();
private readonly IList<IHttpListener> _listeners = new List<IHttpListener>();

private readonly IHttpRequestProvider _requestProvider;


public HttpServer(int port, IHttpRequestProvider requestProvider)
public HttpServer(IHttpRequestProvider requestProvider)
{
_port = port;
_requestProvider = requestProvider;
}

Expand All @@ -51,25 +47,32 @@ public void Use(IHttpRequestHandler handler)
_handlers.Add(handler);
}

public void Start()
public void Use(IHttpListener listener)
{
_listener = new TcpListener(IPAddress.Loopback, _port);
_listener.Start();
_listeners.Add(listener);
}

public void Start()
{
_isActive = true;

Task.Factory.StartNew(Listen);
foreach (var listener in _listeners)
{
IHttpListener tempListener = listener;

Task.Factory.StartNew(() => Listen(tempListener));
}

Logger.InfoFormat("Embedded uhttpserver started.");
}

private async void Listen()
private async void Listen(IHttpListener listener)
{
Logger.InfoFormat("Embedded uhttpserver started @ {0}:{1}", IPAddress.Loopback, _port);

while (_isActive)
{
try
{
new HttpClient(await _listener.AcceptTcpClientAsync().ConfigureAwait(false), _handlers, _requestProvider);
new HttpClient(await listener.GetClient().ConfigureAwait(false), _handlers, _requestProvider);
}
catch (Exception e)
{
Expand All @@ -78,7 +81,7 @@ private async void Listen()

}

Logger.InfoFormat("Embedded uhttpserver stopped @ {0}:{1}", IPAddress.Loopback, _port);
Logger.InfoFormat("Embedded uhttpserver stopped.");
}

public void Dispose()
Expand Down
12 changes: 12 additions & 0 deletions uhttpsharp/Listeners/IHttpListener.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
using System.Threading.Tasks;
using uhttpsharp.Clients;

namespace uhttpsharp.Listeners
{
public interface IHttpListener
{

Task<IClient> GetClient();

}
}
23 changes: 23 additions & 0 deletions uhttpsharp/Listeners/SslListenerDecoerator.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
using System.Security.Cryptography.X509Certificates;
using System.Threading.Tasks;
using uhttpsharp.Clients;

namespace uhttpsharp.Listeners
{
public class ListenerSslDecorator : IHttpListener
{
private readonly IHttpListener _child;
private readonly X509Certificate _certificate;

public ListenerSslDecorator(IHttpListener child, X509Certificate certificate)
{
_child = child;
_certificate = certificate;
}

public async Task<IClient> GetClient()
{
return new ClientSslDecorator(await _child.GetClient().ConfigureAwait(false), _certificate);
}
}
}
21 changes: 21 additions & 0 deletions uhttpsharp/Listeners/TcpListenerAdapter.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
using System.Net.Sockets;
using System.Threading.Tasks;
using uhttpsharp.Clients;

namespace uhttpsharp.Listeners
{
public class TcpListenerAdapter : IHttpListener
{
private readonly TcpListener _listener;

public TcpListenerAdapter(TcpListener listener)
{
_listener = listener;
_listener.Start();
}
public async Task<IClient> GetClient()
{
return new TcpClientAdapter(await _listener.AcceptTcpClientAsync().ConfigureAwait(false));
}
}
}
6 changes: 6 additions & 0 deletions uhttpsharp/uhttpsharp.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,8 @@
<Compile Include="..\AssemblyCommon.cs">
<Link>Properties\AssemblyCommon.cs</Link>
</Compile>
<Compile Include="Clients\ClientSslDecoerator.cs" />
<Compile Include="Clients\IClient.cs" />
<Compile Include="Handlers\FileHandler.cs" />
<Compile Include="HttpClient.cs" />
<Compile Include="HttpMethodProvider.cs" />
Expand All @@ -64,9 +66,13 @@
<Compile Include="HttpServerExtensions.cs" />
<Compile Include="IHttpContext.cs" />
<Compile Include="IHttpMethodProvider.cs" />
<Compile Include="Listeners\IHttpListener.cs" />
<Compile Include="Listeners\SslListenerDecoerator.cs" />
<Compile Include="Listeners\TcpListenerAdapter.cs" />
<Compile Include="Properties\AssemblyInfo.cs" />
<Compile Include="HttpResponseCode.cs" />
<Compile Include="TaskFactoryExtensions.cs" />
<Compile Include="Clients\TcpClientAdapter.cs" />
</ItemGroup>
<ItemGroup>
<None Include="packages.config" />
Expand Down

0 comments on commit a7587af

Please sign in to comment.