-
Notifications
You must be signed in to change notification settings - Fork 357
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Enable Form and Query access off of requests (#451)
- Loading branch information
1 parent
98f1924
commit b890e00
Showing
14 changed files
with
630 additions
and
303 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
45 changes: 45 additions & 0 deletions
45
src/SystemWebAdapters/src/Internal/FormCollectionReadOnlyDictionary.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,45 @@ | ||
// Licensed to the .NET Foundation under one or more agreements. | ||
// The .NET Foundation licenses this file to you under the MIT license. | ||
|
||
using System.Collections; | ||
using System.Collections.Generic; | ||
using System.Diagnostics.CodeAnalysis; | ||
using Microsoft.AspNetCore.Http; | ||
using Microsoft.Extensions.Primitives; | ||
|
||
namespace System.Web.Internal; | ||
|
||
internal class FormCollectionReadOnlyDictionary : IReadOnlyDictionary<string, StringValues> | ||
{ | ||
private readonly IFormCollection _form; | ||
|
||
public FormCollectionReadOnlyDictionary(IFormCollection form) | ||
{ | ||
_form = form; | ||
} | ||
|
||
public StringValues this[string key] => _form[key]; | ||
|
||
public IEnumerable<string> Keys => _form.Keys; | ||
|
||
public IEnumerable<StringValues> Values | ||
{ | ||
get | ||
{ | ||
foreach (var item in _form) | ||
{ | ||
yield return item.Value; | ||
} | ||
} | ||
} | ||
|
||
public int Count => _form.Count; | ||
|
||
public bool ContainsKey(string key) => _form.ContainsKey(key); | ||
|
||
public IEnumerator<KeyValuePair<string, StringValues>> GetEnumerator() => _form.GetEnumerator(); | ||
|
||
public bool TryGetValue(string key, [MaybeNullWhen(false)] out StringValues value) => _form.TryGetValue(key, out value); | ||
|
||
IEnumerator IEnumerable.GetEnumerator() => GetEnumerator(); | ||
} |
39 changes: 39 additions & 0 deletions
39
src/SystemWebAdapters/src/Internal/NameValueCollectionExtensions.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,39 @@ | ||
// Licensed to the .NET Foundation under one or more agreements. | ||
// The .NET Foundation licenses this file to you under the MIT license. | ||
|
||
using System.Collections; | ||
using System.Collections.Generic; | ||
using System.Collections.Specialized; | ||
using System.Diagnostics.CodeAnalysis; | ||
using Microsoft.AspNetCore.Http; | ||
using Microsoft.AspNetCore.Http.Features; | ||
using Microsoft.Extensions.Primitives; | ||
|
||
namespace System.Web.Internal; | ||
|
||
internal static class NameValueCollectionExtensions | ||
{ | ||
public static NameValueCollection ToNameValueCollection(this IQueryCollection query) | ||
{ | ||
if (query.Count == 0) | ||
{ | ||
return StringValuesReadOnlyDictionaryNameValueCollection.Empty; | ||
} | ||
|
||
return new StringValuesReadOnlyDictionaryNameValueCollection(new QueryCollectionReadOnlyDictionary(query)); | ||
} | ||
|
||
public static NameValueCollection ToNameValueCollection(this IFormCollection form) | ||
{ | ||
if (form.Count == 0) | ||
{ | ||
return StringValuesReadOnlyDictionaryNameValueCollection.Empty; | ||
} | ||
|
||
return new StringValuesReadOnlyDictionaryNameValueCollection(new FormCollectionReadOnlyDictionary(form)); | ||
} | ||
|
||
public static NameValueCollection ToNameValueCollection(this IHeaderDictionary headers) => new StringValuesDictionaryNameValueCollection(headers); | ||
|
||
public static NameValueCollection ToNameValueCollection(this IServerVariablesFeature serverVariables) => new ServerVariablesNameValueCollection(serverVariables); | ||
} |
45 changes: 45 additions & 0 deletions
45
src/SystemWebAdapters/src/Internal/QueryCollectionReadOnlyDictionary.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,45 @@ | ||
// Licensed to the .NET Foundation under one or more agreements. | ||
// The .NET Foundation licenses this file to you under the MIT license. | ||
|
||
using System.Collections; | ||
using System.Collections.Generic; | ||
using System.Diagnostics.CodeAnalysis; | ||
using Microsoft.AspNetCore.Http; | ||
using Microsoft.Extensions.Primitives; | ||
|
||
namespace System.Web.Internal; | ||
|
||
internal class QueryCollectionReadOnlyDictionary : IReadOnlyDictionary<string, StringValues> | ||
{ | ||
private readonly IQueryCollection _query; | ||
|
||
public QueryCollectionReadOnlyDictionary(IQueryCollection query) | ||
{ | ||
_query = query; | ||
} | ||
|
||
public StringValues this[string key] => _query[key]; | ||
|
||
public IEnumerable<string> Keys => _query.Keys; | ||
|
||
public IEnumerable<StringValues> Values | ||
{ | ||
get | ||
{ | ||
foreach (var item in _query) | ||
{ | ||
yield return item.Value; | ||
} | ||
} | ||
} | ||
|
||
public int Count => _query.Count; | ||
|
||
public bool ContainsKey(string key) => _query.ContainsKey(key); | ||
|
||
public IEnumerator<KeyValuePair<string, StringValues>> GetEnumerator() => _query.GetEnumerator(); | ||
|
||
public bool TryGetValue(string key, [MaybeNullWhen(false)] out StringValues value) => _query.TryGetValue(key, out value); | ||
|
||
IEnumerator IEnumerable.GetEnumerator() => GetEnumerator(); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
66 changes: 66 additions & 0 deletions
66
src/SystemWebAdapters/src/Internal/StringValuesDictionaryNameValueCollection.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,66 @@ | ||
using System.Collections; | ||
using System.Collections.Generic; | ||
using System.Linq; | ||
using Microsoft.Extensions.Primitives; | ||
|
||
namespace System.Web.Internal | ||
{ | ||
internal class StringValuesDictionaryNameValueCollection : WrappingNameValueCollection | ||
{ | ||
private readonly IDictionary<string, StringValues> _values; | ||
|
||
public StringValuesDictionaryNameValueCollection(IDictionary<string, StringValues> headers) | ||
{ | ||
_values = headers; | ||
} | ||
|
||
public override string?[] AllKeys => _values.Keys.ToArray(); | ||
|
||
public override int Count => _values.Count; | ||
|
||
public override void Add(string? name, string? value) | ||
{ | ||
if (name is null) | ||
{ | ||
return; | ||
} | ||
|
||
if (_values.TryGetValue(name, out var existing)) | ||
{ | ||
_values[name] = StringValues.Concat(existing, value); | ||
} | ||
else | ||
{ | ||
_values.Add(name, value); | ||
} | ||
} | ||
|
||
public override string[]? GetValues(string? name) | ||
=> name is not null && _values.TryGetValue(name, out var values) ? values : default; | ||
|
||
public override void Remove(string? name) | ||
{ | ||
if (name is not null) | ||
{ | ||
_values.Remove(name); | ||
} | ||
} | ||
|
||
public override void Set(string? name, string? value) | ||
{ | ||
if (name is null) | ||
{ | ||
return; | ||
} | ||
|
||
_values[name] = value; | ||
} | ||
|
||
public override string? Get(string? name) | ||
=> name is not null && _values.TryGetValue(name, out var values) ? values : default; | ||
|
||
public override void Clear() => _values.Clear(); | ||
|
||
public override IEnumerator GetEnumerator() => _values.Keys.GetEnumerator(); | ||
} | ||
} |
Oops, something went wrong.