void VerifyThatFastPathAndSlowPathHaveSameResults(Uri uri, Collection <string> fastPathRelativePathSegments, IList <UriTemplateTableMatchCandidate> fastPathCandidates) { Collection <string> slowPathRelativePathSegments = new Collection <string>(); List <UriTemplateTableMatchCandidate> slowPathCandidates = new List <UriTemplateTableMatchCandidate>(); if (!SlowComputeRelativeSegmentsAndLookup(uri, UriTemplateHelpers.GetUriPath(uri), slowPathRelativePathSegments, slowPathCandidates)) { Fx.Assert("fast path yielded a result but slow path yielded no result"); } // compare results if (fastPathRelativePathSegments.Count != slowPathRelativePathSegments.Count) { Fx.Assert("fast path yielded different number of segments from slow path"); } for (int i = 0; i < fastPathRelativePathSegments.Count; ++i) { if (fastPathRelativePathSegments[i] != slowPathRelativePathSegments[i]) { Fx.Assert("fast path yielded different segments from slow path"); } } if (fastPathCandidates.Count != slowPathCandidates.Count) { Fx.Assert("fast path yielded different number of candidates from slow path"); } for (int i = 0; i < fastPathCandidates.Count; i++) { if (!slowPathCandidates.Contains(fastPathCandidates[i])) { Fx.Assert("fast path yielded different candidates from slow path"); } } }
private void ConstructFastPathTable() { this.noTemplateHasQueryPart = true; foreach (KeyValuePair <UriTemplate, object> pair in this.templates) { UriTemplate key = pair.Key; if (!UriTemplateHelpers.CanMatchQueryTrivially(key)) { this.noTemplateHasQueryPart = false; } if (key.HasNoVariables && !key.HasWildcard) { if (this.fastPathTable == null) { this.fastPathTable = new Dictionary <string, FastPathInfo>(); } Uri uri = key.BindByPosition(this.originalUncanonicalizedBaseAddress, new string[0]); string uriPath = UriTemplateHelpers.GetUriPath(uri); if (!this.fastPathTable.ContainsKey(uriPath)) { FastPathInfo info = new FastPathInfo(); if (this.ComputeRelativeSegmentsAndLookup(uri, info.RelativePathSegments, info.Candidates)) { info.Freeze(); this.fastPathTable.Add(uriPath, info); } } } } }
private bool FastComputeRelativeSegmentsAndLookup(Uri uri, out Collection <string> relativePathSegments, out IList <UriTemplateTableMatchCandidate> candidates) { string uriPath = UriTemplateHelpers.GetUriPath(uri); FastPathInfo info = null; if ((this.fastPathTable != null) && this.fastPathTable.TryGetValue(uriPath, out info)) { relativePathSegments = info.RelativePathSegments; candidates = info.Candidates; return(true); } relativePathSegments = new Collection <string>(); candidates = new Collection <UriTemplateTableMatchCandidate>(); return(this.SlowComputeRelativeSegmentsAndLookup(uri, uriPath, relativePathSegments, candidates)); }
private void NormalizeBaseAddress() { if (this.baseAddress != null) { UriBuilder builder = new UriBuilder(this.baseAddress); if (!builder.Path.EndsWith("/", StringComparison.Ordinal)) { builder.Path = builder.Path + "/"; } builder.Host = "localhost"; builder.Port = -1; builder.UserName = null; builder.Password = null; builder.Path = builder.Path.ToUpperInvariant(); builder.Scheme = Uri.UriSchemeHttp; this.baseAddress = builder.Uri; this.basePath = UriTemplateHelpers.GetUriPath(this.baseAddress); } }
void NormalizeBaseAddress() { if (this.baseAddress != null) { // ensure trailing slash on baseAddress, so that IsBaseOf will work later UriBuilder ub = new UriBuilder(this.baseAddress); if (this.addTrailingSlashToBaseAddress && !ub.Path.EndsWith("/", StringComparison.Ordinal)) { ub.Path = ub.Path + "/"; } ub.Host = "localhost"; // always normalize to localhost ub.Port = -1; ub.UserName = null; ub.Password = null; ub.Path = ub.Path.ToUpperInvariant(); ub.Scheme = Uri.UriSchemeHttp; this.baseAddress = ub.Uri; basePath = UriTemplateHelpers.GetUriPath(this.baseAddress); } }
private void VerifyThatFastPathAndSlowPathHaveSameResults(Uri uri, Collection <string> fastPathRelativePathSegments, IList <UriTemplateTableMatchCandidate> fastPathCandidates) { Collection <string> relativePathSegments = new Collection <string>(); List <UriTemplateTableMatchCandidate> candidates = new List <UriTemplateTableMatchCandidate>(); this.SlowComputeRelativeSegmentsAndLookup(uri, UriTemplateHelpers.GetUriPath(uri), relativePathSegments, candidates); int count = relativePathSegments.Count; int num3 = fastPathRelativePathSegments.Count; for (int i = 0; i < fastPathRelativePathSegments.Count; i++) { bool flag1 = fastPathRelativePathSegments[i] != relativePathSegments[i]; } int num4 = candidates.Count; int num5 = fastPathCandidates.Count; for (int j = 0; j < fastPathCandidates.Count; j++) { candidates.Contains(fastPathCandidates[j]); } }
// this method checks the literal cache for a match if none, goes through the slower path of cracking the segments bool FastComputeRelativeSegmentsAndLookup(Uri uri, out Collection <string> relativePathSegments, out IList <UriTemplateTableMatchCandidate> candidates) { // Consider fast-path and lookup // return false if not under base uri string uriPath = UriTemplateHelpers.GetUriPath(uri); FastPathInfo fpInfo = null; if ((this.fastPathTable != null) && this.fastPathTable.TryGetValue(uriPath, out fpInfo)) { relativePathSegments = fpInfo.RelativePathSegments; candidates = fpInfo.Candidates; VerifyThatFastPathAndSlowPathHaveSameResults(uri, relativePathSegments, candidates); return(true); } else { relativePathSegments = new Collection <string>(); candidates = new Collection <UriTemplateTableMatchCandidate>(); return(SlowComputeRelativeSegmentsAndLookup(uri, uriPath, relativePathSegments, candidates)); } }
void ConstructFastPathTable() { this.noTemplateHasQueryPart = true; foreach (KeyValuePair <UriTemplate, object> kvp in this.templates) { UriTemplate ut = kvp.Key; if (!UriTemplateHelpers.CanMatchQueryTrivially(ut)) { this.noTemplateHasQueryPart = false; } if (ut.HasNoVariables && !ut.HasWildcard) { // eligible for fast path if (this.fastPathTable == null) { this.fastPathTable = new Dictionary <string, FastPathInfo>(); } Uri uri = ut.BindByPosition(this.originalUncanonicalizedBaseAddress); string uriPath = UriTemplateHelpers.GetUriPath(uri); if (this.fastPathTable.ContainsKey(uriPath)) { // nothing to do, we've already seen it } else { FastPathInfo fpInfo = new FastPathInfo(); if (ComputeRelativeSegmentsAndLookup(uri, fpInfo.RelativePathSegments, fpInfo.Candidates)) { fpInfo.Freeze(); this.fastPathTable.Add(uriPath, fpInfo); } } } } }