|
| 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 | +} |
0 commit comments