| | | 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.Diagnostics; |
| | | 5 | | using System.Diagnostics.CodeAnalysis; |
| | | 6 | | using System.Globalization; |
| | | 7 | | using System.Text; |
| | | 8 | | |
| | | 9 | | namespace System.Net.Http.Headers |
| | | 10 | | { |
| | | 11 | | public class WarningHeaderValue : ICloneable |
| | | 12 | | { |
| | | 13 | | private readonly int _code; |
| | | 14 | | private readonly string _agent; |
| | | 15 | | private readonly string _text; |
| | | 16 | | private readonly DateTimeOffset _date; |
| | | 17 | | private readonly bool _dateHasValue; |
| | | 18 | | |
| | 0 | 19 | | public int Code => _code; |
| | | 20 | | |
| | 0 | 21 | | public string Agent => _agent; |
| | | 22 | | |
| | 0 | 23 | | public string Text => _text; |
| | | 24 | | |
| | 0 | 25 | | public DateTimeOffset? Date => _dateHasValue ? _date : null; |
| | | 26 | | |
| | 25930 | 27 | | private WarningHeaderValue(int code, string agent, string text, DateTimeOffset? date) |
| | 25930 | 28 | | { |
| | | 29 | | #if DEBUG |
| | | 30 | | // This constructor should only be used with already validated values. |
| | 25930 | 31 | | new WarningHeaderValue(code, agent, text); |
| | | 32 | | #endif |
| | | 33 | | |
| | 25930 | 34 | | _code = code; |
| | 25930 | 35 | | _agent = agent; |
| | 25930 | 36 | | _text = text; |
| | 25930 | 37 | | _date = date.GetValueOrDefault(); |
| | 25930 | 38 | | _dateHasValue = date.HasValue; |
| | 25930 | 39 | | } |
| | | 40 | | |
| | 25930 | 41 | | public WarningHeaderValue(int code, string agent, string text) |
| | 25930 | 42 | | { |
| | 25930 | 43 | | CheckCode(code); |
| | 25930 | 44 | | CheckAgent(agent); |
| | 25930 | 45 | | HeaderUtilities.CheckValidQuotedString(text); |
| | | 46 | | |
| | 25930 | 47 | | _code = code; |
| | 25930 | 48 | | _agent = agent; |
| | 25930 | 49 | | _text = text; |
| | 25930 | 50 | | } |
| | | 51 | | |
| | 0 | 52 | | public WarningHeaderValue(int code, string agent, string text, DateTimeOffset date) |
| | 0 | 53 | | { |
| | 0 | 54 | | CheckCode(code); |
| | 0 | 55 | | CheckAgent(agent); |
| | 0 | 56 | | HeaderUtilities.CheckValidQuotedString(text); |
| | | 57 | | |
| | 0 | 58 | | _code = code; |
| | 0 | 59 | | _agent = agent; |
| | 0 | 60 | | _text = text; |
| | 0 | 61 | | _date = date; |
| | 0 | 62 | | _dateHasValue = true; |
| | 0 | 63 | | } |
| | | 64 | | |
| | 0 | 65 | | private WarningHeaderValue(WarningHeaderValue source) |
| | 0 | 66 | | { |
| | 0 | 67 | | Debug.Assert(source != null); |
| | | 68 | | |
| | 0 | 69 | | _code = source._code; |
| | 0 | 70 | | _agent = source._agent; |
| | 0 | 71 | | _text = source._text; |
| | 0 | 72 | | _date = source._date; |
| | 0 | 73 | | _dateHasValue = source._dateHasValue; |
| | 0 | 74 | | } |
| | | 75 | | |
| | | 76 | | public override string ToString() |
| | 38190 | 77 | | { |
| | 38190 | 78 | | var sb = new ValueStringBuilder(stackalloc char[256]); |
| | | 79 | | |
| | | 80 | | // Warning codes are always 3 digits according to RFC2616 |
| | 38190 | 81 | | sb.AppendSpanFormattable(_code, "000", NumberFormatInfo.InvariantInfo); |
| | | 82 | | |
| | 38190 | 83 | | sb.Append(' '); |
| | 38190 | 84 | | sb.Append(_agent); |
| | 38190 | 85 | | sb.Append(' '); |
| | 38190 | 86 | | sb.Append(_text); |
| | | 87 | | |
| | 38190 | 88 | | if (_dateHasValue) |
| | 0 | 89 | | { |
| | 0 | 90 | | sb.Append(" \""); |
| | 0 | 91 | | sb.AppendSpanFormattable(_date, "r"); |
| | 0 | 92 | | sb.Append('\"'); |
| | 0 | 93 | | } |
| | | 94 | | |
| | 38190 | 95 | | return sb.ToString(); |
| | 38190 | 96 | | } |
| | | 97 | | |
| | | 98 | | public override bool Equals([NotNullWhen(true)] object? obj) => |
| | 0 | 99 | | obj is WarningHeaderValue other && |
| | 0 | 100 | | _code == other._code && |
| | 0 | 101 | | // 'agent' is a host/token, i.e. use case-insensitive comparison. Use case-sensitive comparison for 'text' |
| | 0 | 102 | | // since it is a quoted string. |
| | 0 | 103 | | string.Equals(_agent, other._agent, StringComparison.OrdinalIgnoreCase) && |
| | 0 | 104 | | string.Equals(_text, other._text, StringComparison.Ordinal) && |
| | 0 | 105 | | _dateHasValue == other._dateHasValue && |
| | 0 | 106 | | _date == other._date; |
| | | 107 | | |
| | | 108 | | public override int GetHashCode() => |
| | 0 | 109 | | HashCode.Combine( |
| | 0 | 110 | | _code, |
| | 0 | 111 | | StringComparer.OrdinalIgnoreCase.GetHashCode(_agent), |
| | 0 | 112 | | _text, |
| | 0 | 113 | | _dateHasValue, |
| | 0 | 114 | | _date); |
| | | 115 | | |
| | | 116 | | public static WarningHeaderValue Parse(string input) |
| | 0 | 117 | | { |
| | 0 | 118 | | int index = 0; |
| | 0 | 119 | | return (WarningHeaderValue)GenericHeaderParser.SingleValueWarningParser.ParseValue(input, null, ref index); |
| | 0 | 120 | | } |
| | | 121 | | |
| | | 122 | | public static bool TryParse([NotNullWhen(true)] string? input, [NotNullWhen(true)] out WarningHeaderValue? parse |
| | 0 | 123 | | { |
| | 0 | 124 | | int index = 0; |
| | 0 | 125 | | parsedValue = null; |
| | | 126 | | |
| | 0 | 127 | | if (GenericHeaderParser.SingleValueWarningParser.TryParseValue(input, null, ref index, out object? output)) |
| | 0 | 128 | | { |
| | 0 | 129 | | parsedValue = (WarningHeaderValue)output!; |
| | 0 | 130 | | return true; |
| | | 131 | | } |
| | 0 | 132 | | return false; |
| | 0 | 133 | | } |
| | | 134 | | |
| | | 135 | | internal static int GetWarningLength(string? input, int startIndex, out object? parsedValue) |
| | 27342 | 136 | | { |
| | 27342 | 137 | | Debug.Assert(startIndex >= 0); |
| | | 138 | | |
| | 27342 | 139 | | parsedValue = null; |
| | | 140 | | |
| | 27342 | 141 | | if (string.IsNullOrEmpty(input) || (startIndex >= input.Length)) |
| | 0 | 142 | | { |
| | 0 | 143 | | return 0; |
| | | 144 | | } |
| | | 145 | | |
| | | 146 | | // Read <code> in '<code> <agent> <text> ["<date>"]' |
| | 27342 | 147 | | int current = startIndex; |
| | | 148 | | |
| | 27342 | 149 | | if (!TryReadCode(input, ref current, out int code)) |
| | 216 | 150 | | { |
| | 216 | 151 | | return 0; |
| | | 152 | | } |
| | | 153 | | |
| | | 154 | | // Read <agent> in '<code> <agent> <text> ["<date>"]' |
| | 27126 | 155 | | if (!TryReadAgent(input, ref current, out string? agent)) |
| | 640 | 156 | | { |
| | 640 | 157 | | return 0; |
| | | 158 | | } |
| | | 159 | | |
| | | 160 | | // Read <text> in '<code> <agent> <text> ["<date>"]' |
| | 26486 | 161 | | int textStartIndex = current; |
| | 26486 | 162 | | if (HttpRuleParser.GetQuotedStringLength(input, current, out int textLength) != HttpParseResult.Parsed) |
| | 336 | 163 | | { |
| | 336 | 164 | | return 0; |
| | | 165 | | } |
| | | 166 | | |
| | 26150 | 167 | | string text = input.Substring(textStartIndex, textLength); |
| | | 168 | | |
| | 26150 | 169 | | current += textLength; |
| | | 170 | | |
| | | 171 | | // Read <date> in '<code> <agent> <text> ["<date>"]' |
| | 26150 | 172 | | if (!TryReadDate(input, ref current, out DateTimeOffset? date)) |
| | 220 | 173 | | { |
| | 220 | 174 | | return 0; |
| | | 175 | | } |
| | | 176 | | |
| | 25930 | 177 | | parsedValue = new WarningHeaderValue(code, agent, text, date); |
| | | 178 | | |
| | 25930 | 179 | | return current - startIndex; |
| | 27342 | 180 | | } |
| | | 181 | | |
| | | 182 | | private static bool TryReadAgent(string input, ref int current, [NotNullWhen(true)] out string? agent) |
| | 27126 | 183 | | { |
| | 27126 | 184 | | agent = null; |
| | | 185 | | |
| | 27126 | 186 | | int agentLength = HttpRuleParser.GetHostLength(input, current, true); |
| | 27126 | 187 | | if (agentLength == 0) |
| | 264 | 188 | | { |
| | 264 | 189 | | return false; |
| | | 190 | | } |
| | | 191 | | |
| | 26862 | 192 | | agent = input.Substring(current, agentLength); |
| | 26862 | 193 | | current += agentLength; |
| | | 194 | | |
| | 26862 | 195 | | int whitespaceLength = HttpRuleParser.GetWhitespaceLength(input, current); |
| | 26862 | 196 | | current += whitespaceLength; |
| | | 197 | | |
| | | 198 | | // At least one whitespace required after <agent>. Also make sure we have characters left for <text> |
| | 26862 | 199 | | if ((whitespaceLength == 0) || (current == input.Length)) |
| | 376 | 200 | | { |
| | 376 | 201 | | return false; |
| | | 202 | | } |
| | | 203 | | |
| | 26486 | 204 | | return true; |
| | 27126 | 205 | | } |
| | | 206 | | |
| | | 207 | | private static bool TryReadCode(string input, ref int current, out int code) |
| | 27342 | 208 | | { |
| | 27342 | 209 | | code = 0; |
| | 27342 | 210 | | int codeLength = HttpRuleParser.GetNumberLength(input, current, false); |
| | | 211 | | |
| | | 212 | | // code must be a 3 digit value. We accept less digits, but we don't accept more. |
| | 27342 | 213 | | if ((codeLength == 0) || (codeLength > 3)) |
| | 176 | 214 | | { |
| | 176 | 215 | | return false; |
| | | 216 | | } |
| | | 217 | | |
| | 27166 | 218 | | if (!HeaderUtilities.TryParseInt32(input, current, codeLength, out code)) |
| | 0 | 219 | | { |
| | 0 | 220 | | Debug.Fail("Unable to parse value even though it was parsed as <=3 digits string. Input: '" + |
| | 0 | 221 | | input + "', Current: " + current + ", CodeLength: " + codeLength); |
| | | 222 | | return false; |
| | | 223 | | } |
| | | 224 | | |
| | 27166 | 225 | | current += codeLength; |
| | | 226 | | |
| | 27166 | 227 | | int whitespaceLength = HttpRuleParser.GetWhitespaceLength(input, current); |
| | 27166 | 228 | | current += whitespaceLength; |
| | | 229 | | |
| | | 230 | | // Make sure the number is followed by at least one whitespace and that we have characters left to parse. |
| | 27166 | 231 | | if ((whitespaceLength == 0) || (current == input.Length)) |
| | 40 | 232 | | { |
| | 40 | 233 | | return false; |
| | | 234 | | } |
| | | 235 | | |
| | 27126 | 236 | | return true; |
| | 27342 | 237 | | } |
| | | 238 | | |
| | | 239 | | private static bool TryReadDate(string input, ref int current, out DateTimeOffset? date) |
| | 26150 | 240 | | { |
| | 26150 | 241 | | date = null; |
| | | 242 | | |
| | | 243 | | // Make sure we have at least one whitespace between <text> and <date> (if we have <date>) |
| | 26150 | 244 | | int whitespaceLength = HttpRuleParser.GetWhitespaceLength(input, current); |
| | 26150 | 245 | | current += whitespaceLength; |
| | | 246 | | |
| | | 247 | | // Read <date> in '<code> <agent> <text> ["<date>"]' |
| | 26150 | 248 | | if ((current < input.Length) && (input[current] == '"')) |
| | 220 | 249 | | { |
| | 220 | 250 | | if (whitespaceLength == 0) |
| | 116 | 251 | | { |
| | 116 | 252 | | return false; // we have characters after <text> but they were not separated by a whitespace |
| | | 253 | | } |
| | | 254 | | |
| | 104 | 255 | | current++; // skip opening '"' |
| | | 256 | | |
| | | 257 | | // Find the closing '"' |
| | 104 | 258 | | int dateStartIndex = current; |
| | 104 | 259 | | int quote = input.AsSpan(current).IndexOf('"'); |
| | 104 | 260 | | if (quote <= 0) // no quote was found or it was the first character (meaning an empty quoted string) |
| | 44 | 261 | | { |
| | 44 | 262 | | return false; |
| | | 263 | | } |
| | 60 | 264 | | current += quote; |
| | | 265 | | |
| | 60 | 266 | | if (!HttpDateParser.TryParse(input.AsSpan(dateStartIndex, current - dateStartIndex), out DateTimeOffset |
| | 60 | 267 | | { |
| | 60 | 268 | | return false; |
| | | 269 | | } |
| | | 270 | | |
| | 0 | 271 | | date = temp; |
| | | 272 | | |
| | 0 | 273 | | current++; // skip closing '"' |
| | 0 | 274 | | current += HttpRuleParser.GetWhitespaceLength(input, current); |
| | 0 | 275 | | } |
| | | 276 | | |
| | 25930 | 277 | | return true; |
| | 26150 | 278 | | } |
| | | 279 | | |
| | | 280 | | object ICloneable.Clone() |
| | 0 | 281 | | { |
| | 0 | 282 | | return new WarningHeaderValue(this); |
| | 0 | 283 | | } |
| | | 284 | | |
| | | 285 | | private static void CheckCode(int code) |
| | 25930 | 286 | | { |
| | 25930 | 287 | | ArgumentOutOfRangeException.ThrowIfNegative(code); |
| | 25930 | 288 | | ArgumentOutOfRangeException.ThrowIfGreaterThan(code, 999); |
| | 25930 | 289 | | } |
| | | 290 | | |
| | | 291 | | private static void CheckAgent(string agent) |
| | 25930 | 292 | | { |
| | 25930 | 293 | | ArgumentException.ThrowIfNullOrEmpty(agent); |
| | | 294 | | |
| | | 295 | | // 'Agent' can either be a host or a token. Since a token is a valid host, we only verify if the value |
| | | 296 | | // is a valid host. |
| | 25930 | 297 | | if (HttpRuleParser.GetHostLength(agent, 0, true) != agent.Length) |
| | 0 | 298 | | { |
| | 0 | 299 | | throw new FormatException(SR.Format(SR.net_http_headers_invalid_value, agent)); |
| | | 300 | | } |
| | 25930 | 301 | | } |
| | | 302 | | } |
| | | 303 | | } |