| | | 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 | | |
| | | 8 | | namespace System.Net.Http |
| | | 9 | | { |
| | | 10 | | /// <summary> |
| | | 11 | | /// Represents a collection of options for an HTTP request. |
| | | 12 | | /// </summary> |
| | | 13 | | [DebuggerDisplay("{DebuggerToString(),nq}")] |
| | | 14 | | [DebuggerTypeProxy(typeof(HttpRequestOptionsDebugView))] |
| | | 15 | | public sealed class HttpRequestOptions : IDictionary<string, object?>, IReadOnlyDictionary<string, object?> |
| | | 16 | | { |
| | 0 | 17 | | private Dictionary<string, object?> Options { get; } = new Dictionary<string, object?>(); |
| | 0 | 18 | | bool IReadOnlyDictionary<string, object?>.TryGetValue(string key, out object? value) => Options.TryGetValue(key, |
| | 0 | 19 | | object? IReadOnlyDictionary<string, object?>.this[string key] => Options[key]; |
| | 0 | 20 | | IEnumerable<string> IReadOnlyDictionary<string, object?>.Keys => Options.Keys; |
| | 0 | 21 | | IEnumerable<object?> IReadOnlyDictionary<string, object?>.Values => Options.Values; |
| | | 22 | | object? IDictionary<string, object?>.this[string key] |
| | | 23 | | { |
| | | 24 | | get |
| | 0 | 25 | | { |
| | 0 | 26 | | return Options[key]; |
| | 0 | 27 | | } |
| | | 28 | | set |
| | 0 | 29 | | { |
| | 0 | 30 | | Options[key] = value; |
| | 0 | 31 | | } |
| | | 32 | | } |
| | 0 | 33 | | ICollection<string> IDictionary<string, object?>.Keys => Options.Keys; |
| | 0 | 34 | | ICollection<object?> IDictionary<string, object?>.Values => Options.Values; |
| | 0 | 35 | | int ICollection<KeyValuePair<string, object?>>.Count => Options.Count; |
| | 0 | 36 | | bool ICollection<KeyValuePair<string, object?>>.IsReadOnly => ((IDictionary<string, object?>)Options).IsReadOnly |
| | 0 | 37 | | void IDictionary<string, object?>.Add(string key, object? value) => Options.Add(key, value); |
| | 0 | 38 | | void ICollection<KeyValuePair<string, object?>>.Add(KeyValuePair<string, object?> item) => ((IDictionary<string, |
| | 0 | 39 | | void ICollection<KeyValuePair<string, object?>>.Clear() => Options.Clear(); |
| | 0 | 40 | | bool ICollection<KeyValuePair<string, object?>>.Contains(KeyValuePair<string, object?> item) => ((IDictionary<st |
| | 0 | 41 | | bool IDictionary<string, object?>.ContainsKey(string key) => Options.ContainsKey(key); |
| | | 42 | | void ICollection<KeyValuePair<string, object?>>.CopyTo(KeyValuePair<string, object?>[] array, int arrayIndex) => |
| | 0 | 43 | | ((IDictionary<string, object?>)Options).CopyTo(array, arrayIndex); |
| | 0 | 44 | | IEnumerator<KeyValuePair<string, object?>> IEnumerable<KeyValuePair<string, object?>>.GetEnumerator() => Options |
| | 0 | 45 | | System.Collections.IEnumerator System.Collections.IEnumerable.GetEnumerator() => ((System.Collections.IEnumerabl |
| | 0 | 46 | | bool IDictionary<string, object?>.Remove(string key) => Options.Remove(key); |
| | 0 | 47 | | bool ICollection<KeyValuePair<string, object?>>.Remove(KeyValuePair<string, object?> item) => ((IDictionary<stri |
| | 0 | 48 | | bool IReadOnlyDictionary<string, object?>.ContainsKey(string key) => Options.ContainsKey(key); |
| | 0 | 49 | | bool IDictionary<string, object?>.TryGetValue(string key, out object? value) => Options.TryGetValue(key, out val |
| | 0 | 50 | | int IReadOnlyCollection<KeyValuePair<string, object?>>.Count => Options.Count; |
| | | 51 | | |
| | | 52 | | /// <summary> |
| | | 53 | | /// Initializes a new instance of the HttpRequestOptions class. |
| | | 54 | | /// </summary> |
| | 0 | 55 | | public HttpRequestOptions() { } |
| | | 56 | | |
| | | 57 | | /// <summary> |
| | | 58 | | /// Gets the value of a given HTTP request option. |
| | | 59 | | /// </summary> |
| | | 60 | | /// <param name="key">Strongly typed key to get the value of HTTP request option. For example <code>new HttpRequ |
| | | 61 | | /// <param name="value">Returns the value of HTTP request option.</param> |
| | | 62 | | /// <typeparam name="TValue">The type of the HTTP value as defined by <code>key</code> parameter.</typeparam> |
| | | 63 | | /// <returns>True, if an option is retrieved.</returns> |
| | | 64 | | public bool TryGetValue<TValue>(HttpRequestOptionsKey<TValue> key, [MaybeNullWhen(false)] out TValue value) |
| | 0 | 65 | | { |
| | 0 | 66 | | if (Options.TryGetValue(key.Key, out object? _value) && _value is TValue tvalue) |
| | 0 | 67 | | { |
| | 0 | 68 | | value = tvalue; |
| | 0 | 69 | | return true; |
| | | 70 | | } |
| | | 71 | | |
| | 0 | 72 | | value = default(TValue); |
| | 0 | 73 | | return false; |
| | 0 | 74 | | } |
| | | 75 | | |
| | | 76 | | /// <summary> |
| | | 77 | | /// Sets the value of a given request option. |
| | | 78 | | /// </summary> |
| | | 79 | | /// <param name="key">Strongly typed key to get the value of HTTP request option. For example <code>new HttpRequ |
| | | 80 | | /// <param name="value">The value of the HTTP request option.</param> |
| | | 81 | | /// <typeparam name="TValue">The type of the HTTP value as defined by <code>key</code> parameter.</typeparam> |
| | | 82 | | public void Set<TValue>(HttpRequestOptionsKey<TValue> key, TValue value) |
| | 0 | 83 | | { |
| | 0 | 84 | | Options[key.Key] = value; |
| | 0 | 85 | | } |
| | | 86 | | |
| | 0 | 87 | | private string DebuggerToString() => $"Count = {Options.Count}"; |
| | | 88 | | |
| | 0 | 89 | | private sealed class HttpRequestOptionsDebugView(HttpRequestOptions options) |
| | | 90 | | { |
| | | 91 | | [DebuggerBrowsable(DebuggerBrowsableState.RootHidden)] |
| | | 92 | | public KeyValuePair<string, object?>[] Items |
| | | 93 | | { |
| | | 94 | | get |
| | 0 | 95 | | { |
| | 0 | 96 | | var dictionary = (IDictionary<string, object?>)options; |
| | 0 | 97 | | var items = new KeyValuePair<string, object?>[dictionary.Count]; |
| | 0 | 98 | | dictionary.CopyTo(items, 0); |
| | 0 | 99 | | return items; |
| | 0 | 100 | | } |
| | | 101 | | } |
| | | 102 | | } |
| | | 103 | | } |
| | | 104 | | } |