| | | 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; |
| | | 5 | | using System.Collections.Generic; |
| | | 6 | | using System.Collections.ObjectModel; |
| | | 7 | | using System.Collections.Specialized; |
| | | 8 | | using System.Diagnostics; |
| | | 9 | | using CoreWCF.Runtime; |
| | | 10 | | |
| | | 11 | | namespace CoreWCF |
| | | 12 | | { |
| | | 13 | | public class UriTemplateTable |
| | | 14 | | { |
| | | 15 | | private Uri _baseAddress; |
| | | 16 | | private string _basePath; |
| | | 17 | | private Dictionary<string, FastPathInfo> _fastPathTable; // key is uri.PathAndQuery, fastPathTable may be null |
| | | 18 | | private bool _noTemplateHasQueryPart; |
| | | 19 | | private int _numSegmentsInBaseAddress; |
| | | 20 | | private UriTemplateTrieNode _rootNode; |
| | | 21 | | private readonly UriTemplatesCollection _templates; |
| | | 22 | | private readonly object _thisLock; |
| | | 23 | | private readonly bool _addTrailingSlashToBaseAddress; |
| | | 24 | | |
| | | 25 | | public UriTemplateTable() |
| | 0 | 26 | | : this(null, null, true) |
| | | 27 | | { |
| | 0 | 28 | | } |
| | | 29 | | |
| | | 30 | | public UriTemplateTable(IEnumerable<KeyValuePair<UriTemplate, object>> keyValuePairs) |
| | 0 | 31 | | : this(null, keyValuePairs, true) |
| | | 32 | | { |
| | 0 | 33 | | } |
| | | 34 | | |
| | | 35 | | public UriTemplateTable(Uri baseAddress) |
| | 47 | 36 | | : this(baseAddress, null, true) |
| | | 37 | | { |
| | 47 | 38 | | } |
| | | 39 | | |
| | | 40 | | internal UriTemplateTable(Uri baseAddress, bool addTrailingSlashToBaseAddress) |
| | 0 | 41 | | : this(baseAddress, null, addTrailingSlashToBaseAddress) |
| | | 42 | | { |
| | 0 | 43 | | } |
| | | 44 | | |
| | | 45 | | public UriTemplateTable(Uri baseAddress, IEnumerable<KeyValuePair<UriTemplate, object>> keyValuePairs) |
| | 0 | 46 | | : this(baseAddress, keyValuePairs, true) |
| | | 47 | | { |
| | 0 | 48 | | } |
| | | 49 | | |
| | 47 | 50 | | internal UriTemplateTable(Uri baseAddress, IEnumerable<KeyValuePair<UriTemplate, object>> keyValuePairs, bool ad |
| | | 51 | | { |
| | 47 | 52 | | if (baseAddress != null && !baseAddress.IsAbsoluteUri) |
| | | 53 | | { |
| | 0 | 54 | | throw DiagnosticUtility.ExceptionUtility.ThrowHelperArgument(nameof(baseAddress), SR.UTTMustBeAbsolute); |
| | | 55 | | } |
| | | 56 | | |
| | 47 | 57 | | _addTrailingSlashToBaseAddress = addTrailingSlashToBaseAddress; |
| | 47 | 58 | | OriginalBaseAddress = baseAddress; |
| | | 59 | | |
| | 47 | 60 | | if (keyValuePairs != null) |
| | | 61 | | { |
| | 0 | 62 | | _templates = new UriTemplatesCollection(keyValuePairs); |
| | | 63 | | } |
| | | 64 | | else |
| | | 65 | | { |
| | 47 | 66 | | _templates = new UriTemplatesCollection(); |
| | | 67 | | } |
| | | 68 | | |
| | 47 | 69 | | _thisLock = new object(); |
| | 47 | 70 | | _baseAddress = baseAddress; |
| | 47 | 71 | | NormalizeBaseAddress(); |
| | 47 | 72 | | } |
| | | 73 | | |
| | | 74 | | public Uri BaseAddress |
| | | 75 | | { |
| | | 76 | | get |
| | | 77 | | { |
| | 0 | 78 | | return _baseAddress; |
| | | 79 | | } |
| | | 80 | | set |
| | | 81 | | { |
| | 0 | 82 | | if (value == null) |
| | | 83 | | { |
| | 0 | 84 | | throw DiagnosticUtility.ExceptionUtility.ThrowHelperArgumentNull(nameof(value)); |
| | | 85 | | } |
| | | 86 | | |
| | 0 | 87 | | lock (_thisLock) |
| | | 88 | | { |
| | 0 | 89 | | if (IsReadOnly) |
| | | 90 | | { |
| | 0 | 91 | | throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(new InvalidOperationException( |
| | 0 | 92 | | SR.UTTCannotChangeBaseAddress)); |
| | | 93 | | } |
| | | 94 | | else |
| | | 95 | | { |
| | 0 | 96 | | if (!value.IsAbsoluteUri) |
| | | 97 | | { |
| | 0 | 98 | | throw DiagnosticUtility.ExceptionUtility.ThrowHelperArgument(nameof(value), SR.UTTBaseAddres |
| | | 99 | | } |
| | | 100 | | else |
| | | 101 | | { |
| | 0 | 102 | | OriginalBaseAddress = value; |
| | 0 | 103 | | _baseAddress = value; |
| | 0 | 104 | | NormalizeBaseAddress(); |
| | | 105 | | } |
| | | 106 | | } |
| | 0 | 107 | | } |
| | 0 | 108 | | } |
| | | 109 | | } |
| | | 110 | | |
| | 197 | 111 | | public Uri OriginalBaseAddress { get; private set; } |
| | | 112 | | |
| | 83 | 113 | | public bool IsReadOnly => _templates.IsFrozen; |
| | | 114 | | |
| | 163 | 115 | | public IList<KeyValuePair<UriTemplate, object>> KeyValuePairs => _templates; |
| | | 116 | | |
| | | 117 | | public void MakeReadOnly(bool allowDuplicateEquivalentUriTemplates) |
| | | 118 | | { |
| | | 119 | | // idempotent |
| | 83 | 120 | | lock (_thisLock) |
| | | 121 | | { |
| | 83 | 122 | | if (!IsReadOnly) |
| | | 123 | | { |
| | 47 | 124 | | _templates.Freeze(); |
| | 47 | 125 | | Validate(allowDuplicateEquivalentUriTemplates); |
| | 47 | 126 | | ConstructFastPathTable(); |
| | | 127 | | } |
| | 83 | 128 | | } |
| | 83 | 129 | | } |
| | | 130 | | |
| | | 131 | | public Collection<UriTemplateMatch> Match(Uri uri) |
| | | 132 | | { |
| | 36 | 133 | | if (uri == null) |
| | | 134 | | { |
| | 0 | 135 | | throw DiagnosticUtility.ExceptionUtility.ThrowHelperArgumentNull(nameof(uri)); |
| | | 136 | | } |
| | | 137 | | |
| | 36 | 138 | | if (!uri.IsAbsoluteUri) |
| | | 139 | | { |
| | 0 | 140 | | return None(); |
| | | 141 | | } |
| | | 142 | | |
| | 36 | 143 | | MakeReadOnly(true); |
| | | 144 | | |
| | | 145 | | // Matching path : |
| | 36 | 146 | | if (!FastComputeRelativeSegmentsAndLookup(uri, out Collection<string> relativeSegments, out IList<UriTemplat |
| | | 147 | | { |
| | 1 | 148 | | return None(); |
| | | 149 | | } |
| | | 150 | | |
| | | 151 | | // Matching query : |
| | 35 | 152 | | NameValueCollection queryParameters = null; |
| | 35 | 153 | | if (!_noTemplateHasQueryPart && AtLeastOneCandidateHasQueryPart(candidates)) |
| | | 154 | | { |
| | 1 | 155 | | Collection<UriTemplateTableMatchCandidate> nextCandidates = new Collection<UriTemplateTableMatchCandidat |
| | | 156 | | Fx.Assert(nextCandidates.Count == 0, "nextCandidates should be empty"); |
| | | 157 | | |
| | | 158 | | // then deal with query |
| | 1 | 159 | | queryParameters = UriTemplateHelpers.ParseQueryString(uri.Query); |
| | 1 | 160 | | bool mustBeEspeciallyInteresting = NoCandidateHasQueryLiteralRequirementsAndThereIsAnEmptyFallback(candi |
| | 4 | 161 | | for (int i = 0; i < candidates.Count; i++) |
| | | 162 | | { |
| | 1 | 163 | | if (UriTemplateHelpers.CanMatchQueryInterestingly(candidates[i].Template, queryParameters, mustBeEsp |
| | | 164 | | { |
| | 1 | 165 | | nextCandidates.Add(candidates[i]); |
| | | 166 | | } |
| | | 167 | | } |
| | | 168 | | |
| | 1 | 169 | | if (nextCandidates.Count > 1) |
| | | 170 | | { |
| | | 171 | | Fx.Assert(AllEquivalent(nextCandidates, 0, nextCandidates.Count), "demux algorithm problem, multiple |
| | | 172 | | } |
| | | 173 | | |
| | 1 | 174 | | if (nextCandidates.Count == 0) |
| | | 175 | | { |
| | 0 | 176 | | for (int i = 0; i < candidates.Count; i++) |
| | | 177 | | { |
| | 0 | 178 | | if (UriTemplateHelpers.CanMatchQueryTrivially(candidates[i].Template)) |
| | | 179 | | { |
| | 0 | 180 | | nextCandidates.Add(candidates[i]); |
| | | 181 | | } |
| | | 182 | | } |
| | | 183 | | } |
| | | 184 | | |
| | 1 | 185 | | if (nextCandidates.Count == 0) |
| | | 186 | | { |
| | 0 | 187 | | return None(); |
| | | 188 | | } |
| | | 189 | | |
| | 1 | 190 | | if (nextCandidates.Count > 1) |
| | | 191 | | { |
| | | 192 | | Fx.Assert(AllEquivalent(nextCandidates, 0, nextCandidates.Count), "demux algorithm problem, multiple |
| | | 193 | | } |
| | | 194 | | |
| | 1 | 195 | | candidates = nextCandidates; |
| | | 196 | | } |
| | | 197 | | |
| | | 198 | | // Verifying that we have not broken the allowDuplicates settings because of terminal defaults |
| | | 199 | | // This situation can be caused when we are hosting ".../" and ".../{foo=xyz}" in the same |
| | | 200 | | // table. They are not equivalent; yet they reside together in the same path partially-equivalent |
| | | 201 | | // set. If we hit a uri that ends up in that particular end-of-path set, we want to provide the |
| | | 202 | | // user only the 'best' match and not both; thus preventing inconsistancy between the MakeReadonly |
| | | 203 | | // settings and the matching results. We will assume that the 'best' matches will be the ones with |
| | | 204 | | // the smallest number of segments - this will prefer ".../" over ".../{x=1}[/...]". |
| | 35 | 205 | | if (NotAllCandidatesArePathFullyEquivalent(candidates)) |
| | | 206 | | { |
| | 0 | 207 | | Collection<UriTemplateTableMatchCandidate> nextCandidates = new Collection<UriTemplateTableMatchCandidat |
| | 0 | 208 | | int minSegmentsCount = -1; |
| | 0 | 209 | | for (int i = 0; i < candidates.Count; i++) |
| | | 210 | | { |
| | 0 | 211 | | UriTemplateTableMatchCandidate candidate = candidates[i]; |
| | 0 | 212 | | if (minSegmentsCount == -1) |
| | | 213 | | { |
| | 0 | 214 | | minSegmentsCount = candidate.Template._segments.Count; |
| | 0 | 215 | | nextCandidates.Add(candidate); |
| | | 216 | | } |
| | 0 | 217 | | else if (candidate.Template._segments.Count < minSegmentsCount) |
| | | 218 | | { |
| | 0 | 219 | | minSegmentsCount = candidate.Template._segments.Count; |
| | 0 | 220 | | nextCandidates.Clear(); |
| | 0 | 221 | | nextCandidates.Add(candidate); |
| | | 222 | | } |
| | 0 | 223 | | else if (candidate.Template._segments.Count == minSegmentsCount) |
| | | 224 | | { |
| | 0 | 225 | | nextCandidates.Add(candidate); |
| | | 226 | | } |
| | | 227 | | } |
| | | 228 | | |
| | | 229 | | Fx.Assert(minSegmentsCount != -1, "At least the first entry in the list should be kept"); |
| | | 230 | | Fx.Assert(nextCandidates.Count >= 1, "At least the first entry in the list should be kept"); |
| | | 231 | | Fx.Assert(nextCandidates[0].Template._segments.Count == minSegmentsCount, "Trivial"); |
| | | 232 | | |
| | 0 | 233 | | candidates = nextCandidates; |
| | | 234 | | } |
| | | 235 | | |
| | | 236 | | // Building the actual result |
| | 35 | 237 | | Collection<UriTemplateMatch> actualResults = new Collection<UriTemplateMatch>(); |
| | 140 | 238 | | for (int i = 0; i < candidates.Count; i++) |
| | | 239 | | { |
| | 35 | 240 | | UriTemplateTableMatchCandidate candidate = candidates[i]; |
| | 35 | 241 | | UriTemplateMatch match = candidate.Template.CreateUriTemplateMatch(OriginalBaseAddress, |
| | 35 | 242 | | uri, candidate.Data, candidate.SegmentsCount, relativeSegments, queryParameters); |
| | 35 | 243 | | actualResults.Add(match); |
| | | 244 | | } |
| | | 245 | | |
| | 35 | 246 | | return actualResults; |
| | | 247 | | } |
| | | 248 | | |
| | | 249 | | public UriTemplateMatch MatchSingle(Uri uri) |
| | | 250 | | { |
| | 36 | 251 | | Collection<UriTemplateMatch> c = Match(uri); |
| | 36 | 252 | | if (c.Count == 0) |
| | | 253 | | { |
| | 1 | 254 | | return null; |
| | | 255 | | } |
| | | 256 | | |
| | 35 | 257 | | if (c.Count == 1) |
| | | 258 | | { |
| | 35 | 259 | | return c[0]; |
| | | 260 | | } |
| | | 261 | | |
| | 0 | 262 | | throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(new UriTemplateMatchException(SR.UTTMultipleMatche |
| | | 263 | | } |
| | | 264 | | |
| | | 265 | | private static bool AllEquivalent(IList<UriTemplateTableMatchCandidate> list, int a, int b) |
| | | 266 | | { |
| | 0 | 267 | | for (int i = a; i < b - 1; ++i) |
| | | 268 | | { |
| | 0 | 269 | | if (!list[i].Template.IsPathPartiallyEquivalentAt(list[i + 1].Template, list[i].SegmentsCount)) |
| | | 270 | | { |
| | 0 | 271 | | return false; |
| | | 272 | | } |
| | | 273 | | |
| | 0 | 274 | | if (!list[i].Template.IsQueryEquivalent(list[i + 1].Template)) |
| | | 275 | | { |
| | 0 | 276 | | return false; |
| | | 277 | | } |
| | | 278 | | } |
| | | 279 | | |
| | 0 | 280 | | return true; |
| | | 281 | | } |
| | | 282 | | |
| | | 283 | | private static bool AtLeastOneCandidateHasQueryPart(IList<UriTemplateTableMatchCandidate> candidates) |
| | | 284 | | { |
| | 26 | 285 | | for (int i = 0; i < candidates.Count; i++) |
| | | 286 | | { |
| | 7 | 287 | | if (!UriTemplateHelpers.CanMatchQueryTrivially(candidates[i].Template)) |
| | | 288 | | { |
| | 1 | 289 | | return true; |
| | | 290 | | } |
| | | 291 | | } |
| | | 292 | | |
| | 6 | 293 | | return false; |
| | | 294 | | } |
| | | 295 | | |
| | | 296 | | private static bool NoCandidateHasQueryLiteralRequirementsAndThereIsAnEmptyFallback( |
| | | 297 | | IList<UriTemplateTableMatchCandidate> candidates) |
| | | 298 | | { |
| | 1 | 299 | | bool thereIsAmEmptyFallback = false; |
| | 4 | 300 | | for (int i = 0; i < candidates.Count; i++) |
| | | 301 | | { |
| | 1 | 302 | | if (UriTemplateHelpers.HasQueryLiteralRequirements(candidates[i].Template)) |
| | | 303 | | { |
| | 0 | 304 | | return false; |
| | | 305 | | } |
| | | 306 | | |
| | 1 | 307 | | if (candidates[i].Template._queries.Count == 0) |
| | | 308 | | { |
| | 0 | 309 | | thereIsAmEmptyFallback = true; |
| | | 310 | | } |
| | | 311 | | } |
| | | 312 | | |
| | 1 | 313 | | return thereIsAmEmptyFallback; |
| | | 314 | | } |
| | | 315 | | |
| | | 316 | | private static Collection<UriTemplateMatch> None() |
| | | 317 | | { |
| | 1 | 318 | | return new Collection<UriTemplateMatch>(); |
| | | 319 | | } |
| | | 320 | | |
| | | 321 | | private static bool NotAllCandidatesArePathFullyEquivalent(IList<UriTemplateTableMatchCandidate> candidates) |
| | | 322 | | { |
| | 35 | 323 | | if (candidates.Count <= 1) |
| | | 324 | | { |
| | 35 | 325 | | return false; |
| | | 326 | | } |
| | | 327 | | |
| | 0 | 328 | | int segmentsCount = -1; |
| | 0 | 329 | | for (int i = 0; i < candidates.Count; i++) |
| | | 330 | | { |
| | 0 | 331 | | if (segmentsCount == -1) |
| | | 332 | | { |
| | 0 | 333 | | segmentsCount = candidates[i].Template._segments.Count; |
| | | 334 | | } |
| | 0 | 335 | | else if (segmentsCount != candidates[i].Template._segments.Count) |
| | | 336 | | { |
| | 0 | 337 | | return true; |
| | | 338 | | } |
| | | 339 | | } |
| | | 340 | | |
| | 0 | 341 | | return false; |
| | | 342 | | } |
| | | 343 | | |
| | | 344 | | private bool ComputeRelativeSegmentsAndLookup(Uri uri, |
| | | 345 | | ICollection<string> relativePathSegments, // add to this |
| | | 346 | | ICollection<UriTemplateTableMatchCandidate> candidates) // matched candidates |
| | | 347 | | { |
| | 122 | 348 | | string[] uriSegments = uri.Segments; |
| | 122 | 349 | | int numRelativeSegments = uriSegments.Length - _numSegmentsInBaseAddress; |
| | | 350 | | Fx.Assert(numRelativeSegments >= 0, "bad num segments"); |
| | 122 | 351 | | UriTemplateLiteralPathSegment[] uSegments = new UriTemplateLiteralPathSegment[numRelativeSegments]; |
| | 504 | 352 | | for (int i = 0; i < numRelativeSegments; ++i) |
| | | 353 | | { |
| | 130 | 354 | | string seg = uriSegments[i + _numSegmentsInBaseAddress]; |
| | | 355 | | // compute representation for matching |
| | 130 | 356 | | UriTemplateLiteralPathSegment lps = UriTemplateLiteralPathSegment.CreateFromWireData(seg); |
| | 130 | 357 | | uSegments[i] = lps; |
| | | 358 | | // compute representation to project out into results |
| | 130 | 359 | | string relPathSeg = Uri.UnescapeDataString(seg); |
| | 130 | 360 | | if (lps.EndsWithSlash) |
| | | 361 | | { |
| | | 362 | | Fx.Assert(relPathSeg.EndsWith("/", StringComparison.Ordinal), "problem with relative path segment"); |
| | 9 | 363 | | relPathSeg = relPathSeg.Substring(0, relPathSeg.Length - 1); // trim slash |
| | | 364 | | } |
| | | 365 | | |
| | 130 | 366 | | relativePathSegments.Add(relPathSeg); |
| | | 367 | | } |
| | | 368 | | |
| | 122 | 369 | | return _rootNode.Match(uSegments, candidates); |
| | | 370 | | } |
| | | 371 | | |
| | | 372 | | private void ConstructFastPathTable() |
| | | 373 | | { |
| | 47 | 374 | | _noTemplateHasQueryPart = true; |
| | 420 | 375 | | foreach (KeyValuePair<UriTemplate, object> kvp in _templates) |
| | | 376 | | { |
| | 163 | 377 | | UriTemplate ut = kvp.Key; |
| | 163 | 378 | | if (!UriTemplateHelpers.CanMatchQueryTrivially(ut)) |
| | | 379 | | { |
| | 8 | 380 | | _noTemplateHasQueryPart = false; |
| | | 381 | | } |
| | | 382 | | |
| | 163 | 383 | | if (ut.HasNoVariables && !ut.HasWildcard) |
| | | 384 | | { |
| | | 385 | | // eligible for fast path |
| | 115 | 386 | | if (_fastPathTable == null) |
| | | 387 | | { |
| | 39 | 388 | | _fastPathTable = new Dictionary<string, FastPathInfo>(); |
| | | 389 | | } |
| | | 390 | | |
| | 115 | 391 | | Uri uri = ut.BindByPosition(OriginalBaseAddress); |
| | 115 | 392 | | string uriPath = UriTemplateHelpers.GetUriPath(uri); |
| | 115 | 393 | | if (_fastPathTable.ContainsKey(uriPath)) |
| | | 394 | | { |
| | | 395 | | // nothing to do, we've already seen it |
| | | 396 | | } |
| | | 397 | | else |
| | | 398 | | { |
| | 115 | 399 | | FastPathInfo fpInfo = new FastPathInfo(); |
| | 115 | 400 | | if (ComputeRelativeSegmentsAndLookup(uri, fpInfo.RelativePathSegments, |
| | 115 | 401 | | fpInfo.Candidates)) |
| | | 402 | | { |
| | 115 | 403 | | fpInfo.Freeze(); |
| | 115 | 404 | | _fastPathTable.Add(uriPath, fpInfo); |
| | | 405 | | } |
| | | 406 | | } |
| | | 407 | | } |
| | | 408 | | } |
| | 47 | 409 | | } |
| | | 410 | | |
| | | 411 | | // this method checks the literal cache for a match if none, goes through the slower path of cracking the segmen |
| | | 412 | | private bool FastComputeRelativeSegmentsAndLookup(Uri uri, out Collection<string> relativePathSegments, |
| | | 413 | | out IList<UriTemplateTableMatchCandidate> candidates) |
| | | 414 | | { |
| | | 415 | | // Consider fast-path and lookup |
| | | 416 | | // return false if not under base uri |
| | 36 | 417 | | string uriPath = UriTemplateHelpers.GetUriPath(uri); |
| | 36 | 418 | | if ((_fastPathTable != null) && _fastPathTable.TryGetValue(uriPath, out FastPathInfo fpInfo)) |
| | | 419 | | { |
| | 29 | 420 | | relativePathSegments = fpInfo.RelativePathSegments; |
| | 29 | 421 | | candidates = fpInfo.Candidates; |
| | | 422 | | VerifyThatFastPathAndSlowPathHaveSameResults(uri, relativePathSegments, candidates); |
| | 29 | 423 | | return true; |
| | | 424 | | } |
| | | 425 | | else |
| | | 426 | | { |
| | 7 | 427 | | relativePathSegments = new Collection<string>(); |
| | 7 | 428 | | candidates = new Collection<UriTemplateTableMatchCandidate>(); |
| | 7 | 429 | | return SlowComputeRelativeSegmentsAndLookup(uri, uriPath, relativePathSegments, candidates); |
| | | 430 | | } |
| | | 431 | | } |
| | | 432 | | |
| | | 433 | | private void NormalizeBaseAddress() |
| | | 434 | | { |
| | 47 | 435 | | if (_baseAddress != null) |
| | | 436 | | { |
| | | 437 | | // ensure trailing slash on baseAddress, so that IsBaseOf will work later |
| | 47 | 438 | | UriBuilder ub = new UriBuilder(_baseAddress); |
| | 47 | 439 | | if (_addTrailingSlashToBaseAddress && !ub.Path.EndsWith("/", StringComparison.Ordinal)) |
| | | 440 | | { |
| | 45 | 441 | | ub.Path = ub.Path + "/"; |
| | | 442 | | } |
| | | 443 | | |
| | 47 | 444 | | ub.Host = "localhost"; // always normalize to localhost |
| | 47 | 445 | | ub.Port = -1; |
| | 47 | 446 | | ub.UserName = null; |
| | 47 | 447 | | ub.Password = null; |
| | 47 | 448 | | ub.Path = ub.Path.ToUpperInvariant(); |
| | 47 | 449 | | ub.Scheme = Uri.UriSchemeHttp; |
| | 47 | 450 | | _baseAddress = ub.Uri; |
| | 47 | 451 | | _basePath = UriTemplateHelpers.GetUriPath(_baseAddress); |
| | | 452 | | } |
| | 47 | 453 | | } |
| | | 454 | | |
| | | 455 | | private bool SlowComputeRelativeSegmentsAndLookup(Uri uri, string uriPath, Collection<string> relativePathSegmen |
| | | 456 | | ICollection<UriTemplateTableMatchCandidate> candidates) |
| | | 457 | | { |
| | | 458 | | // ensure 'under' the base address |
| | 7 | 459 | | if (uriPath.Length < _basePath.Length) |
| | | 460 | | { |
| | 0 | 461 | | return false; |
| | | 462 | | } |
| | | 463 | | |
| | 7 | 464 | | if (!uriPath.StartsWith(_basePath, StringComparison.OrdinalIgnoreCase)) |
| | | 465 | | { |
| | 0 | 466 | | return false; |
| | | 467 | | } |
| | | 468 | | else |
| | | 469 | | { |
| | | 470 | | // uriPath StartsWith basePath, but this check is not enough - basePath 'service1' should not match with |
| | | 471 | | // make sure that after the match the next character is /, this is to avoid a uriPath of the form /servi |
| | 7 | 472 | | if (uriPath.Length > _basePath.Length && !_basePath.EndsWith("/", StringComparison.Ordinal) && uriPath[_ |
| | | 473 | | { |
| | 0 | 474 | | return false; |
| | | 475 | | } |
| | | 476 | | } |
| | | 477 | | |
| | 7 | 478 | | return ComputeRelativeSegmentsAndLookup(uri, relativePathSegments, candidates); |
| | | 479 | | } |
| | | 480 | | |
| | | 481 | | private void Validate(bool allowDuplicateEquivalentUriTemplates) |
| | | 482 | | { |
| | 47 | 483 | | if (_baseAddress == null) |
| | | 484 | | { |
| | 0 | 485 | | throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(new InvalidOperationException(SR.UTTBaseAddres |
| | | 486 | | } |
| | | 487 | | |
| | 47 | 488 | | _numSegmentsInBaseAddress = _baseAddress.Segments.Length; |
| | 47 | 489 | | if (_templates.Count == 0) |
| | | 490 | | { |
| | 0 | 491 | | throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(new InvalidOperationException(SR.UTTEmptyKeyVa |
| | | 492 | | } |
| | | 493 | | |
| | | 494 | | // build the trie and |
| | | 495 | | // validate that forall Uri u, at most one UriTemplate is a best match for u |
| | 47 | 496 | | _rootNode = UriTemplateTrieNode.Make(_templates, allowDuplicateEquivalentUriTemplates); |
| | 47 | 497 | | } |
| | | 498 | | |
| | | 499 | | [Conditional("DEBUG")] |
| | | 500 | | private void VerifyThatFastPathAndSlowPathHaveSameResults(Uri uri, Collection<string> fastPathRelativePathSegmen |
| | | 501 | | IList<UriTemplateTableMatchCandidate> fastPathCandidates) |
| | | 502 | | { |
| | 0 | 503 | | Collection<string> slowPathRelativePathSegments = new Collection<string>(); |
| | 0 | 504 | | List<UriTemplateTableMatchCandidate> slowPathCandidates = new List<UriTemplateTableMatchCandidate>(); |
| | 0 | 505 | | if (!SlowComputeRelativeSegmentsAndLookup(uri, UriTemplateHelpers.GetUriPath(uri), |
| | 0 | 506 | | slowPathRelativePathSegments, slowPathCandidates)) |
| | | 507 | | { |
| | | 508 | | Fx.Assert("fast path yielded a result but slow path yielded no result"); |
| | | 509 | | } |
| | | 510 | | |
| | | 511 | | // compare results |
| | 0 | 512 | | if (fastPathRelativePathSegments.Count != slowPathRelativePathSegments.Count) |
| | | 513 | | { |
| | | 514 | | Fx.Assert("fast path yielded different number of segments from slow path"); |
| | | 515 | | } |
| | | 516 | | |
| | 0 | 517 | | for (int i = 0; i < fastPathRelativePathSegments.Count; ++i) |
| | | 518 | | { |
| | 0 | 519 | | if (fastPathRelativePathSegments[i] != slowPathRelativePathSegments[i]) |
| | | 520 | | { |
| | | 521 | | Fx.Assert("fast path yielded different segments from slow path"); |
| | | 522 | | } |
| | | 523 | | } |
| | | 524 | | |
| | 0 | 525 | | if (fastPathCandidates.Count != slowPathCandidates.Count) |
| | | 526 | | { |
| | | 527 | | Fx.Assert("fast path yielded different number of candidates from slow path"); |
| | | 528 | | } |
| | | 529 | | |
| | 0 | 530 | | for (int i = 0; i < fastPathCandidates.Count; i++) |
| | | 531 | | { |
| | 0 | 532 | | if (!slowPathCandidates.Contains(fastPathCandidates[i])) |
| | | 533 | | { |
| | | 534 | | Fx.Assert("fast path yielded different candidates from slow path"); |
| | | 535 | | } |
| | | 536 | | } |
| | 0 | 537 | | } |
| | | 538 | | |
| | | 539 | | internal class FastPathInfo |
| | | 540 | | { |
| | | 541 | | private readonly FreezableCollection<UriTemplateTableMatchCandidate> _candidates; |
| | | 542 | | private readonly FreezableCollection<string> _relativePathSegments; |
| | | 543 | | |
| | 115 | 544 | | public FastPathInfo() |
| | | 545 | | { |
| | 115 | 546 | | _relativePathSegments = new FreezableCollection<string>(); |
| | 115 | 547 | | _candidates = new FreezableCollection<UriTemplateTableMatchCandidate>(); |
| | 115 | 548 | | } |
| | | 549 | | |
| | 144 | 550 | | public Collection<UriTemplateTableMatchCandidate> Candidates => _candidates; |
| | | 551 | | |
| | 144 | 552 | | public Collection<string> RelativePathSegments => _relativePathSegments; |
| | | 553 | | |
| | | 554 | | public void Freeze() |
| | | 555 | | { |
| | 115 | 556 | | _relativePathSegments.Freeze(); |
| | 115 | 557 | | _candidates.Freeze(); |
| | 115 | 558 | | } |
| | | 559 | | } |
| | | 560 | | |
| | | 561 | | internal class UriTemplatesCollection : FreezableCollection<KeyValuePair<UriTemplate, object>> |
| | | 562 | | { |
| | | 563 | | public UriTemplatesCollection() |
| | 47 | 564 | | : base() |
| | | 565 | | { |
| | 47 | 566 | | } |
| | | 567 | | |
| | | 568 | | public UriTemplatesCollection(IEnumerable<KeyValuePair<UriTemplate, object>> keyValuePairs) |
| | 0 | 569 | | : base() |
| | | 570 | | { |
| | 0 | 571 | | foreach (KeyValuePair<UriTemplate, object> kvp in keyValuePairs) |
| | | 572 | | { |
| | 0 | 573 | | ThrowIfInvalid(kvp.Key, "keyValuePairs"); |
| | 0 | 574 | | Add(kvp); |
| | | 575 | | } |
| | 0 | 576 | | } |
| | | 577 | | |
| | | 578 | | protected override void InsertItem(int index, KeyValuePair<UriTemplate, object> item) |
| | | 579 | | { |
| | 163 | 580 | | ThrowIfInvalid(item.Key, "item"); |
| | 163 | 581 | | base.InsertItem(index, item); |
| | 163 | 582 | | } |
| | | 583 | | |
| | | 584 | | protected override void SetItem(int index, KeyValuePair<UriTemplate, object> item) |
| | | 585 | | { |
| | 0 | 586 | | ThrowIfInvalid(item.Key, "item"); |
| | 0 | 587 | | base.SetItem(index, item); |
| | 0 | 588 | | } |
| | | 589 | | |
| | | 590 | | private static void ThrowIfInvalid(UriTemplate template, string argName) |
| | | 591 | | { |
| | 163 | 592 | | if (template == null) |
| | | 593 | | { |
| | 0 | 594 | | throw DiagnosticUtility.ExceptionUtility.ThrowHelperArgument(argName, |
| | 0 | 595 | | SR.UTTNullTemplateKey); |
| | | 596 | | } |
| | | 597 | | |
| | 163 | 598 | | if (template.IgnoreTrailingSlash) |
| | | 599 | | { |
| | 0 | 600 | | throw DiagnosticUtility.ExceptionUtility.ThrowHelperArgument(argName, |
| | 0 | 601 | | SR.Format(SR.UTTInvalidTemplateKey, template)); |
| | | 602 | | } |
| | 163 | 603 | | } |
| | | 604 | | } |
| | | 605 | | } |
| | | 606 | | } |