| | | 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.Concurrent; |
| | | 6 | | using System.Collections.Generic; |
| | | 7 | | using System.Collections.ObjectModel; |
| | | 8 | | using System.Collections.Specialized; |
| | | 9 | | using System.Globalization; |
| | | 10 | | using System.Text; |
| | | 11 | | using System.Threading; |
| | | 12 | | using CoreWCF.Runtime; |
| | | 13 | | |
| | | 14 | | namespace CoreWCF |
| | | 15 | | { |
| | | 16 | | public class UriTemplate |
| | | 17 | | { |
| | | 18 | | internal readonly int _firstOptionalSegment; |
| | | 19 | | internal readonly string _originalTemplate; |
| | | 20 | | internal readonly Dictionary<string, UriTemplateQueryValue> _queries; // keys are original case specified in Uri |
| | | 21 | | internal readonly List<UriTemplatePathSegment> _segments; |
| | | 22 | | |
| | | 23 | | internal const string WildcardPath = "*"; |
| | | 24 | | |
| | | 25 | | private readonly Dictionary<string, string> _additionalDefaults; // keys are original case specified in UriTempl |
| | | 26 | | private readonly string _fragment; |
| | | 27 | | private const string NullableDefault = "null"; |
| | | 28 | | private readonly WildcardInfo _wildcard; |
| | | 29 | | private IDictionary<string, string> _defaults; |
| | | 30 | | private ConcurrentDictionary<string, string> _unescapedDefaults; |
| | | 31 | | |
| | | 32 | | private VariablesCollection _variables; |
| | | 33 | | |
| | | 34 | | // constructors validates that template is well-formed |
| | | 35 | | public UriTemplate(string template) |
| | 512 | 36 | | : this(template, false) |
| | | 37 | | { |
| | 512 | 38 | | } |
| | | 39 | | |
| | | 40 | | public UriTemplate(string template, bool ignoreTrailingSlash) |
| | 512 | 41 | | : this(template, ignoreTrailingSlash, null) |
| | | 42 | | { |
| | 512 | 43 | | } |
| | | 44 | | |
| | | 45 | | public UriTemplate(string template, IDictionary<string, string> additionalDefaults) |
| | 0 | 46 | | : this(template, false, additionalDefaults) |
| | | 47 | | { |
| | 0 | 48 | | } |
| | | 49 | | |
| | 512 | 50 | | public UriTemplate(string template, bool ignoreTrailingSlash, IDictionary<string, string> additionalDefaults) |
| | | 51 | | { |
| | 512 | 52 | | _originalTemplate = template ?? throw DiagnosticUtility.ExceptionUtility.ThrowHelperArgumentNull(nameof(temp |
| | | 53 | | |
| | 512 | 54 | | IgnoreTrailingSlash = ignoreTrailingSlash; |
| | 512 | 55 | | _segments = new List<UriTemplatePathSegment>(); |
| | 512 | 56 | | _queries = new Dictionary<string, UriTemplateQueryValue>(StringComparer.OrdinalIgnoreCase); |
| | | 57 | | |
| | | 58 | | // parse it |
| | | 59 | | string pathTemplate; |
| | | 60 | | string queryTemplate; |
| | | 61 | | // ignore a leading slash |
| | 512 | 62 | | if (template.StartsWith("/", StringComparison.Ordinal)) |
| | | 63 | | { |
| | 416 | 64 | | template = template.Substring(1); |
| | | 65 | | } |
| | | 66 | | |
| | | 67 | | // pull out fragment |
| | 512 | 68 | | int fragmentStart = template.IndexOf('#'); |
| | 512 | 69 | | if (fragmentStart == -1) |
| | | 70 | | { |
| | 512 | 71 | | _fragment = ""; |
| | | 72 | | } |
| | | 73 | | else |
| | | 74 | | { |
| | 0 | 75 | | _fragment = template.Substring(fragmentStart + 1); |
| | 0 | 76 | | template = template.Substring(0, fragmentStart); |
| | | 77 | | } |
| | | 78 | | |
| | | 79 | | // pull out path and query |
| | 512 | 80 | | int queryStart = template.IndexOf('?'); |
| | 512 | 81 | | if (queryStart == -1) |
| | | 82 | | { |
| | 485 | 83 | | queryTemplate = string.Empty; |
| | 485 | 84 | | pathTemplate = template; |
| | | 85 | | } |
| | | 86 | | else |
| | | 87 | | { |
| | 27 | 88 | | queryTemplate = template.Substring(queryStart + 1); |
| | 27 | 89 | | pathTemplate = template.Substring(0, queryStart); |
| | | 90 | | } |
| | | 91 | | |
| | 512 | 92 | | template = null; // to ensure we don't accidentally reference this variable any more |
| | | 93 | | |
| | | 94 | | // setup path template and validate |
| | 512 | 95 | | if (!string.IsNullOrEmpty(pathTemplate)) |
| | | 96 | | { |
| | 512 | 97 | | int startIndex = 0; |
| | 1136 | 98 | | while (startIndex < pathTemplate.Length) |
| | | 99 | | { |
| | | 100 | | // Identify the next segment |
| | 624 | 101 | | int endIndex = pathTemplate.IndexOf('/', startIndex); |
| | | 102 | | string segment; |
| | 624 | 103 | | if (endIndex != -1) |
| | | 104 | | { |
| | 112 | 105 | | segment = pathTemplate.Substring(startIndex, endIndex + 1 - startIndex); |
| | 112 | 106 | | startIndex = endIndex + 1; |
| | | 107 | | } |
| | | 108 | | else |
| | | 109 | | { |
| | 512 | 110 | | segment = pathTemplate.Substring(startIndex); |
| | 512 | 111 | | startIndex = pathTemplate.Length; |
| | | 112 | | } |
| | | 113 | | |
| | | 114 | | // Checking for wildcard segment ("*") or ("{*<var name>}") |
| | 624 | 115 | | if ((startIndex == pathTemplate.Length) && |
| | 624 | 116 | | UriTemplateHelpers.IsWildcardSegment(segment, out UriTemplatePartType wildcardType)) |
| | | 117 | | { |
| | | 118 | | switch (wildcardType) |
| | | 119 | | { |
| | | 120 | | case UriTemplatePartType.Literal: |
| | 24 | 121 | | _wildcard = new WildcardInfo(this); |
| | 24 | 122 | | break; |
| | | 123 | | |
| | | 124 | | case UriTemplatePartType.Variable: |
| | 24 | 125 | | _wildcard = new WildcardInfo(this, segment); |
| | 24 | 126 | | break; |
| | | 127 | | |
| | | 128 | | default: |
| | | 129 | | Fx.Assert("Error in identifying the type of the wildcard segment"); |
| | | 130 | | break; |
| | | 131 | | } |
| | | 132 | | } |
| | | 133 | | else |
| | | 134 | | { |
| | 576 | 135 | | _segments.Add(UriTemplatePathSegment.CreateFromUriTemplate(segment, this)); |
| | | 136 | | } |
| | | 137 | | } |
| | | 138 | | } |
| | | 139 | | |
| | | 140 | | // setup query template and validate |
| | 512 | 141 | | if (!string.IsNullOrEmpty(queryTemplate)) |
| | | 142 | | { |
| | 27 | 143 | | int startIndex = 0; |
| | 56 | 144 | | while (startIndex < queryTemplate.Length) |
| | | 145 | | { |
| | | 146 | | // Identify the next query part |
| | 29 | 147 | | int endIndex = queryTemplate.IndexOf('&', startIndex); |
| | 29 | 148 | | int queryPartStart = startIndex; |
| | | 149 | | int queryPartEnd; |
| | 29 | 150 | | if (endIndex != -1) |
| | | 151 | | { |
| | 2 | 152 | | queryPartEnd = endIndex; |
| | 2 | 153 | | startIndex = endIndex + 1; |
| | 2 | 154 | | if (startIndex >= queryTemplate.Length) |
| | | 155 | | { |
| | 0 | 156 | | throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(new InvalidOperationException(SR.F |
| | 0 | 157 | | SR.UTQueryCannotEndInAmpersand, _originalTemplate))); |
| | | 158 | | } |
| | | 159 | | } |
| | | 160 | | else |
| | | 161 | | { |
| | 27 | 162 | | queryPartEnd = queryTemplate.Length; |
| | 27 | 163 | | startIndex = queryTemplate.Length; |
| | | 164 | | } |
| | | 165 | | |
| | | 166 | | // Checking query part type; identifying key and value |
| | 29 | 167 | | int equalSignIndex = queryTemplate.IndexOf('=', queryPartStart, queryPartEnd - queryPartStart); |
| | | 168 | | string key; |
| | | 169 | | string value; |
| | 29 | 170 | | if (equalSignIndex >= 0) |
| | | 171 | | { |
| | 29 | 172 | | key = queryTemplate.Substring(queryPartStart, equalSignIndex - queryPartStart); |
| | 29 | 173 | | value = queryTemplate.Substring(equalSignIndex + 1, queryPartEnd - equalSignIndex - 1); |
| | | 174 | | } |
| | | 175 | | else |
| | | 176 | | { |
| | 0 | 177 | | key = queryTemplate.Substring(queryPartStart, queryPartEnd - queryPartStart); |
| | 0 | 178 | | value = null; |
| | | 179 | | } |
| | | 180 | | |
| | 29 | 181 | | if (string.IsNullOrEmpty(key)) |
| | | 182 | | { |
| | 0 | 183 | | throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(new InvalidOperationException(SR.Forma |
| | 0 | 184 | | SR.UTQueryCannotHaveEmptyName, _originalTemplate))); |
| | | 185 | | } |
| | | 186 | | |
| | 29 | 187 | | if (UriTemplateHelpers.IdentifyPartType(key) != UriTemplatePartType.Literal) |
| | | 188 | | { |
| | 0 | 189 | | throw DiagnosticUtility.ExceptionUtility.ThrowHelperArgument(nameof(template), SR.Format( |
| | 0 | 190 | | SR.UTQueryMustHaveLiteralNames, _originalTemplate)); |
| | | 191 | | } |
| | | 192 | | |
| | | 193 | | // Adding a new entry to the queries dictionary |
| | 29 | 194 | | key = UrlUtility.UrlDecode(key, Encoding.UTF8); |
| | 29 | 195 | | if (_queries.ContainsKey(key)) |
| | | 196 | | { |
| | 0 | 197 | | throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(new InvalidOperationException(SR.Forma |
| | 0 | 198 | | SR.UTQueryNamesMustBeUnique, _originalTemplate))); |
| | | 199 | | } |
| | | 200 | | |
| | 29 | 201 | | _queries.Add(key, UriTemplateQueryValue.CreateFromUriTemplate(value, this)); |
| | | 202 | | } |
| | | 203 | | } |
| | | 204 | | |
| | | 205 | | // Process additional defaults (if has some) : |
| | 512 | 206 | | if (additionalDefaults != null) |
| | | 207 | | { |
| | 0 | 208 | | if (_variables == null) |
| | | 209 | | { |
| | 0 | 210 | | if (additionalDefaults.Count > 0) |
| | | 211 | | { |
| | 0 | 212 | | _additionalDefaults = new Dictionary<string, string>(additionalDefaults, StringComparer.OrdinalI |
| | | 213 | | } |
| | | 214 | | } |
| | | 215 | | else |
| | | 216 | | { |
| | 0 | 217 | | foreach (KeyValuePair<string, string> kvp in additionalDefaults) |
| | | 218 | | { |
| | 0 | 219 | | string uppercaseKey = kvp.Key.ToUpperInvariant(); |
| | 0 | 220 | | if ((_variables.DefaultValues != null) && _variables.DefaultValues.ContainsKey(uppercaseKey)) |
| | | 221 | | { |
| | 0 | 222 | | throw DiagnosticUtility.ExceptionUtility.ThrowHelperArgument(nameof(additionalDefaults), |
| | 0 | 223 | | SR.Format(SR.UTAdditionalDefaultIsInvalid, kvp.Key, _originalTemplate)); |
| | | 224 | | } |
| | | 225 | | |
| | 0 | 226 | | if (_variables.PathSegmentVariableNames.Contains(uppercaseKey)) |
| | | 227 | | { |
| | 0 | 228 | | _variables.AddDefaultValue(uppercaseKey, kvp.Value); |
| | | 229 | | } |
| | 0 | 230 | | else if (_variables.QueryValueVariableNames.Contains(uppercaseKey)) |
| | | 231 | | { |
| | 0 | 232 | | throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(new InvalidOperationException( |
| | 0 | 233 | | SR.Format(SR.UTDefaultValueToQueryVarFromAdditionalDefaults, _originalTemplate, |
| | 0 | 234 | | uppercaseKey))); |
| | | 235 | | } |
| | 0 | 236 | | else if (string.Compare(kvp.Value, UriTemplate.NullableDefault, StringComparison.OrdinalIgnoreCa |
| | | 237 | | { |
| | 0 | 238 | | throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(new InvalidOperationException( |
| | 0 | 239 | | SR.Format(SR.UTNullableDefaultAtAdditionalDefaults, _originalTemplate, |
| | 0 | 240 | | uppercaseKey))); |
| | | 241 | | } |
| | | 242 | | else |
| | | 243 | | { |
| | 0 | 244 | | if (_additionalDefaults == null) |
| | | 245 | | { |
| | 0 | 246 | | _additionalDefaults = new Dictionary<string, string>(additionalDefaults.Count, StringCom |
| | | 247 | | } |
| | | 248 | | |
| | 0 | 249 | | _additionalDefaults.Add(kvp.Key, kvp.Value); |
| | | 250 | | } |
| | | 251 | | } |
| | | 252 | | } |
| | | 253 | | } |
| | | 254 | | |
| | | 255 | | // Validate defaults (if should) |
| | 512 | 256 | | if ((_variables != null) && (_variables.DefaultValues != null)) |
| | | 257 | | { |
| | 24 | 258 | | _variables.ValidateDefaults(out _firstOptionalSegment); |
| | | 259 | | } |
| | | 260 | | else |
| | | 261 | | { |
| | 488 | 262 | | _firstOptionalSegment = _segments.Count; |
| | | 263 | | } |
| | 488 | 264 | | } |
| | | 265 | | |
| | | 266 | | public IDictionary<string, string> Defaults |
| | | 267 | | { |
| | | 268 | | get |
| | | 269 | | { |
| | 0 | 270 | | if (_defaults == null) |
| | | 271 | | { |
| | 0 | 272 | | Interlocked.CompareExchange(ref _defaults, new UriTemplateDefaults(this), null); |
| | | 273 | | } |
| | | 274 | | |
| | 0 | 275 | | return _defaults; |
| | | 276 | | } |
| | | 277 | | } |
| | | 278 | | |
| | 278 | 279 | | public bool IgnoreTrailingSlash { get; } |
| | | 280 | | |
| | | 281 | | public ReadOnlyCollection<string> PathSegmentVariableNames |
| | | 282 | | { |
| | | 283 | | get |
| | | 284 | | { |
| | 349 | 285 | | if (_variables == null) |
| | | 286 | | { |
| | 264 | 287 | | return VariablesCollection.EmptyCollection; |
| | | 288 | | } |
| | | 289 | | else |
| | | 290 | | { |
| | 85 | 291 | | return _variables.PathSegmentVariableNames; |
| | | 292 | | } |
| | | 293 | | } |
| | | 294 | | } |
| | | 295 | | |
| | | 296 | | public ReadOnlyCollection<string> QueryValueVariableNames |
| | | 297 | | { |
| | | 298 | | get |
| | | 299 | | { |
| | 347 | 300 | | if (_variables == null) |
| | | 301 | | { |
| | 264 | 302 | | return VariablesCollection.EmptyCollection; |
| | | 303 | | } |
| | | 304 | | else |
| | | 305 | | { |
| | 83 | 306 | | return _variables.QueryValueVariableNames; |
| | | 307 | | } |
| | | 308 | | } |
| | | 309 | | } |
| | | 310 | | |
| | | 311 | | internal bool HasNoVariables |
| | | 312 | | { |
| | | 313 | | get |
| | | 314 | | { |
| | 163 | 315 | | return _variables == null; |
| | | 316 | | } |
| | | 317 | | } |
| | | 318 | | |
| | | 319 | | internal bool HasWildcard |
| | | 320 | | { |
| | | 321 | | get |
| | | 322 | | { |
| | 318 | 323 | | return (_wildcard != null); |
| | | 324 | | } |
| | | 325 | | } |
| | | 326 | | |
| | | 327 | | // make a Uri by subbing in the values, throw on bad input |
| | | 328 | | public Uri BindByName(Uri baseAddress, IDictionary<string, string> parameters) |
| | | 329 | | { |
| | 0 | 330 | | return BindByName(baseAddress, parameters, false); |
| | | 331 | | } |
| | | 332 | | |
| | | 333 | | public Uri BindByName(Uri baseAddress, IDictionary<string, string> parameters, bool omitDefaults) |
| | | 334 | | { |
| | 0 | 335 | | if (baseAddress == null) |
| | | 336 | | { |
| | 0 | 337 | | throw DiagnosticUtility.ExceptionUtility.ThrowHelperArgumentNull(nameof(baseAddress)); |
| | | 338 | | } |
| | | 339 | | |
| | 0 | 340 | | if (!baseAddress.IsAbsoluteUri) |
| | | 341 | | { |
| | 0 | 342 | | throw DiagnosticUtility.ExceptionUtility.ThrowHelperArgument(nameof(baseAddress), SR.Format( |
| | 0 | 343 | | SR.UTBadBaseAddress)); |
| | | 344 | | } |
| | | 345 | | |
| | | 346 | | BindInformation bindInfo; |
| | 0 | 347 | | if (_variables == null) |
| | | 348 | | { |
| | 0 | 349 | | bindInfo = PrepareBindInformation(parameters, omitDefaults); |
| | | 350 | | } |
| | | 351 | | else |
| | | 352 | | { |
| | 0 | 353 | | bindInfo = _variables.PrepareBindInformation(parameters, omitDefaults); |
| | | 354 | | } |
| | | 355 | | |
| | 0 | 356 | | return Bind(baseAddress, bindInfo, omitDefaults); |
| | | 357 | | } |
| | | 358 | | |
| | | 359 | | public Uri BindByName(Uri baseAddress, NameValueCollection parameters) |
| | | 360 | | { |
| | 0 | 361 | | return BindByName(baseAddress, parameters, false); |
| | | 362 | | } |
| | | 363 | | |
| | | 364 | | public Uri BindByName(Uri baseAddress, NameValueCollection parameters, bool omitDefaults) |
| | | 365 | | { |
| | 0 | 366 | | if (baseAddress == null) |
| | | 367 | | { |
| | 0 | 368 | | throw DiagnosticUtility.ExceptionUtility.ThrowHelperArgumentNull(nameof(baseAddress)); |
| | | 369 | | } |
| | | 370 | | |
| | 0 | 371 | | if (!baseAddress.IsAbsoluteUri) |
| | | 372 | | { |
| | 0 | 373 | | throw DiagnosticUtility.ExceptionUtility.ThrowHelperArgument(nameof(baseAddress), SR.Format( |
| | 0 | 374 | | SR.UTBadBaseAddress)); |
| | | 375 | | } |
| | | 376 | | |
| | | 377 | | BindInformation bindInfo; |
| | 0 | 378 | | if (_variables == null) |
| | | 379 | | { |
| | 0 | 380 | | bindInfo = PrepareBindInformation(parameters, omitDefaults); |
| | | 381 | | } |
| | | 382 | | else |
| | | 383 | | { |
| | 0 | 384 | | bindInfo = _variables.PrepareBindInformation(parameters, omitDefaults); |
| | | 385 | | } |
| | | 386 | | |
| | 0 | 387 | | return Bind(baseAddress, bindInfo, omitDefaults); |
| | | 388 | | } |
| | | 389 | | |
| | | 390 | | public Uri BindByPosition(Uri baseAddress, params string[] values) |
| | | 391 | | { |
| | 115 | 392 | | if (baseAddress == null) |
| | | 393 | | { |
| | 0 | 394 | | throw DiagnosticUtility.ExceptionUtility.ThrowHelperArgumentNull(nameof(baseAddress)); |
| | | 395 | | } |
| | | 396 | | |
| | 115 | 397 | | if (!baseAddress.IsAbsoluteUri) |
| | | 398 | | { |
| | 0 | 399 | | throw DiagnosticUtility.ExceptionUtility.ThrowHelperArgument(nameof(baseAddress), SR.Format( |
| | 0 | 400 | | SR.UTBadBaseAddress)); |
| | | 401 | | } |
| | | 402 | | |
| | | 403 | | BindInformation bindInfo; |
| | 115 | 404 | | if (_variables == null) |
| | | 405 | | { |
| | 115 | 406 | | if (values.Length > 0) |
| | | 407 | | { |
| | 0 | 408 | | throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(new FormatException(SR.Format( |
| | 0 | 409 | | SR.UTBindByPositionNoVariables, _originalTemplate, values.Length))); |
| | | 410 | | } |
| | 115 | 411 | | bindInfo = new BindInformation(_additionalDefaults); |
| | | 412 | | } |
| | | 413 | | else |
| | | 414 | | { |
| | 0 | 415 | | bindInfo = _variables.PrepareBindInformation(values); |
| | | 416 | | } |
| | | 417 | | |
| | 115 | 418 | | return Bind(baseAddress, bindInfo, false); |
| | | 419 | | } |
| | | 420 | | |
| | | 421 | | // A note about UriTemplate equivalency: |
| | | 422 | | // The introduction of defaults and, more over, terminal defaults, broke the simple |
| | | 423 | | // intuitive notion of equivalency between templates. We will define equivalent |
| | | 424 | | // templates as such based on the structure of them and not based on the set of uri |
| | | 425 | | // that are matched by them. The result is that, even though they do not match the |
| | | 426 | | // same set of uri's, the following templates are equivalent: |
| | | 427 | | // - "/foo/{bar}" |
| | | 428 | | // - "/foo/{bar=xyz}" |
| | | 429 | | // A direct result from the support for 'terminal defaults' is that the IsPathEquivalentTo |
| | | 430 | | // method, which was used both to determine the equivalence between templates, as |
| | | 431 | | // well as verify that all the templates, combined together in the same PathEquivalentSet, |
| | | 432 | | // are equivalent in their path is no longer valid for both purposes. We will break |
| | | 433 | | // it to two distinct methods, each will be called in a different case. |
| | | 434 | | public bool IsEquivalentTo(UriTemplate other) |
| | | 435 | | { |
| | 0 | 436 | | if (other == null) |
| | | 437 | | { |
| | 0 | 438 | | return false; |
| | | 439 | | } |
| | | 440 | | |
| | 0 | 441 | | if (other._segments == null || other._queries == null) |
| | | 442 | | { |
| | | 443 | | // they never are null, but PreSharp is complaining, |
| | | 444 | | // and warning suppression isn't working |
| | 0 | 445 | | return false; |
| | | 446 | | } |
| | | 447 | | |
| | 0 | 448 | | if (!IsPathFullyEquivalent(other)) |
| | | 449 | | { |
| | 0 | 450 | | return false; |
| | | 451 | | } |
| | | 452 | | |
| | 0 | 453 | | if (!IsQueryEquivalent(other)) |
| | | 454 | | { |
| | 0 | 455 | | return false; |
| | | 456 | | } |
| | | 457 | | |
| | | 458 | | Fx.Assert(UriTemplateEquivalenceComparer.Instance.GetHashCode(this) == UriTemplateEquivalenceComparer.Instan |
| | 0 | 459 | | return true; |
| | | 460 | | } |
| | | 461 | | |
| | | 462 | | public UriTemplateMatch Match(Uri baseAddress, Uri candidate) |
| | | 463 | | { |
| | 0 | 464 | | if (baseAddress == null) |
| | | 465 | | { |
| | 0 | 466 | | throw DiagnosticUtility.ExceptionUtility.ThrowHelperArgumentNull(nameof(baseAddress)); |
| | | 467 | | } |
| | | 468 | | |
| | 0 | 469 | | if (!baseAddress.IsAbsoluteUri) |
| | | 470 | | { |
| | 0 | 471 | | throw DiagnosticUtility.ExceptionUtility.ThrowHelperArgument(nameof(baseAddress), SR.Format( |
| | 0 | 472 | | SR.UTBadBaseAddress)); |
| | | 473 | | } |
| | | 474 | | |
| | 0 | 475 | | if (candidate == null) |
| | | 476 | | { |
| | 0 | 477 | | throw DiagnosticUtility.ExceptionUtility.ThrowHelperArgumentNull(nameof(candidate)); |
| | | 478 | | } |
| | | 479 | | |
| | | 480 | | // ensure that the candidate is 'under' the base address |
| | 0 | 481 | | if (!candidate.IsAbsoluteUri) |
| | | 482 | | { |
| | 0 | 483 | | return null; |
| | | 484 | | } |
| | | 485 | | |
| | 0 | 486 | | string basePath = UriTemplateHelpers.GetUriPath(baseAddress); |
| | 0 | 487 | | string candidatePath = UriTemplateHelpers.GetUriPath(candidate); |
| | 0 | 488 | | if (candidatePath.Length < basePath.Length) |
| | | 489 | | { |
| | 0 | 490 | | return null; |
| | | 491 | | } |
| | | 492 | | |
| | 0 | 493 | | if (!candidatePath.StartsWith(basePath, StringComparison.OrdinalIgnoreCase)) |
| | | 494 | | { |
| | 0 | 495 | | return null; |
| | | 496 | | } |
| | | 497 | | |
| | | 498 | | // Identifying the relative segments \ checking matching to the path : |
| | 0 | 499 | | int numSegmentsInBaseAddress = baseAddress.Segments.Length; |
| | 0 | 500 | | string[] candidateSegments = candidate.Segments; |
| | 0 | 501 | | if (!IsCandidatePathMatch(numSegmentsInBaseAddress, candidateSegments, |
| | 0 | 502 | | out int numMatchedSegments, out Collection<string> relativeCandidateSegments)) |
| | | 503 | | { |
| | 0 | 504 | | return null; |
| | | 505 | | } |
| | | 506 | | |
| | | 507 | | // Checking matching to the query (if should) : |
| | 0 | 508 | | NameValueCollection candidateQuery = null; |
| | 0 | 509 | | if (!UriTemplateHelpers.CanMatchQueryTrivially(this)) |
| | | 510 | | { |
| | 0 | 511 | | candidateQuery = UriTemplateHelpers.ParseQueryString(candidate.Query); |
| | 0 | 512 | | if (!UriTemplateHelpers.CanMatchQueryInterestingly(this, candidateQuery, false)) |
| | | 513 | | { |
| | 0 | 514 | | return null; |
| | | 515 | | } |
| | | 516 | | } |
| | | 517 | | |
| | | 518 | | // We matched; lets build the UriTemplateMatch |
| | 0 | 519 | | return CreateUriTemplateMatch(baseAddress, candidate, null, numMatchedSegments, |
| | 0 | 520 | | relativeCandidateSegments, candidateQuery); |
| | | 521 | | } |
| | | 522 | | |
| | 0 | 523 | | public override string ToString() => _originalTemplate; |
| | | 524 | | |
| | | 525 | | internal string AddPathVariable(UriTemplatePartType sourceNature, string varDeclaration) |
| | | 526 | | { |
| | 52 | 527 | | return AddPathVariable(sourceNature, varDeclaration, out _); |
| | | 528 | | } |
| | | 529 | | |
| | | 530 | | internal string AddPathVariable(UriTemplatePartType sourceNature, string varDeclaration, |
| | | 531 | | out bool hasDefaultValue) |
| | | 532 | | { |
| | 124 | 533 | | if (_variables == null) |
| | | 534 | | { |
| | 98 | 535 | | _variables = new VariablesCollection(this); |
| | | 536 | | } |
| | | 537 | | |
| | 124 | 538 | | return _variables.AddPathVariable(sourceNature, varDeclaration, out hasDefaultValue); |
| | | 539 | | } |
| | | 540 | | |
| | | 541 | | internal string AddQueryVariable(string varDeclaration) |
| | | 542 | | { |
| | 29 | 543 | | if (_variables == null) |
| | | 544 | | { |
| | 27 | 545 | | _variables = new VariablesCollection(this); |
| | | 546 | | } |
| | | 547 | | |
| | 29 | 548 | | return _variables.AddQueryVariable(varDeclaration); |
| | | 549 | | } |
| | | 550 | | |
| | | 551 | | internal UriTemplateMatch CreateUriTemplateMatch(Uri baseUri, Uri uri, object data, |
| | | 552 | | int numMatchedSegments, Collection<string> relativePathSegments, NameValueCollection uriQuery) |
| | | 553 | | { |
| | 35 | 554 | | UriTemplateMatch result = new UriTemplateMatch |
| | 35 | 555 | | { |
| | 35 | 556 | | RequestUri = uri, |
| | 35 | 557 | | BaseUri = baseUri |
| | 35 | 558 | | }; |
| | | 559 | | |
| | 35 | 560 | | if (uriQuery != null) |
| | | 561 | | { |
| | 1 | 562 | | result.SetQueryParameters(uriQuery); |
| | | 563 | | } |
| | | 564 | | |
| | 35 | 565 | | result.SetRelativePathSegments(relativePathSegments); |
| | 35 | 566 | | result.Data = data; |
| | 35 | 567 | | result.Template = this; |
| | 146 | 568 | | for (int i = 0; i < numMatchedSegments; i++) |
| | | 569 | | { |
| | 38 | 570 | | _segments[i].Lookup(result.RelativePathSegments[i], result.BoundVariables); |
| | | 571 | | } |
| | | 572 | | |
| | 35 | 573 | | if (_wildcard != null) |
| | | 574 | | { |
| | 2 | 575 | | _wildcard.Lookup(numMatchedSegments, result.RelativePathSegments, |
| | 2 | 576 | | result.BoundVariables); |
| | | 577 | | } |
| | 33 | 578 | | else if (numMatchedSegments < _segments.Count) |
| | | 579 | | { |
| | 1 | 580 | | BindTerminalDefaults(numMatchedSegments, result.BoundVariables); |
| | | 581 | | } |
| | | 582 | | |
| | 35 | 583 | | if (_queries.Count > 0) |
| | | 584 | | { |
| | 4 | 585 | | foreach (KeyValuePair<string, UriTemplateQueryValue> kvp in _queries) |
| | | 586 | | { |
| | 1 | 587 | | kvp.Value.Lookup(result.QueryParameters[kvp.Key], result.BoundVariables); |
| | | 588 | | //UriTemplateHelpers.AssertCanonical(varName); |
| | | 589 | | } |
| | | 590 | | } |
| | | 591 | | |
| | 35 | 592 | | if (_additionalDefaults != null) |
| | | 593 | | { |
| | 0 | 594 | | foreach (KeyValuePair<string, string> kvp in _additionalDefaults) |
| | | 595 | | { |
| | 0 | 596 | | result.BoundVariables.Add(kvp.Key, UnescapeDefaultValue(kvp.Value)); |
| | | 597 | | } |
| | | 598 | | } |
| | | 599 | | |
| | | 600 | | Fx.Assert(result.RelativePathSegments.Count - numMatchedSegments >= 0, "bad segment computation"); |
| | 35 | 601 | | result.SetWildcardPathSegmentsStart(numMatchedSegments); |
| | | 602 | | |
| | 35 | 603 | | return result; |
| | | 604 | | } |
| | | 605 | | |
| | | 606 | | internal bool IsPathPartiallyEquivalentAt(UriTemplate other, int segmentsCount) |
| | | 607 | | { |
| | | 608 | | // Refer to the note on template equivalency at IsEquivalentTo |
| | | 609 | | // This method checks if any uri with given number of segments, which can be matched |
| | | 610 | | // by this template, can be also matched by the other template. |
| | | 611 | | Fx.Assert(segmentsCount >= _firstOptionalSegment - 1, "How can that be? The Trie is constructed that way!"); |
| | | 612 | | Fx.Assert(segmentsCount <= _segments.Count, "How can that be? The Trie is constructed that way!"); |
| | | 613 | | Fx.Assert(segmentsCount >= other._firstOptionalSegment - 1, "How can that be? The Trie is constructed that w |
| | | 614 | | Fx.Assert(segmentsCount <= other._segments.Count, "How can that be? The Trie is constructed that way!"); |
| | | 615 | | |
| | 0 | 616 | | for (int i = 0; i < segmentsCount; ++i) |
| | | 617 | | { |
| | 0 | 618 | | if (!_segments[i].IsEquivalentTo(other._segments[i], |
| | 0 | 619 | | ((i == segmentsCount - 1) && (IgnoreTrailingSlash || other.IgnoreTrailingSlash)))) |
| | | 620 | | { |
| | 0 | 621 | | return false; |
| | | 622 | | } |
| | | 623 | | } |
| | | 624 | | |
| | 0 | 625 | | return true; |
| | | 626 | | } |
| | | 627 | | |
| | | 628 | | internal bool IsQueryEquivalent(UriTemplate other) |
| | | 629 | | { |
| | 0 | 630 | | if (_queries.Count != other._queries.Count) |
| | | 631 | | { |
| | 0 | 632 | | return false; |
| | | 633 | | } |
| | | 634 | | |
| | 0 | 635 | | foreach (string key in _queries.Keys) |
| | | 636 | | { |
| | 0 | 637 | | UriTemplateQueryValue utqv = _queries[key]; |
| | 0 | 638 | | if (!other._queries.TryGetValue(key, out UriTemplateQueryValue otherUtqv)) |
| | | 639 | | { |
| | 0 | 640 | | return false; |
| | | 641 | | } |
| | | 642 | | |
| | 0 | 643 | | if (!utqv.IsEquivalentTo(otherUtqv)) |
| | | 644 | | { |
| | 0 | 645 | | return false; |
| | | 646 | | } |
| | | 647 | | } |
| | | 648 | | |
| | 0 | 649 | | return true; |
| | 0 | 650 | | } |
| | | 651 | | |
| | | 652 | | internal static Uri RewriteUri(Uri uri, string host) |
| | | 653 | | { |
| | 0 | 654 | | if (!string.IsNullOrEmpty(host)) |
| | | 655 | | { |
| | 0 | 656 | | string originalHostHeader = uri.Host + ((!uri.IsDefaultPort) ? ":" + uri.Port.ToString(CultureInfo.Invar |
| | 0 | 657 | | if (!string.Equals(originalHostHeader, host, StringComparison.OrdinalIgnoreCase)) |
| | | 658 | | { |
| | 0 | 659 | | var sourceUri = new Uri(string.Format(CultureInfo.InvariantCulture, "{0}://{1}", uri.Scheme, host)); |
| | 0 | 660 | | return (new UriBuilder(uri) { Host = sourceUri.Host, Port = sourceUri.Port }).Uri; |
| | | 661 | | } |
| | | 662 | | } |
| | | 663 | | |
| | 0 | 664 | | return uri; |
| | | 665 | | } |
| | | 666 | | |
| | | 667 | | private Uri Bind(Uri baseAddress, BindInformation bindInfo, bool omitDefaults) |
| | | 668 | | { |
| | 115 | 669 | | UriBuilder result = new UriBuilder(baseAddress); |
| | 115 | 670 | | int parameterIndex = 0; |
| | 115 | 671 | | int lastPathParameter = ((_variables == null) ? -1 : _variables.PathSegmentVariableNames.Count - 1); |
| | | 672 | | int lastPathParameterToBind; |
| | 115 | 673 | | if (lastPathParameter == -1) |
| | | 674 | | { |
| | 115 | 675 | | lastPathParameterToBind = -1; |
| | | 676 | | } |
| | 0 | 677 | | else if (omitDefaults) |
| | | 678 | | { |
| | 0 | 679 | | lastPathParameterToBind = bindInfo.LastNonDefaultPathParameter; |
| | | 680 | | } |
| | | 681 | | else |
| | | 682 | | { |
| | 0 | 683 | | lastPathParameterToBind = bindInfo.LastNonNullablePathParameter; |
| | | 684 | | } |
| | | 685 | | |
| | 115 | 686 | | string[] parameters = bindInfo.NormalizedParameters; |
| | 115 | 687 | | IDictionary<string, string> extraQueryParameters = bindInfo.AdditionalParameters; |
| | | 688 | | // Binding the path : |
| | 115 | 689 | | StringBuilder pathString = new StringBuilder(result.Path); |
| | 115 | 690 | | if (pathString[pathString.Length - 1] != '/') |
| | | 691 | | { |
| | 114 | 692 | | pathString.Append('/'); |
| | | 693 | | } |
| | | 694 | | |
| | 115 | 695 | | if (lastPathParameterToBind < lastPathParameter) |
| | | 696 | | { |
| | | 697 | | // Binding all the parameters we need |
| | 0 | 698 | | int segmentIndex = 0; |
| | 0 | 699 | | while (parameterIndex <= lastPathParameterToBind) |
| | | 700 | | { |
| | | 701 | | Fx.Assert(segmentIndex < _segments.Count, |
| | | 702 | | "Calculation of LastNonDefaultPathParameter,lastPathParameter or parameterIndex failed"); |
| | 0 | 703 | | _segments[segmentIndex++].Bind(parameters, ref parameterIndex, pathString); |
| | | 704 | | } |
| | | 705 | | Fx.Assert(parameterIndex == lastPathParameterToBind + 1, |
| | | 706 | | "That is the exit criteria from the loop"); |
| | | 707 | | // Maybe we have some literals yet to bind |
| | | 708 | | Fx.Assert(segmentIndex < _segments.Count, |
| | | 709 | | "Calculation of LastNonDefaultPathParameter,lastPathParameter or parameterIndex failed"); |
| | 0 | 710 | | while (_segments[segmentIndex].Nature == UriTemplatePartType.Literal) |
| | | 711 | | { |
| | 0 | 712 | | _segments[segmentIndex++].Bind(parameters, ref parameterIndex, pathString); |
| | | 713 | | Fx.Assert(parameterIndex == lastPathParameterToBind + 1, |
| | | 714 | | "We have moved the parameter index in a literal binding"); |
| | | 715 | | Fx.Assert(segmentIndex < _segments.Count, |
| | | 716 | | "Calculation of LastNonDefaultPathParameter,lastPathParameter or parameterIndex failed"); |
| | | 717 | | } |
| | | 718 | | // We're done; skip to the beggining of the query parameters |
| | 0 | 719 | | parameterIndex = lastPathParameter + 1; |
| | | 720 | | } |
| | 115 | 721 | | else if (_segments.Count > 0 || _wildcard != null) |
| | | 722 | | { |
| | 468 | 723 | | for (int i = 0; i < _segments.Count; i++) |
| | | 724 | | { |
| | 119 | 725 | | _segments[i].Bind(parameters, ref parameterIndex, pathString); |
| | | 726 | | } |
| | | 727 | | |
| | 115 | 728 | | if (_wildcard != null) |
| | | 729 | | { |
| | 0 | 730 | | _wildcard.Bind(parameters, ref parameterIndex, pathString); |
| | | 731 | | } |
| | | 732 | | } |
| | | 733 | | |
| | 115 | 734 | | if (IgnoreTrailingSlash && (pathString[pathString.Length - 1] == '/')) |
| | | 735 | | { |
| | 0 | 736 | | pathString.Remove(pathString.Length - 1, 1); |
| | | 737 | | } |
| | | 738 | | |
| | 115 | 739 | | result.Path = pathString.ToString(); |
| | | 740 | | // Binding the query : |
| | 115 | 741 | | if ((_queries.Count != 0) || (extraQueryParameters != null)) |
| | | 742 | | { |
| | 0 | 743 | | StringBuilder query = new StringBuilder(""); |
| | 0 | 744 | | foreach (string key in _queries.Keys) |
| | | 745 | | { |
| | 0 | 746 | | _queries[key].Bind(key, parameters, ref parameterIndex, query); |
| | | 747 | | } |
| | | 748 | | |
| | 0 | 749 | | if (extraQueryParameters != null) |
| | | 750 | | { |
| | 0 | 751 | | foreach (string key in extraQueryParameters.Keys) |
| | | 752 | | { |
| | 0 | 753 | | if (_queries.ContainsKey(key.ToUpperInvariant())) |
| | | 754 | | { |
| | | 755 | | // This can only be if the key passed has the same name as some literal key |
| | 0 | 756 | | throw DiagnosticUtility.ExceptionUtility.ThrowHelperArgument(nameof(parameters), SR.Format( |
| | 0 | 757 | | SR.UTBothLiteralAndNameValueCollectionKey, key)); |
| | | 758 | | } |
| | 0 | 759 | | string value = extraQueryParameters[key]; |
| | 0 | 760 | | string escapedValue = (string.IsNullOrEmpty(value) ? string.Empty : UrlUtility.UrlEncode(value, |
| | 0 | 761 | | query.AppendFormat("&{0}={1}", UrlUtility.UrlEncode(key, Encoding.UTF8), escapedValue); |
| | | 762 | | } |
| | | 763 | | } |
| | | 764 | | |
| | 0 | 765 | | if (query.Length != 0) |
| | | 766 | | { |
| | 0 | 767 | | query.Remove(0, 1); // remove extra leading '&' |
| | | 768 | | } |
| | | 769 | | |
| | 0 | 770 | | result.Query = query.ToString(); |
| | | 771 | | } |
| | | 772 | | |
| | | 773 | | // Adding the fragment (if needed) |
| | 115 | 774 | | if (_fragment != null) |
| | | 775 | | { |
| | 115 | 776 | | result.Fragment = _fragment; |
| | | 777 | | } |
| | | 778 | | |
| | 115 | 779 | | return result.Uri; |
| | | 780 | | } |
| | | 781 | | |
| | | 782 | | private void BindTerminalDefaults(int numMatchedSegments, NameValueCollection boundParameters) |
| | | 783 | | { |
| | | 784 | | Fx.Assert(!HasWildcard, "There are no terminal default when ends with wildcard"); |
| | | 785 | | Fx.Assert(numMatchedSegments < _segments.Count, "Otherwise - no defaults to bind"); |
| | | 786 | | Fx.Assert(_variables != null, "Otherwise - no default values to bind"); |
| | | 787 | | Fx.Assert(_variables.DefaultValues != null, "Otherwise - no default values to bind"); |
| | | 788 | | |
| | 4 | 789 | | for (int i = numMatchedSegments; i < _segments.Count; i++) |
| | | 790 | | { |
| | 1 | 791 | | switch (_segments[i].Nature) |
| | | 792 | | { |
| | | 793 | | case UriTemplatePartType.Variable: |
| | | 794 | | { |
| | 1 | 795 | | UriTemplateVariablePathSegment vps = _segments[i] as UriTemplateVariablePathSegment; |
| | | 796 | | Fx.Assert(vps != null, "How can that be? That its nature"); |
| | 1 | 797 | | _variables.LookupDefault(vps.VarName, boundParameters); |
| | | 798 | | } |
| | | 799 | | break; |
| | | 800 | | |
| | | 801 | | default: |
| | | 802 | | Fx.Assert("We only support terminal defaults on Variable segments"); |
| | | 803 | | break; |
| | | 804 | | } |
| | | 805 | | } |
| | 1 | 806 | | } |
| | | 807 | | |
| | | 808 | | private bool IsCandidatePathMatch(int numSegmentsInBaseAddress, string[] candidateSegments, |
| | | 809 | | out int numMatchedSegments, out Collection<string> relativeSegments) |
| | | 810 | | { |
| | 0 | 811 | | int numRelativeSegments = candidateSegments.Length - numSegmentsInBaseAddress; |
| | | 812 | | Fx.Assert(numRelativeSegments >= 0, "bad segments num"); |
| | 0 | 813 | | relativeSegments = new Collection<string>(); |
| | 0 | 814 | | bool isStillMatch = true; |
| | 0 | 815 | | int relativeSegmentsIndex = 0; |
| | | 816 | | |
| | 0 | 817 | | while (isStillMatch && (relativeSegmentsIndex < numRelativeSegments)) |
| | | 818 | | { |
| | 0 | 819 | | string segment = candidateSegments[relativeSegmentsIndex + numSegmentsInBaseAddress]; |
| | | 820 | | // Mathcing to next regular segment in the template (if there is one); building the wire segment represe |
| | 0 | 821 | | if (relativeSegmentsIndex < _segments.Count) |
| | | 822 | | { |
| | 0 | 823 | | bool ignoreSlash = (this.IgnoreTrailingSlash && (relativeSegmentsIndex == numRelativeSegments - 1)); |
| | 0 | 824 | | UriTemplateLiteralPathSegment lps = UriTemplateLiteralPathSegment.CreateFromWireData(segment); |
| | 0 | 825 | | if (!_segments[relativeSegmentsIndex].IsMatch(lps, ignoreSlash)) |
| | | 826 | | { |
| | 0 | 827 | | isStillMatch = false; |
| | 0 | 828 | | break; |
| | | 829 | | } |
| | | 830 | | |
| | 0 | 831 | | string relPathSeg = Uri.UnescapeDataString(segment); |
| | 0 | 832 | | if (lps.EndsWithSlash) |
| | | 833 | | { |
| | | 834 | | Fx.Assert(relPathSeg.EndsWith("/", StringComparison.Ordinal), "problem with relative path segmen |
| | 0 | 835 | | relPathSeg = relPathSeg.Substring(0, relPathSeg.Length - 1); // trim slash |
| | | 836 | | } |
| | | 837 | | |
| | 0 | 838 | | relativeSegments.Add(relPathSeg); |
| | | 839 | | } |
| | | 840 | | // Checking if the template has a wild card ('*') or a final star var segment ("{*<var name>}" |
| | 0 | 841 | | else if (HasWildcard) |
| | | 842 | | { |
| | | 843 | | break; |
| | | 844 | | } |
| | | 845 | | else |
| | | 846 | | { |
| | 0 | 847 | | isStillMatch = false; |
| | 0 | 848 | | break; |
| | | 849 | | } |
| | | 850 | | |
| | 0 | 851 | | relativeSegmentsIndex++; |
| | | 852 | | } |
| | | 853 | | |
| | 0 | 854 | | if (isStillMatch) |
| | | 855 | | { |
| | 0 | 856 | | numMatchedSegments = relativeSegmentsIndex; |
| | | 857 | | // building the wire representation to segments that were matched to a wild card |
| | 0 | 858 | | if (relativeSegmentsIndex < numRelativeSegments) |
| | | 859 | | { |
| | 0 | 860 | | while (relativeSegmentsIndex < numRelativeSegments) |
| | | 861 | | { |
| | 0 | 862 | | string relPathSeg = Uri.UnescapeDataString(candidateSegments[relativeSegmentsIndex + numSegments |
| | 0 | 863 | | if (relPathSeg.EndsWith("/", StringComparison.Ordinal)) |
| | | 864 | | { |
| | 0 | 865 | | relPathSeg = relPathSeg.Substring(0, relPathSeg.Length - 1); // trim slash |
| | | 866 | | } |
| | 0 | 867 | | relativeSegments.Add(relPathSeg); |
| | 0 | 868 | | relativeSegmentsIndex++; |
| | | 869 | | } |
| | | 870 | | } |
| | | 871 | | // Checking if we matched all required segments already |
| | 0 | 872 | | else if (numMatchedSegments < _firstOptionalSegment) |
| | | 873 | | { |
| | 0 | 874 | | isStillMatch = false; |
| | | 875 | | } |
| | | 876 | | } |
| | | 877 | | else |
| | | 878 | | { |
| | 0 | 879 | | numMatchedSegments = 0; |
| | | 880 | | } |
| | | 881 | | |
| | 0 | 882 | | return isStillMatch; |
| | | 883 | | } |
| | | 884 | | |
| | | 885 | | private bool IsPathFullyEquivalent(UriTemplate other) |
| | | 886 | | { |
| | | 887 | | // Refer to the note on template equivalency at IsEquivalentTo |
| | | 888 | | // This method checks if both templates has a fully equivalent path. |
| | 0 | 889 | | if (HasWildcard != other.HasWildcard) |
| | | 890 | | { |
| | 0 | 891 | | return false; |
| | | 892 | | } |
| | | 893 | | |
| | 0 | 894 | | if (_segments.Count != other._segments.Count) |
| | | 895 | | { |
| | 0 | 896 | | return false; |
| | | 897 | | } |
| | | 898 | | |
| | 0 | 899 | | for (int i = 0; i < _segments.Count; ++i) |
| | | 900 | | { |
| | 0 | 901 | | if (!_segments[i].IsEquivalentTo(other._segments[i], |
| | 0 | 902 | | (i == _segments.Count - 1) && !HasWildcard && (IgnoreTrailingSlash || other.IgnoreTrailingSlash))) |
| | | 903 | | { |
| | 0 | 904 | | return false; |
| | | 905 | | } |
| | | 906 | | } |
| | | 907 | | |
| | 0 | 908 | | return true; |
| | | 909 | | } |
| | | 910 | | |
| | | 911 | | private BindInformation PrepareBindInformation(IDictionary<string, string> parameters, bool omitDefaults) |
| | | 912 | | { |
| | 0 | 913 | | if (parameters == null) |
| | | 914 | | { |
| | 0 | 915 | | throw DiagnosticUtility.ExceptionUtility.ThrowHelperArgumentNull(nameof(parameters)); |
| | | 916 | | } |
| | | 917 | | |
| | 0 | 918 | | IDictionary<string, string> extraParameters = new Dictionary<string, string>(UriTemplateHelpers.GetQueryKeyC |
| | 0 | 919 | | foreach (KeyValuePair<string, string> kvp in parameters) |
| | | 920 | | { |
| | 0 | 921 | | if (string.IsNullOrEmpty(kvp.Key)) |
| | | 922 | | { |
| | 0 | 923 | | throw DiagnosticUtility.ExceptionUtility.ThrowHelperArgument(nameof(parameters), |
| | 0 | 924 | | SR.Format(SR.UTBindByNameCalledWithEmptyKey)); |
| | | 925 | | } |
| | | 926 | | |
| | 0 | 927 | | extraParameters.Add(kvp); |
| | | 928 | | } |
| | | 929 | | |
| | 0 | 930 | | ProcessDefaultsAndCreateBindInfo(omitDefaults, extraParameters, out BindInformation bindInfo); |
| | | 931 | | |
| | 0 | 932 | | return bindInfo; |
| | | 933 | | } |
| | | 934 | | |
| | | 935 | | private BindInformation PrepareBindInformation(NameValueCollection parameters, bool omitDefaults) |
| | | 936 | | { |
| | 0 | 937 | | if (parameters == null) |
| | | 938 | | { |
| | 0 | 939 | | throw DiagnosticUtility.ExceptionUtility.ThrowHelperArgumentNull(nameof(parameters)); |
| | | 940 | | } |
| | | 941 | | |
| | 0 | 942 | | IDictionary<string, string> extraParameters = new Dictionary<string, string>(UriTemplateHelpers.GetQueryKeyC |
| | 0 | 943 | | foreach (string key in parameters.AllKeys) |
| | | 944 | | { |
| | 0 | 945 | | if (string.IsNullOrEmpty(key)) |
| | | 946 | | { |
| | 0 | 947 | | throw DiagnosticUtility.ExceptionUtility.ThrowHelperArgument(nameof(parameters), |
| | 0 | 948 | | SR.Format(SR.UTBindByNameCalledWithEmptyKey)); |
| | | 949 | | } |
| | | 950 | | |
| | 0 | 951 | | extraParameters.Add(key, parameters[key]); |
| | | 952 | | } |
| | | 953 | | |
| | 0 | 954 | | ProcessDefaultsAndCreateBindInfo(omitDefaults, extraParameters, out BindInformation bindInfo); |
| | | 955 | | |
| | 0 | 956 | | return bindInfo; |
| | | 957 | | } |
| | | 958 | | |
| | | 959 | | private void ProcessDefaultsAndCreateBindInfo(bool omitDefaults, IDictionary<string, string> extraParameters, |
| | | 960 | | out BindInformation bindInfo) |
| | | 961 | | { |
| | | 962 | | Fx.Assert(extraParameters != null, "We are expected to create it at the calling PrepareBindInformation"); |
| | | 963 | | |
| | 0 | 964 | | if (_additionalDefaults != null) |
| | | 965 | | { |
| | 0 | 966 | | if (omitDefaults) |
| | | 967 | | { |
| | 0 | 968 | | foreach (KeyValuePair<string, string> kvp in _additionalDefaults) |
| | | 969 | | { |
| | 0 | 970 | | if (extraParameters.TryGetValue(kvp.Key, out string extraParameter)) |
| | | 971 | | { |
| | 0 | 972 | | if (string.Compare(extraParameter, kvp.Value, StringComparison.Ordinal) == 0) |
| | | 973 | | { |
| | 0 | 974 | | extraParameters.Remove(kvp.Key); |
| | | 975 | | } |
| | | 976 | | } |
| | | 977 | | } |
| | | 978 | | } |
| | | 979 | | else |
| | | 980 | | { |
| | 0 | 981 | | foreach (KeyValuePair<string, string> kvp in _additionalDefaults) |
| | | 982 | | { |
| | 0 | 983 | | if (!extraParameters.ContainsKey(kvp.Key)) |
| | | 984 | | { |
| | 0 | 985 | | extraParameters.Add(kvp.Key, kvp.Value); |
| | | 986 | | } |
| | | 987 | | } |
| | | 988 | | } |
| | | 989 | | } |
| | | 990 | | |
| | 0 | 991 | | if (extraParameters.Count == 0) |
| | | 992 | | { |
| | 0 | 993 | | extraParameters = null; |
| | | 994 | | } |
| | | 995 | | |
| | 0 | 996 | | bindInfo = new BindInformation(extraParameters); |
| | 0 | 997 | | } |
| | | 998 | | |
| | | 999 | | private string UnescapeDefaultValue(string escapedValue) |
| | | 1000 | | { |
| | 1 | 1001 | | if (string.IsNullOrEmpty(escapedValue)) |
| | | 1002 | | { |
| | 0 | 1003 | | return escapedValue; |
| | | 1004 | | } |
| | | 1005 | | |
| | 1 | 1006 | | if (_unescapedDefaults == null) |
| | | 1007 | | { |
| | 1 | 1008 | | _unescapedDefaults = new ConcurrentDictionary<string, string>(StringComparer.Ordinal); |
| | | 1009 | | } |
| | | 1010 | | |
| | 1 | 1011 | | return _unescapedDefaults.GetOrAdd(escapedValue, Uri.UnescapeDataString); |
| | | 1012 | | } |
| | | 1013 | | |
| | | 1014 | | internal struct BindInformation |
| | | 1015 | | { |
| | | 1016 | | public BindInformation(string[] normalizedParameters, int lastNonDefaultPathParameter, |
| | | 1017 | | int lastNonNullablePathParameter, IDictionary<string, string> additionalParameters) |
| | | 1018 | | { |
| | 0 | 1019 | | NormalizedParameters = normalizedParameters; |
| | 0 | 1020 | | LastNonDefaultPathParameter = lastNonDefaultPathParameter; |
| | 0 | 1021 | | LastNonNullablePathParameter = lastNonNullablePathParameter; |
| | 0 | 1022 | | AdditionalParameters = additionalParameters; |
| | 0 | 1023 | | } |
| | | 1024 | | |
| | | 1025 | | public BindInformation(IDictionary<string, string> additionalParameters) |
| | | 1026 | | { |
| | 115 | 1027 | | NormalizedParameters = null; |
| | 115 | 1028 | | LastNonDefaultPathParameter = -1; |
| | 115 | 1029 | | LastNonNullablePathParameter = -1; |
| | 115 | 1030 | | AdditionalParameters = additionalParameters; |
| | 115 | 1031 | | } |
| | | 1032 | | |
| | 115 | 1033 | | public IDictionary<string, string> AdditionalParameters { get; } |
| | 0 | 1034 | | public int LastNonDefaultPathParameter { get; } |
| | 0 | 1035 | | public int LastNonNullablePathParameter { get; } |
| | 115 | 1036 | | public string[] NormalizedParameters { get; } |
| | | 1037 | | } |
| | | 1038 | | |
| | | 1039 | | internal class UriTemplateDefaults : IDictionary<string, string> |
| | | 1040 | | { |
| | | 1041 | | private readonly Dictionary<string, string> _defaults; |
| | | 1042 | | private readonly ReadOnlyCollection<string> _keys; |
| | | 1043 | | private readonly ReadOnlyCollection<string> _values; |
| | | 1044 | | |
| | 0 | 1045 | | public UriTemplateDefaults(UriTemplate template) |
| | | 1046 | | { |
| | 0 | 1047 | | _defaults = new Dictionary<string, string>(StringComparer.OrdinalIgnoreCase); |
| | 0 | 1048 | | if ((template._variables != null) && (template._variables.DefaultValues != null)) |
| | | 1049 | | { |
| | 0 | 1050 | | foreach (KeyValuePair<string, string> kvp in template._variables.DefaultValues) |
| | | 1051 | | { |
| | 0 | 1052 | | _defaults.Add(kvp.Key, kvp.Value); |
| | | 1053 | | } |
| | | 1054 | | } |
| | | 1055 | | |
| | 0 | 1056 | | if (template._additionalDefaults != null) |
| | | 1057 | | { |
| | 0 | 1058 | | foreach (KeyValuePair<string, string> kvp in template._additionalDefaults) |
| | | 1059 | | { |
| | 0 | 1060 | | _defaults.Add(kvp.Key.ToUpperInvariant(), kvp.Value); |
| | | 1061 | | } |
| | | 1062 | | } |
| | | 1063 | | |
| | 0 | 1064 | | _keys = new ReadOnlyCollection<string>(new List<string>(_defaults.Keys)); |
| | 0 | 1065 | | _values = new ReadOnlyCollection<string>(new List<string>(_defaults.Values)); |
| | 0 | 1066 | | } |
| | | 1067 | | |
| | 0 | 1068 | | public int Count => _defaults.Count; |
| | | 1069 | | |
| | 0 | 1070 | | public bool IsReadOnly => true; |
| | | 1071 | | |
| | 0 | 1072 | | public ICollection<string> Keys => _keys; |
| | | 1073 | | |
| | 0 | 1074 | | public ICollection<string> Values => _values; |
| | | 1075 | | |
| | | 1076 | | public string this[string key] |
| | | 1077 | | { |
| | | 1078 | | get |
| | | 1079 | | { |
| | 0 | 1080 | | return _defaults[key]; |
| | | 1081 | | } |
| | | 1082 | | set |
| | | 1083 | | { |
| | 0 | 1084 | | throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(new NotSupportedException( |
| | 0 | 1085 | | SR.Format(SR.UTDefaultValuesAreImmutable))); |
| | | 1086 | | } |
| | | 1087 | | } |
| | | 1088 | | |
| | | 1089 | | public void Add(string key, string value) |
| | | 1090 | | { |
| | 0 | 1091 | | throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(new NotSupportedException( |
| | 0 | 1092 | | SR.Format(SR.UTDefaultValuesAreImmutable))); |
| | | 1093 | | } |
| | | 1094 | | |
| | | 1095 | | public void Add(KeyValuePair<string, string> item) |
| | | 1096 | | { |
| | 0 | 1097 | | throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(new NotSupportedException( |
| | 0 | 1098 | | SR.Format(SR.UTDefaultValuesAreImmutable))); |
| | | 1099 | | } |
| | | 1100 | | |
| | | 1101 | | public void Clear() |
| | | 1102 | | { |
| | 0 | 1103 | | throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(new NotSupportedException( |
| | 0 | 1104 | | SR.Format(SR.UTDefaultValuesAreImmutable))); |
| | | 1105 | | } |
| | | 1106 | | |
| | | 1107 | | public bool Contains(KeyValuePair<string, string> item) |
| | | 1108 | | { |
| | 0 | 1109 | | return (_defaults as ICollection<KeyValuePair<string, string>>).Contains(item); |
| | | 1110 | | } |
| | | 1111 | | |
| | | 1112 | | public bool ContainsKey(string key) |
| | | 1113 | | { |
| | 0 | 1114 | | return _defaults.ContainsKey(key); |
| | | 1115 | | } |
| | | 1116 | | |
| | | 1117 | | public void CopyTo(KeyValuePair<string, string>[] array, int arrayIndex) |
| | | 1118 | | { |
| | 0 | 1119 | | (_defaults as ICollection<KeyValuePair<string, string>>).CopyTo(array, arrayIndex); |
| | 0 | 1120 | | } |
| | | 1121 | | |
| | | 1122 | | public IEnumerator<KeyValuePair<string, string>> GetEnumerator() |
| | | 1123 | | { |
| | 0 | 1124 | | return _defaults.GetEnumerator(); |
| | | 1125 | | } |
| | | 1126 | | |
| | | 1127 | | public bool Remove(string key) |
| | | 1128 | | { |
| | 0 | 1129 | | throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(new NotSupportedException( |
| | 0 | 1130 | | SR.Format(SR.UTDefaultValuesAreImmutable))); |
| | | 1131 | | } |
| | | 1132 | | |
| | | 1133 | | public bool Remove(KeyValuePair<string, string> item) |
| | | 1134 | | { |
| | 0 | 1135 | | throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(new NotSupportedException( |
| | 0 | 1136 | | SR.Format(SR.UTDefaultValuesAreImmutable))); |
| | | 1137 | | } |
| | | 1138 | | |
| | | 1139 | | System.Collections.IEnumerator System.Collections.IEnumerable.GetEnumerator() |
| | | 1140 | | { |
| | 0 | 1141 | | return _defaults.GetEnumerator(); |
| | | 1142 | | } |
| | | 1143 | | |
| | | 1144 | | public bool TryGetValue(string key, out string value) |
| | | 1145 | | { |
| | 0 | 1146 | | return _defaults.TryGetValue(key, out value); |
| | | 1147 | | } |
| | | 1148 | | } |
| | | 1149 | | |
| | | 1150 | | internal class VariablesCollection |
| | | 1151 | | { |
| | | 1152 | | private readonly UriTemplate _owner; |
| | | 1153 | | private static ReadOnlyCollection<string> s_emptyStringCollection = null; |
| | | 1154 | | private int _firstNullablePathVariable; |
| | | 1155 | | private readonly List<string> _pathSegmentVariableNames; // ToUpperInvariant, in order they occur in the ori |
| | | 1156 | | private ReadOnlyCollection<string> _pathSegmentVariableNamesSnapshot = null; |
| | | 1157 | | private readonly List<UriTemplatePartType> _pathSegmentVariableNature; |
| | | 1158 | | private List<string> _queryValueVariableNames; // ToUpperInvariant, in order they occur in the original temp |
| | | 1159 | | private ReadOnlyCollection<string> _queryValueVariableNamesSnapshot = null; |
| | | 1160 | | |
| | 125 | 1161 | | public VariablesCollection(UriTemplate owner) |
| | | 1162 | | { |
| | 125 | 1163 | | _owner = owner; |
| | 125 | 1164 | | _pathSegmentVariableNames = new List<string>(); |
| | 125 | 1165 | | _pathSegmentVariableNature = new List<UriTemplatePartType>(); |
| | 125 | 1166 | | _queryValueVariableNames = new List<string>(); |
| | 125 | 1167 | | _firstNullablePathVariable = -1; |
| | 125 | 1168 | | } |
| | | 1169 | | |
| | | 1170 | | public static ReadOnlyCollection<string> EmptyCollection |
| | | 1171 | | { |
| | | 1172 | | get |
| | | 1173 | | { |
| | 528 | 1174 | | if (s_emptyStringCollection == null) |
| | | 1175 | | { |
| | 1 | 1176 | | s_emptyStringCollection = new ReadOnlyCollection<string>(new List<string>()); |
| | | 1177 | | } |
| | | 1178 | | |
| | 528 | 1179 | | return s_emptyStringCollection; |
| | | 1180 | | } |
| | | 1181 | | } |
| | | 1182 | | |
| | 246 | 1183 | | public Dictionary<string, string> DefaultValues { get; private set; } |
| | | 1184 | | |
| | | 1185 | | public ReadOnlyCollection<string> PathSegmentVariableNames |
| | | 1186 | | { |
| | | 1187 | | get |
| | | 1188 | | { |
| | 85 | 1189 | | if (_pathSegmentVariableNamesSnapshot == null) |
| | | 1190 | | { |
| | 85 | 1191 | | Interlocked.CompareExchange(ref _pathSegmentVariableNamesSnapshot, new ReadOnlyCollection<string |
| | 85 | 1192 | | _pathSegmentVariableNames), null); |
| | | 1193 | | } |
| | | 1194 | | |
| | 85 | 1195 | | return _pathSegmentVariableNamesSnapshot; |
| | | 1196 | | } |
| | | 1197 | | } |
| | | 1198 | | |
| | | 1199 | | public ReadOnlyCollection<string> QueryValueVariableNames |
| | | 1200 | | { |
| | | 1201 | | get |
| | | 1202 | | { |
| | 83 | 1203 | | if (_queryValueVariableNamesSnapshot == null) |
| | | 1204 | | { |
| | 83 | 1205 | | Interlocked.CompareExchange(ref _queryValueVariableNamesSnapshot, new ReadOnlyCollection<string> |
| | 83 | 1206 | | _queryValueVariableNames), null); |
| | | 1207 | | } |
| | | 1208 | | |
| | 83 | 1209 | | return _queryValueVariableNamesSnapshot; |
| | | 1210 | | } |
| | | 1211 | | } |
| | | 1212 | | |
| | | 1213 | | public void AddDefaultValue(string varName, string value) |
| | | 1214 | | { |
| | 0 | 1215 | | int varIndex = _pathSegmentVariableNames.IndexOf(varName); |
| | | 1216 | | Fx.Assert(varIndex != -1, "Adding default value is restricted to path variables"); |
| | 0 | 1217 | | if ((_owner._wildcard != null) && _owner._wildcard.HasVariable && |
| | 0 | 1218 | | (varIndex == _pathSegmentVariableNames.Count - 1)) |
| | | 1219 | | { |
| | 0 | 1220 | | throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(new InvalidOperationException( |
| | 0 | 1221 | | SR.Format(SR.UTStarVariableWithDefaultsFromAdditionalDefaults, |
| | 0 | 1222 | | _owner._originalTemplate, varName))); |
| | | 1223 | | } |
| | | 1224 | | |
| | 0 | 1225 | | if (_pathSegmentVariableNature[varIndex] != UriTemplatePartType.Variable) |
| | | 1226 | | { |
| | 0 | 1227 | | throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(new InvalidOperationException( |
| | 0 | 1228 | | SR.Format(SR.UTDefaultValueToCompoundSegmentVarFromAdditionalDefaults, |
| | 0 | 1229 | | _owner._originalTemplate, varName))); |
| | | 1230 | | } |
| | | 1231 | | |
| | 0 | 1232 | | if (string.IsNullOrEmpty(value) || |
| | 0 | 1233 | | (string.Compare(value, NullableDefault, StringComparison.OrdinalIgnoreCase) == 0)) |
| | | 1234 | | { |
| | 0 | 1235 | | value = null; |
| | | 1236 | | } |
| | | 1237 | | |
| | 0 | 1238 | | if (DefaultValues == null) |
| | | 1239 | | { |
| | 0 | 1240 | | DefaultValues = new Dictionary<string, string>(); |
| | | 1241 | | } |
| | | 1242 | | |
| | 0 | 1243 | | DefaultValues.Add(varName, value); |
| | 0 | 1244 | | } |
| | | 1245 | | |
| | | 1246 | | public string AddPathVariable(UriTemplatePartType sourceNature, string varDeclaration, out bool hasDefaultVa |
| | | 1247 | | { |
| | | 1248 | | Fx.Assert(sourceNature != UriTemplatePartType.Literal, "Literal path segments can't be the source for pa |
| | | 1249 | | |
| | 124 | 1250 | | ParseVariableDeclaration(varDeclaration, out string varName, out string defaultValue); |
| | 124 | 1251 | | hasDefaultValue = (defaultValue != null); |
| | 124 | 1252 | | if (varName.IndexOf(WildcardPath, StringComparison.Ordinal) != -1) |
| | | 1253 | | { |
| | 0 | 1254 | | throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(new FormatException( |
| | 0 | 1255 | | SR.Format(SR.UTInvalidWildcardInVariableOrLiteral, _owner._originalTemplate, WildcardPath))); |
| | | 1256 | | } |
| | | 1257 | | |
| | 124 | 1258 | | string uppercaseVarName = varName.ToUpperInvariant(); |
| | 124 | 1259 | | if (_pathSegmentVariableNames.Contains(uppercaseVarName) || |
| | 124 | 1260 | | _queryValueVariableNames.Contains(uppercaseVarName)) |
| | | 1261 | | { |
| | 0 | 1262 | | throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(new InvalidOperationException( |
| | 0 | 1263 | | SR.Format(SR.UTVarNamesMustBeUnique, _owner._originalTemplate, varName))); |
| | | 1264 | | } |
| | | 1265 | | |
| | 124 | 1266 | | _pathSegmentVariableNames.Add(uppercaseVarName); |
| | 124 | 1267 | | _pathSegmentVariableNature.Add(sourceNature); |
| | 124 | 1268 | | if (hasDefaultValue) |
| | | 1269 | | { |
| | 24 | 1270 | | if (defaultValue == string.Empty) |
| | | 1271 | | { |
| | 0 | 1272 | | throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(new InvalidOperationException( |
| | 0 | 1273 | | SR.Format(SR.UTInvalidDefaultPathValue, _owner._originalTemplate, |
| | 0 | 1274 | | varDeclaration, varName))); |
| | | 1275 | | } |
| | | 1276 | | |
| | 24 | 1277 | | if (string.Compare(defaultValue, NullableDefault, StringComparison.OrdinalIgnoreCase) == 0) |
| | | 1278 | | { |
| | 0 | 1279 | | defaultValue = null; |
| | | 1280 | | } |
| | | 1281 | | |
| | 24 | 1282 | | if (DefaultValues == null) |
| | | 1283 | | { |
| | 24 | 1284 | | DefaultValues = new Dictionary<string, string>(); |
| | | 1285 | | } |
| | | 1286 | | |
| | 24 | 1287 | | DefaultValues.Add(uppercaseVarName, defaultValue); |
| | | 1288 | | } |
| | | 1289 | | |
| | 124 | 1290 | | return uppercaseVarName; |
| | | 1291 | | } |
| | | 1292 | | |
| | | 1293 | | public string AddQueryVariable(string varDeclaration) |
| | | 1294 | | { |
| | 29 | 1295 | | ParseVariableDeclaration(varDeclaration, out string varName, out string defaultValue); |
| | 29 | 1296 | | if (varName.IndexOf(WildcardPath, StringComparison.Ordinal) != -1) |
| | | 1297 | | { |
| | 0 | 1298 | | throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(new FormatException( |
| | 0 | 1299 | | SR.Format(SR.UTInvalidWildcardInVariableOrLiteral, _owner._originalTemplate, WildcardPath))); |
| | | 1300 | | } |
| | | 1301 | | |
| | 29 | 1302 | | if (defaultValue != null) |
| | | 1303 | | { |
| | 0 | 1304 | | throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(new InvalidOperationException( |
| | 0 | 1305 | | SR.Format(SR.UTDefaultValueToQueryVar, _owner._originalTemplate, |
| | 0 | 1306 | | varDeclaration, varName))); |
| | | 1307 | | } |
| | | 1308 | | |
| | 29 | 1309 | | string uppercaseVarName = varName.ToUpperInvariant(); |
| | 29 | 1310 | | if (_pathSegmentVariableNames.Contains(uppercaseVarName) || |
| | 29 | 1311 | | _queryValueVariableNames.Contains(uppercaseVarName)) |
| | | 1312 | | { |
| | 0 | 1313 | | throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(new InvalidOperationException( |
| | 0 | 1314 | | SR.Format(SR.UTVarNamesMustBeUnique, _owner._originalTemplate, varName))); |
| | | 1315 | | } |
| | 29 | 1316 | | _queryValueVariableNames.Add(uppercaseVarName); |
| | | 1317 | | |
| | 29 | 1318 | | return uppercaseVarName; |
| | | 1319 | | } |
| | | 1320 | | |
| | | 1321 | | public void LookupDefault(string varName, NameValueCollection boundParameters) |
| | | 1322 | | { |
| | | 1323 | | Fx.Assert(DefaultValues.ContainsKey(varName), "Otherwise, we don't have a value to bind"); |
| | | 1324 | | |
| | 1 | 1325 | | boundParameters.Add(varName, _owner.UnescapeDefaultValue(DefaultValues[varName])); |
| | 1 | 1326 | | } |
| | | 1327 | | |
| | | 1328 | | public BindInformation PrepareBindInformation(IDictionary<string, string> parameters, bool omitDefaults) |
| | | 1329 | | { |
| | 0 | 1330 | | if (parameters == null) |
| | | 1331 | | { |
| | 0 | 1332 | | throw DiagnosticUtility.ExceptionUtility.ThrowHelperArgumentNull(nameof(parameters)); |
| | | 1333 | | } |
| | | 1334 | | |
| | 0 | 1335 | | string[] normalizedParameters = PrepareNormalizedParameters(); |
| | 0 | 1336 | | IDictionary<string, string> extraParameters = null; |
| | 0 | 1337 | | foreach (string key in parameters.Keys) |
| | | 1338 | | { |
| | 0 | 1339 | | ProcessBindParameter(key, parameters[key], normalizedParameters, ref extraParameters); |
| | | 1340 | | } |
| | | 1341 | | |
| | 0 | 1342 | | ProcessDefaultsAndCreateBindInfo(omitDefaults, normalizedParameters, extraParameters, out BindInformatio |
| | | 1343 | | |
| | 0 | 1344 | | return bindInfo; |
| | | 1345 | | } |
| | | 1346 | | |
| | | 1347 | | public BindInformation PrepareBindInformation(NameValueCollection parameters, bool omitDefaults) |
| | | 1348 | | { |
| | 0 | 1349 | | if (parameters == null) |
| | | 1350 | | { |
| | 0 | 1351 | | throw DiagnosticUtility.ExceptionUtility.ThrowHelperArgumentNull(nameof(parameters)); |
| | | 1352 | | } |
| | | 1353 | | |
| | 0 | 1354 | | string[] normalizedParameters = PrepareNormalizedParameters(); |
| | 0 | 1355 | | IDictionary<string, string> extraParameters = null; |
| | 0 | 1356 | | foreach (string key in parameters.AllKeys) |
| | | 1357 | | { |
| | 0 | 1358 | | ProcessBindParameter(key, parameters[key], normalizedParameters, ref extraParameters); |
| | | 1359 | | } |
| | | 1360 | | |
| | 0 | 1361 | | ProcessDefaultsAndCreateBindInfo(omitDefaults, normalizedParameters, extraParameters, out BindInformatio |
| | | 1362 | | |
| | 0 | 1363 | | return bindInfo; |
| | | 1364 | | } |
| | | 1365 | | |
| | | 1366 | | public BindInformation PrepareBindInformation(params string[] parameters) |
| | | 1367 | | { |
| | 0 | 1368 | | if (parameters == null) |
| | | 1369 | | { |
| | 0 | 1370 | | throw DiagnosticUtility.ExceptionUtility.ThrowHelperArgumentNull(nameof(parameters)); |
| | | 1371 | | } |
| | | 1372 | | |
| | 0 | 1373 | | if ((parameters.Length < _pathSegmentVariableNames.Count) || |
| | 0 | 1374 | | (parameters.Length > _pathSegmentVariableNames.Count + _queryValueVariableNames.Count)) |
| | | 1375 | | { |
| | 0 | 1376 | | throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(new FormatException( |
| | 0 | 1377 | | SR.Format(SR.UTBindByPositionWrongCount, _owner._originalTemplate, |
| | 0 | 1378 | | _pathSegmentVariableNames.Count, _queryValueVariableNames.Count, |
| | 0 | 1379 | | parameters.Length))); |
| | | 1380 | | } |
| | | 1381 | | |
| | | 1382 | | string[] normalizedParameters; |
| | 0 | 1383 | | if (parameters.Length == _pathSegmentVariableNames.Count + _queryValueVariableNames.Count) |
| | | 1384 | | { |
| | 0 | 1385 | | normalizedParameters = parameters; |
| | | 1386 | | } |
| | | 1387 | | else |
| | | 1388 | | { |
| | 0 | 1389 | | normalizedParameters = new string[_pathSegmentVariableNames.Count + _queryValueVariableNames.Count]; |
| | 0 | 1390 | | parameters.CopyTo(normalizedParameters, 0); |
| | 0 | 1391 | | for (int i = parameters.Length; i < normalizedParameters.Length; i++) |
| | | 1392 | | { |
| | 0 | 1393 | | normalizedParameters[i] = null; |
| | | 1394 | | } |
| | | 1395 | | } |
| | | 1396 | | |
| | 0 | 1397 | | LoadDefaultsAndValidate(normalizedParameters, out int lastNonDefaultPathParameter, |
| | 0 | 1398 | | out int lastNonNullablePathParameter); |
| | | 1399 | | |
| | 0 | 1400 | | return new BindInformation(normalizedParameters, lastNonDefaultPathParameter, |
| | 0 | 1401 | | lastNonNullablePathParameter, _owner._additionalDefaults); |
| | | 1402 | | } |
| | | 1403 | | |
| | | 1404 | | public void ValidateDefaults(out int firstOptionalSegment) |
| | | 1405 | | { |
| | | 1406 | | Fx.Assert(DefaultValues != null, "We are checking this condition from the c'tor"); |
| | | 1407 | | Fx.Assert(_pathSegmentVariableNames.Count > 0, "Otherwise, how can we have default values"); |
| | | 1408 | | |
| | | 1409 | | // Finding the first valid nullable defaults |
| | 96 | 1410 | | for (int i = _pathSegmentVariableNames.Count - 1; (i >= 0) && (_firstNullablePathVariable == -1); i--) |
| | | 1411 | | { |
| | 24 | 1412 | | string varName = _pathSegmentVariableNames[i]; |
| | 24 | 1413 | | if (!DefaultValues.TryGetValue(varName, out string defaultValue)) |
| | | 1414 | | { |
| | 0 | 1415 | | _firstNullablePathVariable = i + 1; |
| | | 1416 | | } |
| | 24 | 1417 | | else if (defaultValue != null) |
| | | 1418 | | { |
| | 24 | 1419 | | _firstNullablePathVariable = i + 1; |
| | | 1420 | | } |
| | | 1421 | | } |
| | | 1422 | | |
| | 24 | 1423 | | if (_firstNullablePathVariable == -1) |
| | | 1424 | | { |
| | 0 | 1425 | | _firstNullablePathVariable = 0; |
| | | 1426 | | } |
| | | 1427 | | |
| | | 1428 | | // Making sure that there are no nullables to the left of the first valid nullable |
| | 24 | 1429 | | if (_firstNullablePathVariable > 1) |
| | | 1430 | | { |
| | 0 | 1431 | | for (int i = _firstNullablePathVariable - 2; i >= 0; i--) |
| | | 1432 | | { |
| | 0 | 1433 | | string varName = _pathSegmentVariableNames[i]; |
| | 0 | 1434 | | if (DefaultValues.TryGetValue(varName, out string defaultValue)) |
| | | 1435 | | { |
| | 0 | 1436 | | if (defaultValue == null) |
| | | 1437 | | { |
| | 0 | 1438 | | throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(new InvalidOperationException( |
| | 0 | 1439 | | SR.Format(SR.UTNullableDefaultMustBeFollowedWithNullables, _owner._originalTemplate, |
| | 0 | 1440 | | varName, _pathSegmentVariableNames[i + 1]))); |
| | | 1441 | | } |
| | | 1442 | | } |
| | | 1443 | | } |
| | | 1444 | | } |
| | | 1445 | | |
| | | 1446 | | // Making sure that there are no Literals\WildCards to the right |
| | | 1447 | | // Based on the fact that only Variable Path Segments support default values, |
| | | 1448 | | // if firstNullablePathVariable=N and pathSegmentVariableNames.Count=M then |
| | | 1449 | | // the nature of the last M-N path segments should be StringNature.Variable; otherwise, |
| | | 1450 | | // there was a literal segment in between. Also, there shouldn't be a wildcard. |
| | 24 | 1451 | | if (_firstNullablePathVariable < _pathSegmentVariableNames.Count) |
| | | 1452 | | { |
| | 0 | 1453 | | if (_owner.HasWildcard) |
| | | 1454 | | { |
| | 0 | 1455 | | throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(new InvalidOperationException( |
| | 0 | 1456 | | SR.Format(SR.UTNullableDefaultMustNotBeFollowedWithWildcard, |
| | 0 | 1457 | | _owner._originalTemplate, _pathSegmentVariableNames[_firstNullablePathVariable]))); |
| | | 1458 | | } |
| | | 1459 | | |
| | 0 | 1460 | | for (int i = _pathSegmentVariableNames.Count - 1; i >= _firstNullablePathVariable; i--) |
| | | 1461 | | { |
| | 0 | 1462 | | int segmentIndex = _owner._segments.Count - (_pathSegmentVariableNames.Count - i); |
| | 0 | 1463 | | if (_owner._segments[segmentIndex].Nature != UriTemplatePartType.Variable) |
| | | 1464 | | { |
| | 0 | 1465 | | throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(new InvalidOperationException( |
| | 0 | 1466 | | SR.Format(SR.UTNullableDefaultMustNotBeFollowedWithLiteral, |
| | 0 | 1467 | | _owner._originalTemplate, _pathSegmentVariableNames[_firstNullablePathVariable], |
| | 0 | 1468 | | _owner._segments[segmentIndex].OriginalSegment))); |
| | | 1469 | | } |
| | | 1470 | | } |
| | | 1471 | | } |
| | | 1472 | | |
| | | 1473 | | // Now that we have the firstNullablePathVariable set, lets calculate the firstOptionalSegment. |
| | | 1474 | | // We already knows that the last M-N path segments (when M=pathSegmentVariableNames.Count and |
| | | 1475 | | // N=firstNullablePathVariable) are optional (see the previos comment). We will start there and |
| | | 1476 | | // move to the left, stopping at the first segment, which is not a variable or is a variable |
| | | 1477 | | // and doesn't have a default value. |
| | 24 | 1478 | | int numNullablePathVariables = (_pathSegmentVariableNames.Count - _firstNullablePathVariable); |
| | 24 | 1479 | | firstOptionalSegment = _owner._segments.Count - numNullablePathVariables; |
| | 24 | 1480 | | if (!_owner.HasWildcard) |
| | | 1481 | | { |
| | 48 | 1482 | | while (firstOptionalSegment > 0) |
| | | 1483 | | { |
| | 48 | 1484 | | UriTemplatePathSegment ps = _owner._segments[firstOptionalSegment - 1]; |
| | 48 | 1485 | | if (ps.Nature != UriTemplatePartType.Variable) |
| | | 1486 | | { |
| | | 1487 | | break; |
| | | 1488 | | } |
| | | 1489 | | |
| | 24 | 1490 | | UriTemplateVariablePathSegment vps = (ps as UriTemplateVariablePathSegment); |
| | | 1491 | | Fx.Assert(vps != null, "Should be; that's his nature"); |
| | 24 | 1492 | | if (!DefaultValues.ContainsKey(vps.VarName)) |
| | | 1493 | | { |
| | | 1494 | | break; |
| | | 1495 | | } |
| | | 1496 | | |
| | 24 | 1497 | | firstOptionalSegment--; |
| | | 1498 | | } |
| | | 1499 | | } |
| | 24 | 1500 | | } |
| | | 1501 | | |
| | | 1502 | | private void AddAdditionalDefaults(ref IDictionary<string, string> extraParameters) |
| | | 1503 | | { |
| | 0 | 1504 | | if (extraParameters == null) |
| | | 1505 | | { |
| | 0 | 1506 | | extraParameters = _owner._additionalDefaults; |
| | | 1507 | | } |
| | | 1508 | | else |
| | | 1509 | | { |
| | 0 | 1510 | | foreach (KeyValuePair<string, string> kvp in _owner._additionalDefaults) |
| | | 1511 | | { |
| | 0 | 1512 | | if (!extraParameters.ContainsKey(kvp.Key)) |
| | | 1513 | | { |
| | 0 | 1514 | | extraParameters.Add(kvp.Key, kvp.Value); |
| | | 1515 | | } |
| | | 1516 | | } |
| | | 1517 | | } |
| | 0 | 1518 | | } |
| | | 1519 | | |
| | | 1520 | | private void LoadDefaultsAndValidate(string[] normalizedParameters, out int lastNonDefaultPathParameter, |
| | | 1521 | | out int lastNonNullablePathParameter) |
| | | 1522 | | { |
| | | 1523 | | // First step - loading defaults |
| | 0 | 1524 | | for (int i = 0; i < _pathSegmentVariableNames.Count; i++) |
| | | 1525 | | { |
| | 0 | 1526 | | if (string.IsNullOrEmpty(normalizedParameters[i]) && (DefaultValues != null)) |
| | | 1527 | | { |
| | 0 | 1528 | | DefaultValues.TryGetValue(_pathSegmentVariableNames[i], out normalizedParameters[i]); |
| | | 1529 | | } |
| | | 1530 | | } |
| | | 1531 | | |
| | | 1532 | | // Second step - calculating bind constrains |
| | 0 | 1533 | | lastNonDefaultPathParameter = _pathSegmentVariableNames.Count - 1; |
| | 0 | 1534 | | if ((DefaultValues != null) && |
| | 0 | 1535 | | (_owner._segments[_owner._segments.Count - 1].Nature != UriTemplatePartType.Literal)) |
| | | 1536 | | { |
| | 0 | 1537 | | bool foundNonDefaultPathParameter = false; |
| | 0 | 1538 | | while (!foundNonDefaultPathParameter && (lastNonDefaultPathParameter >= 0)) |
| | | 1539 | | { |
| | 0 | 1540 | | if (DefaultValues.TryGetValue(_pathSegmentVariableNames[lastNonDefaultPathParameter], |
| | 0 | 1541 | | out string defaultValue)) |
| | | 1542 | | { |
| | 0 | 1543 | | if (string.Compare(normalizedParameters[lastNonDefaultPathParameter], |
| | 0 | 1544 | | defaultValue, StringComparison.Ordinal) != 0) |
| | | 1545 | | { |
| | 0 | 1546 | | foundNonDefaultPathParameter = true; |
| | | 1547 | | } |
| | | 1548 | | else |
| | | 1549 | | { |
| | 0 | 1550 | | lastNonDefaultPathParameter--; |
| | | 1551 | | } |
| | | 1552 | | } |
| | | 1553 | | else |
| | | 1554 | | { |
| | 0 | 1555 | | foundNonDefaultPathParameter = true; |
| | | 1556 | | } |
| | | 1557 | | } |
| | | 1558 | | } |
| | | 1559 | | |
| | 0 | 1560 | | if (_firstNullablePathVariable > lastNonDefaultPathParameter) |
| | | 1561 | | { |
| | 0 | 1562 | | lastNonNullablePathParameter = _firstNullablePathVariable - 1; |
| | | 1563 | | } |
| | | 1564 | | else |
| | | 1565 | | { |
| | 0 | 1566 | | lastNonNullablePathParameter = lastNonDefaultPathParameter; |
| | | 1567 | | } |
| | | 1568 | | |
| | | 1569 | | // Third step - validate |
| | 0 | 1570 | | for (int i = 0; i <= lastNonNullablePathParameter; i++) |
| | | 1571 | | { |
| | | 1572 | | // Skip validation for terminating star variable segment : |
| | 0 | 1573 | | if (_owner.HasWildcard && _owner._wildcard.HasVariable && |
| | 0 | 1574 | | (i == _pathSegmentVariableNames.Count - 1)) |
| | | 1575 | | { |
| | | 1576 | | continue; |
| | | 1577 | | } |
| | | 1578 | | |
| | | 1579 | | // Validate |
| | 0 | 1580 | | if (string.IsNullOrEmpty(normalizedParameters[i])) |
| | | 1581 | | { |
| | 0 | 1582 | | throw DiagnosticUtility.ExceptionUtility.ThrowHelperArgument(nameof(normalizedParameters), |
| | 0 | 1583 | | SR.Format(SR.BindUriTemplateToNullOrEmptyPathParam, _pathSegmentVariableNames[i])); |
| | | 1584 | | } |
| | | 1585 | | } |
| | 0 | 1586 | | } |
| | | 1587 | | |
| | | 1588 | | private void ParseVariableDeclaration(string varDeclaration, out string varName, out string defaultValue) |
| | | 1589 | | { |
| | 153 | 1590 | | if ((varDeclaration.IndexOf('{') != -1) || (varDeclaration.IndexOf('}') != -1)) |
| | | 1591 | | { |
| | 0 | 1592 | | throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(new FormatException( |
| | 0 | 1593 | | SR.Format(SR.UTInvalidVarDeclaration, _owner._originalTemplate, varDeclaration))); |
| | | 1594 | | } |
| | | 1595 | | |
| | 153 | 1596 | | int equalSignIndex = varDeclaration.IndexOf('='); |
| | | 1597 | | switch (equalSignIndex) |
| | | 1598 | | { |
| | | 1599 | | case -1: |
| | 129 | 1600 | | varName = varDeclaration; |
| | 129 | 1601 | | defaultValue = null; |
| | 129 | 1602 | | break; |
| | | 1603 | | |
| | | 1604 | | case 0: |
| | 0 | 1605 | | throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(new FormatException( |
| | 0 | 1606 | | SR.Format(SR.UTInvalidVarDeclaration, _owner._originalTemplate, varDeclaration))); |
| | | 1607 | | |
| | | 1608 | | default: |
| | 24 | 1609 | | varName = varDeclaration.Substring(0, equalSignIndex); |
| | 24 | 1610 | | defaultValue = varDeclaration.Substring(equalSignIndex + 1); |
| | 24 | 1611 | | if (defaultValue.IndexOf('=') != -1) |
| | | 1612 | | { |
| | 0 | 1613 | | throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(new FormatException( |
| | 0 | 1614 | | SR.Format(SR.UTInvalidVarDeclaration, _owner._originalTemplate, varDeclaration))); |
| | | 1615 | | } |
| | | 1616 | | break; |
| | | 1617 | | } |
| | 24 | 1618 | | } |
| | | 1619 | | |
| | | 1620 | | private string[] PrepareNormalizedParameters() |
| | | 1621 | | { |
| | 0 | 1622 | | string[] normalizedParameters = new string[_pathSegmentVariableNames.Count + _queryValueVariableNames.Co |
| | 0 | 1623 | | for (int i = 0; i < normalizedParameters.Length; i++) |
| | | 1624 | | { |
| | 0 | 1625 | | normalizedParameters[i] = null; |
| | | 1626 | | } |
| | | 1627 | | |
| | 0 | 1628 | | return normalizedParameters; |
| | | 1629 | | } |
| | | 1630 | | |
| | | 1631 | | private void ProcessBindParameter(string name, string value, string[] normalizedParameters, |
| | | 1632 | | ref IDictionary<string, string> extraParameters) |
| | | 1633 | | { |
| | 0 | 1634 | | if (string.IsNullOrEmpty(name)) |
| | | 1635 | | { |
| | 0 | 1636 | | throw DiagnosticUtility.ExceptionUtility.ThrowHelperArgument(nameof(name), |
| | 0 | 1637 | | SR.Format(SR.UTBindByNameCalledWithEmptyKey)); |
| | | 1638 | | } |
| | | 1639 | | |
| | 0 | 1640 | | string uppercaseVarName = name.ToUpperInvariant(); |
| | 0 | 1641 | | int pathVarIndex = _pathSegmentVariableNames.IndexOf(uppercaseVarName); |
| | 0 | 1642 | | if (pathVarIndex != -1) |
| | | 1643 | | { |
| | 0 | 1644 | | normalizedParameters[pathVarIndex] = (string.IsNullOrEmpty(value) ? string.Empty : value); |
| | 0 | 1645 | | return; |
| | | 1646 | | } |
| | | 1647 | | |
| | 0 | 1648 | | int queryVarIndex = _queryValueVariableNames.IndexOf(uppercaseVarName); |
| | 0 | 1649 | | if (queryVarIndex != -1) |
| | | 1650 | | { |
| | 0 | 1651 | | normalizedParameters[_pathSegmentVariableNames.Count + queryVarIndex] = (string.IsNullOrEmpty(value) |
| | 0 | 1652 | | return; |
| | | 1653 | | } |
| | | 1654 | | |
| | 0 | 1655 | | if (extraParameters == null) |
| | | 1656 | | { |
| | 0 | 1657 | | extraParameters = new Dictionary<string, string>(UriTemplateHelpers.GetQueryKeyComparer()); |
| | | 1658 | | } |
| | | 1659 | | |
| | 0 | 1660 | | extraParameters.Add(name, value); |
| | 0 | 1661 | | } |
| | | 1662 | | |
| | | 1663 | | private void ProcessDefaultsAndCreateBindInfo(bool omitDefaults, string[] normalizedParameters, |
| | | 1664 | | IDictionary<string, string> extraParameters, out BindInformation bindInfo) |
| | | 1665 | | { |
| | 0 | 1666 | | LoadDefaultsAndValidate(normalizedParameters, out int lastNonDefaultPathParameter, |
| | 0 | 1667 | | out int lastNonNullablePathParameter); |
| | 0 | 1668 | | if (_owner._additionalDefaults != null) |
| | | 1669 | | { |
| | 0 | 1670 | | if (omitDefaults) |
| | | 1671 | | { |
| | 0 | 1672 | | RemoveAdditionalDefaults(ref extraParameters); |
| | | 1673 | | } |
| | | 1674 | | else |
| | | 1675 | | { |
| | 0 | 1676 | | AddAdditionalDefaults(ref extraParameters); |
| | | 1677 | | } |
| | | 1678 | | } |
| | | 1679 | | |
| | 0 | 1680 | | bindInfo = new BindInformation(normalizedParameters, lastNonDefaultPathParameter, |
| | 0 | 1681 | | lastNonNullablePathParameter, extraParameters); |
| | 0 | 1682 | | } |
| | | 1683 | | |
| | | 1684 | | private void RemoveAdditionalDefaults(ref IDictionary<string, string> extraParameters) |
| | | 1685 | | { |
| | 0 | 1686 | | if (extraParameters == null) |
| | | 1687 | | { |
| | 0 | 1688 | | return; |
| | | 1689 | | } |
| | | 1690 | | |
| | 0 | 1691 | | foreach (KeyValuePair<string, string> kvp in _owner._additionalDefaults) |
| | | 1692 | | { |
| | 0 | 1693 | | if (extraParameters.TryGetValue(kvp.Key, out string extraParameter)) |
| | | 1694 | | { |
| | 0 | 1695 | | if (string.Compare(extraParameter, kvp.Value, StringComparison.Ordinal) == 0) |
| | | 1696 | | { |
| | 0 | 1697 | | extraParameters.Remove(kvp.Key); |
| | | 1698 | | } |
| | | 1699 | | } |
| | | 1700 | | } |
| | | 1701 | | |
| | 0 | 1702 | | if (extraParameters.Count == 0) |
| | | 1703 | | { |
| | 0 | 1704 | | extraParameters = null; |
| | | 1705 | | } |
| | 0 | 1706 | | } |
| | | 1707 | | } |
| | | 1708 | | |
| | | 1709 | | internal class WildcardInfo |
| | | 1710 | | { |
| | | 1711 | | private readonly UriTemplate _owner; |
| | | 1712 | | private readonly string _varName; |
| | | 1713 | | |
| | 24 | 1714 | | public WildcardInfo(UriTemplate owner) |
| | | 1715 | | { |
| | 24 | 1716 | | _varName = null; |
| | 24 | 1717 | | _owner = owner; |
| | 24 | 1718 | | } |
| | | 1719 | | |
| | 24 | 1720 | | public WildcardInfo(UriTemplate owner, string segment) |
| | | 1721 | | { |
| | | 1722 | | Fx.Assert(!segment.EndsWith("/", StringComparison.Ordinal), "We are expecting to check this earlier"); |
| | | 1723 | | |
| | 24 | 1724 | | _varName = owner.AddPathVariable(UriTemplatePartType.Variable, |
| | 24 | 1725 | | segment.Substring(1 + WildcardPath.Length, segment.Length - 2 - WildcardPath.Length), |
| | 24 | 1726 | | out bool hasDefault); |
| | | 1727 | | |
| | | 1728 | | // Since this is a terminating star segment there shouldn't be a default |
| | 24 | 1729 | | if (hasDefault) |
| | | 1730 | | { |
| | 0 | 1731 | | throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(new InvalidOperationException( |
| | 0 | 1732 | | SR. Format(SR.UTStarVariableWithDefaults, owner._originalTemplate, |
| | 0 | 1733 | | segment, _varName))); |
| | | 1734 | | } |
| | | 1735 | | |
| | 24 | 1736 | | _owner = owner; |
| | 24 | 1737 | | } |
| | | 1738 | | |
| | | 1739 | | internal bool HasVariable |
| | | 1740 | | { |
| | | 1741 | | get |
| | | 1742 | | { |
| | 2 | 1743 | | return (!string.IsNullOrEmpty(_varName)); |
| | | 1744 | | } |
| | | 1745 | | } |
| | | 1746 | | |
| | | 1747 | | public void Bind(string[] values, ref int valueIndex, StringBuilder path) |
| | | 1748 | | { |
| | 0 | 1749 | | if (HasVariable) |
| | | 1750 | | { |
| | | 1751 | | Fx.Assert(valueIndex < values.Length, "Not enough values to bind"); |
| | 0 | 1752 | | if (string.IsNullOrEmpty(values[valueIndex])) |
| | | 1753 | | { |
| | 0 | 1754 | | valueIndex++; |
| | | 1755 | | } |
| | | 1756 | | else |
| | | 1757 | | { |
| | 0 | 1758 | | path.Append(values[valueIndex++]); |
| | | 1759 | | } |
| | | 1760 | | } |
| | 0 | 1761 | | } |
| | | 1762 | | |
| | | 1763 | | public void Lookup(int numMatchedSegments, Collection<string> relativePathSegments, |
| | | 1764 | | NameValueCollection boundParameters) |
| | | 1765 | | { |
| | | 1766 | | Fx.Assert(numMatchedSegments == _owner._segments.Count, "We should have matched the other segments"); |
| | | 1767 | | |
| | 2 | 1768 | | if (HasVariable) |
| | | 1769 | | { |
| | 1 | 1770 | | StringBuilder remainingPath = new StringBuilder(); |
| | 6 | 1771 | | for (int i = numMatchedSegments; i < relativePathSegments.Count; i++) |
| | | 1772 | | { |
| | 2 | 1773 | | if (i < relativePathSegments.Count - 1) |
| | | 1774 | | { |
| | 1 | 1775 | | remainingPath.AppendFormat("{0}/", relativePathSegments[i]); |
| | | 1776 | | } |
| | | 1777 | | else |
| | | 1778 | | { |
| | 1 | 1779 | | remainingPath.Append(relativePathSegments[i]); |
| | | 1780 | | } |
| | | 1781 | | } |
| | | 1782 | | |
| | 1 | 1783 | | boundParameters.Add(_varName, remainingPath.ToString()); |
| | | 1784 | | } |
| | 2 | 1785 | | } |
| | | 1786 | | } |
| | | 1787 | | } |
| | | 1788 | | } |