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

Commit 114d834

Browse files
committed
Add Session feature, object model, etc..
1 parent dc600a6 commit 114d834

File tree

12 files changed

+267
-17
lines changed

12 files changed

+267
-17
lines changed

src/Microsoft.AspNet.FeatureModel/FeatureCollection.cs

Lines changed: 6 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -31,9 +31,8 @@ public object GetInterface()
3131
return GetInterface(null);
3232
}
3333

34-
public object GetInterface(Type type)
34+
public object GetInterface([NotNull] Type type)
3535
{
36-
if (type == null) throw new ArgumentNullException("type");
3736
object feature;
3837
if (_featureByFeatureType.TryGetValue(type, out feature))
3938
{
@@ -63,10 +62,8 @@ public object GetInterface(Type type)
6362
return null;
6463
}
6564

66-
void SetInterface(Type type, object feature)
65+
void SetInterface([NotNull] Type type, object feature)
6766
{
68-
if (type == null) throw new ArgumentNullException("type");
69-
7067
if (feature == null)
7168
{
7269
Remove(type);
@@ -153,27 +150,22 @@ public bool IsReadOnly
153150
get { return false; }
154151
}
155152

156-
public bool ContainsKey(Type key)
153+
public bool ContainsKey([NotNull] Type key)
157154
{
158-
if (key == null) throw new ArgumentNullException("key");
159155
return GetInterface(key) != null;
160156
}
161157

162-
public void Add(Type key, object value)
158+
public void Add([NotNull] Type key, [NotNull] object value)
163159
{
164-
if (key == null) throw new ArgumentNullException("key");
165-
if (value == null) throw new ArgumentNullException("value");
166160
if (ContainsKey(key))
167161
{
168162
throw new ArgumentException();
169163
}
170164
SetInterface(key, value);
171165
}
172166

173-
public bool Remove(Type key)
167+
public bool Remove([NotNull] Type key)
174168
{
175-
if (key == null) throw new ArgumentNullException("key");
176-
177169
lock (_containerSync)
178170
{
179171
Type priorFeatureType;
@@ -188,7 +180,7 @@ public bool Remove(Type key)
188180
}
189181
}
190182

191-
public bool TryGetValue(Type key, out object value)
183+
public bool TryGetValue([NotNull] Type key, out object value)
192184
{
193185
value = GetInterface(key);
194186
return value != null;
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
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+
6+
namespace Microsoft.AspNet.FeatureModel
7+
{
8+
[AttributeUsage(AttributeTargets.Parameter, AllowMultiple = false)]
9+
internal sealed class NotNullAttribute : Attribute
10+
{
11+
}
12+
}

src/Microsoft.AspNet.FeatureModel/project.json

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
{
22
"version": "1.0.0-*",
3-
"dependencies": {},
3+
"dependencies": {
4+
"Microsoft.Framework.Runtime.Interfaces": "1.0.0-*"
5+
},
46
"frameworks": {
57
"aspnet50": {},
68
"aspnetcore50": {
Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
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.Text;
6+
7+
namespace Microsoft.AspNet.Http
8+
{
9+
public static class SessionCollectionExtensions
10+
{
11+
public static void SetInt(this ISessionCollection session, string key, int value)
12+
{
13+
var bytes = new byte[]
14+
{
15+
(byte)(value >> 24),
16+
(byte)(0xFF & (value >> 16)),
17+
(byte)(0xFF & (value >> 8)),
18+
(byte)(0xFF & value)
19+
};
20+
session.Set(key, bytes);
21+
}
22+
23+
public static int? GetInt(this ISessionCollection session, string key)
24+
{
25+
var data = session.Get(key);
26+
if (data == null || data.Length < 4)
27+
{
28+
return null;
29+
}
30+
return data[0] << 24 | data[1] << 16 | data[2] << 8 | data[3];
31+
}
32+
33+
public static void SetString(this ISessionCollection session, string key, string value)
34+
{
35+
session.Set(key, Encoding.UTF8.GetBytes(value));
36+
}
37+
38+
public static string GetString(this ISessionCollection session, string key)
39+
{
40+
var data = session.Get(key);
41+
if (data == null)
42+
{
43+
return null;
44+
}
45+
return Encoding.UTF8.GetString(data);
46+
}
47+
48+
public static byte[] Get(this ISessionCollection session, string key)
49+
{
50+
byte[] value = null;
51+
session.TryGetValue(key, out value);
52+
return value;
53+
}
54+
55+
public static void Set(this ISessionCollection session, string key, byte[] value)
56+
{
57+
session.Set(key, new ArraySegment<byte>(value));
58+
}
59+
}
60+
}

src/Microsoft.AspNet.Http.Extensions/project.json

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,8 +9,6 @@
99
},
1010
"aspnetcore50" : {
1111
"dependencies": {
12-
"System.Reflection": "4.0.10-beta-*",
13-
"System.Reflection.Extensions": "4.0.0-beta-*",
1412
"System.Reflection.TypeExtensions": "4.0.0-beta-*",
1513
"System.Runtime": "4.0.20-beta-*"
1614
}

src/Microsoft.AspNet.Http/HttpContext.cs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,8 @@ public abstract class HttpContext : IDisposable
2828

2929
public abstract CancellationToken RequestAborted { get; }
3030

31+
public abstract ISessionCollection Session { get; }
32+
3133
public abstract bool IsWebSocketRequest { get; }
3234

3335
public abstract IList<string> WebSocketRequestedProtocols { get; }
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
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+
7+
namespace Microsoft.AspNet.Http
8+
{
9+
public interface ISessionCollection : IEnumerable<KeyValuePair<string, byte[]>>
10+
{
11+
byte[] this[string key] { get; set; }
12+
13+
bool TryGetValue(string key, out byte[] value);
14+
15+
void Set(string key, ArraySegment<byte> value);
16+
17+
void Remove(string key);
18+
19+
void Clear();
20+
}
21+
}
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
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 Microsoft.Framework.Runtime;
7+
8+
namespace Microsoft.AspNet.HttpFeature
9+
{
10+
[AssemblyNeutral]
11+
public interface ISession
12+
{
13+
void Load();
14+
15+
void Commit();
16+
17+
bool TryGetValue(string key, out byte[] value);
18+
19+
void Set(string key, ArraySegment<byte> value);
20+
21+
void Remove(string key);
22+
23+
void Clear();
24+
25+
IEnumerable<string> Keys { get; }
26+
}
27+
}
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
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 Microsoft.Framework.Runtime;
6+
7+
namespace Microsoft.AspNet.HttpFeature
8+
{
9+
[AssemblyNeutral]
10+
public interface ISessionFactory
11+
{
12+
bool IsAvailable { get; }
13+
14+
ISession Create();
15+
}
16+
}
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
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 Microsoft.Framework.Runtime;
5+
6+
namespace Microsoft.AspNet.HttpFeature
7+
{
8+
// TODO: Is there any reason not to flatten the Factory down into the Feature?
9+
[AssemblyNeutral]
10+
public interface ISessionFeature
11+
{
12+
ISessionFactory Factory { get; set; }
13+
14+
ISession Session { get; set; }
15+
}
16+
}
Lines changed: 75 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,75 @@
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;
6+
using System.Collections.Generic;
7+
using Microsoft.AspNet.Http;
8+
using Microsoft.AspNet.HttpFeature;
9+
10+
namespace Microsoft.AspNet.PipelineCore.Collections
11+
{
12+
public class SessionCollection : ISessionCollection
13+
{
14+
private readonly ISession _session;
15+
16+
public SessionCollection(ISession session)
17+
{
18+
_session = session;
19+
}
20+
21+
public byte[] this[string key]
22+
{
23+
get
24+
{
25+
byte[] value;
26+
TryGetValue(key, out value);
27+
return value;
28+
}
29+
set
30+
{
31+
if (value == null)
32+
{
33+
Remove(key);
34+
}
35+
else
36+
{
37+
Set(key, new ArraySegment<byte>(value));
38+
}
39+
}
40+
}
41+
42+
public bool TryGetValue(string key, out byte[] value)
43+
{
44+
return _session.TryGetValue(key, out value);
45+
}
46+
47+
public void Set(string key, ArraySegment<byte> value)
48+
{
49+
_session.Set(key, value);
50+
}
51+
52+
public void Remove(string key)
53+
{
54+
_session.Remove(key);
55+
}
56+
57+
public void Clear()
58+
{
59+
_session.Clear();
60+
}
61+
62+
IEnumerator IEnumerable.GetEnumerator()
63+
{
64+
return GetEnumerator();
65+
}
66+
67+
public IEnumerator<KeyValuePair<string, byte[]>> GetEnumerator()
68+
{
69+
foreach (var key in _session.Keys)
70+
{
71+
yield return new KeyValuePair<string, byte[]>(key, this[key]);
72+
}
73+
}
74+
}
75+
}

src/Microsoft.AspNet.PipelineCore/DefaultHttpContext.cs

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414
using Microsoft.AspNet.Http.Security;
1515
using Microsoft.AspNet.HttpFeature;
1616
using Microsoft.AspNet.HttpFeature.Security;
17+
using Microsoft.AspNet.PipelineCore.Collections;
1718
using Microsoft.AspNet.PipelineCore.Infrastructure;
1819
using Microsoft.AspNet.PipelineCore.Security;
1920

@@ -31,6 +32,7 @@ public class DefaultHttpContext : HttpContext
3132
private FeatureReference<IHttpAuthenticationFeature> _authentication;
3233
private FeatureReference<IHttpRequestLifetimeFeature> _lifetime;
3334
private FeatureReference<IHttpWebSocketFeature> _webSockets;
35+
private FeatureReference<ISessionFeature> _session;
3436
private IFeatureCollection _features;
3537

3638
public DefaultHttpContext()
@@ -51,6 +53,7 @@ public DefaultHttpContext(IFeatureCollection features)
5153
_authentication = FeatureReference<IHttpAuthenticationFeature>.Default;
5254
_lifetime = FeatureReference<IHttpRequestLifetimeFeature>.Default;
5355
_webSockets = FeatureReference<IHttpWebSocketFeature>.Default;
56+
_session = FeatureReference<ISessionFeature>.Default;
5457
}
5558

5659
IItemsFeature ItemsFeature
@@ -78,6 +81,11 @@ private IHttpWebSocketFeature WebSocketFeature
7881
get { return _webSockets.Fetch(_features); }
7982
}
8083

84+
private ISessionFeature SessionFeature
85+
{
86+
get { return _session.Fetch(_features); }
87+
}
88+
8189
public override HttpRequest Request { get { return _request; } }
8290

8391
public override HttpResponse Response { get { return _response; } }
@@ -129,6 +137,27 @@ public override CancellationToken RequestAborted
129137
}
130138
}
131139

140+
public override ISessionCollection Session
141+
{
142+
get
143+
{
144+
var feature = SessionFeature;
145+
if (feature == null)
146+
{
147+
throw new InvalidOperationException("Session has not been configured for this application or request.");
148+
}
149+
if (feature.Session == null)
150+
{
151+
if (feature.Factory == null)
152+
{
153+
throw new InvalidOperationException("No ISessionFactory available to create the ISession.");
154+
}
155+
feature.Session = feature.Factory.Create();
156+
}
157+
return new SessionCollection(feature.Session);
158+
}
159+
}
160+
132161
public override bool IsWebSocketRequest
133162
{
134163
get

0 commit comments

Comments
 (0)