Node.cs 1.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364
  1. using System.Collections.Generic;
  2. namespace Best.HTTP.Hosts.Settings
  3. {
  4. internal sealed class Node
  5. {
  6. public string key;
  7. public SortedList<string, Node> childNodes;
  8. public HostSettings hostSettings;
  9. public Node(string key) => this.key = key;
  10. public Node(string key, HostSettings settings) : this(key) => this.hostSettings = settings;
  11. public void Add(string subKey, Node subNode)
  12. {
  13. if (childNodes == null)
  14. childNodes = new SortedList<string, Node>(AsteriskStringComparer.Instance);
  15. childNodes.Add(subKey, subNode);
  16. }
  17. public void AddSetting(HostSettings settings)
  18. {
  19. this.hostSettings = settings;
  20. }
  21. public void Add(List<string> segments, HostSettings settings)
  22. {
  23. if (segments.Count == 0)
  24. {
  25. this.hostSettings = settings;
  26. return;
  27. }
  28. string subKey = segments[0];
  29. segments.RemoveAt(0);
  30. if (this.childNodes == null)
  31. this.childNodes = new SortedList<string, Node>(AsteriskStringComparer.Instance);
  32. if (!this.childNodes.TryGetValue(subKey, out var node))
  33. this.childNodes.Add(subKey, node = new Node(subKey, null));
  34. node.Add(segments, settings);
  35. }
  36. public HostSettings Find(List<string> segments)
  37. {
  38. if (segments.Count == 0)
  39. return this.hostSettings;
  40. if (this.childNodes == null || this.childNodes.Count == 0)
  41. return null;
  42. string subKey = segments[0];
  43. segments.RemoveAt(0);
  44. if (this.childNodes.TryGetValue(subKey, out var node))
  45. return node.Find(segments);
  46. return null;
  47. }
  48. }
  49. }