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

Commit

Permalink
#426 Move IHeaderDictionary to Features to reduce wrapping.
Browse files Browse the repository at this point in the history
  • Loading branch information
Tratcher committed Oct 8, 2015
1 parent 8ecb147 commit 3741d38
Show file tree
Hide file tree
Showing 15 changed files with 61 additions and 69 deletions.
34 changes: 0 additions & 34 deletions src/Microsoft.AspNet.Http.Abstractions/IHeaderDictionary.cs

This file was deleted.

21 changes: 21 additions & 0 deletions src/Microsoft.AspNet.Http.Features/IHeaderDictionary.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
// 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.Collections.Generic;
using Microsoft.Extensions.Primitives;

namespace Microsoft.AspNet.Http
{
/// <summary>
/// Represents request and response headers
/// </summary>
public interface IHeaderDictionary : IDictionary<string, StringValues>
{
/// <summary>
/// IHeaderDictionary has a different indexer contract than IDictionary, where it will return StringValues.Empty for missing entries.
/// </summary>
/// <param name="key"></param>
/// <returns>The stored value, or StringValues.Empty if the key is not present.</returns>
new StringValues this[string key] { get; set; }
}
}
4 changes: 1 addition & 3 deletions src/Microsoft.AspNet.Http.Features/IHttpRequestFeature.cs
Original file line number Diff line number Diff line change
@@ -1,9 +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.Collections.Generic;
using System.IO;
using Microsoft.Extensions.Primitives;

namespace Microsoft.AspNet.Http.Features
{
Expand All @@ -15,7 +13,7 @@ public interface IHttpRequestFeature
string PathBase { get; set; }
string Path { get; set; }
string QueryString { get; set; }
IDictionary<string, StringValues> Headers { get; set; }
IHeaderDictionary Headers { get; set; }
Stream Body { get; set; }
}
}
4 changes: 1 addition & 3 deletions src/Microsoft.AspNet.Http.Features/IHttpResponseFeature.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,18 +2,16 @@
// 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 System.IO;
using System.Threading.Tasks;
using Microsoft.Extensions.Primitives;

namespace Microsoft.AspNet.Http.Features
{
public interface IHttpResponseFeature
{
int StatusCode { get; set; }
string ReasonPhrase { get; set; }
IDictionary<string, StringValues> Headers { get; set; }
IHeaderDictionary Headers { get; set; }
Stream Body { get; set; }
bool HasStarted { get; }
void OnStarting(Func<object, Task> callback, object state);
Expand Down
2 changes: 1 addition & 1 deletion src/Microsoft.AspNet.Http/DefaultHttpRequest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -123,7 +123,7 @@ public override string Protocol

public override IHeaderDictionary Headers
{
get { return new HeaderDictionary(HttpRequestFeature.Headers); }
get { return HttpRequestFeature.Headers; }
}

public override IReadableStringCollection Cookies
Expand Down
2 changes: 1 addition & 1 deletion src/Microsoft.AspNet.Http/DefaultHttpResponse.cs
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ public override int StatusCode

public override IHeaderDictionary Headers
{
get { return new HeaderDictionary(HttpResponseFeature.Headers); }
get { return HttpResponseFeature.Headers; }
}

public override Stream Body
Expand Down
8 changes: 3 additions & 5 deletions src/Microsoft.AspNet.Http/Features/HttpRequestFeature.cs
Original file line number Diff line number Diff line change
@@ -1,18 +1,16 @@
// 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 System.IO;
using Microsoft.Extensions.Primitives;
using Microsoft.AspNet.Http.Internal;

namespace Microsoft.AspNet.Http.Features.Internal
{
public class HttpRequestFeature : IHttpRequestFeature
{
public HttpRequestFeature()
{
Headers = new Dictionary<string, StringValues>(StringComparer.OrdinalIgnoreCase);
Headers = new HeaderDictionary();
Body = Stream.Null;
Protocol = string.Empty;
Scheme = string.Empty;
Expand All @@ -28,7 +26,7 @@ public HttpRequestFeature()
public string PathBase { get; set; }
public string Path { get; set; }
public string QueryString { get; set; }
public IDictionary<string, StringValues> Headers { get; set; }
public IHeaderDictionary Headers { get; set; }
public Stream Body { get; set; }
}
}
7 changes: 3 additions & 4 deletions src/Microsoft.AspNet.Http/Features/HttpResponseFeature.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,9 @@
// 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 System.IO;
using System.Threading.Tasks;
using Microsoft.Extensions.Primitives;
using Microsoft.AspNet.Http.Internal;

namespace Microsoft.AspNet.Http.Features.Internal
{
Expand All @@ -14,15 +13,15 @@ public class HttpResponseFeature : IHttpResponseFeature
public HttpResponseFeature()
{
StatusCode = 200;
Headers = new Dictionary<string, StringValues>(StringComparer.OrdinalIgnoreCase);
Headers = new HeaderDictionary();
Body = Stream.Null;
}

public int StatusCode { get; set; }

public string ReasonPhrase { get; set; }

public IDictionary<string, StringValues> Headers { get; set; }
public IHeaderDictionary Headers { get; set; }

public Stream Body { get; set; }

Expand Down
7 changes: 4 additions & 3 deletions src/Microsoft.AspNet.Owin/DictionaryStringArrayWrapper.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,18 +4,19 @@
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using Microsoft.AspNet.Http;
using Microsoft.Extensions.Primitives;

namespace Microsoft.AspNet.Owin
{
internal class DictionaryStringArrayWrapper : IDictionary<string, string[]>
{
public DictionaryStringArrayWrapper(IDictionary<string, StringValues> inner)
public DictionaryStringArrayWrapper(IHeaderDictionary inner)
{
Inner = inner;
}

public readonly IDictionary<string, StringValues> Inner;
public readonly IHeaderDictionary Inner;

private KeyValuePair<string, StringValues> Convert(KeyValuePair<string, string[]> item) => new KeyValuePair<string, StringValues>(item.Key, item.Value);

Expand All @@ -27,7 +28,7 @@ public DictionaryStringArrayWrapper(IDictionary<string, StringValues> inner)

string[] IDictionary<string, string[]>.this[string key]
{
get { return Inner[key]; }
get { return ((IDictionary<string, StringValues>)Inner)[key]; }
set { Inner[key] = value; }
}

Expand Down
13 changes: 12 additions & 1 deletion src/Microsoft.AspNet.Owin/DictionaryStringValuesWrapper.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,12 @@
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using Microsoft.AspNet.Http;
using Microsoft.Extensions.Primitives;

namespace Microsoft.AspNet.Owin
{
internal class DictionaryStringValuesWrapper : IDictionary<string, StringValues>
internal class DictionaryStringValuesWrapper : IHeaderDictionary
{
public DictionaryStringValuesWrapper(IDictionary<string, string[]> inner)
{
Expand All @@ -25,6 +26,16 @@ public DictionaryStringValuesWrapper(IDictionary<string, string[]> inner)

private string[] Convert(StringValues item) => item;

StringValues IHeaderDictionary.this[string key]
{
get
{
string[] values;
return Inner.TryGetValue(key, out values) ? values : null;
}
set { Inner[key] = value; }
}

StringValues IDictionary<string, StringValues>.this[string key]
{
get { return Inner[key]; }
Expand Down
4 changes: 2 additions & 2 deletions src/Microsoft.AspNet.Owin/OwinEnvironment.cs
Original file line number Diff line number Diff line change
Expand Up @@ -56,13 +56,13 @@ public OwinEnvironment(HttpContext context)
{ OwinConstants.RequestPath, new FeatureMap<IHttpRequestFeature>(feature => feature.Path, () => string.Empty, (feature, value) => feature.Path = Convert.ToString(value)) },
{ OwinConstants.RequestQueryString, new FeatureMap<IHttpRequestFeature>(feature => Utilities.RemoveQuestionMark(feature.QueryString), () => string.Empty,
(feature, value) => feature.QueryString = Utilities.AddQuestionMark(Convert.ToString(value))) },
{ OwinConstants.RequestHeaders, new FeatureMap<IHttpRequestFeature>(feature => Utilities.MakeDictionaryStringArray(feature.Headers), (feature, value) => feature.Headers = Utilities.MakeDictionaryStringValues((IDictionary<string, string[]>)value)) },
{ OwinConstants.RequestHeaders, new FeatureMap<IHttpRequestFeature>(feature => Utilities.MakeDictionaryStringArray(feature.Headers), (feature, value) => feature.Headers = Utilities.MakeHeaderDictionary((IDictionary<string, string[]>)value)) },
{ OwinConstants.RequestBody, new FeatureMap<IHttpRequestFeature>(feature => feature.Body, () => Stream.Null, (feature, value) => feature.Body = (Stream)value) },
{ OwinConstants.RequestUser, new FeatureMap<IHttpAuthenticationFeature>(feature => feature.User, () => null, (feature, value) => feature.User = (ClaimsPrincipal)value) },

{ OwinConstants.ResponseStatusCode, new FeatureMap<IHttpResponseFeature>(feature => feature.StatusCode, () => 200, (feature, value) => feature.StatusCode = Convert.ToInt32(value)) },
{ OwinConstants.ResponseReasonPhrase, new FeatureMap<IHttpResponseFeature>(feature => feature.ReasonPhrase, (feature, value) => feature.ReasonPhrase = Convert.ToString(value)) },
{ OwinConstants.ResponseHeaders, new FeatureMap<IHttpResponseFeature>(feature => Utilities.MakeDictionaryStringArray(feature.Headers), (feature, value) => feature.Headers = Utilities.MakeDictionaryStringValues((IDictionary<string, string[]>)value)) },
{ OwinConstants.ResponseHeaders, new FeatureMap<IHttpResponseFeature>(feature => Utilities.MakeDictionaryStringArray(feature.Headers), (feature, value) => feature.Headers = Utilities.MakeHeaderDictionary((IDictionary<string, string[]>)value)) },
{ OwinConstants.ResponseBody, new FeatureMap<IHttpResponseFeature>(feature => feature.Body, () => Stream.Null, (feature, value) => feature.Body = (Stream)value) },
{ OwinConstants.CommonKeys.OnSendingHeaders, new FeatureMap<IHttpResponseFeature>(
feature => new Action<Action<object>, object>((cb, state) => {
Expand Down
8 changes: 4 additions & 4 deletions src/Microsoft.AspNet.Owin/OwinFeatureCollection.cs
Original file line number Diff line number Diff line change
Expand Up @@ -106,9 +106,9 @@ string IHttpRequestFeature.QueryString
set { Prop(OwinConstants.RequestQueryString, Utilities.RemoveQuestionMark(value)); }
}

IDictionary<string, StringValues> IHttpRequestFeature.Headers
IHeaderDictionary IHttpRequestFeature.Headers
{
get { return Utilities.MakeDictionaryStringValues(Prop<IDictionary<string, string[]>>(OwinConstants.RequestHeaders)); }
get { return Utilities.MakeHeaderDictionary(Prop<IDictionary<string, string[]>>(OwinConstants.RequestHeaders)); }
set { Prop(OwinConstants.RequestHeaders, Utilities.MakeDictionaryStringArray(value)); }
}

Expand Down Expand Up @@ -136,9 +136,9 @@ string IHttpResponseFeature.ReasonPhrase
set { Prop(OwinConstants.ResponseReasonPhrase, value); }
}

IDictionary<string, StringValues> IHttpResponseFeature.Headers
IHeaderDictionary IHttpResponseFeature.Headers
{
get { return Utilities.MakeDictionaryStringValues(Prop<IDictionary<string, string[]>>(OwinConstants.ResponseHeaders)); }
get { return Utilities.MakeHeaderDictionary(Prop<IDictionary<string, string[]>>(OwinConstants.ResponseHeaders)); }
set { Prop(OwinConstants.ResponseHeaders, Utilities.MakeDictionaryStringArray(value)); }
}

Expand Down
4 changes: 2 additions & 2 deletions src/Microsoft.AspNet.Owin/Utilities.cs
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ internal static ClaimsPrincipal MakeClaimsPrincipal(IPrincipal principal)
return new ClaimsPrincipal(principal);
}

internal static IDictionary<string, StringValues> MakeDictionaryStringValues(IDictionary<string, string[]> dictionary)
internal static IHeaderDictionary MakeHeaderDictionary(IDictionary<string, string[]> dictionary)
{
var wrapper = dictionary as DictionaryStringArrayWrapper;
if (wrapper != null)
Expand All @@ -56,7 +56,7 @@ internal static IDictionary<string, StringValues> MakeDictionaryStringValues(IDi
return new DictionaryStringValuesWrapper(dictionary);
}

internal static IDictionary<string, string[]> MakeDictionaryStringArray(IDictionary<string, StringValues> dictionary)
internal static IDictionary<string, string[]> MakeDictionaryStringArray(IHeaderDictionary dictionary)
{
var wrapper = dictionary as DictionaryStringValuesWrapper;
if (wrapper != null)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ private class StartedResponseFeature : IHttpResponseFeature

public bool HasStarted { get { return true; } }

public IDictionary<string, StringValues> Headers { get; set; }
public IHeaderDictionary Headers { get; set; }

public string ReasonPhrase { get; set; }

Expand Down
10 changes: 5 additions & 5 deletions test/Microsoft.AspNet.Http.Tests/DefaultHttpRequestTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@ public void Host_GetsHostFromHeaders()
// Arrange
const string expected = "localhost:9001";

var headers = new Dictionary<string, StringValues>(StringComparer.OrdinalIgnoreCase)
var headers = new HeaderDictionary()
{
{ "Host", expected },
};
Expand All @@ -85,7 +85,7 @@ public void Host_DecodesPunyCode()
// Arrange
const string expected = "löcalhöst";

var headers = new Dictionary<string, StringValues>(StringComparer.OrdinalIgnoreCase)
var headers = new HeaderDictionary()
{
{ "Host", "xn--lcalhst-90ae" },
};
Expand All @@ -105,7 +105,7 @@ public void Host_EncodesPunyCode()
// Arrange
const string expected = "xn--lcalhst-90ae";

var headers = new Dictionary<string, StringValues>(StringComparer.OrdinalIgnoreCase);
var headers = new HeaderDictionary();

var request = CreateRequest(headers);

Expand Down Expand Up @@ -188,7 +188,7 @@ public void Cookies_GetAndSet()
Assert.Equal(new[] { "name2=value2" }, cookieHeaders);
}

private static HttpRequest CreateRequest(IDictionary<string, StringValues> headers)
private static HttpRequest CreateRequest(IHeaderDictionary headers)
{
var context = new DefaultHttpContext();
context.Features.Get<IHttpRequestFeature>().Headers = headers;
Expand Down Expand Up @@ -217,7 +217,7 @@ private static HttpRequest GetRequestWithAcceptCharsetHeader(string acceptCharse

private static HttpRequest GetRequestWithHeader(string headerName, string headerValue)
{
var headers = new Dictionary<string, StringValues>(StringComparer.OrdinalIgnoreCase);
var headers = new HeaderDictionary();
if (headerValue != null)
{
headers.Add(headerName, headerValue);
Expand Down

0 comments on commit 3741d38

Please sign in to comment.