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

Commit

Permalink
React to string[] -> StringValues changes.
Browse files Browse the repository at this point in the history
  • Loading branch information
Tratcher committed Aug 20, 2015
1 parent e61ebca commit 6331a9c
Show file tree
Hide file tree
Showing 42 changed files with 154 additions and 126 deletions.
3 changes: 2 additions & 1 deletion src/Microsoft.AspNet.Mvc.Core/CreatedAtActionResult.cs
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
using Microsoft.AspNet.Mvc.Core;
using Microsoft.Framework.DependencyInjection;
using Microsoft.Framework.Internal;
using Microsoft.Net.Http.Headers;

namespace Microsoft.AspNet.Mvc
{
Expand Down Expand Up @@ -73,7 +74,7 @@ protected override void OnFormatting([NotNull] ActionContext context)
throw new InvalidOperationException(Resources.NoRoutesMatched);
}

context.HttpContext.Response.Headers.Set("Location", url);
context.HttpContext.Response.Headers[HeaderNames.Location] = url;
}
}
}
3 changes: 2 additions & 1 deletion src/Microsoft.AspNet.Mvc.Core/CreatedAtRouteResult.cs
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
using Microsoft.AspNet.Mvc.Core;
using Microsoft.Framework.DependencyInjection;
using Microsoft.Framework.Internal;
using Microsoft.Net.Http.Headers;

namespace Microsoft.AspNet.Mvc
{
Expand Down Expand Up @@ -70,7 +71,7 @@ protected override void OnFormatting([NotNull] ActionContext context)
throw new InvalidOperationException(Resources.NoRoutesMatched);
}

context.HttpContext.Response.Headers.Set("Location", url);
context.HttpContext.Response.Headers[HeaderNames.Location] = url;
}
}
}
3 changes: 2 additions & 1 deletion src/Microsoft.AspNet.Mvc.Core/CreatedResult.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
using System;
using Microsoft.AspNet.Http;
using Microsoft.Framework.Internal;
using Microsoft.Net.Http.Headers;

namespace Microsoft.AspNet.Mvc
{
Expand Down Expand Up @@ -50,7 +51,7 @@ public string Location
/// <inheritdoc />
protected override void OnFormatting([NotNull] ActionContext context)
{
context.HttpContext.Response.Headers.Set("Location", Location);
context.HttpContext.Response.Headers[HeaderNames.Location] = Location;
}
}
}
2 changes: 1 addition & 1 deletion src/Microsoft.AspNet.Mvc.Core/FileResult.cs
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,7 @@ public override Task ExecuteResultAsync([NotNull] ActionContext context)
// basis for the actual filename, where possible.
var cd = new ContentDispositionHeaderValue("attachment");
cd.SetHttpFileName(FileDownloadName);
context.HttpContext.Response.Headers.Set(HeaderNames.ContentDisposition, cd.ToString());
context.HttpContext.Response.Headers[HeaderNames.ContentDisposition] = cd.ToString();
}

// We aren't flowing the cancellation token appropriately, see
Expand Down
5 changes: 3 additions & 2 deletions src/Microsoft.AspNet.Mvc.Core/HttpMethodConstraint.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Linq;
using Microsoft.AspNet.Primitives;
using Microsoft.Framework.Internal;

namespace Microsoft.AspNet.Mvc
Expand Down Expand Up @@ -62,12 +63,12 @@ public bool Accept([NotNull] ActionConstraintContext context)
if (request.Headers.ContainsKey(OriginHeader))
{
// Update the http method if it is preflight request.
var accessControlRequestMethod = request.Headers.Get(AccessControlRequestMethod);
var accessControlRequestMethod = request.Headers[AccessControlRequestMethod];
if (string.Equals(
request.Method,
PreflightHttpMethod,
StringComparison.Ordinal) &&
accessControlRequestMethod != null)
!StringValues.IsNullOrEmpty(accessControlRequestMethod))
{
method = accessControlRequestMethod;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
using System.Threading.Tasks;
using Microsoft.AspNet.Http;
using Microsoft.AspNet.Http.Internal;
using Microsoft.AspNet.Primitives;
using Microsoft.Framework.Internal;

namespace Microsoft.AspNet.Mvc.ModelBinding
Expand Down Expand Up @@ -42,7 +43,7 @@ public async Task<ModelBindingResult> BindModelAsync([NotNull] ModelBindingConte
}
else
{
model = new FormCollection(new Dictionary<string, string[]>());
model = new FormCollection(new Dictionary<string, StringValues>());
}

var validationNode =
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
using System.Reflection;
#endif
using System.Threading.Tasks;
using Microsoft.AspNet.Primitives;
using Microsoft.Framework.Internal;

namespace Microsoft.AspNet.Mvc.ModelBinding
Expand Down Expand Up @@ -36,7 +37,7 @@ protected override Task<ModelBindingResult> BindModelCoreAsync([NotNull] ModelBi
object model = null;
if (bindingContext.ModelType == typeof(string))
{
var value = request.Headers.Get(headerName);
string value = request.Headers[headerName];
if (value != null)
{
model = value;
Expand All @@ -45,7 +46,7 @@ protected override Task<ModelBindingResult> BindModelCoreAsync([NotNull] ModelBi
else if (typeof(IEnumerable<string>).IsAssignableFrom(bindingContext.ModelType))
{
var values = request.Headers.GetCommaSeparatedValues(headerName);
if (values != null)
if (values.Count > 0)
{
model = ModelBindingHelper.ConvertValuesToCollectionType(
bindingContext.ModelType,
Expand All @@ -61,7 +62,7 @@ protected override Task<ModelBindingResult> BindModelCoreAsync([NotNull] ModelBi
bindingContext.ModelMetadata,
model);

var attemptedValue = (model as string) ?? request.Headers.Get(headerName);
var attemptedValue = (model as string) ?? request.Headers[headerName];
var valueProviderResult = new ValueProviderResult(model, attemptedValue, CultureInfo.InvariantCulture);
bindingContext.ModelState.SetModelValue(bindingContext.ModelName, valueProviderResult);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
using System.Diagnostics;
using System.Globalization;
using System.Threading.Tasks;
using Microsoft.AspNet.Primitives;
using Microsoft.Framework.Internal;

namespace Microsoft.AspNet.Mvc.ModelBinding
Expand All @@ -16,10 +17,10 @@ namespace Microsoft.AspNet.Mvc.ModelBinding
/// </summary>
public class JQueryFormValueProvider : BindingSourceValueProvider, IEnumerableValueProvider
{
private readonly Func<Task<IDictionary<string, string[]>>> _valuesFactory;
private readonly Func<Task<IDictionary<string, StringValues>>> _valuesFactory;

private PrefixContainer _prefixContainer;
private IDictionary<string, string[]> _values;
private IDictionary<string, StringValues> _values;

/// <summary>
/// Initializes a new instance of the <see cref="DictionaryBasedValueProvider"/> class.
Expand All @@ -29,7 +30,7 @@ public class JQueryFormValueProvider : BindingSourceValueProvider, IEnumerableVa
/// <param name="culture">The culture to return with ValueProviderResult instances.</param>
public JQueryFormValueProvider(
[NotNull] BindingSource bindingSource,
[NotNull] Func<Task<IDictionary<string, string[]>>> valuesFactory,
[NotNull] Func<Task<IDictionary<string, StringValues>>> valuesFactory,

This comment has been minimized.

Copy link
@rynowak

rynowak Aug 24, 2015

Member

Sort to hurt you so, but this has changed, you'll have to rebase 👍

CultureInfo culture)
: base(bindingSource)
{
Expand All @@ -40,7 +41,7 @@ public JQueryFormValueProvider(
// Internal for testing.
internal JQueryFormValueProvider(
[NotNull] BindingSource bindingSource,
[NotNull] IDictionary<string, string[]> values,
[NotNull] IDictionary<string, StringValues> values,
CultureInfo culture)
: base(bindingSource)
{
Expand Down Expand Up @@ -70,22 +71,26 @@ public override async Task<ValueProviderResult> GetValueAsync(string key)
{
var dictionary = await GetDictionary();

string[] values;
if (dictionary.TryGetValue(key, out values) && values != null && values.Length > 0)
StringValues values;
if (dictionary.TryGetValue(key, out values) && values.Count > 0)
{
// Success.
if (values.Length == 1)
if (values.Count == 1)
{
return new ValueProviderResult(values[0], values[0], Culture);
var value = (string)values;
return new ValueProviderResult(value, value, Culture);
}
else
{
var rawValue = (string[])values;
var value = (string)values;
return new ValueProviderResult(rawValue, value, Culture);
}

return new ValueProviderResult(values, string.Join(",", values), Culture);
}

return null;
}

private async Task<IDictionary<string, string[]>> GetDictionary()
private async Task<IDictionary<string, StringValues>> GetDictionary()
{
if (_values == null)
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
using System.Threading.Tasks;
using Microsoft.AspNet.Http;
using Microsoft.AspNet.Mvc.Core;
using Microsoft.AspNet.Primitives;
using Microsoft.Framework.Internal;

namespace Microsoft.AspNet.Mvc.ModelBinding
Expand All @@ -29,11 +30,11 @@ public IValueProvider GetValueProvider([NotNull] ValueProviderFactoryContext con
return null;
}

private static async Task<IDictionary<string, string[]>> GetValueCollectionAsync(HttpRequest request)
private static async Task<IDictionary<string, StringValues>> GetValueCollectionAsync(HttpRequest request)
{
var formCollection = await request.ReadFormAsync();

var dictionary = new Dictionary<string, string[]>(StringComparer.OrdinalIgnoreCase);
var dictionary = new Dictionary<string, StringValues>(StringComparer.OrdinalIgnoreCase);
foreach (var entry in formCollection)
{
var key = NormalizeJQueryToMvc(entry.Key);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
using System.Globalization;
using System.Threading.Tasks;
using Microsoft.AspNet.Http;
using Microsoft.AspNet.Primitives;
using Microsoft.Framework.Internal;

namespace Microsoft.AspNet.Mvc.ModelBinding
Expand Down Expand Up @@ -80,21 +81,21 @@ public virtual async Task<IDictionary<string, string>> GetKeysFromPrefixAsync([N
public override async Task<ValueProviderResult> GetValueAsync([NotNull] string key)
{
var collection = await GetValueCollectionAsync();
var values = collection.GetValues(key);
var values = collection[key];

ValueProviderResult result;
if (values == null)
if (values.Count == 0)
{
result = null;
}
else if (values.Count == 1)
{
var value = (string)values[0];
var value = (string)values;
result = new ValueProviderResult(value, value, _culture);
}
else
{
result = new ValueProviderResult(values, _values.Get(key), _culture);
result = new ValueProviderResult((string[])values, _values[key], _culture);
}

return result;
Expand Down
19 changes: 10 additions & 9 deletions src/Microsoft.AspNet.Mvc.Core/ResponseCacheFilter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
using System.Linq;
using Microsoft.AspNet.Mvc.Core;
using Microsoft.Framework.Internal;
using Microsoft.Net.Http.Headers;

namespace Microsoft.AspNet.Mvc
{
Expand Down Expand Up @@ -94,24 +95,24 @@ public void OnActionExecuting([NotNull] ActionExecutingContext context)
var headers = context.HttpContext.Response.Headers;

// Clear all headers
headers.Remove("Vary");
headers.Remove("Cache-control");
headers.Remove("Pragma");
headers.Remove(HeaderNames.Vary);
headers.Remove(HeaderNames.CacheControl);
headers.Remove(HeaderNames.Pragma);

if (!string.IsNullOrEmpty(VaryByHeader))
{
headers.Set("Vary", VaryByHeader);
headers[HeaderNames.Vary] = VaryByHeader;
}

if (NoStore)
{
headers.Set("Cache-control", "no-store");
headers[HeaderNames.CacheControl] = "no-store";

// Cache-control: no-store, no-cache is valid.
if (Location == ResponseCacheLocation.None)
{
headers.Append("Cache-control", "no-cache");
headers.Set("Pragma", "no-cache");
headers.AppendCommaSeparatedValues(HeaderNames.CacheControl, "no-cache");

This comment has been minimized.

Copy link
@davidfowl

davidfowl Aug 24, 2015

Member

Was this logic wrong before? Why AppendCommaSeparatedValues?

This comment has been minimized.

Copy link
@Tratcher

Tratcher Aug 24, 2015

Author Member

Append and AppendCSV used to do the same thing, except Append took string and AppendCSV took params string[], but both resulted in new[] { "value1, value2" }. AppendValues (now Append) results in new[] { "value1", "value2" }

headers[HeaderNames.Pragma] = "no-cache";
}
}
else
Expand All @@ -127,7 +128,7 @@ public void OnActionExecuting([NotNull] ActionExecutingContext context)
break;
case ResponseCacheLocation.None:
cacheControlValue = "no-cache";
headers.Set("Pragma", "no-cache");
headers[HeaderNames.Pragma] = "no-cache";
break;
}

Expand All @@ -140,7 +141,7 @@ public void OnActionExecuting([NotNull] ActionExecutingContext context)

if (cacheControlValue != null)
{
headers.Set("Cache-control", cacheControlValue);
headers[HeaderNames.CacheControl] = cacheControlValue;
}
}
}
Expand Down
5 changes: 3 additions & 2 deletions src/Microsoft.AspNet.Mvc.Cors/CorsAuthorizationFilter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
using System.Threading.Tasks;
using Microsoft.AspNet.Cors.Core;
using Microsoft.AspNet.Http;
using Microsoft.AspNet.Primitives;
using Microsoft.Framework.Internal;

namespace Microsoft.AspNet.Mvc
Expand Down Expand Up @@ -63,12 +64,12 @@ public async Task OnAuthorizationAsync([NotNull] AuthorizationContext context)
_corsService.ApplyResult(result, context.HttpContext.Response);

var accessControlRequestMethod =
httpContext.Request.Headers.Get(CorsConstants.AccessControlRequestMethod);
httpContext.Request.Headers[CorsConstants.AccessControlRequestMethod];
if (string.Equals(
request.Method,
CorsConstants.PreflightHttpMethod,
StringComparison.Ordinal) &&
accessControlRequestMethod != null)
!StringValues.IsNullOrEmpty(accessControlRequestMethod))
{
// If this was a preflight, there is no need to run anything else.
// Also the response is always 200 so that anyone after mvc can handle the pre flight request.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
using System.Threading.Tasks;
using Microsoft.AspNet.Cors.Core;
using Microsoft.AspNet.Http;
using Microsoft.AspNet.Primitives;
using Microsoft.Framework.Internal;

namespace Microsoft.AspNet.Mvc
Expand All @@ -27,12 +28,12 @@ public int Order
public Task OnAuthorizationAsync([NotNull] AuthorizationContext context)
{
var accessControlRequestMethod =
context.HttpContext.Request.Headers.Get(CorsConstants.AccessControlRequestMethod);
context.HttpContext.Request.Headers[CorsConstants.AccessControlRequestMethod];
if (string.Equals(
context.HttpContext.Request.Method,
CorsConstants.PreflightHttpMethod,
StringComparison.Ordinal) &&
accessControlRequestMethod != null)
!StringValues.IsNullOrEmpty(accessControlRequestMethod))
{
// Short circuit if the request is preflight as that should not result in action execution.
context.Result = new HttpStatusCodeResult(StatusCodes.Status200OK);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ public async Task WriteAsync(OutputFormatterContext context)

foreach (var header in responseHeaders)
{
response.Headers.AppendValues(header.Key, header.Value.ToArray());
response.Headers.Append(header.Key, header.Value.ToArray());
}

if (responseMessage.Content != null)
Expand All @@ -67,7 +67,7 @@ public async Task WriteAsync(OutputFormatterContext context)

foreach (var header in contentHeaders)
{
response.Headers.AppendValues(header.Key, header.Value.ToArray());
response.Headers.Append(header.Key, header.Value.ToArray());
}

await responseMessage.Content.CopyToAsync(response.Body);
Expand Down
Loading

0 comments on commit 6331a9c

Please sign in to comment.