| | | 1 | | // Licensed to the .NET Foundation under one or more agreements. |
| | | 2 | | // The .NET Foundation licenses this file to you under the MIT license. |
| | | 3 | | |
| | | 4 | | using System.Buffers; |
| | | 5 | | using System.Collections.Generic; |
| | | 6 | | using System.Diagnostics; |
| | | 7 | | using System.Globalization; |
| | | 8 | | using System.Runtime.CompilerServices; |
| | | 9 | | using System.Text; |
| | | 10 | | |
| | | 11 | | namespace System.Net.Http.Headers |
| | | 12 | | { |
| | | 13 | | internal static class HeaderUtilities |
| | | 14 | | { |
| | | 15 | | private const string qualityName = "q"; |
| | | 16 | | |
| | | 17 | | internal const string ConnectionClose = "close"; |
| | 0 | 18 | | internal static readonly TransferCodingHeaderValue TransferEncodingChunked = |
| | 0 | 19 | | new TransferCodingHeaderValue("chunked"); |
| | 0 | 20 | | internal static readonly NameValueWithParametersHeaderValue ExpectContinue = |
| | 0 | 21 | | new NameValueWithParametersHeaderValue("100-continue"); |
| | | 22 | | |
| | | 23 | | internal const string BytesUnit = "bytes"; |
| | | 24 | | |
| | | 25 | | // attr-char = ALPHA / DIGIT / "!" / "#" / "$" / "&" / "+" / "-" / "." / "^" / "_" / "`" / "|" / "~" |
| | | 26 | | // ; token except ( "*" / "'" / "%" ) |
| | 0 | 27 | | private static readonly SearchValues<byte> s_rfc5987AttrBytes = |
| | 0 | 28 | | SearchValues.Create("!#$&+-.0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ^_`abcdefghijklmnopqrstuvwxyz|~"u8); |
| | | 29 | | |
| | | 30 | | internal static void SetQuality(UnvalidatedObjectCollection<NameValueHeaderValue> parameters, double? value) |
| | 0 | 31 | | { |
| | 0 | 32 | | Debug.Assert(parameters != null); |
| | | 33 | | |
| | 0 | 34 | | NameValueHeaderValue? qualityParameter = NameValueHeaderValue.Find(parameters, qualityName); |
| | 0 | 35 | | if (value.HasValue) |
| | 0 | 36 | | { |
| | | 37 | | // Note that even if we check the value here, we can't prevent a user from adding an invalid quality |
| | | 38 | | // value using Parameters.Add(). Even if we would prevent the user from adding an invalid value |
| | | 39 | | // using Parameters.Add() they could always add invalid values using HttpHeaders.AddWithoutValidation(). |
| | | 40 | | // So this check is really for convenience to show users that they're trying to add an invalid |
| | | 41 | | // value. |
| | 0 | 42 | | double d = value.GetValueOrDefault(); |
| | 0 | 43 | | ArgumentOutOfRangeException.ThrowIfNegative(d); |
| | 0 | 44 | | ArgumentOutOfRangeException.ThrowIfGreaterThan(d, 1); |
| | | 45 | | |
| | 0 | 46 | | string qualityString = d.ToString("0.0##", NumberFormatInfo.InvariantInfo); |
| | 0 | 47 | | if (qualityParameter != null) |
| | 0 | 48 | | { |
| | 0 | 49 | | qualityParameter.Value = qualityString; |
| | 0 | 50 | | } |
| | | 51 | | else |
| | 0 | 52 | | { |
| | 0 | 53 | | parameters.Add(new NameValueHeaderValue(qualityName, qualityString)); |
| | 0 | 54 | | } |
| | 0 | 55 | | } |
| | | 56 | | else |
| | 0 | 57 | | { |
| | | 58 | | // Remove quality parameter |
| | 0 | 59 | | if (qualityParameter != null) |
| | 0 | 60 | | { |
| | 0 | 61 | | parameters.Remove(qualityParameter); |
| | 0 | 62 | | } |
| | 0 | 63 | | } |
| | 0 | 64 | | } |
| | | 65 | | |
| | | 66 | | // Encode a string using RFC 5987 encoding. |
| | | 67 | | // encoding'lang'PercentEncodedSpecials |
| | | 68 | | internal static string Encode5987(string input) |
| | 0 | 69 | | { |
| | 0 | 70 | | var builder = new ValueStringBuilder(stackalloc char[256]); |
| | 0 | 71 | | byte[] utf8bytes = ArrayPool<byte>.Shared.Rent(Encoding.UTF8.GetMaxByteCount(input.Length)); |
| | 0 | 72 | | int utf8length = Encoding.UTF8.GetBytes(input, 0, input.Length, utf8bytes, 0); |
| | | 73 | | |
| | 0 | 74 | | builder.Append("utf-8\'\'"); |
| | | 75 | | |
| | 0 | 76 | | ReadOnlySpan<byte> utf8 = utf8bytes.AsSpan(0, utf8length); |
| | | 77 | | do |
| | 0 | 78 | | { |
| | 0 | 79 | | int length = utf8.IndexOfAnyExcept(s_rfc5987AttrBytes); |
| | 0 | 80 | | if (length < 0) |
| | 0 | 81 | | { |
| | 0 | 82 | | length = utf8.Length; |
| | 0 | 83 | | } |
| | | 84 | | |
| | 0 | 85 | | Encoding.ASCII.GetChars(utf8.Slice(0, length), builder.AppendSpan(length)); |
| | | 86 | | |
| | 0 | 87 | | utf8 = utf8.Slice(length); |
| | | 88 | | |
| | 0 | 89 | | if (utf8.IsEmpty) |
| | 0 | 90 | | { |
| | 0 | 91 | | break; |
| | | 92 | | } |
| | | 93 | | |
| | 0 | 94 | | length = utf8.IndexOfAny(s_rfc5987AttrBytes); |
| | 0 | 95 | | if (length < 0) |
| | 0 | 96 | | { |
| | 0 | 97 | | length = utf8.Length; |
| | 0 | 98 | | } |
| | | 99 | | |
| | 0 | 100 | | foreach (byte b in utf8.Slice(0, length)) |
| | 0 | 101 | | { |
| | 0 | 102 | | AddHexEscaped(b, ref builder); |
| | 0 | 103 | | } |
| | | 104 | | |
| | 0 | 105 | | utf8 = utf8.Slice(length); |
| | 0 | 106 | | } |
| | 0 | 107 | | while (!utf8.IsEmpty); |
| | | 108 | | |
| | 0 | 109 | | ArrayPool<byte>.Shared.Return(utf8bytes); |
| | | 110 | | |
| | 0 | 111 | | return builder.ToString(); |
| | 0 | 112 | | } |
| | | 113 | | |
| | | 114 | | /// <summary>Transforms an ASCII character into its hexadecimal representation, adding the characters to a Strin |
| | | 115 | | private static void AddHexEscaped(byte c, ref ValueStringBuilder destination) |
| | 0 | 116 | | { |
| | 0 | 117 | | destination.Append('%'); |
| | 0 | 118 | | destination.Append(HexConverter.ToCharUpper(c >> 4)); |
| | 0 | 119 | | destination.Append(HexConverter.ToCharUpper(c)); |
| | 0 | 120 | | } |
| | | 121 | | |
| | | 122 | | internal static double? GetQuality(UnvalidatedObjectCollection<NameValueHeaderValue> parameters) |
| | 0 | 123 | | { |
| | 0 | 124 | | Debug.Assert(parameters != null); |
| | | 125 | | |
| | 0 | 126 | | NameValueHeaderValue? qualityParameter = NameValueHeaderValue.Find(parameters, qualityName); |
| | 0 | 127 | | if (qualityParameter != null) |
| | 0 | 128 | | { |
| | | 129 | | // Note that the RFC requires decimal '.' regardless of the culture. I.e. using ',' as decimal |
| | | 130 | | // separator is considered invalid (even if the current culture would allow it). |
| | | 131 | | double qualityValue; |
| | 0 | 132 | | if (double.TryParse(qualityParameter.Value, NumberStyles.AllowDecimalPoint, |
| | 0 | 133 | | NumberFormatInfo.InvariantInfo, out qualityValue)) |
| | 0 | 134 | | { |
| | 0 | 135 | | return qualityValue; |
| | | 136 | | } |
| | | 137 | | // If the stored value is an invalid quality value, just return null and log a warning. |
| | 0 | 138 | | if (NetEventSource.Log.IsEnabled()) NetEventSource.Error(null, SR.Format(SR.net_http_log_headers_invalid |
| | 0 | 139 | | } |
| | 0 | 140 | | return null; |
| | 0 | 141 | | } |
| | | 142 | | |
| | | 143 | | internal static void CheckValidToken(string value, [CallerArgumentExpression(nameof(value))] string? parameterNa |
| | 197971 | 144 | | { |
| | 197971 | 145 | | ArgumentException.ThrowIfNullOrEmpty(value, parameterName); |
| | | 146 | | |
| | 197971 | 147 | | if (!HttpRuleParser.IsToken(value)) |
| | 0 | 148 | | { |
| | 0 | 149 | | throw new FormatException(SR.Format(CultureInfo.InvariantCulture, SR.net_http_headers_invalid_value, val |
| | | 150 | | } |
| | 197971 | 151 | | } |
| | | 152 | | |
| | | 153 | | internal static void CheckValidComment(string value, [CallerArgumentExpression(nameof(value))] string? parameter |
| | 1226 | 154 | | { |
| | 1226 | 155 | | ArgumentException.ThrowIfNullOrEmpty(value, parameterName); |
| | | 156 | | |
| | 1226 | 157 | | if ((HttpRuleParser.GetCommentLength(value, 0, out int length) != HttpParseResult.Parsed) || |
| | 1226 | 158 | | (length != value.Length)) // no trailing spaces allowed |
| | 0 | 159 | | { |
| | 0 | 160 | | throw new FormatException(SR.Format(CultureInfo.InvariantCulture, SR.net_http_headers_invalid_value, val |
| | | 161 | | } |
| | 1226 | 162 | | } |
| | | 163 | | |
| | | 164 | | internal static void CheckValidQuotedString(string value, [CallerArgumentExpression(nameof(value))] string? para |
| | 27716 | 165 | | { |
| | 27716 | 166 | | ArgumentException.ThrowIfNullOrEmpty(value, parameterName); |
| | | 167 | | |
| | 27716 | 168 | | if ((HttpRuleParser.GetQuotedStringLength(value, 0, out int length) != HttpParseResult.Parsed) || |
| | 27716 | 169 | | (length != value.Length)) // no trailing spaces allowed |
| | 0 | 170 | | { |
| | 0 | 171 | | throw new FormatException(SR.Format(CultureInfo.InvariantCulture, SR.net_http_headers_invalid_value, val |
| | | 172 | | } |
| | 27716 | 173 | | } |
| | | 174 | | |
| | | 175 | | internal static bool AreEqualCollections<T>(ObjectCollection<T>? x, ObjectCollection<T>? y) where T : class |
| | 0 | 176 | | { |
| | 0 | 177 | | return AreEqualCollections(x, y, null); |
| | 0 | 178 | | } |
| | | 179 | | |
| | | 180 | | internal static bool AreEqualCollections<T>(ObjectCollection<T>? x, ObjectCollection<T>? y, IEqualityComparer<T> |
| | 0 | 181 | | { |
| | 0 | 182 | | if (x == null) |
| | 0 | 183 | | { |
| | 0 | 184 | | return (y == null) || (y.Count == 0); |
| | | 185 | | } |
| | | 186 | | |
| | 0 | 187 | | if (y == null) |
| | 0 | 188 | | { |
| | 0 | 189 | | return (x.Count == 0); |
| | | 190 | | } |
| | | 191 | | |
| | 0 | 192 | | if (x.Count != y.Count) |
| | 0 | 193 | | { |
| | 0 | 194 | | return false; |
| | | 195 | | } |
| | | 196 | | |
| | 0 | 197 | | if (x.Count == 0) |
| | 0 | 198 | | { |
| | 0 | 199 | | return true; |
| | | 200 | | } |
| | | 201 | | |
| | | 202 | | // We have two unordered lists. So comparison is an O(n*m) operation which is expensive. Usually |
| | | 203 | | // headers have 1-2 parameters (if any), so this comparison shouldn't be too expensive. |
| | 0 | 204 | | bool[] alreadyFound = new bool[x.Count]; |
| | 0 | 205 | | int i = 0; |
| | 0 | 206 | | foreach (var xItem in x) |
| | 0 | 207 | | { |
| | 0 | 208 | | Debug.Assert(xItem != null); |
| | | 209 | | |
| | 0 | 210 | | i = 0; |
| | 0 | 211 | | bool found = false; |
| | 0 | 212 | | foreach (var yItem in y) |
| | 0 | 213 | | { |
| | 0 | 214 | | if (!alreadyFound[i]) |
| | 0 | 215 | | { |
| | 0 | 216 | | if (((comparer == null) && xItem.Equals(yItem)) || |
| | 0 | 217 | | ((comparer != null) && comparer.Equals(xItem, yItem))) |
| | 0 | 218 | | { |
| | 0 | 219 | | alreadyFound[i] = true; |
| | 0 | 220 | | found = true; |
| | 0 | 221 | | break; |
| | | 222 | | } |
| | 0 | 223 | | } |
| | 0 | 224 | | i++; |
| | 0 | 225 | | } |
| | | 226 | | |
| | 0 | 227 | | if (!found) |
| | 0 | 228 | | { |
| | 0 | 229 | | return false; |
| | | 230 | | } |
| | 0 | 231 | | } |
| | | 232 | | |
| | | 233 | | // Since we never re-use a "found" value in 'y', we expect 'alreadyFound' to have all fields set to 'true'. |
| | | 234 | | // Otherwise the two collections can't be equal and we should not get here. |
| | 0 | 235 | | Debug.Assert(Array.TrueForAll(alreadyFound, value => value), |
| | 0 | 236 | | "Expected all values in 'alreadyFound' to be true since collections are considered equal."); |
| | | 237 | | |
| | 0 | 238 | | return true; |
| | 0 | 239 | | } |
| | | 240 | | |
| | | 241 | | internal static int GetNextNonEmptyOrWhitespaceIndex(string input, int startIndex, bool skipEmptyValues, |
| | | 242 | | out bool separatorFound) |
| | 2006158 | 243 | | { |
| | 2006158 | 244 | | Debug.Assert(input != null); |
| | 2006158 | 245 | | Debug.Assert(startIndex <= input.Length); // it's OK if index == value.Length. |
| | | 246 | | |
| | 2006158 | 247 | | separatorFound = false; |
| | 2006158 | 248 | | int current = startIndex + HttpRuleParser.GetWhitespaceLength(input, startIndex); |
| | | 249 | | |
| | 2006158 | 250 | | if ((current == input.Length) || (input[current] != ',')) |
| | 1015667 | 251 | | { |
| | 1015667 | 252 | | return current; |
| | | 253 | | } |
| | | 254 | | |
| | | 255 | | // If we have a separator, skip the separator and all following whitespace. If we support |
| | | 256 | | // empty values, continue until the current character is neither a separator nor a whitespace. |
| | 990491 | 257 | | separatorFound = true; |
| | 990491 | 258 | | current++; // skip delimiter. |
| | 990491 | 259 | | current += HttpRuleParser.GetWhitespaceLength(input, current); |
| | | 260 | | |
| | 990491 | 261 | | if (skipEmptyValues) |
| | 990409 | 262 | | { |
| | 1048594 | 263 | | while ((current < input.Length) && (input[current] == ',')) |
| | 58185 | 264 | | { |
| | 58185 | 265 | | current++; // skip delimiter. |
| | 58185 | 266 | | current += HttpRuleParser.GetWhitespaceLength(input, current); |
| | 58185 | 267 | | } |
| | 990409 | 268 | | } |
| | | 269 | | |
| | 990491 | 270 | | return current; |
| | 2006158 | 271 | | } |
| | | 272 | | |
| | | 273 | | internal static DateTimeOffset? GetDateTimeOffsetValue(HeaderDescriptor descriptor, HttpHeaders store, DateTimeO |
| | 0 | 274 | | { |
| | 0 | 275 | | Debug.Assert(store != null); |
| | | 276 | | |
| | 0 | 277 | | object? storedValue = store.GetSingleParsedValue(descriptor); |
| | 0 | 278 | | if (storedValue != null) |
| | 0 | 279 | | { |
| | 0 | 280 | | return (DateTimeOffset)storedValue; |
| | | 281 | | } |
| | 0 | 282 | | else if (defaultValue != null && store.Contains(descriptor)) |
| | 0 | 283 | | { |
| | 0 | 284 | | return defaultValue; |
| | | 285 | | } |
| | | 286 | | |
| | 0 | 287 | | return null; |
| | 0 | 288 | | } |
| | | 289 | | |
| | | 290 | | internal static TimeSpan? GetTimeSpanValue(HeaderDescriptor descriptor, HttpHeaders store) |
| | 0 | 291 | | { |
| | 0 | 292 | | Debug.Assert(store != null); |
| | | 293 | | |
| | 0 | 294 | | object? storedValue = store.GetSingleParsedValue(descriptor); |
| | 0 | 295 | | if (storedValue != null) |
| | 0 | 296 | | { |
| | 0 | 297 | | return (TimeSpan)storedValue; |
| | | 298 | | } |
| | 0 | 299 | | return null; |
| | 0 | 300 | | } |
| | | 301 | | |
| | | 302 | | internal static bool TryParseInt32(string value, out int result) => |
| | 0 | 303 | | int.TryParse(value, NumberStyles.None, CultureInfo.InvariantCulture, out result); |
| | | 304 | | |
| | | 305 | | internal static bool TryParseInt32(string value, int offset, int length, out int result) |
| | 27190 | 306 | | { |
| | 27190 | 307 | | if (offset < 0 || length < 0 || offset > value.Length - length) |
| | 0 | 308 | | { |
| | 0 | 309 | | result = 0; |
| | 0 | 310 | | return false; |
| | | 311 | | } |
| | | 312 | | |
| | 27190 | 313 | | return int.TryParse(value.AsSpan(offset, length), NumberStyles.None, CultureInfo.InvariantCulture, out resul |
| | 27190 | 314 | | } |
| | | 315 | | |
| | | 316 | | internal static bool TryParseInt64(string value, int offset, int length, out long result) |
| | 55250 | 317 | | { |
| | 55250 | 318 | | if (offset < 0 || length < 0 || offset > value.Length - length) |
| | 0 | 319 | | { |
| | 0 | 320 | | result = 0; |
| | 0 | 321 | | return false; |
| | | 322 | | } |
| | | 323 | | |
| | 55250 | 324 | | return long.TryParse(value.AsSpan(offset, length), NumberStyles.None, CultureInfo.InvariantCulture, out resu |
| | 55250 | 325 | | } |
| | | 326 | | |
| | | 327 | | internal static void DumpHeaders(ref ValueStringBuilder sb, params ReadOnlySpan<HttpHeaders?> headers) |
| | 0 | 328 | | { |
| | | 329 | | // Dumps all headers in the following format: |
| | | 330 | | // { |
| | | 331 | | // HeaderName1: Value1, Value2 |
| | | 332 | | // HeaderName2: Value1 |
| | | 333 | | // ... |
| | | 334 | | // } |
| | 0 | 335 | | sb.Append('{'); |
| | 0 | 336 | | sb.Append(Environment.NewLine); |
| | | 337 | | |
| | 0 | 338 | | for (int i = 0; i < headers.Length; i++) |
| | 0 | 339 | | { |
| | 0 | 340 | | if (headers[i] is HttpHeaders hh) |
| | 0 | 341 | | { |
| | 0 | 342 | | hh.Dump(ref sb, indentLines: true); |
| | 0 | 343 | | } |
| | 0 | 344 | | } |
| | | 345 | | |
| | 0 | 346 | | sb.Append('}'); |
| | 0 | 347 | | } |
| | | 348 | | |
| | | 349 | | internal static UnvalidatedObjectCollection<NameValueHeaderValue>? Clone(this UnvalidatedObjectCollection<NameVa |
| | 0 | 350 | | { |
| | 0 | 351 | | if (source == null) |
| | 0 | 352 | | return null; |
| | | 353 | | |
| | 0 | 354 | | var copy = new UnvalidatedObjectCollection<NameValueHeaderValue>(); |
| | 0 | 355 | | foreach (NameValueHeaderValue item in source) |
| | 0 | 356 | | { |
| | 0 | 357 | | copy.Add(new NameValueHeaderValue(item)); |
| | 0 | 358 | | } |
| | | 359 | | |
| | 0 | 360 | | return copy; |
| | 0 | 361 | | } |
| | | 362 | | } |
| | | 363 | | } |