| | | 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.Collections.Generic; |
| | | 5 | | using System.Diagnostics; |
| | | 6 | | using System.Diagnostics.CodeAnalysis; |
| | | 7 | | using System.IO; |
| | | 8 | | using System.Text; |
| | | 9 | | |
| | | 10 | | namespace System.Net.Http.Headers |
| | | 11 | | { |
| | | 12 | | // According to the RFC, in places where a "parameter" is required, the value is mandatory |
| | | 13 | | // (e.g. Media-Type, Accept). However, we don't introduce a dedicated type for this. |
| | | 14 | | public class NameValueWithParametersHeaderValue : NameValueHeaderValue, ICloneable |
| | | 15 | | { |
| | 1 | 16 | | private static readonly Func<NameValueHeaderValue> s_nameValueCreator = CreateNameValue; |
| | | 17 | | |
| | | 18 | | private UnvalidatedObjectCollection<NameValueHeaderValue>? _parameters; |
| | | 19 | | |
| | | 20 | | public ICollection<NameValueHeaderValue> Parameters => |
| | 28887 | 21 | | _parameters ??= new UnvalidatedObjectCollection<NameValueHeaderValue>(); |
| | | 22 | | |
| | | 23 | | public NameValueWithParametersHeaderValue(string name) |
| | 0 | 24 | | : base(name) |
| | 0 | 25 | | { |
| | 0 | 26 | | } |
| | | 27 | | |
| | | 28 | | public NameValueWithParametersHeaderValue(string name, string? value) |
| | 0 | 29 | | : base(name, value) |
| | 0 | 30 | | { |
| | 0 | 31 | | } |
| | | 32 | | |
| | 163593 | 33 | | internal NameValueWithParametersHeaderValue() |
| | 163593 | 34 | | { |
| | 163593 | 35 | | } |
| | | 36 | | |
| | | 37 | | protected NameValueWithParametersHeaderValue(NameValueWithParametersHeaderValue source) |
| | 0 | 38 | | : base(source) |
| | 0 | 39 | | { |
| | 0 | 40 | | _parameters = source._parameters.Clone(); |
| | 0 | 41 | | } |
| | | 42 | | |
| | | 43 | | public override bool Equals([NotNullWhen(true)] object? obj) |
| | 0 | 44 | | { |
| | 0 | 45 | | bool result = base.Equals(obj); |
| | | 46 | | |
| | 0 | 47 | | if (result) |
| | 0 | 48 | | { |
| | 0 | 49 | | NameValueWithParametersHeaderValue? other = obj as NameValueWithParametersHeaderValue; |
| | | 50 | | |
| | 0 | 51 | | if (other == null) |
| | 0 | 52 | | { |
| | 0 | 53 | | return false; |
| | | 54 | | } |
| | 0 | 55 | | return HeaderUtilities.AreEqualCollections(_parameters, other._parameters); |
| | | 56 | | } |
| | | 57 | | |
| | 0 | 58 | | return false; |
| | 0 | 59 | | } |
| | | 60 | | |
| | | 61 | | public override int GetHashCode() |
| | 0 | 62 | | { |
| | 0 | 63 | | return base.GetHashCode() ^ NameValueHeaderValue.GetHashCode(_parameters); |
| | 0 | 64 | | } |
| | | 65 | | |
| | | 66 | | public override string ToString() |
| | 272095 | 67 | | { |
| | 272095 | 68 | | string baseString = base.ToString(); |
| | 272095 | 69 | | StringBuilder sb = StringBuilderCache.Acquire(); |
| | 272095 | 70 | | sb.Append(baseString); |
| | 272095 | 71 | | NameValueHeaderValue.ToString(_parameters, ';', true, sb); |
| | 272095 | 72 | | return StringBuilderCache.GetStringAndRelease(sb); |
| | 272095 | 73 | | } |
| | | 74 | | |
| | | 75 | | public static new NameValueWithParametersHeaderValue Parse(string input) |
| | 0 | 76 | | { |
| | 0 | 77 | | int index = 0; |
| | 0 | 78 | | return (NameValueWithParametersHeaderValue)GenericHeaderParser.SingleValueNameValueWithParametersParser |
| | 0 | 79 | | .ParseValue(input, null, ref index); |
| | 0 | 80 | | } |
| | | 81 | | |
| | | 82 | | public static bool TryParse([NotNullWhen(true)] string? input, [NotNullWhen(true)] out NameValueWithParametersHe |
| | 0 | 83 | | { |
| | 0 | 84 | | int index = 0; |
| | 0 | 85 | | parsedValue = null; |
| | | 86 | | |
| | 0 | 87 | | if (GenericHeaderParser.SingleValueNameValueWithParametersParser.TryParseValue(input, |
| | 0 | 88 | | null, ref index, out object? output)) |
| | 0 | 89 | | { |
| | 0 | 90 | | parsedValue = (NameValueWithParametersHeaderValue)output!; |
| | 0 | 91 | | return true; |
| | | 92 | | } |
| | 0 | 93 | | return false; |
| | 0 | 94 | | } |
| | | 95 | | |
| | | 96 | | internal static int GetNameValueWithParametersLength(string? input, int startIndex, out object? parsedValue) |
| | 163697 | 97 | | { |
| | 163697 | 98 | | Debug.Assert(input != null); |
| | 163697 | 99 | | Debug.Assert(startIndex >= 0); |
| | | 100 | | |
| | 163697 | 101 | | parsedValue = null; |
| | | 102 | | |
| | 163697 | 103 | | if (string.IsNullOrEmpty(input) || (startIndex >= input.Length)) |
| | 0 | 104 | | { |
| | 0 | 105 | | return 0; |
| | | 106 | | } |
| | | 107 | | |
| | 163697 | 108 | | int nameValueLength = NameValueHeaderValue.GetNameValueLength(input, startIndex, |
| | 163697 | 109 | | s_nameValueCreator, out NameValueHeaderValue? nameValue); |
| | | 110 | | |
| | 163697 | 111 | | if (nameValueLength == 0) |
| | 104 | 112 | | { |
| | 104 | 113 | | return 0; |
| | | 114 | | } |
| | | 115 | | |
| | 163593 | 116 | | int current = startIndex + nameValueLength; |
| | 163593 | 117 | | current += HttpRuleParser.GetWhitespaceLength(input, current); |
| | 163593 | 118 | | NameValueWithParametersHeaderValue? nameValueWithParameters = |
| | 163593 | 119 | | nameValue as NameValueWithParametersHeaderValue; |
| | 163593 | 120 | | Debug.Assert(nameValueWithParameters != null); |
| | | 121 | | |
| | | 122 | | // So far we have a valid name/value pair. Check if we have also parameters for the name/value pair. If |
| | | 123 | | // yes, parse parameters. E.g. something like "name=value; param1=value1; param2=value2". |
| | 163593 | 124 | | if ((current < input.Length) && (input[current] == ';')) |
| | 28887 | 125 | | { |
| | 28887 | 126 | | current++; // skip delimiter. |
| | 28887 | 127 | | int parameterLength = NameValueHeaderValue.GetNameValueListLength(input, current, ';', |
| | 28887 | 128 | | (UnvalidatedObjectCollection<NameValueHeaderValue>)nameValueWithParameters.Parameters); |
| | | 129 | | |
| | 28887 | 130 | | if (parameterLength == 0) |
| | 26 | 131 | | { |
| | 26 | 132 | | return 0; |
| | | 133 | | } |
| | | 134 | | |
| | 28861 | 135 | | parsedValue = nameValueWithParameters; |
| | 28861 | 136 | | return current + parameterLength - startIndex; |
| | | 137 | | } |
| | | 138 | | |
| | | 139 | | // We have a name/value pair without parameters. |
| | 134706 | 140 | | parsedValue = nameValueWithParameters; |
| | 134706 | 141 | | return current - startIndex; |
| | 163697 | 142 | | } |
| | | 143 | | |
| | | 144 | | private static NameValueHeaderValue CreateNameValue() |
| | 163593 | 145 | | { |
| | 163593 | 146 | | return new NameValueWithParametersHeaderValue(); |
| | 163593 | 147 | | } |
| | | 148 | | |
| | | 149 | | object ICloneable.Clone() |
| | 0 | 150 | | { |
| | 0 | 151 | | return new NameValueWithParametersHeaderValue(this); |
| | 0 | 152 | | } |
| | | 153 | | } |
| | | 154 | | } |