Skip to content

Commit 6d962b1

Browse files
committed
Adding support for limiting message frame size
1 parent 82a672e commit 6d962b1

File tree

15 files changed

+126
-46
lines changed

15 files changed

+126
-46
lines changed

src/netstandard/Extensions/WampSharp.AspNet.WebSockets.Server/AspNetWebsocketTransport.cs

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ public sealed class AspNetWebSocketTransport : WebSocketTransport<WebSocketData>
1818
private readonly string mUrl;
1919
private readonly object mLock = new object();
2020
private Route mRoute;
21+
private readonly int? mMaxFrameSize;
2122

2223
/// <exclude />
2324
public AspNetWebSocketTransport(string url,
@@ -55,7 +56,8 @@ protected override IWampConnection<TMessage> CreateBinaryConnection<TMessage>
5556
return new BinaryWebSocketConnection<TMessage>(connection.WebSocket,
5657
binding,
5758
new AspNetCookieProvider(connection.HttpContext),
58-
AuthenticatorFactory);
59+
AuthenticatorFactory,
60+
mMaxFrameSize);
5961
}
6062

6163
/// <exclude />
@@ -66,7 +68,8 @@ protected override IWampConnection<TMessage> CreateTextConnection<TMessage>
6668
return new TextWebSocketConnection<TMessage>(connection.WebSocket,
6769
binding,
6870
new AspNetCookieProvider(connection.HttpContext),
69-
AuthenticatorFactory);
71+
AuthenticatorFactory,
72+
mMaxFrameSize);
7073
}
7174

7275
/// <exclude />

src/netstandard/Extensions/WampSharp.AspNetCore.WebSockets.Server/AspNetCoreWebSocketTransport.cs

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ namespace WampSharp.AspNetCore.WebSockets.Server
1515
public class AspNetCoreWebSocketTransport : WebSocketTransport<WebSocketData>
1616
{
1717
private Func<HttpContext, Func<Task>, Task> mHandler;
18+
private int? mMaxFrameSize;
1819

1920
public AspNetCoreWebSocketTransport
2021
(IApplicationBuilder app,
@@ -54,7 +55,8 @@ protected override IWampConnection<TMessage> CreateBinaryConnection<TMessage>
5455
(connection.WebSocket,
5556
binding,
5657
new AspNetCoreCookieProvider(connection.HttpContext),
57-
AuthenticatorFactory);
58+
AuthenticatorFactory,
59+
mMaxFrameSize);
5860
}
5961

6062
protected override IWampConnection<TMessage> CreateTextConnection<TMessage>
@@ -67,7 +69,8 @@ protected override IWampConnection<TMessage> CreateTextConnection<TMessage>
6769
(connection.WebSocket,
6870
binding,
6971
new AspNetCoreCookieProvider(connection.HttpContext),
70-
AuthenticatorFactory);
72+
AuthenticatorFactory,
73+
mMaxFrameSize);
7174
}
7275

7376
private void ConfigureComputeBytes<TMessage, TRaw>(IWampTransportBinding<TMessage, TRaw> binding)

src/netstandard/Extensions/WampSharp.HttpListener/HttpListenerWebSocketTransport.cs

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ public sealed class HttpListenerWebSocketTransport : WebSocketTransport<WebSocke
1818
private readonly string mUrl;
1919
private readonly Action<HttpListenerContext> mOnUnknownRequest;
2020
private System.Net.HttpListener mHttpListener;
21+
private int? mMaxFrameSize;
2122

2223
/// <exclude />
2324
public HttpListenerWebSocketTransport
@@ -65,7 +66,8 @@ protected override IWampConnection<TMessage> CreateBinaryConnection<TMessage>
6566
return new BinaryWebSocketConnection<TMessage>(connection.Context.WebSocket,
6667
binding,
6768
new HttpListenerCookieProvider(connection.Context),
68-
AuthenticatorFactory);
69+
AuthenticatorFactory,
70+
mMaxFrameSize);
6971
}
7072

7173
/// <exclude />
@@ -76,7 +78,8 @@ protected override IWampConnection<TMessage> CreateTextConnection<TMessage>
7678
return new TextWebSocketConnection<TMessage>(connection.Context.WebSocket,
7779
binding,
7880
new HttpListenerCookieProvider(connection.Context),
79-
AuthenticatorFactory);
81+
AuthenticatorFactory,
82+
mMaxFrameSize);
8083
}
8184

8285
/// <exclude />

src/netstandard/Extensions/WampSharp.Owin/Owin/OwinWebSocketTransport.cs

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -18,14 +18,17 @@ public class OwinWebSocketTransport : WebSocketTransport<WebSocketData>
1818
private const string SecWebSocketProtocolHeader = "Sec-WebSocket-Protocol";
1919

2020
private Func<IOwinContext, Func<Task>, Task> mHandler;
21+
private int? mMaxFrameSize;
2122

2223
public OwinWebSocketTransport
23-
(IAppBuilder app,
24-
ICookieAuthenticatorFactory authenticatorFactory = null) :
24+
(IAppBuilder app,
25+
int? maxFrameSize,
26+
ICookieAuthenticatorFactory authenticatorFactory = null) :
2527
base(authenticatorFactory)
2628
{
2729
mHandler = this.EmptyHandler;
2830
app.Use(HttpHandler);
31+
mMaxFrameSize = maxFrameSize;
2932
}
3033

3134
public override void Dispose()
@@ -55,7 +58,8 @@ protected override IWampConnection<TMessage> CreateBinaryConnection<TMessage>
5558
(new OwinWebSocketWrapper(connection.WebSocketContext),
5659
binding,
5760
new OwinCookieProvider(connection.OwinContext),
58-
AuthenticatorFactory);
61+
AuthenticatorFactory,
62+
mMaxFrameSize);
5963
}
6064

6165
protected override IWampConnection<TMessage> CreateTextConnection<TMessage>
@@ -66,7 +70,8 @@ protected override IWampConnection<TMessage> CreateTextConnection<TMessage>
6670
(new OwinWebSocketWrapper(connection.WebSocketContext),
6771
binding,
6872
new OwinCookieProvider(connection.OwinContext),
69-
AuthenticatorFactory);
73+
AuthenticatorFactory,
74+
mMaxFrameSize);
7075
}
7176

7277
public override void Open()

src/netstandard/Extensions/WampSharp.WebSockets/WAMP2/V2/Fluent/IWebSocketTransportSyntax.cs

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,9 @@ namespace WampSharp.V2.Fluent
55
{
66
public interface IWebSocketTransportSyntax : ChannelFactorySyntax.ITransportSyntax
77
{
8-
ChannelFactorySyntax.ITransportSyntax SetClientWebSocketOptions
8+
IWebSocketTransportSyntax SetClientWebSocketOptions
99
(Action<ClientWebSocketOptions> configureClientWebSocketOptions);
10+
11+
IWebSocketTransportSyntax SetMaxFrameSize(int maxFrameSize);
1012
}
1113
}

src/netstandard/Extensions/WampSharp.WebSockets/WAMP2/V2/Fluent/WebSocketActivator.cs

Lines changed: 10 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,8 @@ public WebSocketActivator(Uri serverAddress)
2020

2121
public Action<ClientWebSocketOptions> ConfigureOptions { get; set; }
2222

23+
public int? MaxFrameSize { get; set; }
24+
2325
public IControlledWampConnection<TMessage> Activate<TMessage>(IWampBinding<TMessage> binding)
2426
{
2527
Func<IControlledWampConnection<TMessage>> factory =
@@ -36,22 +38,24 @@ private IControlledWampConnection<TMessage> GetConnectionFactory<TMessage>(IWamp
3638
switch (binding)
3739
{
3840
case IWampTextBinding<TMessage> textBinding:
39-
return CreateTextConnection(textBinding);
41+
return CreateTextConnection(textBinding, MaxFrameSize);
4042
case IWampBinaryBinding<TMessage> binaryBinding:
41-
return CreateBinaryConnection(binaryBinding);
43+
return CreateBinaryConnection(binaryBinding, MaxFrameSize);
4244
}
4345

4446
throw new Exception();
4547
}
4648

47-
protected IControlledWampConnection<TMessage> CreateBinaryConnection<TMessage>(IWampBinaryBinding<TMessage> binaryBinding)
49+
protected IControlledWampConnection<TMessage> CreateBinaryConnection<TMessage>(
50+
IWampBinaryBinding<TMessage> binaryBinding, int? maxFrameSize)
4851
{
49-
return new ControlledBinaryWebSocketConnection<TMessage>(ActivateWebSocket(), mServerAddress, binaryBinding);
52+
return new ControlledBinaryWebSocketConnection<TMessage>(ActivateWebSocket(), mServerAddress, binaryBinding, maxFrameSize);
5053
}
5154

52-
protected IControlledWampConnection<TMessage> CreateTextConnection<TMessage>(IWampTextBinding<TMessage> textBinding)
55+
protected IControlledWampConnection<TMessage> CreateTextConnection<TMessage>(
56+
IWampTextBinding<TMessage> textBinding, int? maxFrameSize)
5357
{
54-
return new ControlledTextWebSocketConnection<TMessage>(ActivateWebSocket(), mServerAddress, textBinding);
58+
return new ControlledTextWebSocketConnection<TMessage>(ActivateWebSocket(), mServerAddress, textBinding, maxFrameSize);
5559
}
5660

5761
private ClientWebSocket ActivateWebSocket()

src/netstandard/Extensions/WampSharp.WebSockets/WAMP2/V2/Fluent/WebSocketTransportSyntax.cs

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,12 +10,18 @@ public WebSocketTransportSyntax(ChannelState state)
1010
State = state;
1111
}
1212

13-
public ChannelFactorySyntax.ITransportSyntax SetClientWebSocketOptions(Action<ClientWebSocketOptions> configureClientWebSocketOptions)
13+
public IWebSocketTransportSyntax SetClientWebSocketOptions(Action<ClientWebSocketOptions> configureClientWebSocketOptions)
1414
{
1515
((WebSocketActivator) State.ConnectionActivator).ConfigureOptions = configureClientWebSocketOptions;
1616
return this;
1717
}
1818

19+
public IWebSocketTransportSyntax SetMaxFrameSize(int maxFrameSize)
20+
{
21+
((WebSocketActivator) State.ConnectionActivator).MaxFrameSize = maxFrameSize;
22+
return this;
23+
}
24+
1925
public ChannelState State { get; }
2026
}
2127
}

src/netstandard/Extensions/WampSharp.WebSockets/WebSockets/BinaryWebSocketConnection.cs

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -7,12 +7,15 @@ namespace WampSharp.WebSockets
77
{
88
public class BinaryWebSocketConnection<TMessage> : BinaryWebSocketWrapperConnection<TMessage>
99
{
10-
public BinaryWebSocketConnection(WebSocket webSocket, IWampBinaryBinding<TMessage> binding, ICookieProvider cookieProvider, ICookieAuthenticatorFactory cookieAuthenticatorFactory) :
11-
base(new WebSocketWrapper(webSocket), binding, cookieProvider, cookieAuthenticatorFactory)
10+
public BinaryWebSocketConnection(WebSocket webSocket, IWampBinaryBinding<TMessage> binding,
11+
ICookieProvider cookieProvider,
12+
ICookieAuthenticatorFactory cookieAuthenticatorFactory, int? maxFrameSize) :
13+
base(new WebSocketWrapper(webSocket), binding, cookieProvider, cookieAuthenticatorFactory, maxFrameSize)
1214
{
1315
}
1416

15-
protected BinaryWebSocketConnection(ClientWebSocket clientWebSocket, Uri addressUri, IWampBinaryBinding<TMessage> binding) : base(new ClientWebSocketWrapper(clientWebSocket), addressUri, binding)
17+
protected BinaryWebSocketConnection(ClientWebSocket clientWebSocket, Uri addressUri,
18+
IWampBinaryBinding<TMessage> binding, int? maxFrameSize) : base(new ClientWebSocketWrapper(clientWebSocket), addressUri, binding, maxFrameSize)
1619
{
1720
}
1821
}

src/netstandard/Extensions/WampSharp.WebSockets/WebSockets/BinaryWebSocketWrapperConnection.cs

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -9,14 +9,18 @@ public class BinaryWebSocketWrapperConnection<TMessage> : WebSocketWrapperConnec
99
{
1010
private readonly IWampBinaryBinding<TMessage> mBinding;
1111

12-
public BinaryWebSocketWrapperConnection(IWebSocketWrapper webSocket, IWampBinaryBinding<TMessage> binding, ICookieProvider cookieProvider, ICookieAuthenticatorFactory cookieAuthenticatorFactory) :
13-
base(webSocket, binding, cookieProvider, cookieAuthenticatorFactory)
12+
public BinaryWebSocketWrapperConnection(IWebSocketWrapper webSocket, IWampBinaryBinding<TMessage> binding,
13+
ICookieProvider cookieProvider,
14+
ICookieAuthenticatorFactory cookieAuthenticatorFactory,
15+
int? maxFrameSize) :
16+
base(webSocket, binding, cookieProvider, cookieAuthenticatorFactory, maxFrameSize)
1417
{
1518
mBinding = binding;
1619
}
1720

18-
protected BinaryWebSocketWrapperConnection(IClientWebSocketWrapper clientWebSocket, Uri addressUri, IWampBinaryBinding<TMessage> binding) :
19-
base(clientWebSocket, addressUri, binding.Name, binding)
21+
protected BinaryWebSocketWrapperConnection(IClientWebSocketWrapper clientWebSocket, Uri addressUri,
22+
IWampBinaryBinding<TMessage> binding, int? maxFrameSize) :
23+
base(clientWebSocket, addressUri, binding.Name, binding, maxFrameSize)
2024
{
2125
mBinding = binding;
2226
}

src/netstandard/Extensions/WampSharp.WebSockets/WebSockets/ControlledBinaryWebSocketConnection.cs

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -7,13 +7,15 @@ namespace WampSharp.WebSockets
77
{
88
public class ControlledBinaryWebSocketConnection<TMessage> : BinaryWebSocketConnection<TMessage>, IControlledWampConnection<TMessage>
99
{
10-
public ControlledBinaryWebSocketConnection(Uri addressUri, IWampBinaryBinding<TMessage> binding) :
11-
this(new ClientWebSocket(), addressUri, binding)
10+
public ControlledBinaryWebSocketConnection(Uri addressUri, IWampBinaryBinding<TMessage> binding,
11+
int? maxFrameSize) :
12+
this(new ClientWebSocket(), addressUri, binding, maxFrameSize)
1213
{
1314
}
1415

15-
public ControlledBinaryWebSocketConnection(ClientWebSocket clientWebSocket, Uri addressUri, IWampBinaryBinding<TMessage> binding) :
16-
base(clientWebSocket, addressUri, binding)
16+
public ControlledBinaryWebSocketConnection(ClientWebSocket clientWebSocket, Uri addressUri,
17+
IWampBinaryBinding<TMessage> binding, int? maxFrameSize) :
18+
base(clientWebSocket, addressUri, binding, maxFrameSize)
1719
{
1820
}
1921

0 commit comments

Comments
 (0)