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

Feature/websocket/refactor #575

Open
wants to merge 2 commits into
base: dev
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
@@ -0,0 +1,24 @@
// Copyright (c) Microsoft. All rights reserved.
// Licensed under the MIT license. See LICENSE file in the project root for full license information.

namespace DotNetty.Codecs.Http.WebSockets.Handshaker
{
using System;
using DotNetty.Common.Utilities;

public class WebsocketHandshaker00Selector: WebsocketHandshakerVersionSelector
{
public WebsocketHandshaker00Selector(string webSocketUrl, string subprotocols, bool allowExtensions, int maxFramePayloadLength, bool allowMaskMismatch)
: base(webSocketUrl, subprotocols, allowExtensions, maxFramePayloadLength, allowMaskMismatch){ }

protected override Func<WebSocketServerHandshaker> InstanceFactory(string webSocketUrl, string subprotocols, bool allowExtensions, int maxFramePayloadLength, bool allowMaskMismatch)
{
return () => new WebSocketServerHandshaker00(webSocketUrl, subprotocols, maxFramePayloadLength);
}

protected override bool Selected(ICharSequence version)
{
return version == null;
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
// Copyright (c) Microsoft. All rights reserved.
// Licensed under the MIT license. See LICENSE file in the project root for full license information.

namespace DotNetty.Codecs.Http.WebSockets.Handshaker
{
using System;
using DotNetty.Common.Utilities;

public class WebsocketHandshaker07Selector: WebsocketHandshakerVersionSelector
{
public WebsocketHandshaker07Selector(string webSocketUrl, string subprotocols, bool allowExtensions, int maxFramePayloadLength, bool allowMaskMismatch)
: base(webSocketUrl, subprotocols, allowExtensions, maxFramePayloadLength, allowMaskMismatch){ }

protected override Func<WebSocketServerHandshaker> InstanceFactory(string webSocketUrl, string subprotocols, bool allowExtensions, int maxFramePayloadLength, bool allowMaskMismatch)
{
return () => new WebSocketServerHandshaker07(webSocketUrl, subprotocols, allowExtensions, maxFramePayloadLength);
}

protected override bool Selected(ICharSequence version)
{
return version.Equals(WebSocketVersion.V07.ToHttpHeaderValue());
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
// Copyright (c) Microsoft. All rights reserved.
// Licensed under the MIT license. See LICENSE file in the project root for full license information.

namespace DotNetty.Codecs.Http.WebSockets.Handshaker
{
using System;
using DotNetty.Common.Utilities;

public class WebsocketHandshaker08Selector: WebsocketHandshakerVersionSelector
{
public WebsocketHandshaker08Selector(string webSocketUrl, string subprotocols, bool allowExtensions, int maxFramePayloadLength, bool allowMaskMismatch)
: base(webSocketUrl, subprotocols, allowExtensions, maxFramePayloadLength, allowMaskMismatch){ }

protected override Func<WebSocketServerHandshaker> InstanceFactory(string webSocketUrl, string subprotocols, bool allowExtensions, int maxFramePayloadLength, bool allowMaskMismatch)
{
return () => new WebSocketServerHandshaker08(webSocketUrl, subprotocols, allowExtensions, maxFramePayloadLength);
}

protected override bool Selected(ICharSequence version)
{
return version.Equals(WebSocketVersion.V08.ToHttpHeaderValue());
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
// Copyright (c) Microsoft. All rights reserved.
// Licensed under the MIT license. See LICENSE file in the project root for full license information.

namespace DotNetty.Codecs.Http.WebSockets.Handshaker
{
using System;
using DotNetty.Common.Utilities;

public class WebsocketHandshaker13Selector : WebsocketHandshakerVersionSelector
{
public WebsocketHandshaker13Selector(string webSocketUrl, string subprotocols, bool allowExtensions, int maxFramePayloadLength, bool allowMaskMismatch)
: base(webSocketUrl, subprotocols, allowExtensions, maxFramePayloadLength, allowMaskMismatch){ }

protected override Func<WebSocketServerHandshaker> InstanceFactory(string webSocketUrl, string subprotocols, bool allowExtensions, int maxFramePayloadLength, bool allowMaskMismatch)
{
return () => new WebSocketServerHandshaker13(webSocketUrl, subprotocols, allowExtensions, maxFramePayloadLength);
}

protected override bool Selected(ICharSequence version)
{
return version.Equals(WebSocketVersion.V13.ToHttpHeaderValue());
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
// Copyright (c) Microsoft. All rights reserved.
// Licensed under the MIT license. See LICENSE file in the project root for full license information.

namespace DotNetty.Codecs.Http.WebSockets.Handshaker
{
using System;
using DotNetty.Common.Utilities;

public abstract class WebsocketHandshakerVersionSelector
{
protected Func<WebSocketServerHandshaker> Factory;

protected WebsocketHandshakerVersionSelector(
string webSocketUrl,
string subprotocols,
bool allowExtensions,
int maxFramePayloadLength,
bool allowMaskMismatch)
{
this.Factory = this.InstanceFactory(webSocketUrl, subprotocols, allowExtensions, maxFramePayloadLength, allowExtensions);
}

protected abstract Func<WebSocketServerHandshaker> InstanceFactory(
string webSocketUrl,
string subprotocols,
bool allowExtensions,
int maxFramePayloadLength,
bool allowMaskMismatch);

protected abstract bool Selected(ICharSequence version);


public bool Selector(ICharSequence version, out WebSocketServerHandshaker handshaker)
{
handshaker = null;
if (!this.Selected(version))
{
return false;
}

handshaker = this.Factory();
return true;
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
namespace DotNetty.Codecs.Http.WebSockets
{
using System.Threading.Tasks;
using DotNetty.Codecs.Http.WebSockets.Handshaker;
using DotNetty.Common.Utilities;
using DotNetty.Transport.Channels;

Expand All @@ -19,6 +20,8 @@ public class WebSocketServerHandshakerFactory

readonly bool allowMaskMismatch;

readonly WebsocketHandshakerVersionSelector[] selectors;

public WebSocketServerHandshakerFactory(string webSocketUrl, string subprotocols, bool allowExtensions)
: this(webSocketUrl, subprotocols, allowExtensions, 65536)
{
Expand All @@ -38,53 +41,50 @@ public WebSocketServerHandshakerFactory(string webSocketUrl, string subprotocols
this.allowExtensions = allowExtensions;
this.maxFramePayloadLength = maxFramePayloadLength;
this.allowMaskMismatch = allowMaskMismatch;

this.selectors = new WebsocketHandshakerVersionSelector[]
{
new WebsocketHandshaker00Selector(
this.subprotocols,
this.subprotocols,
this.allowExtensions,
this.maxFramePayloadLength,
this.allowMaskMismatch),
new WebsocketHandshaker07Selector(
this.subprotocols,
this.subprotocols,
this.allowExtensions,
this.maxFramePayloadLength,
this.allowMaskMismatch),
new WebsocketHandshaker08Selector(
this.subprotocols,
this.subprotocols,
this.allowExtensions,
this.maxFramePayloadLength,
this.allowMaskMismatch),
new WebsocketHandshaker13Selector(
this.subprotocols,
this.subprotocols,
this.allowExtensions,
this.maxFramePayloadLength,
this.allowMaskMismatch)
};
}

public WebSocketServerHandshaker NewHandshaker(IHttpRequest req)
{
if (req.Headers.TryGet(HttpHeaderNames.SecWebsocketVersion, out ICharSequence version)
&& version != null)
req.Headers.TryGet(HttpHeaderNames.SecWebsocketVersion, out ICharSequence version);

WebSocketServerHandshaker result = null;
foreach (WebsocketHandshakerVersionSelector selector in this.selectors)
{
if (version.Equals(WebSocketVersion.V13.ToHttpHeaderValue()))
if (selector.Selector(version, out result))
{
// Version 13 of the wire protocol - RFC 6455 (version 17 of the draft hybi specification).
return new WebSocketServerHandshaker13(
this.webSocketUrl,
this.subprotocols,
this.allowExtensions,
this.maxFramePayloadLength,
this.allowMaskMismatch);
}
else if (version.Equals(WebSocketVersion.V08.ToHttpHeaderValue()))
{
// Version 8 of the wire protocol - version 10 of the draft hybi specification.
return new WebSocketServerHandshaker08(
this.webSocketUrl,
this.subprotocols,
this.allowExtensions,
this.maxFramePayloadLength,
this.allowMaskMismatch);
}
else if (version.Equals(WebSocketVersion.V07.ToHttpHeaderValue()))
{
// Version 8 of the wire protocol - version 07 of the draft hybi specification.
return new WebSocketServerHandshaker07(
this.webSocketUrl,
this.subprotocols,
this.allowExtensions,
this.maxFramePayloadLength,
this.allowMaskMismatch);
}
else
{
return null;
break;
}
}
else
{
// Assume version 00 where version header was not specified
return new WebSocketServerHandshaker00(this.webSocketUrl, this.subprotocols, this.maxFramePayloadLength);
}

return result;
}

public static Task SendUnsupportedVersionResponse(IChannel channel)
Expand Down