public RestPath(Type requestType, RestServiceAttribute attr) { this.RequestType = requestType; this.restPath = attr.Path; this.DefaultContentType = attr.DefaultContentType; this.allowsAllVerbs = attr.Verbs == null || attr.Verbs == WildCard; if (!this.allowsAllVerbs) { this.allowedVerbs = attr.Verbs.ToUpper(); } var componentsList = new List <string>(); //We only split on '.' if the restPath has them. Allows for /{action}.{type} var hasSeparators = new List <bool>(); foreach (var component in this.restPath.Split(PathSeperatorChar)) { if (string.IsNullOrEmpty(component)) { continue; } if (component.Contains(VariablePrefix) && component.Contains(ComponentSeperator)) { hasSeparators.Add(true); componentsList.AddRange(component.Split(ComponentSeperator)); } else { hasSeparators.Add(false); componentsList.Add(component); } } var components = componentsList.ToArray(); this.TotalComponentsCount = components.Length; this.literalsToMatch = new string[this.TotalComponentsCount]; this.variablesNames = new string[this.TotalComponentsCount]; this.componentsWithSeparators = hasSeparators.ToArray(); this.PathComponentsCount = this.componentsWithSeparators.Length; string firstLiteralMatch = null; var lastVariableMatchPos = -1; var sbHashKey = new StringBuilder(); for (var i = 0; i < components.Length; i++) { var component = components[i]; if (component.StartsWith(VariablePrefix)) { this.variablesNames[i] = component.Substring(1, component.Length - 2); lastVariableMatchPos = i; } else { this.literalsToMatch[i] = component.ToLower(); sbHashKey.Append(i + PathSeperatorChar.ToString() + this.literalsToMatch); if (firstLiteralMatch == null) { firstLiteralMatch = this.literalsToMatch[i]; } } } if (lastVariableMatchPos != -1) { var lastVariableMatch = this.variablesNames[lastVariableMatchPos]; this.isWildCardPath = lastVariableMatch[lastVariableMatch.Length - 1] == WildCardChar; if (this.isWildCardPath) { this.variablesNames[lastVariableMatchPos] = lastVariableMatch.Substring(0, lastVariableMatch.Length - 1); } } this.FirstMatchHashKey = !this.isWildCardPath ? this.PathComponentsCount + PathSeperator + firstLiteralMatch : WildCardChar + PathSeperator + firstLiteralMatch; this.IsValid = sbHashKey.Length > 0; this.UniqueMatchHashKey = sbHashKey.ToString(); this.typeDeserializer = new StringMapTypeDeserializer(this.RequestType); RegisterCaseInsenstivePropertyNameMappings(); }
public RestPath(Type requestType, RestServiceAttribute attr) { this.RequestType = requestType; this.restPath = attr.Path; this.DefaultContentType = attr.DefaultContentType; this.allowsAllVerbs = attr.Verbs == null || attr.Verbs == WildCard; if (!this.allowsAllVerbs) { this.allowedVerbs = attr.Verbs.ToUpper(); } var componentsList = new List<string>(); //We only split on '.' if the restPath has them. Allows for /{action}.{type} var hasSeparators = new List<bool>(); foreach (var component in this.restPath.Split(PathSeperatorChar)) { if (string.IsNullOrEmpty(component)) continue; if (component.Contains(VariablePrefix) && component.Contains(ComponentSeperator)) { hasSeparators.Add(true); componentsList.AddRange(component.Split(ComponentSeperator)); } else { hasSeparators.Add(false); componentsList.Add(component); } } var components = componentsList.ToArray(); this.TotalComponentsCount = components.Length; this.literalsToMatch = new string[this.TotalComponentsCount]; this.variablesNames = new string[this.TotalComponentsCount]; this.componentsWithSeparators = hasSeparators.ToArray(); this.PathComponentsCount = this.componentsWithSeparators.Length; string firstLiteralMatch = null; var lastVariableMatchPos = -1; var sbHashKey = new StringBuilder(); for (var i = 0; i < components.Length; i++) { var component = components[i]; if (component.StartsWith(VariablePrefix)) { this.variablesNames[i] = component.Substring(1, component.Length - 2); lastVariableMatchPos = i; } else { this.literalsToMatch[i] = component.ToLower(); sbHashKey.Append(i + PathSeperatorChar.ToString() + this.literalsToMatch); if (firstLiteralMatch == null) { firstLiteralMatch = this.literalsToMatch[i]; } } } if (lastVariableMatchPos != -1) { var lastVariableMatch = this.variablesNames[lastVariableMatchPos]; this.isWildCardPath = lastVariableMatch[lastVariableMatch.Length - 1] == WildCardChar; if (this.isWildCardPath) { this.variablesNames[lastVariableMatchPos] = lastVariableMatch.Substring(0, lastVariableMatch.Length - 1); } } this.FirstMatchHashKey = !this.isWildCardPath ? this.PathComponentsCount + PathSeperator + firstLiteralMatch : WildCardChar + PathSeperator + firstLiteralMatch; this.IsValid = sbHashKey.Length > 0; this.UniqueMatchHashKey = sbHashKey.ToString(); this.typeDeserializer = new StringMapTypeDeserializer(this.RequestType); RegisterCaseInsenstivePropertyNameMappings(); }