| | | 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 | | public class TransferCodingHeaderValue : ICloneable |
| | | 13 | | { |
| | | 14 | | // Use UnvalidatedObjectCollection<T> since we may have multiple parameters with the same name. |
| | | 15 | | private UnvalidatedObjectCollection<NameValueHeaderValue>? _parameters; |
| | 358648 | 16 | | private string _value = null!; // empty constructor only used internally and value set with non null |
| | | 17 | | |
| | 0 | 18 | | public string Value => _value; |
| | | 19 | | |
| | 92628 | 20 | | public ICollection<NameValueHeaderValue> Parameters => _parameters ??= new UnvalidatedObjectCollection<NameValue |
| | | 21 | | |
| | 358648 | 22 | | internal TransferCodingHeaderValue() |
| | 358648 | 23 | | { |
| | 358648 | 24 | | } |
| | | 25 | | |
| | 0 | 26 | | protected TransferCodingHeaderValue(TransferCodingHeaderValue source) |
| | 0 | 27 | | { |
| | 0 | 28 | | Debug.Assert(source != null); |
| | | 29 | | |
| | 0 | 30 | | _value = source._value; |
| | 0 | 31 | | _parameters = source._parameters.Clone(); |
| | 0 | 32 | | } |
| | | 33 | | |
| | 0 | 34 | | public TransferCodingHeaderValue(string value) |
| | 0 | 35 | | { |
| | 0 | 36 | | HeaderUtilities.CheckValidToken(value); |
| | 0 | 37 | | _value = value; |
| | 0 | 38 | | } |
| | | 39 | | |
| | | 40 | | public static TransferCodingHeaderValue Parse(string input) |
| | 0 | 41 | | { |
| | 0 | 42 | | int index = 0; |
| | 0 | 43 | | return (TransferCodingHeaderValue)TransferCodingHeaderParser.SingleValueParser.ParseValue( |
| | 0 | 44 | | input, null, ref index); |
| | 0 | 45 | | } |
| | | 46 | | |
| | | 47 | | public static bool TryParse([NotNullWhen(true)] string? input, [NotNullWhen(true)] out TransferCodingHeaderValue |
| | 0 | 48 | | { |
| | 0 | 49 | | int index = 0; |
| | 0 | 50 | | parsedValue = null; |
| | | 51 | | |
| | 0 | 52 | | if (TransferCodingHeaderParser.SingleValueParser.TryParseValue(input, null, ref index, out object? output)) |
| | 0 | 53 | | { |
| | 0 | 54 | | parsedValue = (TransferCodingHeaderValue)output!; |
| | 0 | 55 | | return true; |
| | | 56 | | } |
| | 0 | 57 | | return false; |
| | 0 | 58 | | } |
| | | 59 | | |
| | | 60 | | internal static int GetTransferCodingLength(string input, int startIndex, |
| | | 61 | | Func<TransferCodingHeaderValue> transferCodingCreator, out TransferCodingHeaderValue? parsedValue) |
| | 358822 | 62 | | { |
| | 358822 | 63 | | Debug.Assert(transferCodingCreator != null); |
| | 358822 | 64 | | Debug.Assert(startIndex >= 0); |
| | | 65 | | |
| | 358822 | 66 | | parsedValue = null; |
| | | 67 | | |
| | 358822 | 68 | | if (string.IsNullOrEmpty(input) || (startIndex >= input.Length)) |
| | 0 | 69 | | { |
| | 0 | 70 | | return 0; |
| | | 71 | | } |
| | | 72 | | |
| | | 73 | | // Caller must remove leading whitespace. If not, we'll return 0. |
| | 358822 | 74 | | int valueLength = HttpRuleParser.GetTokenLength(input, startIndex); |
| | | 75 | | |
| | 358822 | 76 | | if (valueLength == 0) |
| | 174 | 77 | | { |
| | 174 | 78 | | return 0; |
| | | 79 | | } |
| | | 80 | | |
| | 358648 | 81 | | string value = input.Substring(startIndex, valueLength); |
| | 358648 | 82 | | int current = startIndex + valueLength; |
| | 358648 | 83 | | current += HttpRuleParser.GetWhitespaceLength(input, current); |
| | | 84 | | TransferCodingHeaderValue transferCodingHeader; |
| | | 85 | | |
| | | 86 | | // If we're not done and we have a parameter delimiter, then we have a list of parameters. |
| | 358648 | 87 | | if ((current < input.Length) && (input[current] == ';')) |
| | 92628 | 88 | | { |
| | 92628 | 89 | | transferCodingHeader = transferCodingCreator(); |
| | 92628 | 90 | | transferCodingHeader._value = value; |
| | | 91 | | |
| | 92628 | 92 | | current++; // skip delimiter. |
| | 92628 | 93 | | int parameterLength = NameValueHeaderValue.GetNameValueListLength(input, current, ';', |
| | 92628 | 94 | | (UnvalidatedObjectCollection<NameValueHeaderValue>)transferCodingHeader.Parameters); |
| | | 95 | | |
| | 92628 | 96 | | if (parameterLength == 0) |
| | 330 | 97 | | { |
| | 330 | 98 | | return 0; |
| | | 99 | | } |
| | | 100 | | |
| | 92298 | 101 | | parsedValue = transferCodingHeader; |
| | 92298 | 102 | | return current + parameterLength - startIndex; |
| | | 103 | | } |
| | | 104 | | |
| | | 105 | | // We have a transfer coding without parameters. |
| | 266020 | 106 | | transferCodingHeader = transferCodingCreator(); |
| | 266020 | 107 | | transferCodingHeader._value = value; |
| | 266020 | 108 | | parsedValue = transferCodingHeader; |
| | 266020 | 109 | | return current - startIndex; |
| | 358822 | 110 | | } |
| | | 111 | | |
| | | 112 | | public override string ToString() |
| | 548050 | 113 | | { |
| | 548050 | 114 | | StringBuilder sb = StringBuilderCache.Acquire(); |
| | 548050 | 115 | | sb.Append(_value); |
| | 548050 | 116 | | NameValueHeaderValue.ToString(_parameters, ';', true, sb); |
| | 548050 | 117 | | return StringBuilderCache.GetStringAndRelease(sb); |
| | 548050 | 118 | | } |
| | | 119 | | |
| | | 120 | | public override bool Equals([NotNullWhen(true)] object? obj) |
| | 0 | 121 | | { |
| | 0 | 122 | | TransferCodingHeaderValue? other = obj as TransferCodingHeaderValue; |
| | | 123 | | |
| | 0 | 124 | | if (other == null) |
| | 0 | 125 | | { |
| | 0 | 126 | | return false; |
| | | 127 | | } |
| | | 128 | | |
| | 0 | 129 | | return string.Equals(_value, other._value, StringComparison.OrdinalIgnoreCase) && |
| | 0 | 130 | | HeaderUtilities.AreEqualCollections(_parameters, other._parameters); |
| | 0 | 131 | | } |
| | | 132 | | |
| | | 133 | | public override int GetHashCode() |
| | 0 | 134 | | { |
| | | 135 | | // The value string is case-insensitive. |
| | 0 | 136 | | return StringComparer.OrdinalIgnoreCase.GetHashCode(_value) ^ NameValueHeaderValue.GetHashCode(_parameters); |
| | 0 | 137 | | } |
| | | 138 | | |
| | | 139 | | // Implement ICloneable explicitly to allow derived types to "override" the implementation. |
| | | 140 | | object ICloneable.Clone() |
| | 0 | 141 | | { |
| | 0 | 142 | | return new TransferCodingHeaderValue(this); |
| | 0 | 143 | | } |
| | | 144 | | } |
| | | 145 | | } |