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

Commit dce1d0e

Browse files
committed
#272 Make more properties settable (Items, RequestAborted, IsHttps, Query, Cookies).
1 parent c4df4a0 commit dce1d0e

18 files changed

+307
-124
lines changed

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

+4-4
Original file line numberDiff line numberDiff line change
@@ -17,22 +17,22 @@ public abstract class HttpContext : IDisposable
1717

1818
public abstract ConnectionInfo Connection { get; }
1919

20+
public abstract WebSocketManager WebSockets { get; }
21+
2022
public abstract AuthenticationManager Authentication { get; }
2123

2224
public abstract ClaimsPrincipal User { get; set; }
2325

24-
public abstract IDictionary<object, object> Items { get; }
26+
public abstract IDictionary<object, object> Items { get; set; }
2527

2628
public abstract IServiceProvider ApplicationServices { get; set; }
2729

2830
public abstract IServiceProvider RequestServices { get; set; }
2931

30-
public abstract CancellationToken RequestAborted { get; }
32+
public abstract CancellationToken RequestAborted { get; set; }
3133

3234
public abstract ISessionCollection Session { get; }
3335

34-
public abstract WebSocketManager WebSockets { get; }
35-
3636
public abstract void Abort();
3737

3838
public abstract void Dispose();

src/Microsoft.AspNet.Http.Abstractions/HttpRequest.cs

+3-3
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ public abstract class HttpRequest
2727
/// Returns true if the owin.RequestScheme is https.
2828
/// </summary>
2929
/// <returns>true if this request is using https; otherwise, false.</returns>
30-
public abstract bool IsHttps { get; }
30+
public abstract bool IsHttps { get; set; }
3131

3232
/// <summary>
3333
/// Gets or set the Host header. May include the port.
@@ -57,7 +57,7 @@ public abstract class HttpRequest
5757
/// Gets the query value collection parsed from owin.RequestQueryString.
5858
/// </summary>
5959
/// <returns>The query value collection parsed from owin.RequestQueryString.</returns>
60-
public abstract IReadableStringCollection Query { get; }
60+
public abstract IReadableStringCollection Query { get; set; }
6161

6262
/// <summary>
6363
/// Gets or set the owin.RequestProtocol.
@@ -75,7 +75,7 @@ public abstract class HttpRequest
7575
/// Gets the collection of Cookies for this request.
7676
/// </summary>
7777
/// <returns>The collection of Cookies for this request.</returns>
78-
public abstract IReadableStringCollection Cookies { get; }
78+
public abstract IReadableStringCollection Cookies { get; set; }
7979

8080
/// <summary>
8181
/// Gets or sets the Content-Length header

src/Microsoft.AspNet.Http.Abstractions/QueryString.cs

+26-1
Original file line numberDiff line numberDiff line change
@@ -111,6 +111,7 @@ public static QueryString FromUriComponent([NotNull] Uri uri)
111111
/// </summary>
112112
/// <param name="name">The un-encoded parameter name</param>
113113
/// <param name="value">The un-encoded parameter value</param>
114+
/// <returns>The resulting QueryString</returns>
114115
public static QueryString Create(string name, string value)
115116
{
116117
return new QueryString("?" + UrlEncoder.Default.UrlEncode(name) + '=' + UrlEncoder.Default.UrlEncode(value));
@@ -120,7 +121,7 @@ public static QueryString Create(string name, string value)
120121
/// Creates a query string composed from the given name value pairs.
121122
/// </summary>
122123
/// <param name="parameters"></param>
123-
/// <returns></returns>
124+
/// <returns>The resulting QueryString</returns>
124125
public static QueryString Create(IEnumerable<KeyValuePair<string, string>> parameters)
125126
{
126127
var builder = new StringBuilder();
@@ -137,6 +138,30 @@ public static QueryString Create(IEnumerable<KeyValuePair<string, string>> param
137138
return new QueryString(builder.ToString());
138139
}
139140

141+
/// <summary>
142+
/// Creates a query string composed from the given name value pairs.
143+
/// </summary>
144+
/// <param name="parameters"></param>
145+
/// <returns>The resulting QueryString</returns>
146+
public static QueryString Create(IEnumerable<KeyValuePair<string, string[]>> parameters)
147+
{
148+
var builder = new StringBuilder();
149+
bool first = true;
150+
foreach (var pair in parameters)
151+
{
152+
foreach (var value in pair.Value)
153+
{
154+
builder.Append(first ? "?" : "&");
155+
first = false;
156+
builder.Append(UrlEncoder.Default.UrlEncode(pair.Key));
157+
builder.Append("=");
158+
builder.Append(UrlEncoder.Default.UrlEncode(value));
159+
}
160+
}
161+
162+
return new QueryString(builder.ToString());
163+
}
164+
140165
public QueryString Add(QueryString other)
141166
{
142167
if (!HasValue || Value.Equals("?", StringComparison.Ordinal))

src/Microsoft.AspNet.Http.Features/IHttpRequestLifetimeFeature.cs

+1-1
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ namespace Microsoft.AspNet.Http.Features
77
{
88
public interface IHttpRequestLifetimeFeature
99
{
10-
CancellationToken RequestAborted { get; }
10+
CancellationToken RequestAborted { get; set; }
1111
void Abort();
1212
}
1313
}

src/Microsoft.AspNet.Http/Constants.cs

+2-1
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,8 @@ namespace Microsoft.AspNet.Http.Internal
55
{
66
internal static class Constants
77
{
8-
internal const string Https = "HTTPS";
8+
internal const string Http = "http";
9+
internal const string Https = "https";
910

1011
internal static class BuilderProperties
1112
{

src/Microsoft.AspNet.Http/DefaultHttpContext.cs

+5-17
Original file line numberDiff line numberDiff line change
@@ -69,7 +69,7 @@ private IHttpAuthenticationFeature HttpAuthenticationFeature
6969

7070
private IHttpRequestLifetimeFeature LifetimeFeature
7171
{
72-
get { return _lifetime.Fetch(_features); }
72+
get { return _lifetime.Fetch(_features) ?? _lifetime.Update(_features, new HttpRequestLifetimeFeature()); }
7373
}
7474

7575
private ISessionFeature SessionFeature
@@ -103,6 +103,7 @@ public override ClaimsPrincipal User
103103
public override IDictionary<object, object> Items
104104
{
105105
get { return ItemsFeature.Items; }
106+
set { ItemsFeature.Items = value; }
106107
}
107108

108109
public override IServiceProvider ApplicationServices
@@ -117,19 +118,10 @@ public override IServiceProvider RequestServices
117118
set { ServiceProvidersFeature.RequestServices = value; }
118119
}
119120

120-
public int Revision { get { return _features.Revision; } }
121-
122121
public override CancellationToken RequestAborted
123122
{
124-
get
125-
{
126-
var lifetime = LifetimeFeature;
127-
if (lifetime != null)
128-
{
129-
return lifetime.RequestAborted;
130-
}
131-
return CancellationToken.None;
132-
}
123+
get { return LifetimeFeature.RequestAborted; }
124+
set { LifetimeFeature.RequestAborted = value; }
133125
}
134126

135127
public override ISessionCollection Session
@@ -167,11 +159,7 @@ public override WebSocketManager WebSockets
167159

168160
public override void Abort()
169161
{
170-
var lifetime = LifetimeFeature;
171-
if (lifetime != null)
172-
{
173-
lifetime.Abort();
174-
}
162+
LifetimeFeature.Abort();
175163
}
176164

177165
public override void Dispose()

src/Microsoft.AspNet.Http/DefaultHttpRequest.cs

+3
Original file line numberDiff line numberDiff line change
@@ -101,6 +101,7 @@ public override string Scheme
101101
public override bool IsHttps
102102
{
103103
get { return string.Equals(Constants.Https, Scheme, StringComparison.OrdinalIgnoreCase); }
104+
set { Scheme = value ? Constants.Https : Constants.Http; }
104105
}
105106

106107
public override HostString Host
@@ -112,6 +113,7 @@ public override HostString Host
112113
public override IReadableStringCollection Query
113114
{
114115
get { return QueryFeature.Query; }
116+
set { QueryFeature.Query = value; }
115117
}
116118

117119
public override string Protocol
@@ -128,6 +130,7 @@ public override IHeaderDictionary Headers
128130
public override IReadableStringCollection Cookies
129131
{
130132
get { return RequestCookiesFeature.Cookies; }
133+
set { RequestCookiesFeature.Cookies = value; }
131134
}
132135

133136
public override string ContentType
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
// Copyright (c) .NET Foundation. 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.Threading;
5+
using Microsoft.AspNet.Http.Features;
6+
7+
namespace Microsoft.AspNet.Http.Features.Internal
8+
{
9+
public class HttpRequestLifetimeFeature : IHttpRequestLifetimeFeature
10+
{
11+
public CancellationToken RequestAborted { get; set; }
12+
13+
public void Abort()
14+
{
15+
}
16+
}
17+
}

src/Microsoft.AspNet.Http/Features/IItemsFeature.cs

+1-1
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,6 @@ namespace Microsoft.AspNet.Http.Features.Internal
77
{
88
public interface IItemsFeature
99
{
10-
IDictionary<object, object> Items { get; }
10+
IDictionary<object, object> Items { get; set; }
1111
}
1212
}

src/Microsoft.AspNet.Http/Features/IQueryFeature.cs

+1-1
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,6 @@ namespace Microsoft.AspNet.Http.Features.Internal
55
{
66
public interface IQueryFeature
77
{
8-
IReadableStringCollection Query { get; }
8+
IReadableStringCollection Query { get; set; }
99
}
1010
}

src/Microsoft.AspNet.Http/Features/IRequestCookiesFeature.cs

+1-1
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,6 @@ namespace Microsoft.AspNet.Http.Features.Internal
55
{
66
public interface IRequestCookiesFeature
77
{
8-
IReadableStringCollection Cookies { get; }
8+
IReadableStringCollection Cookies { get; set; }
99
}
1010
}

src/Microsoft.AspNet.Http/Features/ItemsFeature.cs

+1-1
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,6 @@ public ItemsFeature()
1313
Items = new ItemsDictionary();
1414
}
1515

16-
public IDictionary<object, object> Items { get; private set; }
16+
public IDictionary<object, object> Items { get; set; }
1717
}
1818
}

src/Microsoft.AspNet.Http/Features/QueryFeature.cs

+11-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
// Copyright (c) .NET Foundation. All rights reserved.
22
// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information.
33

4+
using System;
45
using System.Collections.Generic;
56
using Microsoft.AspNet.FeatureModel;
67
using Microsoft.AspNet.Http.Internal;
@@ -41,13 +42,22 @@ public IReadableStringCollection Query
4142
}
4243

4344
var queryString = _request.Fetch(_features).QueryString;
44-
if (_query == null || _queryString != queryString)
45+
if (_query == null || !string.Equals(_queryString, queryString, StringComparison.Ordinal))
4546
{
4647
_queryString = queryString;
4748
_query = new ReadableStringCollection(QueryHelpers.ParseQuery(queryString));
4849
}
4950
return _query;
5051
}
52+
set
53+
{
54+
_query = value;
55+
if (_features != null)
56+
{
57+
_queryString = _query == null ? string.Empty : QueryString.Create(_query).ToString();
58+
_request.Fetch(_features).QueryString = _queryString;
59+
}
60+
}
5161
}
5262
}
5363
}

src/Microsoft.AspNet.Http/Features/RequestCookiesFeature.cs

+25-7
Original file line numberDiff line numberDiff line change
@@ -50,19 +50,37 @@ public IReadableStringCollection Cookies
5050
values = new string[0];
5151
}
5252

53-
if (_cookiesCollection == null)
53+
if (_cookieHeaders == null || !Enumerable.SequenceEqual(_cookieHeaders, values, StringComparer.Ordinal))
5454
{
5555
_cookieHeaders = values;
56-
_cookiesCollection = new RequestCookiesCollection();
56+
if (_cookiesCollection == null)
57+
{
58+
_cookiesCollection = new RequestCookiesCollection();
59+
_cookies = _cookiesCollection;
60+
}
5761
_cookiesCollection.Reparse(values);
5862
}
59-
else if (!Enumerable.SequenceEqual(_cookieHeaders, values, StringComparer.Ordinal))
63+
64+
return _cookies;
65+
}
66+
set
67+
{
68+
_cookies = value;
69+
_cookieHeaders = null;
70+
_cookiesCollection = _cookies as RequestCookiesCollection;
71+
if (_cookies != null && _features != null)
6072
{
61-
_cookieHeaders = values;
62-
_cookiesCollection.Reparse(values);
73+
var headers = new List<string>();
74+
foreach (var pair in _cookies)
75+
{
76+
foreach (var cookieValue in pair.Value)
77+
{
78+
headers.Add(new CookieHeaderValue(pair.Key, cookieValue).ToString());
79+
}
80+
}
81+
_cookieHeaders = headers.ToArray();
82+
_request.Fetch(_features).Headers[HeaderNames.Cookie] = _cookieHeaders;
6383
}
64-
65-
return _cookiesCollection;
6684
}
6785
}
6886
}

src/Microsoft.AspNet.Owin/OwinFeatureCollection.cs

+1
Original file line numberDiff line numberDiff line change
@@ -247,6 +247,7 @@ async Task<X509Certificate2> ITlsConnectionFeature.GetClientCertificateAsync(Can
247247
CancellationToken IHttpRequestLifetimeFeature.RequestAborted
248248
{
249249
get { return Prop<CancellationToken>(OwinConstants.CallCancelled); }
250+
set { Prop(OwinConstants.CallCancelled, value); }
250251
}
251252

252253
void IHttpRequestLifetimeFeature.Abort()

0 commit comments

Comments
 (0)