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

Commit 7230a3d

Browse files
committed
Add form and query helpers needed for Facebook auth.
1 parent e2a3f14 commit 7230a3d

File tree

11 files changed

+155
-22
lines changed

11 files changed

+155
-22
lines changed

src/Microsoft.AspNet.PipelineCore/FormFeature.cs

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -9,8 +9,9 @@
99
using Microsoft.AspNet.FeatureModel;
1010
using Microsoft.AspNet.Http;
1111
using Microsoft.AspNet.HttpFeature;
12-
using Microsoft.AspNet.PipelineCore.Collections;
1312
using Microsoft.AspNet.PipelineCore.Infrastructure;
13+
using Microsoft.AspNet.WebUtilities;
14+
using Microsoft.AspNet.WebUtilities.Collections;
1415

1516
namespace Microsoft.AspNet.PipelineCore
1617
{
@@ -60,8 +61,8 @@ public async Task<IReadableStringCollection> GetFormAsync(CancellationToken canc
6061
detectEncodingFromByteOrderMarks: true,
6162
bufferSize: 1024, leaveOpen: true))
6263
{
63-
string formQuery = await streamReader.ReadToEndAsync();
64-
_form = new ReadableStringCollection(ParsingHelpers.GetQuery(formQuery));
64+
string form = await streamReader.ReadToEndAsync();
65+
_form = FormHelpers.ParseForm(form);
6566
}
6667
}
6768
return _form;

src/Microsoft.AspNet.PipelineCore/Infrastructure/ParsingHelpers.cs

Lines changed: 0 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -818,18 +818,6 @@ internal static IDictionary<string, string[]> GetQuery(string queryString)
818818
StringComparer.OrdinalIgnoreCase);
819819
}
820820

821-
internal static IFormCollection GetForm(string text)
822-
{
823-
IDictionary<string, string[]> form = new Dictionary<string, string[]>(StringComparer.OrdinalIgnoreCase);
824-
var accumulator = new Dictionary<string, List<string>>(StringComparer.OrdinalIgnoreCase);
825-
ParseDelimited(text, new[] { '&' }, AppendItemCallback, accumulator);
826-
foreach (var kv in accumulator)
827-
{
828-
form.Add(kv.Key, kv.Value.ToArray());
829-
}
830-
return new FormCollection(form);
831-
}
832-
833821
internal static string GetJoinedValue(IDictionary<string, string[]> store, string key)
834822
{
835823
string[] values = GetUnmodifiedValues(store, key);

src/Microsoft.AspNet.PipelineCore/QueryFeature.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,8 @@
55
using Microsoft.AspNet.FeatureModel;
66
using Microsoft.AspNet.Http;
77
using Microsoft.AspNet.HttpFeature;
8-
using Microsoft.AspNet.PipelineCore.Collections;
98
using Microsoft.AspNet.PipelineCore.Infrastructure;
9+
using Microsoft.AspNet.WebUtilities.Collections;
1010

1111
namespace Microsoft.AspNet.PipelineCore
1212
{

src/Microsoft.AspNet.PipelineCore/RequestCookiesFeature.cs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
using Microsoft.AspNet.HttpFeature;
1010
using Microsoft.AspNet.PipelineCore.Collections;
1111
using Microsoft.AspNet.PipelineCore.Infrastructure;
12+
using Microsoft.AspNet.WebUtilities.Collections;
1213

1314
namespace Microsoft.AspNet.PipelineCore
1415
{

src/Microsoft.AspNet.PipelineCore/project.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,8 @@
44
"dependencies": {
55
"Microsoft.AspNet.FeatureModel": "",
66
"Microsoft.AspNet.Http": "",
7-
"Microsoft.AspNet.HttpFeature": ""
7+
"Microsoft.AspNet.HttpFeature": "",
8+
"Microsoft.AspNet.WebUtilities": ""
89
},
910
"frameworks": {
1011
"net45": {},

src/Microsoft.AspNet.PipelineCore/Collections/FormCollection.cs renamed to src/Microsoft.AspNet.WebUtilities/Collections/FormCollection.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,15 +4,15 @@
44
using Microsoft.AspNet.Http;
55
using System.Collections.Generic;
66

7-
namespace Microsoft.AspNet.PipelineCore.Collections
7+
namespace Microsoft.AspNet.WebUtilities.Collections
88
{
99
/// <summary>
1010
/// Contains the parsed form values.
1111
/// </summary>
1212
public class FormCollection : ReadableStringCollection, IFormCollection
1313
{
1414
/// <summary>
15-
/// Initializes a new instance of the <see cref="T:Microsoft.Owin.FormCollection" /> class.
15+
/// Initializes a new instance of the <see cref="T:Microsoft.AspNet.WebUtilities.FormCollection" /> class.
1616
/// </summary>
1717
/// <param name="store">The store for the form.</param>
1818
public FormCollection(IDictionary<string, string[]> store)

src/Microsoft.AspNet.PipelineCore/Collections/ReadableStringCollection.cs renamed to src/Microsoft.AspNet.WebUtilities/Collections/ReadableStringCollection.cs

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,11 +4,9 @@
44
using System;
55
using System.Collections;
66
using System.Collections.Generic;
7-
using Microsoft.AspNet.Http.Infrastructure;
87
using Microsoft.AspNet.Http;
9-
using Microsoft.AspNet.PipelineCore.Infrastructure;
108

11-
namespace Microsoft.AspNet.PipelineCore.Collections
9+
namespace Microsoft.AspNet.WebUtilities.Collections
1210
{
1311
/// <summary>
1412
/// Accessors for query, forms, etc.
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
using System;
2+
using Microsoft.AspNet.Http;
3+
4+
namespace Microsoft.AspNet.WebUtilities
5+
{
6+
public static class FormHelpers
7+
{
8+
/// <summary>
9+
/// Parses an HTTP form body.
10+
/// </summary>
11+
/// <param name="text">The HTTP form body to parse.</param>
12+
/// <returns>The <see cref="T:Microsoft.Owin.IFormCollection" /> object containing the parsed HTTP form body.</returns>
13+
public static IFormCollection ParseForm(string text)
14+
{
15+
return ParsingHelpers.GetForm(text);
16+
}
17+
}
18+
}
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
// Copyright (c) Microsoft Open Technologies, Inc. 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;
5+
6+
namespace Microsoft.AspNet.WebUtilities
7+
{
8+
[AttributeUsage(AttributeTargets.Parameter, AllowMultiple = false)]
9+
internal sealed class NotNullAttribute : Attribute
10+
{
11+
}
12+
}
Lines changed: 94 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,94 @@
1+
// Copyright (c) Microsoft Open Technologies, Inc. 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;
5+
using System.Collections.Generic;
6+
using Microsoft.AspNet.Http;
7+
using Microsoft.AspNet.WebUtilities.Collections;
8+
9+
namespace Microsoft.AspNet.WebUtilities
10+
{
11+
internal static partial class ParsingHelpers
12+
{
13+
internal static void ParseDelimited(string text, char[] delimiters, Action<string, string, object> callback, object state)
14+
{
15+
int textLength = text.Length;
16+
int equalIndex = text.IndexOf('=');
17+
if (equalIndex == -1)
18+
{
19+
equalIndex = textLength;
20+
}
21+
int scanIndex = 0;
22+
while (scanIndex < textLength)
23+
{
24+
int delimiterIndex = text.IndexOfAny(delimiters, scanIndex);
25+
if (delimiterIndex == -1)
26+
{
27+
delimiterIndex = textLength;
28+
}
29+
if (equalIndex < delimiterIndex)
30+
{
31+
while (scanIndex != equalIndex && char.IsWhiteSpace(text[scanIndex]))
32+
{
33+
++scanIndex;
34+
}
35+
string name = text.Substring(scanIndex, equalIndex - scanIndex);
36+
string value = text.Substring(equalIndex + 1, delimiterIndex - equalIndex - 1);
37+
callback(
38+
Uri.UnescapeDataString(name.Replace('+', ' ')),
39+
Uri.UnescapeDataString(value.Replace('+', ' ')),
40+
state);
41+
equalIndex = text.IndexOf('=', delimiterIndex);
42+
if (equalIndex == -1)
43+
{
44+
equalIndex = textLength;
45+
}
46+
}
47+
scanIndex = delimiterIndex + 1;
48+
}
49+
}
50+
51+
private static readonly Action<string, string, object> AppendItemCallback = (name, value, state) =>
52+
{
53+
var dictionary = (IDictionary<string, List<String>>)state;
54+
55+
List<string> existing;
56+
if (!dictionary.TryGetValue(name, out existing))
57+
{
58+
dictionary.Add(name, new List<string>(1) { value });
59+
}
60+
else
61+
{
62+
existing.Add(value);
63+
}
64+
};
65+
66+
internal static IFormCollection GetForm(string text)
67+
{
68+
IDictionary<string, string[]> form = new Dictionary<string, string[]>(StringComparer.OrdinalIgnoreCase);
69+
var accumulator = new Dictionary<string, List<string>>(StringComparer.OrdinalIgnoreCase);
70+
ParseDelimited(text, new[] { '&' }, AppendItemCallback, accumulator);
71+
foreach (var kv in accumulator)
72+
{
73+
form.Add(kv.Key, kv.Value.ToArray());
74+
}
75+
return new FormCollection(form);
76+
}
77+
78+
internal static string GetJoinedValue(IDictionary<string, string[]> store, string key)
79+
{
80+
string[] values = GetUnmodifiedValues(store, key);
81+
return values == null ? null : string.Join(",", values);
82+
}
83+
84+
internal static string[] GetUnmodifiedValues(IDictionary<string, string[]> store, string key)
85+
{
86+
if (store == null)
87+
{
88+
throw new ArgumentNullException("store");
89+
}
90+
string[] values;
91+
return store.TryGetValue(key, out values) ? values : null;
92+
}
93+
}
94+
}
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
using System;
2+
3+
namespace Microsoft.AspNet.WebUtilities
4+
{
5+
public static class QueryHelpers
6+
{
7+
/// <summary>
8+
/// Append the given query key and value to the uri.
9+
/// </summary>
10+
/// <param name="uri">The base uri.</param>
11+
/// <param name="name">The name of the query key.</param>
12+
/// <param name="value">The query value.</param>
13+
/// <returns>The combine result.</returns>
14+
public static string AddQueryString([NotNull] string uri, [NotNull] string name, [NotNull] string value)
15+
{
16+
bool hasQuery = uri.IndexOf('?') != -1;
17+
return uri + (hasQuery ? "&" : "?") + Uri.EscapeDataString(name) + "=" + Uri.EscapeDataString(value);
18+
}
19+
}
20+
}

0 commit comments

Comments
 (0)