|
| 1 | +using GenHTTP.Api.Protocol; |
| 2 | + |
| 3 | +namespace GenHTTP.Modules.IO; |
| 4 | + |
| 5 | +/// <summary> |
| 6 | +/// Provides access to the key/value pairs of a request body that |
| 7 | +/// has been encoded as "application/x-www-form-urlencoded". |
| 8 | +/// </summary> |
| 9 | +public sealed class BodyArguments : IKeyValueList |
| 10 | +{ |
| 11 | + private readonly KeyValuePair<ByteString, ByteString>[] _entries; |
| 12 | + |
| 13 | + #region Initialization |
| 14 | + |
| 15 | + /// <summary> |
| 16 | + /// A body that does not contain any arguments. |
| 17 | + /// </summary> |
| 18 | + public static readonly BodyArguments Empty = new([]); |
| 19 | + |
| 20 | + private BodyArguments(KeyValuePair<ByteString, ByteString>[] entries) |
| 21 | + { |
| 22 | + _entries = entries; |
| 23 | + } |
| 24 | + |
| 25 | + /// <summary> |
| 26 | + /// Reads and parses the given body as form encoded content. |
| 27 | + /// </summary> |
| 28 | + /// <param name="body">The body to be parsed</param> |
| 29 | + /// <returns>The arguments found within the body</returns> |
| 30 | + public static async ValueTask<BodyArguments> CreateAsync(IRequestBody body) |
| 31 | + { |
| 32 | + var memory = await body.AsMemoryAsync(); |
| 33 | + |
| 34 | + return Parse(memory); |
| 35 | + } |
| 36 | + |
| 37 | + #endregion |
| 38 | + |
| 39 | + #region Get-/Setters |
| 40 | + |
| 41 | + public int Count => _entries.Length; |
| 42 | + |
| 43 | + public KeyValuePair<ByteString, ByteString> this[int index] => _entries[index]; |
| 44 | + |
| 45 | + #endregion |
| 46 | + |
| 47 | + #region Functionality |
| 48 | + |
| 49 | + private static BodyArguments Parse(ReadOnlyMemory<byte> memory) |
| 50 | + { |
| 51 | + if (memory.IsEmpty) |
| 52 | + { |
| 53 | + return Empty; |
| 54 | + } |
| 55 | + |
| 56 | + var entries = new List<KeyValuePair<ByteString, ByteString>>(); |
| 57 | + |
| 58 | + var remaining = memory; |
| 59 | + |
| 60 | + while (!remaining.IsEmpty) |
| 61 | + { |
| 62 | + var separator = remaining.Span.IndexOf((byte)'&'); |
| 63 | + |
| 64 | + var pair = (separator < 0) ? remaining : remaining[..separator]; |
| 65 | + |
| 66 | + if (!pair.IsEmpty) |
| 67 | + { |
| 68 | + entries.Add(ParsePair(pair)); |
| 69 | + } |
| 70 | + |
| 71 | + remaining = (separator < 0) ? default : remaining[(separator + 1)..]; |
| 72 | + } |
| 73 | + |
| 74 | + return new BodyArguments(entries.ToArray()); |
| 75 | + } |
| 76 | + |
| 77 | + private static KeyValuePair<ByteString, ByteString> ParsePair(ReadOnlyMemory<byte> pair) |
| 78 | + { |
| 79 | + var separator = pair.Span.IndexOf((byte)'='); |
| 80 | + |
| 81 | + return (separator < 0) ? new(Decode(pair), default) |
| 82 | + : new(Decode(pair[..separator]), Decode(pair[(separator + 1)..])); |
| 83 | + } |
| 84 | + |
| 85 | + /// <summary> |
| 86 | + /// Decodes a percent- and "+"-encoded token, only allocating a new |
| 87 | + /// buffer if the token actually requires decoding. |
| 88 | + /// </summary> |
| 89 | + private static ByteString Decode(ReadOnlyMemory<byte> raw) |
| 90 | + { |
| 91 | + var source = raw.Span; |
| 92 | + |
| 93 | + if (source.IndexOfAny((byte)'%', (byte)'+') < 0) |
| 94 | + { |
| 95 | + return new ByteString(raw); |
| 96 | + } |
| 97 | + |
| 98 | + var target = new byte[source.Length]; |
| 99 | + |
| 100 | + var written = PercentEncoding.Decode(source, target, decodePlus: true); |
| 101 | + |
| 102 | + return new ByteString(target.AsMemory(0, written)); |
| 103 | + } |
| 104 | + |
| 105 | + #endregion |
| 106 | + |
| 107 | +} |
0 commit comments