| | | 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; |
| | | 5 | | using System.Collections.Generic; |
| | | 6 | | using System.Diagnostics; |
| | | 7 | | using System.Diagnostics.CodeAnalysis; |
| | | 8 | | using System.Globalization; |
| | | 9 | | |
| | | 10 | | namespace System.Net |
| | | 11 | | { |
| | | 12 | | internal sealed class CredentialCacheKey : IEquatable<CredentialCacheKey?> |
| | | 13 | | { |
| | | 14 | | public readonly Uri UriPrefix; |
| | 0 | 15 | | public readonly int UriPrefixLength = -1; |
| | | 16 | | public readonly string AuthenticationType; |
| | | 17 | | |
| | 0 | 18 | | internal CredentialCacheKey(Uri uriPrefix, string authenticationType) |
| | 0 | 19 | | { |
| | 0 | 20 | | Debug.Assert(uriPrefix != null); |
| | 0 | 21 | | Debug.Assert(authenticationType != null); |
| | | 22 | | |
| | 0 | 23 | | UriPrefix = uriPrefix; |
| | 0 | 24 | | UriPrefixLength = UriPrefix.AbsolutePath.LastIndexOf('/'); |
| | 0 | 25 | | AuthenticationType = authenticationType; |
| | 0 | 26 | | } |
| | | 27 | | |
| | | 28 | | internal bool Match(Uri uri, int prefixLen, string authenticationType) |
| | 0 | 29 | | { |
| | 0 | 30 | | if (uri == null || authenticationType == null) |
| | 0 | 31 | | { |
| | 0 | 32 | | return false; |
| | | 33 | | } |
| | | 34 | | |
| | | 35 | | // If the protocols don't match, this credential is not applicable for the given Uri. |
| | 0 | 36 | | if (!string.Equals(authenticationType, AuthenticationType, StringComparison.OrdinalIgnoreCase)) |
| | 0 | 37 | | { |
| | 0 | 38 | | return false; |
| | | 39 | | } |
| | | 40 | | |
| | 0 | 41 | | if (NetEventSource.Log.IsEnabled()) NetEventSource.Info(this, $"Match({UriPrefix} & {uri})"); |
| | | 42 | | |
| | 0 | 43 | | return IsPrefix(uri, prefixLen); |
| | 0 | 44 | | } |
| | | 45 | | |
| | | 46 | | // IsPrefix (Uri) |
| | | 47 | | // |
| | | 48 | | // Determines whether <this> is a prefix of this URI. A prefix |
| | | 49 | | // match is defined as: |
| | | 50 | | // |
| | | 51 | | // scheme match |
| | | 52 | | // + host match |
| | | 53 | | // + port match, if any |
| | | 54 | | // + <prefix> path is a prefix of <URI> path, if any |
| | | 55 | | // |
| | | 56 | | // Returns: |
| | | 57 | | // True if <prefixUri> is a prefix of this URI |
| | | 58 | | private bool IsPrefix(Uri uri, int prefixLen) |
| | 0 | 59 | | { |
| | 0 | 60 | | Debug.Assert(uri != null); |
| | 0 | 61 | | Uri uriPrefix = UriPrefix; |
| | | 62 | | |
| | 0 | 63 | | if (uriPrefix.Scheme != uri.Scheme || uriPrefix.Host != uri.Host || uriPrefix.Port != uri.Port) |
| | 0 | 64 | | { |
| | 0 | 65 | | return false; |
| | | 66 | | } |
| | | 67 | | |
| | 0 | 68 | | if (UriPrefixLength > prefixLen) |
| | 0 | 69 | | { |
| | 0 | 70 | | return false; |
| | | 71 | | } |
| | | 72 | | |
| | 0 | 73 | | return string.Compare(uri.AbsolutePath, 0, uriPrefix.AbsolutePath, 0, UriPrefixLength, StringComparison.Ordi |
| | 0 | 74 | | } |
| | | 75 | | |
| | | 76 | | public override int GetHashCode() => |
| | 0 | 77 | | StringComparer.OrdinalIgnoreCase.GetHashCode(AuthenticationType) ^ |
| | 0 | 78 | | UriPrefix.GetHashCode(); |
| | | 79 | | |
| | | 80 | | public bool Equals([NotNullWhen(true)] CredentialCacheKey? other) |
| | 0 | 81 | | { |
| | 0 | 82 | | if (other == null) |
| | 0 | 83 | | { |
| | 0 | 84 | | return false; |
| | | 85 | | } |
| | | 86 | | |
| | 0 | 87 | | bool equals = |
| | 0 | 88 | | string.Equals(AuthenticationType, other.AuthenticationType, StringComparison.OrdinalIgnoreCase) && |
| | 0 | 89 | | UriPrefix.Equals(other.UriPrefix); |
| | | 90 | | |
| | 0 | 91 | | if (NetEventSource.Log.IsEnabled()) NetEventSource.Info(this, $"Equals({this},{other}) returns {equals}"); |
| | | 92 | | |
| | 0 | 93 | | return equals; |
| | 0 | 94 | | } |
| | | 95 | | |
| | 0 | 96 | | public override bool Equals([NotNullWhen(true)] object? obj) => Equals(obj as CredentialCacheKey); |
| | | 97 | | |
| | | 98 | | public override string ToString() => |
| | 0 | 99 | | string.Create(CultureInfo.InvariantCulture, $"[{UriPrefixLength}]:{UriPrefix}:{AuthenticationType}"); |
| | | 100 | | } |
| | | 101 | | |
| | | 102 | | internal static class CredentialCacheHelper |
| | | 103 | | { |
| | | 104 | | public static bool TryGetCredential(Dictionary<CredentialCacheKey, NetworkCredential> cache, Uri uriPrefix, stri |
| | | 105 | | { |
| | | 106 | | int longestMatchPrefix = -1; |
| | | 107 | | mostSpecificMatch = null; |
| | | 108 | | mostSpecificMatchUri = null; |
| | | 109 | | |
| | | 110 | | if (cache.Count == 0) |
| | | 111 | | { |
| | | 112 | | return false; |
| | | 113 | | } |
| | | 114 | | |
| | | 115 | | // precompute the length of the prefix |
| | | 116 | | int uriPrefixLength = uriPrefix.AbsolutePath.LastIndexOf('/'); |
| | | 117 | | |
| | | 118 | | // Enumerate through every credential in the cache, get match with longest prefix |
| | | 119 | | foreach ((CredentialCacheKey key, NetworkCredential value) in cache) |
| | | 120 | | { |
| | | 121 | | int prefixLen = key.UriPrefixLength; |
| | | 122 | | |
| | | 123 | | if (prefixLen <= longestMatchPrefix) |
| | | 124 | | { |
| | | 125 | | // this credential can't provide a longer prefix match |
| | | 126 | | continue; |
| | | 127 | | } |
| | | 128 | | |
| | | 129 | | // Determine if this credential is applicable to the current Uri/AuthType |
| | | 130 | | if (key.Match(uriPrefix, uriPrefixLength, authType)) |
| | | 131 | | { |
| | | 132 | | // update the information about currently preferred match |
| | | 133 | | longestMatchPrefix = prefixLen; |
| | | 134 | | mostSpecificMatch = value; |
| | | 135 | | mostSpecificMatchUri = key.UriPrefix; |
| | | 136 | | |
| | | 137 | | if (uriPrefixLength == prefixLen) |
| | | 138 | | { |
| | | 139 | | // we can't get any better than this |
| | | 140 | | break; |
| | | 141 | | } |
| | | 142 | | } |
| | | 143 | | } |
| | | 144 | | |
| | | 145 | | return mostSpecificMatch != null; |
| | | 146 | | } |
| | | 147 | | } |
| | | 148 | | } |