Skip to content
This repository was archived by the owner on Nov 20, 2018. It is now read-only.

Commit 2577c16

Browse files
committed
#267, #273, Move WebSocket APIs to their own object, fix context object.
1 parent d81d365 commit 2577c16

12 files changed

+110
-69
lines changed

src/Microsoft.AspNet.Http.Core/HttpContext.cs

+1-12
Original file line numberDiff line numberDiff line change
@@ -3,10 +3,8 @@
33

44
using System;
55
using System.Collections.Generic;
6-
using System.Net.WebSockets;
76
using System.Security.Claims;
87
using System.Threading;
9-
using System.Threading.Tasks;
108
using Microsoft.AspNet.Http.Authentication;
119

1210
namespace Microsoft.AspNet.Http
@@ -33,9 +31,7 @@ public abstract class HttpContext : IDisposable
3331

3432
public abstract ISessionCollection Session { get; }
3533

36-
public abstract bool IsWebSocketRequest { get; }
37-
38-
public abstract IList<string> WebSocketRequestedProtocols { get; }
34+
public abstract WebSocketManager WebSockets { get; }
3935

4036
public abstract void Abort();
4137

@@ -54,12 +50,5 @@ public virtual void SetFeature<T>(T instance)
5450
{
5551
SetFeature(typeof(T), instance);
5652
}
57-
58-
public virtual Task<WebSocket> AcceptWebSocketAsync()
59-
{
60-
return AcceptWebSocketAsync(subProtocol: null);
61-
}
62-
63-
public abstract Task<WebSocket> AcceptWebSocketAsync(string subProtocol);
6453
}
6554
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
// Copyright (c) Microsoft Open Technologies, Inc. All rights reserved.
2+
// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information.
3+
4+
using System.Collections.Generic;
5+
using System.Net.WebSockets;
6+
using System.Threading.Tasks;
7+
8+
namespace Microsoft.AspNet.Http
9+
{
10+
public abstract class WebSocketManager
11+
{
12+
public abstract bool IsWebSocketRequest { get; }
13+
14+
public abstract IList<string> WebSocketRequestedProtocols { get; }
15+
16+
public virtual Task<WebSocket> AcceptWebSocketAsync()
17+
{
18+
return AcceptWebSocketAsync(subProtocol: null);
19+
}
20+
21+
public abstract Task<WebSocket> AcceptWebSocketAsync(string subProtocol);
22+
}
23+
}

src/Microsoft.AspNet.Http.Interfaces/IHttpWebSocketFeature.cs

+1-1
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,6 @@ public interface IHttpWebSocketFeature
1010
{
1111
bool IsWebSocketRequest { get; }
1212

13-
Task<WebSocket> AcceptAsync(IWebSocketAcceptContext context);
13+
Task<WebSocket> AcceptAsync(WebSocketAcceptContext context);
1414
}
1515
}

src/Microsoft.AspNet.Http.Interfaces/IWebSocketAcceptContext.cs

-10
This file was deleted.

src/Microsoft.AspNet.Http/WebSocketAcceptContext.cs renamed to src/Microsoft.AspNet.Http.Interfaces/WebSocketAcceptContext.cs

+1-1
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33

44
namespace Microsoft.AspNet.Http
55
{
6-
public class WebSocketAcceptContext : IWebSocketAcceptContext
6+
public class WebSocketAcceptContext
77
{
88
public virtual string SubProtocol { get; set; }
99
}

src/Microsoft.AspNet.Http/DefaultHttpContext.cs

+7-33
Original file line numberDiff line numberDiff line change
@@ -3,22 +3,17 @@
33

44
using System;
55
using System.Collections.Generic;
6-
using System.Net.WebSockets;
76
using System.Security.Claims;
87
using System.Threading;
9-
using System.Threading.Tasks;
108
using Microsoft.AspNet.FeatureModel;
119
using Microsoft.AspNet.Http.Authentication;
1210
using Microsoft.AspNet.Http.Collections;
1311
using Microsoft.AspNet.Http.Infrastructure;
14-
using Microsoft.Net.Http.Headers;
1512

1613
namespace Microsoft.AspNet.Http
1714
{
1815
public class DefaultHttpContext : HttpContext
1916
{
20-
private static IList<string> EmptyList = new List<string>();
21-
2217
private readonly HttpRequest _request;
2318
private readonly HttpResponse _response;
2419
private readonly ConnectionInfo _connection;
@@ -28,8 +23,8 @@ public class DefaultHttpContext : HttpContext
2823
private FeatureReference<IServiceProvidersFeature> _serviceProviders;
2924
private FeatureReference<IHttpAuthenticationFeature> _authentication;
3025
private FeatureReference<IHttpRequestLifetimeFeature> _lifetime;
31-
private FeatureReference<IHttpWebSocketFeature> _webSockets;
3226
private FeatureReference<ISessionFeature> _session;
27+
private WebSocketManager _websockets;
3328
private IFeatureCollection _features;
3429

3530
public DefaultHttpContext()
@@ -51,7 +46,6 @@ public DefaultHttpContext(IFeatureCollection features)
5146
_serviceProviders = FeatureReference<IServiceProvidersFeature>.Default;
5247
_authentication = FeatureReference<IHttpAuthenticationFeature>.Default;
5348
_lifetime = FeatureReference<IHttpRequestLifetimeFeature>.Default;
54-
_webSockets = FeatureReference<IHttpWebSocketFeature>.Default;
5549
_session = FeatureReference<ISessionFeature>.Default;
5650
}
5751

@@ -75,11 +69,6 @@ private IHttpRequestLifetimeFeature LifetimeFeature
7569
get { return _lifetime.Fetch(_features); }
7670
}
7771

78-
private IHttpWebSocketFeature WebSocketFeature
79-
{
80-
get { return _webSockets.Fetch(_features); }
81-
}
82-
8372
private ISessionFeature SessionFeature
8473
{
8574
get { return _session.Fetch(_features); }
@@ -161,20 +150,15 @@ public override ISessionCollection Session
161150
}
162151
}
163152

164-
public override bool IsWebSocketRequest
153+
public override WebSocketManager WebSockets
165154
{
166155
get
167156
{
168-
var webSocketFeature = WebSocketFeature;
169-
return webSocketFeature != null && webSocketFeature.IsWebSocketRequest;
170-
}
171-
}
172-
173-
public override IList<string> WebSocketRequestedProtocols
174-
{
175-
get
176-
{
177-
return Request.Headers.GetValues(HeaderNames.WebSocketSubProtocols) ?? EmptyList;
157+
if (_websockets == null)
158+
{
159+
_websockets = new DefaultWebSocketManager(_features);
160+
}
161+
return _websockets;
178162
}
179163
}
180164

@@ -203,15 +187,5 @@ public override void SetFeature(Type type, object instance)
203187
{
204188
_features[type] = instance;
205189
}
206-
207-
public override Task<WebSocket> AcceptWebSocketAsync(string subProtocol)
208-
{
209-
var webSocketFeature = WebSocketFeature;
210-
if (WebSocketFeature == null)
211-
{
212-
throw new NotSupportedException("WebSockets are not supported");
213-
}
214-
return WebSocketFeature.AcceptAsync(new WebSocketAcceptContext() { SubProtocol = subProtocol } );
215-
}
216190
}
217191
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,65 @@
1+
// Copyright (c) Microsoft Open Technologies, Inc. All rights reserved.
2+
// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information.
3+
4+
using System;
5+
using System.Collections.Generic;
6+
using System.Net.WebSockets;
7+
using System.Threading.Tasks;
8+
using Microsoft.AspNet.FeatureModel;
9+
using Microsoft.AspNet.Http.Infrastructure;
10+
using Microsoft.Net.Http.Headers;
11+
12+
namespace Microsoft.AspNet.Http
13+
{
14+
public class DefaultWebSocketManager : WebSocketManager
15+
{
16+
private static IList<string> EmptyList = new List<string>();
17+
18+
private IFeatureCollection _features;
19+
private FeatureReference<IHttpRequestFeature> _request = FeatureReference<IHttpRequestFeature>.Default;
20+
private FeatureReference<IHttpWebSocketFeature> _webSockets = FeatureReference<IHttpWebSocketFeature>.Default;
21+
22+
public DefaultWebSocketManager(IFeatureCollection features)
23+
{
24+
_features = features;
25+
}
26+
27+
private IHttpRequestFeature HttpRequestFeature
28+
{
29+
get { return _request.Fetch(_features); }
30+
}
31+
32+
private IHttpWebSocketFeature WebSocketFeature
33+
{
34+
get { return _webSockets.Fetch(_features); }
35+
}
36+
37+
public override bool IsWebSocketRequest
38+
{
39+
get
40+
{
41+
var webSocketFeature = WebSocketFeature;
42+
return webSocketFeature != null && webSocketFeature.IsWebSocketRequest;
43+
}
44+
}
45+
46+
public override IList<string> WebSocketRequestedProtocols
47+
{
48+
get
49+
{
50+
return ParsingHelpers.GetHeaderUnmodified(HttpRequestFeature.Headers,
51+
HeaderNames.WebSocketSubProtocols) ?? EmptyList;
52+
}
53+
}
54+
55+
public override Task<WebSocket> AcceptWebSocketAsync(string subProtocol)
56+
{
57+
var webSocketFeature = WebSocketFeature;
58+
if (WebSocketFeature == null)
59+
{
60+
throw new NotSupportedException("WebSockets are not supported");
61+
}
62+
return WebSocketFeature.AcceptAsync(new WebSocketAcceptContext() { SubProtocol = subProtocol });
63+
}
64+
}
65+
}

src/Microsoft.AspNet.Owin/OwinEnvironment.cs

+2-2
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ namespace Microsoft.AspNet.Owin
2222
using WebSocketAcceptAlt =
2323
Func
2424
<
25-
IWebSocketAcceptContext, // WebSocket Accept parameters
25+
WebSocketAcceptContext, // WebSocket Accept parameters
2626
Task<WebSocket>
2727
>;
2828

@@ -105,7 +105,7 @@ public OwinEnvironment(HttpContext context)
105105
feature => new Func<Task>(() => feature.GetClientCertificateAsync(CancellationToken.None))));
106106
}
107107

108-
if (context.IsWebSocketRequest)
108+
if (context.WebSockets.IsWebSocketRequest)
109109
{
110110
_entries.Add(OwinConstants.WebSocket.AcceptAlt, new FeatureMap<IHttpWebSocketFeature>(feature => new WebSocketAcceptAlt(feature.AcceptAsync)));
111111
}

src/Microsoft.AspNet.Owin/OwinFeatureCollection.cs

+2-2
Original file line numberDiff line numberDiff line change
@@ -277,14 +277,14 @@ bool IHttpWebSocketFeature.IsWebSocketRequest
277277
}
278278
}
279279

280-
Task<WebSocket> IHttpWebSocketFeature.AcceptAsync(IWebSocketAcceptContext context)
280+
Task<WebSocket> IHttpWebSocketFeature.AcceptAsync(WebSocketAcceptContext context)
281281
{
282282
object obj;
283283
if (!Environment.TryGetValue(OwinConstants.WebSocket.AcceptAlt, out obj))
284284
{
285285
throw new NotSupportedException("WebSockets are not supported"); // TODO: LOC
286286
}
287-
var accept = (Func<IWebSocketAcceptContext, Task<WebSocket>>)obj;
287+
var accept = (Func<WebSocketAcceptContext, Task<WebSocket>>)obj;
288288
return accept(context);
289289
}
290290

src/Microsoft.AspNet.Owin/WebSockets/OwinWebSocketAcceptAdapter.cs

+2-2
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ namespace Microsoft.AspNet.Owin
2323
using WebSocketAcceptAlt =
2424
Func
2525
<
26-
IWebSocketAcceptContext, // WebSocket Accept parameters
26+
WebSocketAcceptContext, // WebSocket Accept parameters
2727
Task<WebSocket>
2828
>;
2929

@@ -48,7 +48,7 @@ private OwinWebSocketAcceptAdapter(WebSocketAccept owinWebSocketAccept)
4848
private Task UpstreamTask { get; set; }
4949
private TaskCompletionSource<int> UpstreamWentAsyncTcs { get { return _upstreamWentAsync; } }
5050

51-
private async Task<WebSocket> AcceptWebSocketAsync(IWebSocketAcceptContext context)
51+
private async Task<WebSocket> AcceptWebSocketAsync(WebSocketAcceptContext context)
5252
{
5353
IDictionary<string, object> options = null;
5454
if (context is OwinWebSocketAcceptContext)

src/Microsoft.AspNet.Owin/WebSockets/OwinWebSocketAcceptContext.cs

+2-2
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66

77
namespace Microsoft.AspNet.Owin
88
{
9-
public class OwinWebSocketAcceptContext : IWebSocketAcceptContext
9+
public class OwinWebSocketAcceptContext : WebSocketAcceptContext
1010
{
1111
private IDictionary<string, object> _options;
1212

@@ -19,7 +19,7 @@ public OwinWebSocketAcceptContext(IDictionary<string, object> options)
1919
_options = options;
2020
}
2121

22-
public string SubProtocol
22+
public override string SubProtocol
2323
{
2424
get
2525
{

src/Microsoft.AspNet.Owin/WebSockets/WebSocketAcceptAdapter.cs

+4-4
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ namespace Microsoft.AspNet.Owin
2424
using WebSocketAcceptAlt =
2525
Func
2626
<
27-
IWebSocketAcceptContext, // WebSocket Accept parameters
27+
WebSocketAcceptContext, // WebSocket Accept parameters
2828
Task<WebSocket>
2929
>;
3030

@@ -65,11 +65,11 @@ public static AppFunc AdaptWebSockets(AppFunc next)
6565
await next(environment);
6666
if ((int)environment[OwinConstants.ResponseStatusCode] == 101 && adapter._callback != null)
6767
{
68-
IWebSocketAcceptContext acceptContext = null;
68+
WebSocketAcceptContext acceptContext = null;
6969
object obj;
70-
if (adapter._options != null && adapter._options.TryGetValue(typeof(IWebSocketAcceptContext).FullName, out obj))
70+
if (adapter._options != null && adapter._options.TryGetValue(typeof(WebSocketAcceptContext).FullName, out obj))
7171
{
72-
acceptContext = obj as IWebSocketAcceptContext;
72+
acceptContext = obj as WebSocketAcceptContext;
7373
}
7474
else if (adapter._options != null)
7575
{

0 commit comments

Comments
 (0)