|
| 1 | +using System; |
| 2 | +using System.Collections.Generic; |
| 3 | +using Microsoft.AspNetCore.WebUtilities; |
| 4 | +using Microsoft.Extensions.Primitives; |
| 5 | + |
| 6 | +#pragma warning disable AV1008 // Class should not be static |
| 7 | +#pragma warning disable AV1708 // Type name contains term that should be avoided |
| 8 | +#pragma warning disable AV1130 // Return type in method signature should be a collection interface instead of a concrete type |
| 9 | +#pragma warning disable AV1532 // Loop statement contains nested loop |
| 10 | + |
| 11 | +namespace JsonApiDotNetCore.Middleware |
| 12 | +{ |
| 13 | + /// <summary> |
| 14 | + /// Replacement implementation for the ASP.NET built-in <see cref="QueryHelpers" />, to workaround bug https://github.com/dotnet/aspnetcore/issues/33394. |
| 15 | + /// This is identical to the built-in version, except it properly un-escapes query string keys without a value. |
| 16 | + /// </summary> |
| 17 | + internal static class FixedQueryHelpers |
| 18 | + { |
| 19 | + /// <summary> |
| 20 | + /// Parse a query string into its component key and value parts. |
| 21 | + /// </summary> |
| 22 | + /// <param name="queryString"> |
| 23 | + /// The raw query string value, with or without the leading '?'. |
| 24 | + /// </param> |
| 25 | + /// <returns> |
| 26 | + /// A collection of parsed keys and values, null if there are no entries. |
| 27 | + /// </returns> |
| 28 | + public static Dictionary<string, StringValues> ParseNullableQuery(string queryString) |
| 29 | + { |
| 30 | + var accumulator = new KeyValueAccumulator(); |
| 31 | + |
| 32 | + if (string.IsNullOrEmpty(queryString) || queryString == "?") |
| 33 | + { |
| 34 | + return null; |
| 35 | + } |
| 36 | + |
| 37 | + int scanIndex = 0; |
| 38 | + |
| 39 | + if (queryString[0] == '?') |
| 40 | + { |
| 41 | + scanIndex = 1; |
| 42 | + } |
| 43 | + |
| 44 | + int textLength = queryString.Length; |
| 45 | + int equalIndex = queryString.IndexOf('='); |
| 46 | + |
| 47 | + if (equalIndex == -1) |
| 48 | + { |
| 49 | + equalIndex = textLength; |
| 50 | + } |
| 51 | + |
| 52 | + while (scanIndex < textLength) |
| 53 | + { |
| 54 | + int delimiterIndex = queryString.IndexOf('&', scanIndex); |
| 55 | + |
| 56 | + if (delimiterIndex == -1) |
| 57 | + { |
| 58 | + delimiterIndex = textLength; |
| 59 | + } |
| 60 | + |
| 61 | + if (equalIndex < delimiterIndex) |
| 62 | + { |
| 63 | + while (scanIndex != equalIndex && char.IsWhiteSpace(queryString[scanIndex])) |
| 64 | + { |
| 65 | + ++scanIndex; |
| 66 | + } |
| 67 | + |
| 68 | + string name = queryString.Substring(scanIndex, equalIndex - scanIndex); |
| 69 | + string value = queryString.Substring(equalIndex + 1, delimiterIndex - equalIndex - 1); |
| 70 | + accumulator.Append(Uri.UnescapeDataString(name.Replace('+', ' ')), Uri.UnescapeDataString(value.Replace('+', ' '))); |
| 71 | + equalIndex = queryString.IndexOf('=', delimiterIndex); |
| 72 | + |
| 73 | + if (equalIndex == -1) |
| 74 | + { |
| 75 | + equalIndex = textLength; |
| 76 | + } |
| 77 | + } |
| 78 | + else |
| 79 | + { |
| 80 | + if (delimiterIndex > scanIndex) |
| 81 | + { |
| 82 | + // original code: |
| 83 | + // accumulator.Append(queryString.Substring(scanIndex, delimiterIndex - scanIndex), string.Empty); |
| 84 | + |
| 85 | + // replacement: |
| 86 | + string name = queryString.Substring(scanIndex, delimiterIndex - scanIndex); |
| 87 | + accumulator.Append(Uri.UnescapeDataString(name.Replace('+', ' ')), string.Empty); |
| 88 | + } |
| 89 | + } |
| 90 | + |
| 91 | + scanIndex = delimiterIndex + 1; |
| 92 | + } |
| 93 | + |
| 94 | + if (!accumulator.HasValues) |
| 95 | + { |
| 96 | + return null; |
| 97 | + } |
| 98 | + |
| 99 | + return accumulator.GetResults(); |
| 100 | + } |
| 101 | + } |
| 102 | +} |
0 commit comments