ServicePath.cs 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558
  1. using System;
  2. using System.Collections.Generic;
  3. using System.IO;
  4. using System.Linq;
  5. using System.Reflection;
  6. using System.Text;
  7. using MediaBrowser.Model.Logging;
  8. using MediaBrowser.Model.Extensions;
  9. namespace Emby.Server.Implementations.Services
  10. {
  11. public class RestPath
  12. {
  13. private const string WildCard = "*";
  14. private const char WildCardChar = '*';
  15. private const string PathSeperator = "/";
  16. private const char PathSeperatorChar = '/';
  17. private const char ComponentSeperator = '.';
  18. private const string VariablePrefix = "{";
  19. readonly bool[] componentsWithSeparators;
  20. private readonly string restPath;
  21. public bool IsWildCardPath { get; private set; }
  22. private readonly string[] literalsToMatch;
  23. private readonly string[] variablesNames;
  24. private readonly bool[] isWildcard;
  25. private readonly int wildcardCount = 0;
  26. public int VariableArgsCount { get; set; }
  27. /// <summary>
  28. /// The number of segments separated by '/' determinable by path.Split('/').Length
  29. /// e.g. /path/to/here.ext == 3
  30. /// </summary>
  31. public int PathComponentsCount { get; set; }
  32. /// <summary>
  33. /// The total number of segments after subparts have been exploded ('.')
  34. /// e.g. /path/to/here.ext == 4
  35. /// </summary>
  36. public int TotalComponentsCount { get; set; }
  37. public string[] Verbs { get; private set; }
  38. public Type RequestType { get; private set; }
  39. public string Path { get { return this.restPath; } }
  40. public string Summary { get; private set; }
  41. public int Priority { get; set; } //passed back to RouteAttribute
  42. public static string[] GetPathPartsForMatching(string pathInfo)
  43. {
  44. return pathInfo.ToLower().Split(new[] { PathSeperatorChar }, StringSplitOptions.RemoveEmptyEntries);
  45. }
  46. public static List<string> GetFirstMatchHashKeys(string[] pathPartsForMatching)
  47. {
  48. var hashPrefix = pathPartsForMatching.Length + PathSeperator;
  49. return GetPotentialMatchesWithPrefix(hashPrefix, pathPartsForMatching);
  50. }
  51. public static List<string> GetFirstMatchWildCardHashKeys(string[] pathPartsForMatching)
  52. {
  53. const string hashPrefix = WildCard + PathSeperator;
  54. return GetPotentialMatchesWithPrefix(hashPrefix, pathPartsForMatching);
  55. }
  56. private static List<string> GetPotentialMatchesWithPrefix(string hashPrefix, string[] pathPartsForMatching)
  57. {
  58. var list = new List<string>();
  59. foreach (var part in pathPartsForMatching)
  60. {
  61. list.Add(hashPrefix + part);
  62. var subParts = part.Split(ComponentSeperator);
  63. if (subParts.Length == 1) continue;
  64. foreach (var subPart in subParts)
  65. {
  66. list.Add(hashPrefix + subPart);
  67. }
  68. }
  69. return list;
  70. }
  71. public RestPath(Func<Type, object> createInstanceFn, Func<Type, Func<string, object>> getParseFn, Type requestType, string path, string verbs, string summary = null)
  72. {
  73. this.RequestType = requestType;
  74. this.Summary = summary;
  75. this.restPath = path;
  76. this.Verbs = string.IsNullOrWhiteSpace(verbs) ? ServiceExecExtensions.AllVerbs : verbs.ToUpper().Split(new[] { ' ', ',' }, StringSplitOptions.RemoveEmptyEntries);
  77. var componentsList = new List<string>();
  78. //We only split on '.' if the restPath has them. Allows for /{action}.{type}
  79. var hasSeparators = new List<bool>();
  80. foreach (var component in this.restPath.Split(PathSeperatorChar))
  81. {
  82. if (String.IsNullOrEmpty(component)) continue;
  83. if (StringContains(component, VariablePrefix)
  84. && component.IndexOf(ComponentSeperator) != -1)
  85. {
  86. hasSeparators.Add(true);
  87. componentsList.AddRange(component.Split(ComponentSeperator));
  88. }
  89. else
  90. {
  91. hasSeparators.Add(false);
  92. componentsList.Add(component);
  93. }
  94. }
  95. var components = componentsList.ToArray(componentsList.Count);
  96. this.TotalComponentsCount = components.Length;
  97. this.literalsToMatch = new string[this.TotalComponentsCount];
  98. this.variablesNames = new string[this.TotalComponentsCount];
  99. this.isWildcard = new bool[this.TotalComponentsCount];
  100. this.componentsWithSeparators = hasSeparators.ToArray(hasSeparators.Count);
  101. this.PathComponentsCount = this.componentsWithSeparators.Length;
  102. string firstLiteralMatch = null;
  103. for (var i = 0; i < components.Length; i++)
  104. {
  105. var component = components[i];
  106. if (component.StartsWith(VariablePrefix))
  107. {
  108. var variableName = component.Substring(1, component.Length - 2);
  109. if (variableName[variableName.Length - 1] == WildCardChar)
  110. {
  111. this.isWildcard[i] = true;
  112. variableName = variableName.Substring(0, variableName.Length - 1);
  113. }
  114. this.variablesNames[i] = variableName;
  115. this.VariableArgsCount++;
  116. }
  117. else
  118. {
  119. this.literalsToMatch[i] = component.ToLower();
  120. if (firstLiteralMatch == null)
  121. {
  122. firstLiteralMatch = this.literalsToMatch[i];
  123. }
  124. }
  125. }
  126. for (var i = 0; i < components.Length - 1; i++)
  127. {
  128. if (!this.isWildcard[i]) continue;
  129. if (this.literalsToMatch[i + 1] == null)
  130. {
  131. throw new ArgumentException(
  132. "A wildcard path component must be at the end of the path or followed by a literal path component.");
  133. }
  134. }
  135. this.wildcardCount = this.isWildcard.Count(x => x);
  136. this.IsWildCardPath = this.wildcardCount > 0;
  137. this.FirstMatchHashKey = !this.IsWildCardPath
  138. ? this.PathComponentsCount + PathSeperator + firstLiteralMatch
  139. : WildCardChar + PathSeperator + firstLiteralMatch;
  140. this.typeDeserializer = new StringMapTypeDeserializer(createInstanceFn, getParseFn, this.RequestType);
  141. RegisterCaseInsenstivePropertyNameMappings();
  142. }
  143. private void RegisterCaseInsenstivePropertyNameMappings()
  144. {
  145. foreach (var propertyInfo in GetSerializableProperties(RequestType))
  146. {
  147. var propertyName = propertyInfo.Name;
  148. propertyNamesMap.Add(propertyName.ToLower(), propertyName);
  149. }
  150. }
  151. internal static string[] IgnoreAttributesNamed = new[] {
  152. "IgnoreDataMemberAttribute",
  153. "JsonIgnoreAttribute"
  154. };
  155. private static Type excludeType = typeof(Stream);
  156. internal static List<PropertyInfo> GetSerializableProperties(Type type)
  157. {
  158. var list = new List<PropertyInfo>();
  159. var props = GetPublicProperties(type);
  160. foreach (var prop in props)
  161. {
  162. if (prop.GetMethod == null)
  163. {
  164. continue;
  165. }
  166. if (excludeType == prop.PropertyType)
  167. {
  168. continue;
  169. }
  170. var ignored = false;
  171. foreach (var attr in prop.GetCustomAttributes(true))
  172. {
  173. if (IgnoreAttributesNamed.Contains(attr.GetType().Name))
  174. {
  175. ignored = true;
  176. break;
  177. }
  178. }
  179. if (!ignored)
  180. {
  181. list.Add(prop);
  182. }
  183. }
  184. // else return those properties that are not decorated with IgnoreDataMember
  185. return list;
  186. }
  187. private static List<PropertyInfo> GetPublicProperties(Type type)
  188. {
  189. if (type.GetTypeInfo().IsInterface)
  190. {
  191. var propertyInfos = new List<PropertyInfo>();
  192. var considered = new List<Type>();
  193. var queue = new Queue<Type>();
  194. considered.Add(type);
  195. queue.Enqueue(type);
  196. while (queue.Count > 0)
  197. {
  198. var subType = queue.Dequeue();
  199. foreach (var subInterface in subType.GetTypeInfo().ImplementedInterfaces)
  200. {
  201. if (considered.Contains(subInterface)) continue;
  202. considered.Add(subInterface);
  203. queue.Enqueue(subInterface);
  204. }
  205. var typeProperties = GetTypesPublicProperties(subType);
  206. var newPropertyInfos = typeProperties
  207. .Where(x => !propertyInfos.Contains(x));
  208. propertyInfos.InsertRange(0, newPropertyInfos);
  209. }
  210. return propertyInfos;
  211. }
  212. var list = new List<PropertyInfo>();
  213. foreach (var t in GetTypesPublicProperties(type))
  214. {
  215. if (t.GetIndexParameters().Length == 0)
  216. {
  217. list.Add(t);
  218. }
  219. }
  220. return list;
  221. }
  222. private static PropertyInfo[] GetTypesPublicProperties(Type subType)
  223. {
  224. var pis = new List<PropertyInfo>();
  225. foreach (var pi in subType.GetRuntimeProperties())
  226. {
  227. var mi = pi.GetMethod ?? pi.SetMethod;
  228. if (mi != null && mi.IsStatic) continue;
  229. pis.Add(pi);
  230. }
  231. return pis.ToArray(pis.Count);
  232. }
  233. /// <summary>
  234. /// Provide for quick lookups based on hashes that can be determined from a request url
  235. /// </summary>
  236. public string FirstMatchHashKey { get; private set; }
  237. private readonly StringMapTypeDeserializer typeDeserializer;
  238. private readonly Dictionary<string, string> propertyNamesMap = new Dictionary<string, string>();
  239. public int MatchScore(string httpMethod, string[] withPathInfoParts, ILogger logger)
  240. {
  241. int wildcardMatchCount;
  242. var isMatch = IsMatch(httpMethod, withPathInfoParts, logger, out wildcardMatchCount);
  243. if (!isMatch)
  244. {
  245. return -1;
  246. }
  247. var score = 0;
  248. //Routes with least wildcard matches get the highest score
  249. score += Math.Max((100 - wildcardMatchCount), 1) * 1000;
  250. //Routes with less variable (and more literal) matches
  251. score += Math.Max((10 - VariableArgsCount), 1) * 100;
  252. //Exact verb match is better than ANY
  253. if (Verbs.Length == 1 && string.Equals(httpMethod, Verbs[0], StringComparison.OrdinalIgnoreCase))
  254. {
  255. score += 10;
  256. }
  257. else
  258. {
  259. score += 1;
  260. }
  261. return score;
  262. }
  263. private bool StringContains(string str1, string str2)
  264. {
  265. return str1.IndexOf(str2, StringComparison.OrdinalIgnoreCase) != -1;
  266. }
  267. /// <summary>
  268. /// For performance withPathInfoParts should already be a lower case string
  269. /// to minimize redundant matching operations.
  270. /// </summary>
  271. public bool IsMatch(string httpMethod, string[] withPathInfoParts, ILogger logger, out int wildcardMatchCount)
  272. {
  273. wildcardMatchCount = 0;
  274. if (withPathInfoParts.Length != this.PathComponentsCount && !this.IsWildCardPath)
  275. {
  276. //logger.Info("withPathInfoParts mismatch for {0} for {1}", httpMethod, string.Join("/", withPathInfoParts));
  277. return false;
  278. }
  279. if (!Verbs.Contains(httpMethod, StringComparer.OrdinalIgnoreCase))
  280. {
  281. //logger.Info("allowsAllVerbs mismatch for {0} for {1} allowedverbs {2}", httpMethod, string.Join("/", withPathInfoParts), this.allowedVerbs);
  282. return false;
  283. }
  284. if (!ExplodeComponents(ref withPathInfoParts))
  285. {
  286. //logger.Info("ExplodeComponents mismatch for {0} for {1}", httpMethod, string.Join("/", withPathInfoParts));
  287. return false;
  288. }
  289. if (this.TotalComponentsCount != withPathInfoParts.Length && !this.IsWildCardPath)
  290. {
  291. //logger.Info("TotalComponentsCount mismatch for {0} for {1}", httpMethod, string.Join("/", withPathInfoParts));
  292. return false;
  293. }
  294. int pathIx = 0;
  295. for (var i = 0; i < this.TotalComponentsCount; i++)
  296. {
  297. if (this.isWildcard[i])
  298. {
  299. if (i < this.TotalComponentsCount - 1)
  300. {
  301. // Continue to consume up until a match with the next literal
  302. while (pathIx < withPathInfoParts.Length && !LiteralsEqual(withPathInfoParts[pathIx], this.literalsToMatch[i + 1]))
  303. {
  304. pathIx++;
  305. wildcardMatchCount++;
  306. }
  307. // Ensure there are still enough parts left to match the remainder
  308. if ((withPathInfoParts.Length - pathIx) < (this.TotalComponentsCount - i - 1))
  309. {
  310. //logger.Info("withPathInfoParts length mismatch for {0} for {1}", httpMethod, string.Join("/", withPathInfoParts));
  311. return false;
  312. }
  313. }
  314. else
  315. {
  316. // A wildcard at the end matches the remainder of path
  317. wildcardMatchCount += withPathInfoParts.Length - pathIx;
  318. pathIx = withPathInfoParts.Length;
  319. }
  320. }
  321. else
  322. {
  323. var literalToMatch = this.literalsToMatch[i];
  324. if (literalToMatch == null)
  325. {
  326. // Matching an ordinary (non-wildcard) variable consumes a single part
  327. pathIx++;
  328. continue;
  329. }
  330. if (withPathInfoParts.Length <= pathIx || !LiteralsEqual(withPathInfoParts[pathIx], literalToMatch))
  331. {
  332. //logger.Info("withPathInfoParts2 length mismatch for {0} for {1}. not equals: {2} != {3}.", httpMethod, string.Join("/", withPathInfoParts), withPathInfoParts[pathIx], literalToMatch);
  333. return false;
  334. }
  335. pathIx++;
  336. }
  337. }
  338. return pathIx == withPathInfoParts.Length;
  339. }
  340. private bool LiteralsEqual(string str1, string str2)
  341. {
  342. // Most cases
  343. if (String.Equals(str1, str2, StringComparison.OrdinalIgnoreCase))
  344. {
  345. return true;
  346. }
  347. // Handle turkish i
  348. str1 = str1.ToUpperInvariant();
  349. str2 = str2.ToUpperInvariant();
  350. // Invariant IgnoreCase would probably be better but it's not available in PCL
  351. return String.Equals(str1, str2, StringComparison.CurrentCultureIgnoreCase);
  352. }
  353. private bool ExplodeComponents(ref string[] withPathInfoParts)
  354. {
  355. var totalComponents = new List<string>();
  356. for (var i = 0; i < withPathInfoParts.Length; i++)
  357. {
  358. var component = withPathInfoParts[i];
  359. if (String.IsNullOrEmpty(component)) continue;
  360. if (this.PathComponentsCount != this.TotalComponentsCount
  361. && this.componentsWithSeparators[i])
  362. {
  363. var subComponents = component.Split(ComponentSeperator);
  364. if (subComponents.Length < 2) return false;
  365. totalComponents.AddRange(subComponents);
  366. }
  367. else
  368. {
  369. totalComponents.Add(component);
  370. }
  371. }
  372. withPathInfoParts = totalComponents.ToArray(totalComponents.Count);
  373. return true;
  374. }
  375. public object CreateRequest(string pathInfo, Dictionary<string, string> queryStringAndFormData, object fromInstance)
  376. {
  377. var requestComponents = pathInfo.Split(new[] { PathSeperatorChar }, StringSplitOptions.RemoveEmptyEntries);
  378. ExplodeComponents(ref requestComponents);
  379. if (requestComponents.Length != this.TotalComponentsCount)
  380. {
  381. var isValidWildCardPath = this.IsWildCardPath
  382. && requestComponents.Length >= this.TotalComponentsCount - this.wildcardCount;
  383. if (!isValidWildCardPath)
  384. throw new ArgumentException(String.Format(
  385. "Path Mismatch: Request Path '{0}' has invalid number of components compared to: '{1}'",
  386. pathInfo, this.restPath));
  387. }
  388. var requestKeyValuesMap = new Dictionary<string, string>();
  389. var pathIx = 0;
  390. for (var i = 0; i < this.TotalComponentsCount; i++)
  391. {
  392. var variableName = this.variablesNames[i];
  393. if (variableName == null)
  394. {
  395. pathIx++;
  396. continue;
  397. }
  398. string propertyNameOnRequest;
  399. if (!this.propertyNamesMap.TryGetValue(variableName.ToLower(), out propertyNameOnRequest))
  400. {
  401. if (String.Equals("ignore", variableName, StringComparison.OrdinalIgnoreCase))
  402. {
  403. pathIx++;
  404. continue;
  405. }
  406. throw new ArgumentException("Could not find property "
  407. + variableName + " on " + RequestType.GetMethodName());
  408. }
  409. var value = requestComponents.Length > pathIx ? requestComponents[pathIx] : null; //wildcard has arg mismatch
  410. if (value != null && this.isWildcard[i])
  411. {
  412. if (i == this.TotalComponentsCount - 1)
  413. {
  414. // Wildcard at end of path definition consumes all the rest
  415. var sb = new StringBuilder();
  416. sb.Append(value);
  417. for (var j = pathIx + 1; j < requestComponents.Length; j++)
  418. {
  419. sb.Append(PathSeperatorChar + requestComponents[j]);
  420. }
  421. value = sb.ToString();
  422. }
  423. else
  424. {
  425. // Wildcard in middle of path definition consumes up until it
  426. // hits a match for the next element in the definition (which must be a literal)
  427. // It may consume 0 or more path parts
  428. var stopLiteral = i == this.TotalComponentsCount - 1 ? null : this.literalsToMatch[i + 1];
  429. if (!String.Equals(requestComponents[pathIx], stopLiteral, StringComparison.OrdinalIgnoreCase))
  430. {
  431. var sb = new StringBuilder();
  432. sb.Append(value);
  433. pathIx++;
  434. while (!String.Equals(requestComponents[pathIx], stopLiteral, StringComparison.OrdinalIgnoreCase))
  435. {
  436. sb.Append(PathSeperatorChar + requestComponents[pathIx++]);
  437. }
  438. value = sb.ToString();
  439. }
  440. else
  441. {
  442. value = null;
  443. }
  444. }
  445. }
  446. else
  447. {
  448. // Variable consumes single path item
  449. pathIx++;
  450. }
  451. requestKeyValuesMap[propertyNameOnRequest] = value;
  452. }
  453. if (queryStringAndFormData != null)
  454. {
  455. //Query String and form data can override variable path matches
  456. //path variables < query string < form data
  457. foreach (var name in queryStringAndFormData)
  458. {
  459. requestKeyValuesMap[name.Key] = name.Value;
  460. }
  461. }
  462. return this.typeDeserializer.PopulateFromMap(fromInstance, requestKeyValuesMap);
  463. }
  464. }
  465. }