|
| 1 | +namespace GenHTTP.Api.Protocol; |
| 2 | + |
| 3 | +/// <summary> |
| 4 | +/// Allows cookies to be read from a header list (e.g. the "Cookie" request header). |
| 5 | +/// </summary> |
| 6 | +public static class CookieHeaderExtensions |
| 7 | +{ |
| 8 | + |
| 9 | + #region Functionality |
| 10 | + |
| 11 | + /// <summary> |
| 12 | + /// Searches the given headers for a cookie with the specified name. |
| 13 | + /// </summary> |
| 14 | + /// <param name="headers">The headers to search (typically <see cref="IRequestHeader.Headers"/>)</param> |
| 15 | + /// <param name="key">The name of the cookie to be looked up</param> |
| 16 | + /// <returns>The value of the cookie, if found</returns> |
| 17 | + public static ByteString? GetCookie(this IKeyValueList headers, ByteString key) |
| 18 | + { |
| 19 | + for (var i = 0; i < headers.Count; i++) |
| 20 | + { |
| 21 | + var entry = headers[i]; |
| 22 | + |
| 23 | + if (entry.Key != KnownHeaders.Cookie) |
| 24 | + { |
| 25 | + continue; |
| 26 | + } |
| 27 | + |
| 28 | + var found = FindCookie(entry.Value, key); |
| 29 | + |
| 30 | + if (found is not null) |
| 31 | + { |
| 32 | + return found; |
| 33 | + } |
| 34 | + } |
| 35 | + |
| 36 | + return null; |
| 37 | + } |
| 38 | + |
| 39 | + /// <summary> |
| 40 | + /// Searches the given headers for a cookie with the specified name. |
| 41 | + /// </summary> |
| 42 | + /// <param name="headers">The headers to search (typically <see cref="IRequestHeader.Headers"/>)</param> |
| 43 | + /// <param name="key">The name of the cookie to be looked up</param> |
| 44 | + /// <returns>The value of the cookie, if found</returns> |
| 45 | + public static string? GetCookie(this IKeyValueList headers, string key) |
| 46 | + => headers.GetCookie(new ByteString(key))?.ToString(); |
| 47 | + |
| 48 | + /// <summary> |
| 49 | + /// Collects all cookies found in the given headers into a list that |
| 50 | + /// can be iterated or queried by name. |
| 51 | + /// </summary> |
| 52 | + /// <param name="headers">The headers to search (typically <see cref="IRequestHeader.Headers"/>)</param> |
| 53 | + /// <returns>The cookies found in the given headers</returns> |
| 54 | + public static IKeyValueList GetCookies(this IKeyValueList headers) |
| 55 | + { |
| 56 | + var cookies = new List<KeyValuePair<ByteString, ByteString>>(); |
| 57 | + |
| 58 | + for (var i = 0; i < headers.Count; i++) |
| 59 | + { |
| 60 | + var entry = headers[i]; |
| 61 | + |
| 62 | + if (entry.Key == KnownHeaders.Cookie) |
| 63 | + { |
| 64 | + ParseCookies(entry.Value, cookies); |
| 65 | + } |
| 66 | + } |
| 67 | + |
| 68 | + return new CookieList(cookies); |
| 69 | + } |
| 70 | + |
| 71 | + #endregion |
| 72 | + |
| 73 | + #region Parsing |
| 74 | + |
| 75 | + private static ByteString? FindCookie(ByteString header, ByteString key) |
| 76 | + { |
| 77 | + var memory = header.Bytes; |
| 78 | + var span = memory.Span; |
| 79 | + |
| 80 | + var keySpan = key.Bytes.Span; |
| 81 | + |
| 82 | + var segments = new CookieSegments(span); |
| 83 | + |
| 84 | + while (segments.MoveNext()) |
| 85 | + { |
| 86 | + if (span[segments.Name].SequenceEqual(keySpan)) |
| 87 | + { |
| 88 | + return new ByteString(memory[segments.Value]); |
| 89 | + } |
| 90 | + } |
| 91 | + |
| 92 | + return null; |
| 93 | + } |
| 94 | + |
| 95 | + private static void ParseCookies(ByteString header, List<KeyValuePair<ByteString, ByteString>> target) |
| 96 | + { |
| 97 | + var memory = header.Bytes; |
| 98 | + |
| 99 | + var segments = new CookieSegments(memory.Span); |
| 100 | + |
| 101 | + while (segments.MoveNext()) |
| 102 | + { |
| 103 | + target.Add(new(new ByteString(memory[segments.Name]), new ByteString(memory[segments.Value]))); |
| 104 | + } |
| 105 | + } |
| 106 | + |
| 107 | + #endregion |
| 108 | + |
| 109 | + #region Supporting Data Structures |
| 110 | + |
| 111 | + /// <summary> |
| 112 | + /// Walks a "Cookie" header value, yielding the name/value ranges of |
| 113 | + /// each "name=value" pair it finds (separated by "; "). |
| 114 | + /// </summary> |
| 115 | + private ref struct CookieSegments(ReadOnlySpan<byte> header) |
| 116 | + { |
| 117 | + private readonly ReadOnlySpan<byte> _header = header; |
| 118 | + |
| 119 | + private int _position = 0; |
| 120 | + |
| 121 | + public Range Name { get; private set; } |
| 122 | + |
| 123 | + public Range Value { get; private set; } |
| 124 | + |
| 125 | + public bool MoveNext() |
| 126 | + { |
| 127 | + while (_position < _header.Length) |
| 128 | + { |
| 129 | + while (_position < _header.Length && _header[_position] == (byte)' ') |
| 130 | + { |
| 131 | + _position++; |
| 132 | + } |
| 133 | + |
| 134 | + if (_position >= _header.Length) |
| 135 | + { |
| 136 | + return false; |
| 137 | + } |
| 138 | + |
| 139 | + var start = _position; |
| 140 | + |
| 141 | + var delimiter = _header[start..].IndexOf((byte)';'); |
| 142 | + |
| 143 | + var segmentEnd = delimiter < 0 ? _header.Length : start + delimiter; |
| 144 | + |
| 145 | + var separator = _header[start..segmentEnd].IndexOf((byte)'='); |
| 146 | + |
| 147 | + _position = segmentEnd + 1; |
| 148 | + |
| 149 | + if (separator > -1) |
| 150 | + { |
| 151 | + Name = start..(start + separator); |
| 152 | + Value = (start + separator + 1)..segmentEnd; |
| 153 | + |
| 154 | + return true; |
| 155 | + } |
| 156 | + } |
| 157 | + |
| 158 | + return false; |
| 159 | + } |
| 160 | + |
| 161 | + } |
| 162 | + |
| 163 | + /// <summary> |
| 164 | + /// A read-only list of cookies that have been parsed from one or |
| 165 | + /// more "Cookie" header values. |
| 166 | + /// </summary> |
| 167 | + private sealed class CookieList(List<KeyValuePair<ByteString, ByteString>> entries) : IKeyValueList |
| 168 | + { |
| 169 | + |
| 170 | + public int Count => entries.Count; |
| 171 | + |
| 172 | + public KeyValuePair<ByteString, ByteString> this[int index] => entries[index]; |
| 173 | + |
| 174 | + } |
| 175 | + |
| 176 | + #endregion |
| 177 | + |
| 178 | +} |
0 commit comments