2020-05-29 09:28:19 +00:00
|
|
|
#pragma warning disable CS1591
|
|
|
|
|
2016-11-11 19:55:12 +00:00
|
|
|
using System;
|
|
|
|
using System.Collections.Generic;
|
2019-10-25 10:47:20 +00:00
|
|
|
using System.Globalization;
|
2017-02-13 02:06:54 +00:00
|
|
|
using System.IO;
|
2016-11-11 19:55:12 +00:00
|
|
|
using System.Linq;
|
2017-02-13 02:06:54 +00:00
|
|
|
using System.Reflection;
|
2016-11-11 19:55:12 +00:00
|
|
|
using System.Text;
|
2019-10-15 15:49:49 +00:00
|
|
|
using System.Text.Json.Serialization;
|
2016-11-11 19:55:12 +00:00
|
|
|
|
2017-02-13 20:54:28 +00:00
|
|
|
namespace Emby.Server.Implementations.Services
|
2016-11-11 19:55:12 +00:00
|
|
|
{
|
|
|
|
public class RestPath
|
|
|
|
{
|
|
|
|
private const string WildCard = "*";
|
|
|
|
private const char WildCardChar = '*';
|
|
|
|
private const string PathSeperator = "/";
|
|
|
|
private const char PathSeperatorChar = '/';
|
|
|
|
private const char ComponentSeperator = '.';
|
|
|
|
private const string VariablePrefix = "{";
|
|
|
|
|
2019-02-20 15:49:03 +00:00
|
|
|
private readonly bool[] componentsWithSeparators;
|
2016-11-11 19:55:12 +00:00
|
|
|
|
|
|
|
private readonly string restPath;
|
|
|
|
public bool IsWildCardPath { get; private set; }
|
|
|
|
|
|
|
|
private readonly string[] literalsToMatch;
|
|
|
|
|
|
|
|
private readonly string[] variablesNames;
|
|
|
|
|
|
|
|
private readonly bool[] isWildcard;
|
|
|
|
private readonly int wildcardCount = 0;
|
|
|
|
|
2019-10-15 15:49:49 +00:00
|
|
|
internal static string[] IgnoreAttributesNamed = new[]
|
|
|
|
{
|
|
|
|
nameof(JsonIgnoreAttribute)
|
|
|
|
};
|
|
|
|
|
|
|
|
private static Type _excludeType = typeof(Stream);
|
|
|
|
|
2016-11-11 19:55:12 +00:00
|
|
|
public int VariableArgsCount { get; set; }
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
/// The number of segments separated by '/' determinable by path.Split('/').Length
|
|
|
|
/// e.g. /path/to/here.ext == 3
|
|
|
|
/// </summary>
|
|
|
|
public int PathComponentsCount { get; set; }
|
|
|
|
|
|
|
|
/// <summary>
|
2019-10-25 10:47:20 +00:00
|
|
|
/// Gets or sets the total number of segments after subparts have been exploded ('.')
|
|
|
|
/// e.g. /path/to/here.ext == 4.
|
2016-11-11 19:55:12 +00:00
|
|
|
/// </summary>
|
|
|
|
public int TotalComponentsCount { get; set; }
|
|
|
|
|
2017-08-31 03:49:38 +00:00
|
|
|
public string[] Verbs { get; private set; }
|
2016-11-11 19:55:12 +00:00
|
|
|
|
|
|
|
public Type RequestType { get; private set; }
|
|
|
|
|
2018-09-12 17:26:21 +00:00
|
|
|
public Type ServiceType { get; private set; }
|
|
|
|
|
2019-01-06 20:50:43 +00:00
|
|
|
public string Path => this.restPath;
|
2016-11-11 19:55:12 +00:00
|
|
|
|
|
|
|
public string Summary { get; private set; }
|
2020-06-15 21:43:52 +00:00
|
|
|
|
2017-09-11 19:25:13 +00:00
|
|
|
public string Description { get; private set; }
|
2020-06-15 21:43:52 +00:00
|
|
|
|
2017-09-11 19:25:13 +00:00
|
|
|
public bool IsHidden { get; private set; }
|
2016-11-11 19:55:12 +00:00
|
|
|
|
|
|
|
public static string[] GetPathPartsForMatching(string pathInfo)
|
|
|
|
{
|
2019-01-27 11:03:43 +00:00
|
|
|
return pathInfo.ToLowerInvariant().Split(new[] { PathSeperatorChar }, StringSplitOptions.RemoveEmptyEntries);
|
2016-11-11 19:55:12 +00:00
|
|
|
}
|
|
|
|
|
2017-02-12 22:53:14 +00:00
|
|
|
public static List<string> GetFirstMatchHashKeys(string[] pathPartsForMatching)
|
2016-11-11 19:55:12 +00:00
|
|
|
{
|
|
|
|
var hashPrefix = pathPartsForMatching.Length + PathSeperator;
|
|
|
|
return GetPotentialMatchesWithPrefix(hashPrefix, pathPartsForMatching);
|
|
|
|
}
|
|
|
|
|
2017-02-12 22:53:14 +00:00
|
|
|
public static List<string> GetFirstMatchWildCardHashKeys(string[] pathPartsForMatching)
|
2016-11-11 19:55:12 +00:00
|
|
|
{
|
2020-08-20 10:16:24 +00:00
|
|
|
const string HashPrefix = WildCard + PathSeperator;
|
|
|
|
return GetPotentialMatchesWithPrefix(HashPrefix, pathPartsForMatching);
|
2016-11-11 19:55:12 +00:00
|
|
|
}
|
|
|
|
|
2017-02-12 22:53:14 +00:00
|
|
|
private static List<string> GetPotentialMatchesWithPrefix(string hashPrefix, string[] pathPartsForMatching)
|
2016-11-11 19:55:12 +00:00
|
|
|
{
|
2017-02-12 22:53:14 +00:00
|
|
|
var list = new List<string>();
|
|
|
|
|
2016-11-11 19:55:12 +00:00
|
|
|
foreach (var part in pathPartsForMatching)
|
|
|
|
{
|
2017-02-12 22:53:14 +00:00
|
|
|
list.Add(hashPrefix + part);
|
|
|
|
|
2020-08-20 10:16:24 +00:00
|
|
|
if (part.IndexOf(ComponentSeperator, StringComparison.Ordinal) == -1)
|
2019-02-20 15:49:03 +00:00
|
|
|
{
|
|
|
|
continue;
|
|
|
|
}
|
2016-11-11 19:55:12 +00:00
|
|
|
|
2019-02-20 15:49:03 +00:00
|
|
|
var subParts = part.Split(ComponentSeperator);
|
2016-11-11 19:55:12 +00:00
|
|
|
foreach (var subPart in subParts)
|
|
|
|
{
|
2017-02-12 22:53:14 +00:00
|
|
|
list.Add(hashPrefix + subPart);
|
2016-11-11 19:55:12 +00:00
|
|
|
}
|
|
|
|
}
|
2017-02-12 22:53:14 +00:00
|
|
|
|
|
|
|
return list;
|
2016-11-11 19:55:12 +00:00
|
|
|
}
|
|
|
|
|
2018-09-12 17:26:21 +00:00
|
|
|
public RestPath(Func<Type, object> createInstanceFn, Func<Type, Func<string, object>> getParseFn, Type requestType, Type serviceType, string path, string verbs, bool isHidden = false, string summary = null, string description = null)
|
2016-11-11 19:55:12 +00:00
|
|
|
{
|
|
|
|
this.RequestType = requestType;
|
2018-09-12 17:26:21 +00:00
|
|
|
this.ServiceType = serviceType;
|
2016-11-11 19:55:12 +00:00
|
|
|
this.Summary = summary;
|
2017-09-11 19:25:13 +00:00
|
|
|
this.IsHidden = isHidden;
|
|
|
|
this.Description = description;
|
2016-11-11 19:55:12 +00:00
|
|
|
this.restPath = path;
|
|
|
|
|
2019-01-27 11:03:43 +00:00
|
|
|
this.Verbs = string.IsNullOrWhiteSpace(verbs) ? ServiceExecExtensions.AllVerbs : verbs.ToUpperInvariant().Split(new[] { ' ', ',' }, StringSplitOptions.RemoveEmptyEntries);
|
2016-11-11 19:55:12 +00:00
|
|
|
|
|
|
|
var componentsList = new List<string>();
|
|
|
|
|
2020-06-14 09:11:11 +00:00
|
|
|
// We only split on '.' if the restPath has them. Allows for /{action}.{type}
|
2016-11-11 19:55:12 +00:00
|
|
|
var hasSeparators = new List<bool>();
|
|
|
|
foreach (var component in this.restPath.Split(PathSeperatorChar))
|
|
|
|
{
|
2020-06-20 08:35:29 +00:00
|
|
|
if (string.IsNullOrEmpty(component))
|
|
|
|
{
|
|
|
|
continue;
|
|
|
|
}
|
2016-11-11 19:55:12 +00:00
|
|
|
|
2019-02-20 15:49:03 +00:00
|
|
|
if (component.IndexOf(VariablePrefix, StringComparison.OrdinalIgnoreCase) != -1
|
2020-08-20 10:16:24 +00:00
|
|
|
&& component.IndexOf(ComponentSeperator, StringComparison.Ordinal) != -1)
|
2016-11-11 19:55:12 +00:00
|
|
|
{
|
|
|
|
hasSeparators.Add(true);
|
|
|
|
componentsList.AddRange(component.Split(ComponentSeperator));
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
hasSeparators.Add(false);
|
|
|
|
componentsList.Add(component);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-12-28 15:48:26 +00:00
|
|
|
var components = componentsList.ToArray();
|
2016-11-11 19:55:12 +00:00
|
|
|
this.TotalComponentsCount = components.Length;
|
|
|
|
|
|
|
|
this.literalsToMatch = new string[this.TotalComponentsCount];
|
|
|
|
this.variablesNames = new string[this.TotalComponentsCount];
|
|
|
|
this.isWildcard = new bool[this.TotalComponentsCount];
|
2018-12-28 15:48:26 +00:00
|
|
|
this.componentsWithSeparators = hasSeparators.ToArray();
|
2016-11-11 19:55:12 +00:00
|
|
|
this.PathComponentsCount = this.componentsWithSeparators.Length;
|
|
|
|
string firstLiteralMatch = null;
|
|
|
|
|
|
|
|
for (var i = 0; i < components.Length; i++)
|
|
|
|
{
|
|
|
|
var component = components[i];
|
|
|
|
|
2020-07-22 11:34:51 +00:00
|
|
|
if (component.StartsWith(VariablePrefix, StringComparison.Ordinal))
|
2016-11-11 19:55:12 +00:00
|
|
|
{
|
|
|
|
var variableName = component.Substring(1, component.Length - 2);
|
|
|
|
if (variableName[variableName.Length - 1] == WildCardChar)
|
|
|
|
{
|
|
|
|
this.isWildcard[i] = true;
|
|
|
|
variableName = variableName.Substring(0, variableName.Length - 1);
|
|
|
|
}
|
2020-06-15 21:43:52 +00:00
|
|
|
|
2016-11-11 19:55:12 +00:00
|
|
|
this.variablesNames[i] = variableName;
|
|
|
|
this.VariableArgsCount++;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2019-01-27 11:03:43 +00:00
|
|
|
this.literalsToMatch[i] = component.ToLowerInvariant();
|
2016-11-11 19:55:12 +00:00
|
|
|
|
|
|
|
if (firstLiteralMatch == null)
|
|
|
|
{
|
|
|
|
firstLiteralMatch = this.literalsToMatch[i];
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
for (var i = 0; i < components.Length - 1; i++)
|
|
|
|
{
|
2019-02-20 15:49:03 +00:00
|
|
|
if (!this.isWildcard[i])
|
|
|
|
{
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
2016-11-11 19:55:12 +00:00
|
|
|
if (this.literalsToMatch[i + 1] == null)
|
|
|
|
{
|
|
|
|
throw new ArgumentException(
|
|
|
|
"A wildcard path component must be at the end of the path or followed by a literal path component.");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-02-20 15:49:03 +00:00
|
|
|
this.wildcardCount = this.isWildcard.Length;
|
2016-11-11 19:55:12 +00:00
|
|
|
this.IsWildCardPath = this.wildcardCount > 0;
|
|
|
|
|
|
|
|
this.FirstMatchHashKey = !this.IsWildCardPath
|
|
|
|
? this.PathComponentsCount + PathSeperator + firstLiteralMatch
|
|
|
|
: WildCardChar + PathSeperator + firstLiteralMatch;
|
|
|
|
|
2017-02-13 02:06:54 +00:00
|
|
|
this.typeDeserializer = new StringMapTypeDeserializer(createInstanceFn, getParseFn, this.RequestType);
|
2016-11-11 19:55:12 +00:00
|
|
|
|
2019-02-20 15:49:03 +00:00
|
|
|
_propertyNamesMap = new HashSet<string>(
|
|
|
|
GetSerializableProperties(RequestType).Select(x => x.Name),
|
|
|
|
StringComparer.OrdinalIgnoreCase);
|
2016-11-11 19:55:12 +00:00
|
|
|
}
|
|
|
|
|
2019-02-20 15:49:03 +00:00
|
|
|
internal static IEnumerable<PropertyInfo> GetSerializableProperties(Type type)
|
2017-02-13 02:06:54 +00:00
|
|
|
{
|
2019-02-20 15:49:03 +00:00
|
|
|
foreach (var prop in GetPublicProperties(type))
|
2017-08-31 03:49:38 +00:00
|
|
|
{
|
2019-02-20 15:49:03 +00:00
|
|
|
if (prop.GetMethod == null
|
2019-10-15 15:49:49 +00:00
|
|
|
|| _excludeType == prop.PropertyType)
|
2017-08-31 03:49:38 +00:00
|
|
|
{
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
var ignored = false;
|
|
|
|
foreach (var attr in prop.GetCustomAttributes(true))
|
|
|
|
{
|
|
|
|
if (IgnoreAttributesNamed.Contains(attr.GetType().Name))
|
2017-02-13 02:06:54 +00:00
|
|
|
{
|
2017-08-31 03:49:38 +00:00
|
|
|
ignored = true;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!ignored)
|
|
|
|
{
|
2019-02-20 15:49:03 +00:00
|
|
|
yield return prop;
|
2017-08-31 03:49:38 +00:00
|
|
|
}
|
|
|
|
}
|
2017-02-13 02:06:54 +00:00
|
|
|
}
|
|
|
|
|
2019-02-20 15:49:03 +00:00
|
|
|
private static IEnumerable<PropertyInfo> GetPublicProperties(Type type)
|
2017-02-13 02:06:54 +00:00
|
|
|
{
|
2019-02-20 15:49:03 +00:00
|
|
|
if (type.IsInterface)
|
2017-02-13 02:06:54 +00:00
|
|
|
{
|
|
|
|
var propertyInfos = new List<PropertyInfo>();
|
2019-02-20 15:49:03 +00:00
|
|
|
var considered = new List<Type>()
|
|
|
|
{
|
|
|
|
type
|
|
|
|
};
|
2017-02-13 02:06:54 +00:00
|
|
|
var queue = new Queue<Type>();
|
|
|
|
queue.Enqueue(type);
|
|
|
|
|
|
|
|
while (queue.Count > 0)
|
|
|
|
{
|
|
|
|
var subType = queue.Dequeue();
|
|
|
|
foreach (var subInterface in subType.GetTypeInfo().ImplementedInterfaces)
|
|
|
|
{
|
2019-02-20 15:49:03 +00:00
|
|
|
if (considered.Contains(subInterface))
|
|
|
|
{
|
|
|
|
continue;
|
|
|
|
}
|
2017-02-13 02:06:54 +00:00
|
|
|
|
|
|
|
considered.Add(subInterface);
|
|
|
|
queue.Enqueue(subInterface);
|
|
|
|
}
|
|
|
|
|
2019-02-20 15:49:03 +00:00
|
|
|
var newPropertyInfos = GetTypesPublicProperties(subType)
|
2017-02-13 02:06:54 +00:00
|
|
|
.Where(x => !propertyInfos.Contains(x));
|
|
|
|
|
|
|
|
propertyInfos.InsertRange(0, newPropertyInfos);
|
|
|
|
}
|
|
|
|
|
2017-08-31 03:49:38 +00:00
|
|
|
return propertyInfos;
|
2017-02-13 02:06:54 +00:00
|
|
|
}
|
|
|
|
|
2019-02-20 15:49:03 +00:00
|
|
|
return GetTypesPublicProperties(type)
|
|
|
|
.Where(x => x.GetIndexParameters().Length == 0);
|
2017-02-13 02:06:54 +00:00
|
|
|
}
|
|
|
|
|
2019-02-20 15:49:03 +00:00
|
|
|
private static IEnumerable<PropertyInfo> GetTypesPublicProperties(Type subType)
|
2017-02-13 02:06:54 +00:00
|
|
|
{
|
|
|
|
foreach (var pi in subType.GetRuntimeProperties())
|
|
|
|
{
|
|
|
|
var mi = pi.GetMethod ?? pi.SetMethod;
|
2019-02-20 15:49:03 +00:00
|
|
|
if (mi != null && mi.IsStatic)
|
|
|
|
{
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
yield return pi;
|
2017-02-13 02:06:54 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-11-11 19:55:12 +00:00
|
|
|
/// <summary>
|
2019-10-25 10:47:20 +00:00
|
|
|
/// Provide for quick lookups based on hashes that can be determined from a request url.
|
2016-11-11 19:55:12 +00:00
|
|
|
/// </summary>
|
|
|
|
public string FirstMatchHashKey { get; private set; }
|
|
|
|
|
|
|
|
private readonly StringMapTypeDeserializer typeDeserializer;
|
|
|
|
|
2019-02-20 15:49:03 +00:00
|
|
|
private readonly HashSet<string> _propertyNamesMap;
|
2016-11-11 19:55:12 +00:00
|
|
|
|
2018-12-14 19:17:29 +00:00
|
|
|
public int MatchScore(string httpMethod, string[] withPathInfoParts)
|
2016-11-11 19:55:12 +00:00
|
|
|
{
|
2019-01-13 20:46:33 +00:00
|
|
|
var isMatch = IsMatch(httpMethod, withPathInfoParts, out var wildcardMatchCount);
|
2017-02-12 22:53:14 +00:00
|
|
|
if (!isMatch)
|
|
|
|
{
|
|
|
|
return -1;
|
|
|
|
}
|
2016-11-11 19:55:12 +00:00
|
|
|
|
2020-06-14 09:11:11 +00:00
|
|
|
// Routes with least wildcard matches get the highest score
|
2020-06-19 09:57:37 +00:00
|
|
|
var score = Math.Max(100 - wildcardMatchCount, 1) * 1000
|
2020-06-14 09:11:11 +00:00
|
|
|
// Routes with less variable (and more literal) matches
|
2020-06-19 09:57:37 +00:00
|
|
|
+ Math.Max(10 - VariableArgsCount, 1) * 100;
|
2016-11-11 19:55:12 +00:00
|
|
|
|
2020-06-14 09:11:11 +00:00
|
|
|
// Exact verb match is better than ANY
|
2017-08-31 03:49:38 +00:00
|
|
|
if (Verbs.Length == 1 && string.Equals(httpMethod, Verbs[0], StringComparison.OrdinalIgnoreCase))
|
|
|
|
{
|
|
|
|
score += 10;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
score += 1;
|
|
|
|
}
|
2016-11-11 19:55:12 +00:00
|
|
|
|
|
|
|
return score;
|
|
|
|
}
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
/// For performance withPathInfoParts should already be a lower case string
|
|
|
|
/// to minimize redundant matching operations.
|
|
|
|
/// </summary>
|
2018-12-14 19:17:29 +00:00
|
|
|
public bool IsMatch(string httpMethod, string[] withPathInfoParts, out int wildcardMatchCount)
|
2016-11-11 19:55:12 +00:00
|
|
|
{
|
|
|
|
wildcardMatchCount = 0;
|
|
|
|
|
2017-02-12 22:53:14 +00:00
|
|
|
if (withPathInfoParts.Length != this.PathComponentsCount && !this.IsWildCardPath)
|
|
|
|
{
|
2019-01-12 20:41:08 +00:00
|
|
|
return false;
|
2017-02-12 22:53:14 +00:00
|
|
|
}
|
2016-11-11 19:55:12 +00:00
|
|
|
|
2017-08-31 03:49:38 +00:00
|
|
|
if (!Verbs.Contains(httpMethod, StringComparer.OrdinalIgnoreCase))
|
2017-02-12 22:53:14 +00:00
|
|
|
{
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!ExplodeComponents(ref withPathInfoParts))
|
|
|
|
{
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (this.TotalComponentsCount != withPathInfoParts.Length && !this.IsWildCardPath)
|
|
|
|
{
|
|
|
|
return false;
|
|
|
|
}
|
2016-11-11 19:55:12 +00:00
|
|
|
|
|
|
|
int pathIx = 0;
|
|
|
|
for (var i = 0; i < this.TotalComponentsCount; i++)
|
|
|
|
{
|
|
|
|
if (this.isWildcard[i])
|
|
|
|
{
|
|
|
|
if (i < this.TotalComponentsCount - 1)
|
|
|
|
{
|
|
|
|
// Continue to consume up until a match with the next literal
|
2019-02-20 15:49:03 +00:00
|
|
|
while (pathIx < withPathInfoParts.Length
|
|
|
|
&& !string.Equals(withPathInfoParts[pathIx], this.literalsToMatch[i + 1], StringComparison.InvariantCultureIgnoreCase))
|
2016-11-11 19:55:12 +00:00
|
|
|
{
|
|
|
|
pathIx++;
|
|
|
|
wildcardMatchCount++;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Ensure there are still enough parts left to match the remainder
|
|
|
|
if ((withPathInfoParts.Length - pathIx) < (this.TotalComponentsCount - i - 1))
|
|
|
|
{
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
// A wildcard at the end matches the remainder of path
|
|
|
|
wildcardMatchCount += withPathInfoParts.Length - pathIx;
|
|
|
|
pathIx = withPathInfoParts.Length;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
var literalToMatch = this.literalsToMatch[i];
|
|
|
|
if (literalToMatch == null)
|
|
|
|
{
|
|
|
|
// Matching an ordinary (non-wildcard) variable consumes a single part
|
|
|
|
pathIx++;
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
2019-02-20 15:49:03 +00:00
|
|
|
if (withPathInfoParts.Length <= pathIx
|
|
|
|
|| !string.Equals(withPathInfoParts[pathIx], literalToMatch, StringComparison.InvariantCultureIgnoreCase))
|
2017-02-12 22:53:14 +00:00
|
|
|
{
|
|
|
|
return false;
|
|
|
|
}
|
2019-02-20 15:49:03 +00:00
|
|
|
|
2016-11-11 19:55:12 +00:00
|
|
|
pathIx++;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return pathIx == withPathInfoParts.Length;
|
|
|
|
}
|
|
|
|
|
|
|
|
private bool ExplodeComponents(ref string[] withPathInfoParts)
|
|
|
|
{
|
|
|
|
var totalComponents = new List<string>();
|
|
|
|
for (var i = 0; i < withPathInfoParts.Length; i++)
|
|
|
|
{
|
|
|
|
var component = withPathInfoParts[i];
|
2019-02-20 15:49:03 +00:00
|
|
|
if (string.IsNullOrEmpty(component))
|
|
|
|
{
|
|
|
|
continue;
|
|
|
|
}
|
2016-11-11 19:55:12 +00:00
|
|
|
|
|
|
|
if (this.PathComponentsCount != this.TotalComponentsCount
|
|
|
|
&& this.componentsWithSeparators[i])
|
|
|
|
{
|
|
|
|
var subComponents = component.Split(ComponentSeperator);
|
2019-02-20 15:49:03 +00:00
|
|
|
if (subComponents.Length < 2)
|
|
|
|
{
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2016-11-11 19:55:12 +00:00
|
|
|
totalComponents.AddRange(subComponents);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
totalComponents.Add(component);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-12-28 15:48:26 +00:00
|
|
|
withPathInfoParts = totalComponents.ToArray();
|
2016-11-11 19:55:12 +00:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
public object CreateRequest(string pathInfo, Dictionary<string, string> queryStringAndFormData, object fromInstance)
|
|
|
|
{
|
2017-08-31 03:49:38 +00:00
|
|
|
var requestComponents = pathInfo.Split(new[] { PathSeperatorChar }, StringSplitOptions.RemoveEmptyEntries);
|
2016-11-11 19:55:12 +00:00
|
|
|
|
|
|
|
ExplodeComponents(ref requestComponents);
|
|
|
|
|
|
|
|
if (requestComponents.Length != this.TotalComponentsCount)
|
|
|
|
{
|
|
|
|
var isValidWildCardPath = this.IsWildCardPath
|
|
|
|
&& requestComponents.Length >= this.TotalComponentsCount - this.wildcardCount;
|
|
|
|
|
|
|
|
if (!isValidWildCardPath)
|
2020-06-20 09:12:36 +00:00
|
|
|
{
|
2019-10-25 10:47:20 +00:00
|
|
|
throw new ArgumentException(
|
|
|
|
string.Format(
|
|
|
|
CultureInfo.InvariantCulture,
|
|
|
|
"Path Mismatch: Request Path '{0}' has invalid number of components compared to: '{1}'",
|
|
|
|
pathInfo,
|
|
|
|
this.restPath));
|
2020-06-20 09:12:36 +00:00
|
|
|
}
|
2016-11-11 19:55:12 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
var requestKeyValuesMap = new Dictionary<string, string>();
|
|
|
|
var pathIx = 0;
|
|
|
|
for (var i = 0; i < this.TotalComponentsCount; i++)
|
|
|
|
{
|
|
|
|
var variableName = this.variablesNames[i];
|
|
|
|
if (variableName == null)
|
|
|
|
{
|
|
|
|
pathIx++;
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
2019-02-20 15:49:03 +00:00
|
|
|
if (!this._propertyNamesMap.Contains(variableName))
|
2016-11-11 19:55:12 +00:00
|
|
|
{
|
2019-01-06 20:50:43 +00:00
|
|
|
if (string.Equals("ignore", variableName, StringComparison.OrdinalIgnoreCase))
|
2016-11-11 19:55:12 +00:00
|
|
|
{
|
|
|
|
pathIx++;
|
2017-02-12 22:53:14 +00:00
|
|
|
continue;
|
2016-11-11 19:55:12 +00:00
|
|
|
}
|
2017-02-12 22:53:14 +00:00
|
|
|
|
2016-11-11 19:55:12 +00:00
|
|
|
throw new ArgumentException("Could not find property "
|
2017-02-13 20:54:28 +00:00
|
|
|
+ variableName + " on " + RequestType.GetMethodName());
|
2016-11-11 19:55:12 +00:00
|
|
|
}
|
|
|
|
|
2020-06-14 09:11:11 +00:00
|
|
|
var value = requestComponents.Length > pathIx ? requestComponents[pathIx] : null; // wildcard has arg mismatch
|
2016-11-11 19:55:12 +00:00
|
|
|
if (value != null && this.isWildcard[i])
|
|
|
|
{
|
|
|
|
if (i == this.TotalComponentsCount - 1)
|
|
|
|
{
|
|
|
|
// Wildcard at end of path definition consumes all the rest
|
|
|
|
var sb = new StringBuilder();
|
|
|
|
sb.Append(value);
|
|
|
|
for (var j = pathIx + 1; j < requestComponents.Length; j++)
|
|
|
|
{
|
2020-07-22 12:34:51 +00:00
|
|
|
sb.Append(PathSeperatorChar)
|
|
|
|
.Append(requestComponents[j]);
|
2016-11-11 19:55:12 +00:00
|
|
|
}
|
2019-02-20 15:49:03 +00:00
|
|
|
|
2016-11-11 19:55:12 +00:00
|
|
|
value = sb.ToString();
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
// Wildcard in middle of path definition consumes up until it
|
|
|
|
// hits a match for the next element in the definition (which must be a literal)
|
|
|
|
// It may consume 0 or more path parts
|
|
|
|
var stopLiteral = i == this.TotalComponentsCount - 1 ? null : this.literalsToMatch[i + 1];
|
2019-01-06 20:50:43 +00:00
|
|
|
if (!string.Equals(requestComponents[pathIx], stopLiteral, StringComparison.OrdinalIgnoreCase))
|
2016-11-11 19:55:12 +00:00
|
|
|
{
|
2019-02-20 15:49:03 +00:00
|
|
|
var sb = new StringBuilder(value);
|
2016-11-11 19:55:12 +00:00
|
|
|
pathIx++;
|
2019-01-06 20:50:43 +00:00
|
|
|
while (!string.Equals(requestComponents[pathIx], stopLiteral, StringComparison.OrdinalIgnoreCase))
|
2016-11-11 19:55:12 +00:00
|
|
|
{
|
2020-07-22 12:34:51 +00:00
|
|
|
sb.Append(PathSeperatorChar)
|
|
|
|
.Append(requestComponents[pathIx++]);
|
2016-11-11 19:55:12 +00:00
|
|
|
}
|
2019-02-20 15:49:03 +00:00
|
|
|
|
2016-11-11 19:55:12 +00:00
|
|
|
value = sb.ToString();
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
value = null;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
// Variable consumes single path item
|
|
|
|
pathIx++;
|
|
|
|
}
|
|
|
|
|
2019-02-20 15:49:03 +00:00
|
|
|
requestKeyValuesMap[variableName] = value;
|
2016-11-11 19:55:12 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if (queryStringAndFormData != null)
|
|
|
|
{
|
2020-06-14 09:11:11 +00:00
|
|
|
// Query String and form data can override variable path matches
|
|
|
|
// path variables < query string < form data
|
2016-11-11 19:55:12 +00:00
|
|
|
foreach (var name in queryStringAndFormData)
|
|
|
|
{
|
|
|
|
requestKeyValuesMap[name.Key] = name.Value;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return this.typeDeserializer.PopulateFromMap(fromInstance, requestKeyValuesMap);
|
|
|
|
}
|
2018-09-12 17:26:21 +00:00
|
|
|
|
|
|
|
public class RestPathMap : SortedDictionary<string, List<RestPath>>
|
|
|
|
{
|
|
|
|
public RestPathMap() : base(StringComparer.OrdinalIgnoreCase)
|
|
|
|
{
|
|
|
|
}
|
|
|
|
}
|
2016-11-11 19:55:12 +00:00
|
|
|
}
|
2018-12-28 15:48:26 +00:00
|
|
|
}
|