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

Make more properties settable #305

Merged
merged 1 commit into from
May 12, 2015
Merged
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
8 changes: 4 additions & 4 deletions src/Microsoft.AspNet.Http.Abstractions/HttpContext.cs
Original file line number Diff line number Diff line change
Expand Up @@ -17,22 +17,22 @@ public abstract class HttpContext : IDisposable

public abstract ConnectionInfo Connection { get; }

public abstract WebSocketManager WebSockets { get; }

public abstract AuthenticationManager Authentication { get; }

public abstract ClaimsPrincipal User { get; set; }

public abstract IDictionary<object, object> Items { get; }
public abstract IDictionary<object, object> Items { get; set; }

public abstract IServiceProvider ApplicationServices { get; set; }

public abstract IServiceProvider RequestServices { get; set; }

public abstract CancellationToken RequestAborted { get; }
public abstract CancellationToken RequestAborted { get; set; }

public abstract ISessionCollection Session { get; }

public abstract WebSocketManager WebSockets { get; }

public abstract void Abort();

public abstract void Dispose();
Expand Down
6 changes: 3 additions & 3 deletions src/Microsoft.AspNet.Http.Abstractions/HttpRequest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ public abstract class HttpRequest
/// Returns true if the owin.RequestScheme is https.
/// </summary>
/// <returns>true if this request is using https; otherwise, false.</returns>
public abstract bool IsHttps { get; }
public abstract bool IsHttps { get; set; }

/// <summary>
/// Gets or set the Host header. May include the port.
Expand Down Expand Up @@ -57,7 +57,7 @@ public abstract class HttpRequest
/// Gets the query value collection parsed from owin.RequestQueryString.
/// </summary>
/// <returns>The query value collection parsed from owin.RequestQueryString.</returns>
public abstract IReadableStringCollection Query { get; }
public abstract IReadableStringCollection Query { get; set; }

/// <summary>
/// Gets or set the owin.RequestProtocol.
Expand All @@ -75,7 +75,7 @@ public abstract class HttpRequest
/// Gets the collection of Cookies for this request.
/// </summary>
/// <returns>The collection of Cookies for this request.</returns>
public abstract IReadableStringCollection Cookies { get; }
public abstract IReadableStringCollection Cookies { get; set; }

/// <summary>
/// Gets or sets the Content-Length header
Expand Down
27 changes: 26 additions & 1 deletion src/Microsoft.AspNet.Http.Abstractions/QueryString.cs
Original file line number Diff line number Diff line change
Expand Up @@ -111,6 +111,7 @@ public static QueryString FromUriComponent([NotNull] Uri uri)
/// </summary>
/// <param name="name">The un-encoded parameter name</param>
/// <param name="value">The un-encoded parameter value</param>
/// <returns>The resulting QueryString</returns>
public static QueryString Create(string name, string value)
{
return new QueryString("?" + UrlEncoder.Default.UrlEncode(name) + '=' + UrlEncoder.Default.UrlEncode(value));
Expand All @@ -120,7 +121,7 @@ public static QueryString Create(string name, string value)
/// Creates a query string composed from the given name value pairs.
/// </summary>
/// <param name="parameters"></param>
/// <returns></returns>
/// <returns>The resulting QueryString</returns>
public static QueryString Create(IEnumerable<KeyValuePair<string, string>> parameters)
{
var builder = new StringBuilder();
Expand All @@ -137,6 +138,30 @@ public static QueryString Create(IEnumerable<KeyValuePair<string, string>> param
return new QueryString(builder.ToString());
}

/// <summary>
/// Creates a query string composed from the given name value pairs.
/// </summary>
/// <param name="parameters"></param>
/// <returns>The resulting QueryString</returns>
public static QueryString Create(IEnumerable<KeyValuePair<string, string[]>> parameters)
{
var builder = new StringBuilder();
bool first = true;
foreach (var pair in parameters)
{
foreach (var value in pair.Value)
{
builder.Append(first ? "?" : "&");
first = false;
builder.Append(UrlEncoder.Default.UrlEncode(pair.Key));
builder.Append("=");
builder.Append(UrlEncoder.Default.UrlEncode(value));
}
}

return new QueryString(builder.ToString());
}

public QueryString Add(QueryString other)
{
if (!HasValue || Value.Equals("?", StringComparison.Ordinal))
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ namespace Microsoft.AspNet.Http.Features
{
public interface IHttpRequestLifetimeFeature
{
CancellationToken RequestAborted { get; }
CancellationToken RequestAborted { get; set; }
void Abort();
}
}
3 changes: 2 additions & 1 deletion src/Microsoft.AspNet.Http/Constants.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,8 @@ namespace Microsoft.AspNet.Http.Internal
{
internal static class Constants
{
internal const string Https = "HTTPS";
internal const string Http = "http";
internal const string Https = "https";

internal static class BuilderProperties
{
Expand Down
22 changes: 5 additions & 17 deletions src/Microsoft.AspNet.Http/DefaultHttpContext.cs
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,7 @@ private IHttpAuthenticationFeature HttpAuthenticationFeature

private IHttpRequestLifetimeFeature LifetimeFeature
{
get { return _lifetime.Fetch(_features); }
get { return _lifetime.Fetch(_features) ?? _lifetime.Update(_features, new HttpRequestLifetimeFeature()); }
}

private ISessionFeature SessionFeature
Expand Down Expand Up @@ -103,6 +103,7 @@ public override ClaimsPrincipal User
public override IDictionary<object, object> Items
{
get { return ItemsFeature.Items; }
set { ItemsFeature.Items = value; }
}

public override IServiceProvider ApplicationServices
Expand All @@ -117,19 +118,10 @@ public override IServiceProvider RequestServices
set { ServiceProvidersFeature.RequestServices = value; }
}

public int Revision { get { return _features.Revision; } }

public override CancellationToken RequestAborted
{
get
{
var lifetime = LifetimeFeature;
if (lifetime != null)
{
return lifetime.RequestAborted;
}
return CancellationToken.None;
}
get { return LifetimeFeature.RequestAborted; }
set { LifetimeFeature.RequestAborted = value; }
}

public override ISessionCollection Session
Expand Down Expand Up @@ -167,11 +159,7 @@ public override WebSocketManager WebSockets

public override void Abort()
{
var lifetime = LifetimeFeature;
if (lifetime != null)
{
lifetime.Abort();
}
LifetimeFeature.Abort();
}

public override void Dispose()
Expand Down
3 changes: 3 additions & 0 deletions src/Microsoft.AspNet.Http/DefaultHttpRequest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -101,6 +101,7 @@ public override string Scheme
public override bool IsHttps
{
get { return string.Equals(Constants.Https, Scheme, StringComparison.OrdinalIgnoreCase); }
set { Scheme = value ? Constants.Https : Constants.Http; }
}

public override HostString Host
Expand All @@ -112,6 +113,7 @@ public override HostString Host
public override IReadableStringCollection Query
{
get { return QueryFeature.Query; }
set { QueryFeature.Query = value; }
}

public override string Protocol
Expand All @@ -128,6 +130,7 @@ public override IHeaderDictionary Headers
public override IReadableStringCollection Cookies
{
get { return RequestCookiesFeature.Cookies; }
set { RequestCookiesFeature.Cookies = value; }
}

public override string ContentType
Expand Down
17 changes: 17 additions & 0 deletions src/Microsoft.AspNet.Http/Features/HttpRequestLifetimeFeature.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
// Copyright (c) .NET Foundation. All rights reserved.
// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information.

using System.Threading;
using Microsoft.AspNet.Http.Features;

namespace Microsoft.AspNet.Http.Features.Internal
{
public class HttpRequestLifetimeFeature : IHttpRequestLifetimeFeature
{
public CancellationToken RequestAborted { get; set; }

public void Abort()
{
}
}
}
2 changes: 1 addition & 1 deletion src/Microsoft.AspNet.Http/Features/IItemsFeature.cs
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,6 @@ namespace Microsoft.AspNet.Http.Features.Internal
{
public interface IItemsFeature
{
IDictionary<object, object> Items { get; }
IDictionary<object, object> Items { get; set; }
}
}
2 changes: 1 addition & 1 deletion src/Microsoft.AspNet.Http/Features/IQueryFeature.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,6 @@ namespace Microsoft.AspNet.Http.Features.Internal
{
public interface IQueryFeature
{
IReadableStringCollection Query { get; }
IReadableStringCollection Query { get; set; }
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,6 @@ namespace Microsoft.AspNet.Http.Features.Internal
{
public interface IRequestCookiesFeature
{
IReadableStringCollection Cookies { get; }
IReadableStringCollection Cookies { get; set; }
}
}
2 changes: 1 addition & 1 deletion src/Microsoft.AspNet.Http/Features/ItemsFeature.cs
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,6 @@ public ItemsFeature()
Items = new ItemsDictionary();
}

public IDictionary<object, object> Items { get; private set; }
public IDictionary<object, object> Items { get; set; }
}
}
12 changes: 11 additions & 1 deletion src/Microsoft.AspNet.Http/Features/QueryFeature.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
// Copyright (c) .NET Foundation. All rights reserved.
// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information.

using System;
using System.Collections.Generic;
using Microsoft.AspNet.FeatureModel;
using Microsoft.AspNet.Http.Internal;
Expand Down Expand Up @@ -41,13 +42,22 @@ public IReadableStringCollection Query
}

var queryString = _request.Fetch(_features).QueryString;
if (_query == null || _queryString != queryString)
if (_query == null || !string.Equals(_queryString, queryString, StringComparison.Ordinal))
{
_queryString = queryString;
_query = new ReadableStringCollection(QueryHelpers.ParseQuery(queryString));
}
return _query;
}
set
{
_query = value;
if (_features != null)
{
_queryString = _query == null ? string.Empty : QueryString.Create(_query).ToString();
_request.Fetch(_features).QueryString = _queryString;
}
}
}
}
}
32 changes: 25 additions & 7 deletions src/Microsoft.AspNet.Http/Features/RequestCookiesFeature.cs
Original file line number Diff line number Diff line change
Expand Up @@ -50,19 +50,37 @@ public IReadableStringCollection Cookies
values = new string[0];
}

if (_cookiesCollection == null)
if (_cookieHeaders == null || !Enumerable.SequenceEqual(_cookieHeaders, values, StringComparer.Ordinal))
{
_cookieHeaders = values;
_cookiesCollection = new RequestCookiesCollection();
if (_cookiesCollection == null)
{
_cookiesCollection = new RequestCookiesCollection();
_cookies = _cookiesCollection;
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Q: missing a cast?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nope, RequestCookiesCollection implements IReadableStringCollection.

}
_cookiesCollection.Reparse(values);
}
else if (!Enumerable.SequenceEqual(_cookieHeaders, values, StringComparer.Ordinal))

return _cookies;
}
set
{
_cookies = value;
_cookieHeaders = null;
_cookiesCollection = _cookies as RequestCookiesCollection;
if (_cookies != null && _features != null)
{
_cookieHeaders = values;
_cookiesCollection.Reparse(values);
var headers = new List<string>();
foreach (var pair in _cookies)
{
foreach (var cookieValue in pair.Value)
{
headers.Add(new CookieHeaderValue(pair.Key, cookieValue).ToString());
}
}
_cookieHeaders = headers.ToArray();
_request.Fetch(_features).Headers[HeaderNames.Cookie] = _cookieHeaders;
}

return _cookiesCollection;
}
}
}
Expand Down
1 change: 1 addition & 0 deletions src/Microsoft.AspNet.Owin/OwinFeatureCollection.cs
Original file line number Diff line number Diff line change
Expand Up @@ -247,6 +247,7 @@ async Task<X509Certificate2> ITlsConnectionFeature.GetClientCertificateAsync(Can
CancellationToken IHttpRequestLifetimeFeature.RequestAborted
{
get { return Prop<CancellationToken>(OwinConstants.CallCancelled); }
set { Prop(OwinConstants.CallCancelled, value); }
}

void IHttpRequestLifetimeFeature.Abort()
Expand Down
Loading