Skip to content

Commit

Permalink
Tests pass. ResponseBuilder hooked up. However, draft 76 still doesnt…
Browse files Browse the repository at this point in the history
… work. Something wrong with the answer bytes.
  • Loading branch information
statianzo committed Sep 2, 2011
1 parent 31491cb commit b773d63
Show file tree
Hide file tree
Showing 6 changed files with 29 additions and 29 deletions.
5 changes: 4 additions & 1 deletion src/Fleck.Tests/HandshakeHandlerTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@
using System.Threading.Tasks;
using NUnit.Framework;
using System.Security.Cryptography.X509Certificates;
using Fleck.Interfaces;
using Moq;

namespace Fleck.Tests
{
Expand Down Expand Up @@ -43,7 +45,8 @@ public class HandshakeHandlerTests
[SetUp]
public void Setup()
{
_handler = new HandshakeHandler(null, "ws://fleck-test.com", "ws");
var mockFactory = new Mock<IResponseBuilderFactory>();
_handler = new HandshakeHandler(mockFactory.Object);
}

[Test]
Expand Down
2 changes: 1 addition & 1 deletion src/Fleck.Tests/ReceiverTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ public void ShouldCloseOnException()
_mockSocket.Setup(s => s.Connected).Returns(true);
_receiver.Receive();
_mockSocket.Setup(s => s.Receive(It.IsAny<byte[]>(),It.IsAny<Action<int>>(),It.IsAny<Action<Exception>>(), 0))
.Returns<byte[], Action<int>, Action<Exception>>((buffer, cb, error) =>
.Returns<byte[], Action<int>, Action<Exception>, int>((buffer, cb, error, offset) =>
{
error(new Exception());
return new Task<int>(() => 0);
Expand Down
35 changes: 11 additions & 24 deletions src/Fleck/HandshakeHandler.cs
Original file line number Diff line number Diff line change
Expand Up @@ -10,18 +10,12 @@ namespace Fleck
{
public class HandshakeHandler
{
public HandshakeHandler(string origin, string location, string scheme)
public HandshakeHandler(IResponseBuilderFactory factory)
{
Origin = origin;
Location = location;
Scheme = scheme;
RequestParser = new RequestParser();
ResponseBuilderFactory = new ResponseBuilderFactory();
ResponseBuilderFactory = factory;
}

public string Scheme { get; set; }
public string Origin { get; set; }
public string Location { get; set; }
public ClientHandshake ClientHandshake { get; set; }
public Action<ClientHandshake> OnSuccess { get; set; }
public IRequestParser RequestParser { get; set; }
Expand Down Expand Up @@ -57,29 +51,22 @@ public void DoShake(HandShakeState state, int receivedByteCount)

var request = RequestParser.Parse(state.Buffer);
var builder = ResponseBuilderFactory.Resolve(request);
if (builder == null)
{
FleckLog.Info("Incompatible request.");
state.Socket.Close();
return;
}

var response = builder.Build(request);

}



public void BeginSendServerHandshake(ServerHandshake handshake, ISocket socket)
{
FleckLog.Debug("Begin create server handshake");
string stringShake = handshake.ToResponseString();

byte[] byteResponse = Encoding.UTF8.GetBytes(stringShake);
int byteResponseLength = byteResponse.Length;
Array.Resize(ref byteResponse, byteResponseLength + handshake.AnswerBytes.Length);
Array.Copy(handshake.AnswerBytes, 0, byteResponse, byteResponseLength, handshake.AnswerBytes.Length);


FleckLog.Debug("Sending server handshake");
socket.Send(byteResponse,
state.Socket.Send(response,
EndSendServerHandshake,
e => FleckLog.Error("Send handshake failed", e));
}


private void EndSendServerHandshake()
{
FleckLog.Debug("Ending server handshake");
Expand Down
2 changes: 1 addition & 1 deletion src/Fleck/RequestBuilders/Draft76ResponseBuilder.cs
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ public Draft76ResponseBuilder(string location, string scheme, string origin)

public bool CanHandle(WebSocketHttpRequest request)
{
return request.Headers.ContainsKey("sec-websocket-key1") && request.Headers.ContainsKey("sec-websocket-key2");
return request.Headers.ContainsKey("Sec-WebSocket-Key1") && request.Headers.ContainsKey("Sec-WebSocket-Key2");
}

public byte[] Build(WebSocketHttpRequest request)
Expand Down
2 changes: 1 addition & 1 deletion src/Fleck/SocketWrapper.cs
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ public bool Connected
get { return _socket.Connected; }
}

public Task<int> Receive(byte[] buffer, Action<int> callback, Action<Exception> error, int offset = 0)
public Task<int> Receive(byte[] buffer, Action<int> callback, Action<Exception> error, int offset)
{
Func<AsyncCallback, object, IAsyncResult> begin =
(cb, s) => _stream.BeginRead(buffer, offset, buffer.Length, cb, s);
Expand Down
12 changes: 11 additions & 1 deletion src/Fleck/WebSocketServer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@
using System.Net;
using System.Net.Sockets;
using System.Security.Cryptography.X509Certificates;
using Fleck.Interfaces;
using Fleck.RequestBuilders;

namespace Fleck
{
Expand All @@ -23,13 +25,20 @@ public WebSocketServer(int port, string location)
_scheme = uri.Scheme;
var socket = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.IP);
ListenerSocket = new SocketWrapper(socket);
ResponseBuilderFactory = new ResponseBuilderFactory();
}

private void RegisterResponseBuilders()
{
ResponseBuilderFactory.Register(new Draft76ResponseBuilder(Location, _scheme, Origin));
}

public ISocket ListenerSocket { get; set; }
public string Location { get; private set; }
public int Port { get; private set; }
public string Origin { get; set; }
public string Certificate { get; set; }
public IResponseBuilderFactory ResponseBuilderFactory { get; set; }

public bool IsSecure
{
Expand All @@ -56,6 +65,7 @@ public void Start(Action<IWebSocketConnection> config)
}
_x509Certificate = new X509Certificate2(Certificate);
}
RegisterResponseBuilders();
ListenForClients();
_config = config;
}
Expand All @@ -70,7 +80,7 @@ private void OnClientConnect(ISocket clientSocket)
FleckLog.Debug("Client Connected");
ListenForClients();

var shaker = new HandshakeHandler(Origin, Location, _scheme)
var shaker = new HandshakeHandler(ResponseBuilderFactory)
{
OnSuccess = handshake =>
{
Expand Down

0 comments on commit b773d63

Please sign in to comment.