Permalink
Browse files

SSL support finished. Version bumped to 0.4.

  • Loading branch information...
1 parent 647efce commit 52c4d31e85e5224554461ec51c183f5142eda138 @Bobris committed Jul 18, 2013
Binary file not shown.
Binary file not shown.
Binary file not shown.
@@ -0,0 +1 @@
+abs.exe -c 200 -n 10000 https://localhost:8888/ 2>nul
@@ -0,0 +1 @@
+abs.exe -c 200 -k -n 100000 https://localhost:8888/ 2>nul
Binary file not shown.
View
@@ -0,0 +1,28 @@
+var cluster = require('cluster');
+var https = require('https');
+var fs = require('fs');
+var numCPUs = require('os').cpus().length;
+
+if (cluster.isMaster) {
+ // Fork workers.
+ console.log("CPUs:"+numCPUs);
+ for (var i = 0; i < numCPUs; i++) {
+ cluster.fork();
+ }
+
+ cluster.on('exit', function(worker, code, signal) {
+ console.log('worker ' + worker.process.pid + ' died');
+ });
+} else {
+ // Workers can share any TCP connection
+ // In this case its a HTTP server
+
+ var options = {
+ pfx: fs.readFileSync('../sslcert/test.pfx'),
+ passphrase: "nowin"
+ };
+ https.createServer(options,function(req, res) {
+ res.writeHead(200, {'Content-Type': 'text/plain'});
+ res.end("Hello World!");
+ }).listen(8888);
+}
@@ -1,4 +1,5 @@
using System;
+using System.Security.Cryptography.X509Certificates;
using NowinWebServer;
namespace NowinSample
@@ -7,7 +8,9 @@ static class Program
{
static void Main(string[] args)
{
- using (ServerBuilder.New().SetPort(8888).SetOwinApp(SampleOwinApp.Sample.App).Start())
+ var builder = ServerBuilder.New().SetPort(8888).SetOwinApp(SampleOwinApp.Sample.App);
+ builder.SetCertificate(new X509Certificate2("../../../sslcert/test.pfx", "nowin"));
+ using (builder.Start())
{
Console.WriteLine("Listening on port 8888. Enter to exit.");
Console.ReadLine();
@@ -31,5 +31,5 @@
// You can specify all the values or you can default the Build and Revision Numbers
// by using the '*' as shown below:
// [assembly: AssemblyVersion("1.0.*")]
-[assembly: AssemblyVersion("0.3.0.0")]
-[assembly: AssemblyFileVersion("0.3.0.0")]
+[assembly: AssemblyVersion("0.4.0.0")]
+[assembly: AssemblyFileVersion("0.4.0.0")]
@@ -17,7 +17,7 @@ public SslTransportFactory(int bufferSize, X509Certificate certificate, ILayerFa
public int PerConnectionBufferSize
{
- get { return _next.PerConnectionBufferSize + _bufferSize * 2; }
+ get { return _next.PerConnectionBufferSize + _bufferSize * 2 + SslTransportHandler.SendBufferExtendedBySslSize; }
}
public int CommonBufferSize
@@ -32,7 +32,7 @@ public void InitCommonBuffer(byte[] buffer, int offset)
public ILayerHandler Create(byte[] buffer, int offset, int commonOffset)
{
- var nextHandler = (ITransportLayerHandler) _next.Create(buffer, offset, commonOffset);
+ var nextHandler = (ITransportLayerHandler)_next.Create(buffer, offset, commonOffset);
var handler = new SslTransportHandler(nextHandler, _certificate, buffer,
offset + _next.PerConnectionBufferSize, _bufferSize);
return handler;
@@ -10,11 +10,14 @@ namespace NowinWebServer
{
class SslTransportHandler : ITransportLayerHandler, ITransportLayerCallback
{
+ internal const int SendBufferExtendedBySslSize = 128; // 102 could be enough probably so to have some reserve making it 128
+
readonly ITransportLayerHandler _next;
readonly X509Certificate _serverCertificate;
readonly byte[] _buffer;
readonly int _bufferSize;
- readonly SslStream _ssl;
+ readonly int _sendBufferSize;
+ SslStream _ssl;
readonly int _encryptedReceiveBufferOffset;
readonly int _encryptedSendBufferOffset;
Task _authenticateTask;
@@ -30,8 +33,8 @@ public SslTransportHandler(ITransportLayerHandler next, X509Certificate serverCe
_encryptedReceiveBufferOffset = startBufferOffset;
_encryptedSendBufferOffset = startBufferOffset + bufferSize;
_bufferSize = bufferSize;
+ _sendBufferSize = bufferSize + SendBufferExtendedBySslSize;
_inputStream = new InputStream(this);
- _ssl = new SslStream(_inputStream, true);
next.Callback = this;
}
@@ -179,6 +182,10 @@ public override int EndRead(IAsyncResult asyncResult)
{
return syncAsyncResult.Result;
}
+ if (((Task<int>)asyncResult).IsCanceled)
+ {
+ return 0;
+ }
try
{
return ((Task<int>)asyncResult).Result;
@@ -217,7 +224,7 @@ public override void Write(byte[] buffer, int offset, int count)
public override Task WriteAsync(byte[] buffer, int offset, int count, CancellationToken cancellationToken)
{
- if (count > _owner._bufferSize) throw new ArgumentOutOfRangeException("count", "Buffer size overflow");
+ if (count > _owner._sendBufferSize) throw new ArgumentOutOfRangeException("count", "Buffer size overflow");
Array.Copy(buffer, offset, _buf, _owner._encryptedSendBufferOffset, count);
_tcsSend = new TaskCompletionSource<object>();
_owner.Callback.StartSend(_owner._encryptedSendBufferOffset, count);
@@ -226,7 +233,7 @@ public override Task WriteAsync(byte[] buffer, int offset, int count, Cancellati
public override IAsyncResult BeginWrite(byte[] buffer, int offset, int count, AsyncCallback callback, object state)
{
- if (count > _owner._bufferSize) throw new ArgumentOutOfRangeException("count", "Buffer size overflow");
+ if (count > _owner._sendBufferSize) throw new ArgumentOutOfRangeException("count", "Buffer size overflow");
Array.Copy(buffer, offset, _buf, _owner._encryptedSendBufferOffset, count);
_tcsSend = new TaskCompletionSource<object>(state);
_callbackSend = callback;
@@ -279,6 +286,7 @@ public void Dispose()
public void PrepareAccept()
{
+ _ssl = null;
_next.PrepareAccept();
}
@@ -287,6 +295,7 @@ public void FinishAccept(int offset, int length)
_inputStream.FinishAccept(offset, length);
try
{
+ _ssl = new SslStream(_inputStream, true);
_authenticateTask = _ssl.AuthenticateAsServerAsync(_serverCertificate).ContinueWith((t, selfObject) =>
{
var self = (SslTransportHandler)selfObject;
View
@@ -5,7 +5,7 @@ Fast Owin Web Server in pure .Net 4.5
Current status is more proof of concept. But in keep-alive case with Hello World response is 3 times faster than NodeJs 0.10.7 or HttpListener.
-SSL is not supported, but planned.
+SSL is also supported!
Other Owin .Net server samples also included. Some parts of these samples source code are modified from Katana project.
View
Binary file not shown.

0 comments on commit 52c4d31

Please sign in to comment.