| | | 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.Globalization; |
| | | 8 | | using System.Text; |
| | | 9 | | |
| | | 10 | | namespace System.Net.Http.Headers |
| | | 11 | | { |
| | | 12 | | public class ContentDispositionHeaderValue : ICloneable |
| | | 13 | | { |
| | | 14 | | #region Fields |
| | | 15 | | |
| | | 16 | | private const string fileName = "filename"; |
| | | 17 | | private const string name = "name"; |
| | | 18 | | private const string fileNameStar = "filename*"; |
| | | 19 | | private const string creationDate = "creation-date"; |
| | | 20 | | private const string modificationDate = "modification-date"; |
| | | 21 | | private const string readDate = "read-date"; |
| | | 22 | | private const string size = "size"; |
| | | 23 | | |
| | | 24 | | // Use UnvalidatedObjectCollection<T> since we may have multiple parameters with the same name. |
| | | 25 | | private UnvalidatedObjectCollection<NameValueHeaderValue>? _parameters; |
| | 84 | 26 | | private string _dispositionType = null!; |
| | | 27 | | |
| | | 28 | | #endregion Fields |
| | | 29 | | |
| | | 30 | | #region Properties |
| | | 31 | | |
| | | 32 | | public string DispositionType |
| | | 33 | | { |
| | 0 | 34 | | get { return _dispositionType; } |
| | | 35 | | set |
| | 0 | 36 | | { |
| | 0 | 37 | | HeaderUtilities.CheckValidToken(value); |
| | 0 | 38 | | _dispositionType = value; |
| | 0 | 39 | | } |
| | | 40 | | } |
| | | 41 | | |
| | 72 | 42 | | public ICollection<NameValueHeaderValue> Parameters => _parameters ??= new UnvalidatedObjectCollection<NameValue |
| | | 43 | | |
| | | 44 | | public string? Name |
| | | 45 | | { |
| | 0 | 46 | | get { return GetName(name); } |
| | 0 | 47 | | set { SetName(name, value); } |
| | | 48 | | } |
| | | 49 | | |
| | | 50 | | public string? FileName |
| | | 51 | | { |
| | 0 | 52 | | get { return GetName(fileName); } |
| | 0 | 53 | | set { SetName(fileName, value); } |
| | | 54 | | } |
| | | 55 | | |
| | | 56 | | public string? FileNameStar |
| | | 57 | | { |
| | 0 | 58 | | get { return GetName(fileNameStar); } |
| | 0 | 59 | | set { SetName(fileNameStar, value); } |
| | | 60 | | } |
| | | 61 | | |
| | | 62 | | public DateTimeOffset? CreationDate |
| | | 63 | | { |
| | 0 | 64 | | get { return GetDate(creationDate); } |
| | 0 | 65 | | set { SetDate(creationDate, value); } |
| | | 66 | | } |
| | | 67 | | |
| | | 68 | | public DateTimeOffset? ModificationDate |
| | | 69 | | { |
| | 0 | 70 | | get { return GetDate(modificationDate); } |
| | 0 | 71 | | set { SetDate(modificationDate, value); } |
| | | 72 | | } |
| | | 73 | | |
| | | 74 | | public DateTimeOffset? ReadDate |
| | | 75 | | { |
| | 0 | 76 | | get { return GetDate(readDate); } |
| | 0 | 77 | | set { SetDate(readDate, value); } |
| | | 78 | | } |
| | | 79 | | |
| | | 80 | | public long? Size |
| | | 81 | | { |
| | | 82 | | get |
| | 0 | 83 | | { |
| | 0 | 84 | | NameValueHeaderValue? sizeParameter = NameValueHeaderValue.Find(_parameters, size); |
| | | 85 | | ulong value; |
| | 0 | 86 | | if (sizeParameter != null) |
| | 0 | 87 | | { |
| | 0 | 88 | | string? sizeString = sizeParameter.Value; |
| | 0 | 89 | | if (ulong.TryParse(sizeString, NumberStyles.Integer, CultureInfo.InvariantCulture, out value)) |
| | 0 | 90 | | { |
| | 0 | 91 | | return (long)value; |
| | | 92 | | } |
| | 0 | 93 | | } |
| | 0 | 94 | | return null; |
| | 0 | 95 | | } |
| | | 96 | | set |
| | 0 | 97 | | { |
| | 0 | 98 | | NameValueHeaderValue? sizeParameter = NameValueHeaderValue.Find(_parameters, size); |
| | 0 | 99 | | if (value == null) |
| | 0 | 100 | | { |
| | | 101 | | // Remove parameter. |
| | 0 | 102 | | if (sizeParameter != null) |
| | 0 | 103 | | { |
| | 0 | 104 | | _parameters!.Remove(sizeParameter); |
| | 0 | 105 | | } |
| | 0 | 106 | | } |
| | | 107 | | else |
| | 0 | 108 | | { |
| | 0 | 109 | | ArgumentOutOfRangeException.ThrowIfNegative(value.GetValueOrDefault()); |
| | 0 | 110 | | if (sizeParameter != null) |
| | 0 | 111 | | { |
| | 0 | 112 | | sizeParameter.Value = value.Value.ToString(CultureInfo.InvariantCulture); |
| | 0 | 113 | | } |
| | | 114 | | else |
| | 0 | 115 | | { |
| | 0 | 116 | | string sizeString = value.Value.ToString(CultureInfo.InvariantCulture); |
| | 0 | 117 | | Parameters.Add(new NameValueHeaderValue(size, sizeString)); |
| | 0 | 118 | | } |
| | 0 | 119 | | } |
| | 0 | 120 | | } |
| | | 121 | | } |
| | | 122 | | |
| | | 123 | | #endregion Properties |
| | | 124 | | |
| | | 125 | | #region Constructors |
| | | 126 | | |
| | 84 | 127 | | private ContentDispositionHeaderValue() |
| | 84 | 128 | | { |
| | | 129 | | // Used by the parser to create a new instance of this type. |
| | 84 | 130 | | } |
| | | 131 | | |
| | 0 | 132 | | protected ContentDispositionHeaderValue(ContentDispositionHeaderValue source) |
| | 0 | 133 | | { |
| | 0 | 134 | | Debug.Assert(source != null); |
| | | 135 | | |
| | 0 | 136 | | _dispositionType = source._dispositionType; |
| | 0 | 137 | | _parameters = source._parameters.Clone(); |
| | 0 | 138 | | } |
| | | 139 | | |
| | 0 | 140 | | public ContentDispositionHeaderValue(string dispositionType) |
| | 0 | 141 | | { |
| | 0 | 142 | | HeaderUtilities.CheckValidToken(dispositionType); |
| | | 143 | | |
| | 0 | 144 | | _dispositionType = dispositionType; |
| | 0 | 145 | | } |
| | | 146 | | |
| | | 147 | | #endregion Constructors |
| | | 148 | | |
| | | 149 | | #region Overloads |
| | | 150 | | |
| | | 151 | | public override string ToString() |
| | 39 | 152 | | { |
| | 39 | 153 | | StringBuilder sb = StringBuilderCache.Acquire(); |
| | 39 | 154 | | sb.Append(_dispositionType); |
| | 39 | 155 | | NameValueHeaderValue.ToString(_parameters, ';', true, sb); |
| | 39 | 156 | | return StringBuilderCache.GetStringAndRelease(sb); |
| | 39 | 157 | | } |
| | | 158 | | |
| | | 159 | | public override bool Equals([NotNullWhen(true)] object? obj) |
| | 0 | 160 | | { |
| | 0 | 161 | | ContentDispositionHeaderValue? other = obj as ContentDispositionHeaderValue; |
| | | 162 | | |
| | 0 | 163 | | if (other == null) |
| | 0 | 164 | | { |
| | 0 | 165 | | return false; |
| | | 166 | | } |
| | | 167 | | |
| | 0 | 168 | | return string.Equals(_dispositionType, other._dispositionType, StringComparison.OrdinalIgnoreCase) && |
| | 0 | 169 | | HeaderUtilities.AreEqualCollections(_parameters, other._parameters); |
| | 0 | 170 | | } |
| | | 171 | | |
| | | 172 | | public override int GetHashCode() |
| | 0 | 173 | | { |
| | | 174 | | // The dispositionType string is case-insensitive. |
| | 0 | 175 | | return StringComparer.OrdinalIgnoreCase.GetHashCode(_dispositionType) ^ NameValueHeaderValue.GetHashCode(_pa |
| | 0 | 176 | | } |
| | | 177 | | |
| | | 178 | | // Implement ICloneable explicitly to allow derived types to "override" the implementation. |
| | | 179 | | object ICloneable.Clone() |
| | 0 | 180 | | { |
| | 0 | 181 | | return new ContentDispositionHeaderValue(this); |
| | 0 | 182 | | } |
| | | 183 | | |
| | | 184 | | #endregion Overloads |
| | | 185 | | |
| | | 186 | | #region Parsing |
| | | 187 | | |
| | | 188 | | public static ContentDispositionHeaderValue Parse(string input) |
| | 0 | 189 | | { |
| | 0 | 190 | | int index = 0; |
| | 0 | 191 | | return (ContentDispositionHeaderValue)GenericHeaderParser.ContentDispositionParser.ParseValue(input, null, r |
| | 0 | 192 | | } |
| | | 193 | | |
| | | 194 | | public static bool TryParse([NotNullWhen(true)] string? input, [NotNullWhen(true)] out ContentDispositionHeaderV |
| | 0 | 195 | | { |
| | 0 | 196 | | int index = 0; |
| | 0 | 197 | | parsedValue = null; |
| | | 198 | | |
| | 0 | 199 | | if (GenericHeaderParser.ContentDispositionParser.TryParseValue(input, null, ref index, out object? output)) |
| | 0 | 200 | | { |
| | 0 | 201 | | parsedValue = (ContentDispositionHeaderValue)output!; |
| | 0 | 202 | | return true; |
| | | 203 | | } |
| | 0 | 204 | | return false; |
| | 0 | 205 | | } |
| | | 206 | | |
| | | 207 | | internal static int GetDispositionTypeLength(string? input, int startIndex, out object? parsedValue) |
| | 90 | 208 | | { |
| | 90 | 209 | | Debug.Assert(startIndex >= 0); |
| | | 210 | | |
| | 90 | 211 | | parsedValue = null; |
| | | 212 | | |
| | 90 | 213 | | if (string.IsNullOrEmpty(input) || (startIndex >= input.Length)) |
| | 0 | 214 | | { |
| | 0 | 215 | | return 0; |
| | | 216 | | } |
| | | 217 | | |
| | | 218 | | // Caller must remove leading whitespace. If not, we'll return 0. |
| | | 219 | | string? dispositionType; |
| | 90 | 220 | | int dispositionTypeLength = GetDispositionTypeExpressionLength(input, startIndex, out dispositionType); |
| | | 221 | | |
| | 90 | 222 | | if (dispositionTypeLength == 0) |
| | 6 | 223 | | { |
| | 6 | 224 | | return 0; |
| | | 225 | | } |
| | | 226 | | |
| | 84 | 227 | | int current = startIndex + dispositionTypeLength; |
| | 84 | 228 | | current += HttpRuleParser.GetWhitespaceLength(input, current); |
| | 84 | 229 | | ContentDispositionHeaderValue contentDispositionHeader = new ContentDispositionHeaderValue(); |
| | 84 | 230 | | contentDispositionHeader._dispositionType = dispositionType!; |
| | | 231 | | |
| | | 232 | | // If we're not done and we have a parameter delimiter, then we have a list of parameters. |
| | 84 | 233 | | if ((current < input.Length) && (input[current] == ';')) |
| | 72 | 234 | | { |
| | 72 | 235 | | current++; // Skip delimiter. |
| | 72 | 236 | | int parameterLength = NameValueHeaderValue.GetNameValueListLength(input, current, ';', |
| | 72 | 237 | | (UnvalidatedObjectCollection<NameValueHeaderValue>)contentDispositionHeader.Parameters); |
| | | 238 | | |
| | 72 | 239 | | if (parameterLength == 0) |
| | 48 | 240 | | { |
| | 48 | 241 | | return 0; |
| | | 242 | | } |
| | | 243 | | |
| | 24 | 244 | | parsedValue = contentDispositionHeader; |
| | 24 | 245 | | return current + parameterLength - startIndex; |
| | | 246 | | } |
| | | 247 | | |
| | | 248 | | // We have a ContentDisposition header without parameters. |
| | 12 | 249 | | parsedValue = contentDispositionHeader; |
| | 12 | 250 | | return current - startIndex; |
| | 90 | 251 | | } |
| | | 252 | | |
| | | 253 | | private static int GetDispositionTypeExpressionLength(string input, int startIndex, out string? dispositionType) |
| | 90 | 254 | | { |
| | 90 | 255 | | Debug.Assert((input != null) && (input.Length > 0) && (startIndex < input.Length)); |
| | | 256 | | |
| | | 257 | | // This method just parses the disposition type string, it does not parse parameters. |
| | 90 | 258 | | dispositionType = null; |
| | | 259 | | |
| | | 260 | | // Parse the disposition type, i.e. <dispositiontype> in content-disposition string |
| | | 261 | | // "<dispositiontype>; param1=value1; param2=value2". |
| | 90 | 262 | | int typeLength = HttpRuleParser.GetTokenLength(input, startIndex); |
| | | 263 | | |
| | 90 | 264 | | if (typeLength == 0) |
| | 6 | 265 | | { |
| | 6 | 266 | | return 0; |
| | | 267 | | } |
| | | 268 | | |
| | 84 | 269 | | dispositionType = input.Substring(startIndex, typeLength); |
| | 84 | 270 | | return typeLength; |
| | 90 | 271 | | } |
| | | 272 | | |
| | | 273 | | #endregion Parsing |
| | | 274 | | |
| | | 275 | | #region Helpers |
| | | 276 | | |
| | | 277 | | // Gets a parameter of the given name and attempts to extract a date. |
| | | 278 | | // Returns null if the parameter is not present or the format is incorrect. |
| | | 279 | | private DateTimeOffset? GetDate(string parameter) |
| | 0 | 280 | | { |
| | 0 | 281 | | NameValueHeaderValue? dateParameter = NameValueHeaderValue.Find(_parameters, parameter); |
| | | 282 | | DateTimeOffset date; |
| | 0 | 283 | | if (dateParameter != null) |
| | 0 | 284 | | { |
| | 0 | 285 | | ReadOnlySpan<char> dateString = dateParameter.Value; |
| | | 286 | | // Should have quotes, remove them. |
| | 0 | 287 | | if (IsQuoted(dateString)) |
| | 0 | 288 | | { |
| | 0 | 289 | | dateString = dateString.Slice(1, dateString.Length - 2); |
| | 0 | 290 | | } |
| | 0 | 291 | | if (HttpDateParser.TryParse(dateString, out date)) |
| | 0 | 292 | | { |
| | 0 | 293 | | return date; |
| | | 294 | | } |
| | 0 | 295 | | } |
| | 0 | 296 | | return null; |
| | 0 | 297 | | } |
| | | 298 | | |
| | | 299 | | // Add the given parameter to the list. Remove if date is null. |
| | | 300 | | private void SetDate(string parameter, DateTimeOffset? date) |
| | 0 | 301 | | { |
| | 0 | 302 | | NameValueHeaderValue? dateParameter = NameValueHeaderValue.Find(_parameters, parameter); |
| | 0 | 303 | | if (date == null) |
| | 0 | 304 | | { |
| | | 305 | | // Remove parameter. |
| | 0 | 306 | | if (dateParameter != null) |
| | 0 | 307 | | { |
| | 0 | 308 | | _parameters!.Remove(dateParameter); |
| | 0 | 309 | | } |
| | 0 | 310 | | } |
| | | 311 | | else |
| | 0 | 312 | | { |
| | | 313 | | // Must always be quoted. |
| | 0 | 314 | | string dateString = $"\"{date.GetValueOrDefault():r}\""; |
| | 0 | 315 | | if (dateParameter != null) |
| | 0 | 316 | | { |
| | 0 | 317 | | dateParameter.Value = dateString; |
| | 0 | 318 | | } |
| | | 319 | | else |
| | 0 | 320 | | { |
| | 0 | 321 | | Parameters.Add(new NameValueHeaderValue(parameter, dateString)); |
| | 0 | 322 | | } |
| | 0 | 323 | | } |
| | 0 | 324 | | } |
| | | 325 | | |
| | | 326 | | // Gets a parameter of the given name and attempts to decode it if necessary. |
| | | 327 | | // Returns null if the parameter is not present or the raw value if the encoding is incorrect. |
| | | 328 | | private string? GetName(string parameter) |
| | 0 | 329 | | { |
| | 0 | 330 | | NameValueHeaderValue? nameParameter = NameValueHeaderValue.Find(_parameters, parameter); |
| | 0 | 331 | | if (nameParameter != null) |
| | 0 | 332 | | { |
| | | 333 | | string? result; |
| | | 334 | | // filename*=utf-8'lang'%7FMyString |
| | 0 | 335 | | if (parameter.EndsWith('*')) |
| | 0 | 336 | | { |
| | 0 | 337 | | Debug.Assert(nameParameter.Value != null); |
| | 0 | 338 | | if (TryDecode5987(nameParameter.Value, out result)) |
| | 0 | 339 | | { |
| | 0 | 340 | | return result; |
| | | 341 | | } |
| | 0 | 342 | | return null; // Unrecognized encoding. |
| | | 343 | | } |
| | | 344 | | |
| | | 345 | | // filename="=?utf-8?B?BDFSDFasdfasdc==?=" |
| | 0 | 346 | | if (TryDecodeMime(nameParameter.Value, out result)) |
| | 0 | 347 | | { |
| | 0 | 348 | | return result; |
| | | 349 | | } |
| | | 350 | | // May not have been encoded. |
| | 0 | 351 | | return IsQuoted(nameParameter.Value) ? nameParameter.Value!.Substring(1, nameParameter.Value.Length - 2) |
| | | 352 | | } |
| | 0 | 353 | | return null; |
| | 0 | 354 | | } |
| | | 355 | | |
| | | 356 | | // Add/update the given parameter in the list, encoding if necessary. |
| | | 357 | | // Remove if value is null/Empty |
| | | 358 | | private void SetName(string parameter, string? value) |
| | 0 | 359 | | { |
| | 0 | 360 | | NameValueHeaderValue? nameParameter = NameValueHeaderValue.Find(_parameters, parameter); |
| | 0 | 361 | | if (string.IsNullOrEmpty(value)) |
| | 0 | 362 | | { |
| | | 363 | | // Remove parameter. |
| | 0 | 364 | | if (nameParameter != null) |
| | 0 | 365 | | { |
| | 0 | 366 | | _parameters!.Remove(nameParameter); |
| | 0 | 367 | | } |
| | 0 | 368 | | } |
| | | 369 | | else |
| | 0 | 370 | | { |
| | | 371 | | string processedValue; |
| | 0 | 372 | | if (parameter.EndsWith('*')) |
| | 0 | 373 | | { |
| | 0 | 374 | | processedValue = HeaderUtilities.Encode5987(value); |
| | 0 | 375 | | } |
| | | 376 | | else |
| | 0 | 377 | | { |
| | 0 | 378 | | processedValue = EncodeAndQuoteMime(value); |
| | 0 | 379 | | } |
| | | 380 | | |
| | 0 | 381 | | if (nameParameter != null) |
| | 0 | 382 | | { |
| | 0 | 383 | | nameParameter.Value = processedValue; |
| | 0 | 384 | | } |
| | | 385 | | else |
| | 0 | 386 | | { |
| | 0 | 387 | | Parameters.Add(new NameValueHeaderValue(parameter, processedValue)); |
| | 0 | 388 | | } |
| | 0 | 389 | | } |
| | 0 | 390 | | } |
| | | 391 | | |
| | | 392 | | // Returns input for decoding failures, as the content might not be encoded. |
| | | 393 | | private static string EncodeAndQuoteMime(string input) |
| | 0 | 394 | | { |
| | 0 | 395 | | string result = input; |
| | 0 | 396 | | bool needsQuotes = false; |
| | | 397 | | // Remove bounding quotes, they'll get re-added later. |
| | 0 | 398 | | if (IsQuoted(result)) |
| | 0 | 399 | | { |
| | 0 | 400 | | result = result.Substring(1, result.Length - 2); |
| | 0 | 401 | | needsQuotes = true; |
| | 0 | 402 | | } |
| | | 403 | | |
| | 0 | 404 | | if (result.Contains('"')) // Only bounding quotes are allowed. |
| | 0 | 405 | | { |
| | 0 | 406 | | throw new ArgumentException(SR.Format(CultureInfo.InvariantCulture, |
| | 0 | 407 | | SR.net_http_headers_invalid_value, input)); |
| | | 408 | | } |
| | 0 | 409 | | else if (!Ascii.IsValid(result)) |
| | 0 | 410 | | { |
| | 0 | 411 | | needsQuotes = true; // Encoded data must always be quoted, the equals signs are invalid in tokens. |
| | 0 | 412 | | result = EncodeMime(result); // =?utf-8?B?asdfasdfaesdf?= |
| | 0 | 413 | | } |
| | 0 | 414 | | else if (!needsQuotes && HttpRuleParser.GetTokenLength(result, 0) != result.Length) |
| | 0 | 415 | | { |
| | 0 | 416 | | needsQuotes = true; |
| | 0 | 417 | | } |
| | | 418 | | |
| | 0 | 419 | | if (needsQuotes) |
| | 0 | 420 | | { |
| | | 421 | | // Re-add quotes "value". |
| | 0 | 422 | | result = "\"" + result + "\""; |
| | 0 | 423 | | } |
| | 0 | 424 | | return result; |
| | 0 | 425 | | } |
| | | 426 | | |
| | | 427 | | // Returns true if the value starts and ends with a quote. |
| | | 428 | | private static bool IsQuoted(ReadOnlySpan<char> value) |
| | 0 | 429 | | { |
| | 0 | 430 | | return |
| | 0 | 431 | | value.Length > 1 && |
| | 0 | 432 | | value[0] == '"' && |
| | 0 | 433 | | value[value.Length - 1] == '"'; |
| | 0 | 434 | | } |
| | | 435 | | |
| | | 436 | | // Encode using MIME encoding. |
| | | 437 | | private static string EncodeMime(string input) |
| | 0 | 438 | | { |
| | 0 | 439 | | byte[] buffer = Encoding.UTF8.GetBytes(input); |
| | 0 | 440 | | string encodedName = Convert.ToBase64String(buffer); |
| | 0 | 441 | | return "=?utf-8?B?" + encodedName + "?="; |
| | 0 | 442 | | } |
| | | 443 | | |
| | | 444 | | // Attempt to decode MIME encoded strings. |
| | | 445 | | private static bool TryDecodeMime(string? input, [NotNullWhen(true)] out string? output) |
| | 0 | 446 | | { |
| | 0 | 447 | | Debug.Assert(input != null); |
| | | 448 | | |
| | 0 | 449 | | output = null; |
| | 0 | 450 | | string? processedInput = input; |
| | | 451 | | // Require quotes, min of "=?e?b??=" |
| | 0 | 452 | | if (!IsQuoted(processedInput) || processedInput.Length < 10) |
| | 0 | 453 | | { |
| | 0 | 454 | | return false; |
| | | 455 | | } |
| | | 456 | | |
| | 0 | 457 | | Span<Range> parts = stackalloc Range[6]; |
| | 0 | 458 | | ReadOnlySpan<char> processedInputSpan = processedInput; |
| | | 459 | | // "=, encodingName, encodingType, encodedData, =" |
| | 0 | 460 | | if (processedInputSpan.Split(parts, '?') != 5 || |
| | 0 | 461 | | processedInputSpan[parts[0]] is not "\"=" || |
| | 0 | 462 | | processedInputSpan[parts[4]] is not "=\"" || |
| | 0 | 463 | | !processedInputSpan[parts[2]].Equals("b", StringComparison.OrdinalIgnoreCase)) |
| | 0 | 464 | | { |
| | | 465 | | // Not encoded. |
| | | 466 | | // This does not support multi-line encoding. |
| | | 467 | | // Only base64 encoding is supported, not quoted printable. |
| | 0 | 468 | | return false; |
| | | 469 | | } |
| | | 470 | | |
| | | 471 | | try |
| | 0 | 472 | | { |
| | 0 | 473 | | Encoding encoding = Encoding.GetEncoding(processedInput[parts[1]]); |
| | 0 | 474 | | byte[] bytes = Convert.FromBase64String(processedInput[parts[3]]); |
| | 0 | 475 | | output = encoding.GetString(bytes, 0, bytes.Length); |
| | 0 | 476 | | return true; |
| | | 477 | | } |
| | 0 | 478 | | catch (ArgumentException) |
| | 0 | 479 | | { |
| | | 480 | | // Unknown encoding or bad characters. |
| | 0 | 481 | | } |
| | 0 | 482 | | catch (FormatException) |
| | 0 | 483 | | { |
| | | 484 | | // Bad base64 decoding. |
| | 0 | 485 | | } |
| | 0 | 486 | | return false; |
| | 0 | 487 | | } |
| | | 488 | | |
| | | 489 | | |
| | | 490 | | // Attempt to decode using RFC 5987 encoding. |
| | | 491 | | // encoding'language'my%20string |
| | | 492 | | private static bool TryDecode5987(string input, out string? output) |
| | 0 | 493 | | { |
| | 0 | 494 | | output = null; |
| | | 495 | | |
| | 0 | 496 | | int quoteIndex = input.IndexOf('\''); |
| | 0 | 497 | | if (quoteIndex == -1) |
| | 0 | 498 | | { |
| | 0 | 499 | | return false; |
| | | 500 | | } |
| | | 501 | | |
| | 0 | 502 | | int lastQuoteIndex = input.LastIndexOf('\''); |
| | 0 | 503 | | if (quoteIndex == lastQuoteIndex || input.IndexOf('\'', quoteIndex + 1) != lastQuoteIndex) |
| | 0 | 504 | | { |
| | 0 | 505 | | return false; |
| | | 506 | | } |
| | | 507 | | |
| | 0 | 508 | | string encodingString = input.Substring(0, quoteIndex); |
| | 0 | 509 | | string dataString = input.Substring(lastQuoteIndex + 1); |
| | | 510 | | |
| | 0 | 511 | | StringBuilder decoded = new StringBuilder(); |
| | | 512 | | try |
| | 0 | 513 | | { |
| | 0 | 514 | | Encoding encoding = Encoding.GetEncoding(encodingString); |
| | | 515 | | |
| | 0 | 516 | | byte[] unescapedBytes = new byte[dataString.Length]; |
| | 0 | 517 | | int unescapedBytesCount = 0; |
| | 0 | 518 | | for (int index = 0; index < dataString.Length; index++) |
| | 0 | 519 | | { |
| | 0 | 520 | | if (Uri.IsHexEncoding(dataString, index)) // %FF |
| | 0 | 521 | | { |
| | | 522 | | // Unescape and cache bytes, multi-byte characters must be decoded all at once. |
| | 0 | 523 | | unescapedBytes[unescapedBytesCount++] = (byte)Uri.HexUnescape(dataString, ref index); |
| | 0 | 524 | | index--; // HexUnescape did +=3; Offset the for loop's ++ |
| | 0 | 525 | | } |
| | | 526 | | else |
| | 0 | 527 | | { |
| | 0 | 528 | | if (unescapedBytesCount > 0) |
| | 0 | 529 | | { |
| | | 530 | | // Decode any previously cached bytes. |
| | 0 | 531 | | decoded.Append(encoding.GetString(unescapedBytes, 0, unescapedBytesCount)); |
| | 0 | 532 | | unescapedBytesCount = 0; |
| | 0 | 533 | | } |
| | 0 | 534 | | decoded.Append(dataString[index]); // Normal safe character. |
| | 0 | 535 | | } |
| | 0 | 536 | | } |
| | | 537 | | |
| | 0 | 538 | | if (unescapedBytesCount > 0) |
| | 0 | 539 | | { |
| | | 540 | | // Decode any previously cached bytes. |
| | 0 | 541 | | decoded.Append(encoding.GetString(unescapedBytes, 0, unescapedBytesCount)); |
| | 0 | 542 | | } |
| | 0 | 543 | | } |
| | 0 | 544 | | catch (ArgumentException) |
| | 0 | 545 | | { |
| | 0 | 546 | | return false; // Unknown encoding or bad characters. |
| | | 547 | | } |
| | | 548 | | |
| | 0 | 549 | | output = decoded.ToString(); |
| | 0 | 550 | | return true; |
| | 0 | 551 | | } |
| | | 552 | | #endregion Helpers |
| | | 553 | | } |
| | | 554 | | } |