RouteElement.cs 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160
  1. using System;
  2. using System.IO;
  3. using System.Net;
  4. using System.Net.Sockets;
  5. using System.Text;
  6. using System.Threading;
  7. using System.Collections;
  8. using System.Collections.Generic;
  9. using System.Reflection;
  10. using System.Linq;
  11. using System.Text.RegularExpressions;
  12. namespace System.Net.ApiService {
  13. public class RouteElements {
  14. private object _instance;
  15. private Route _route;
  16. private MethodInfo _method;
  17. private Type _t;
  18. private string _baseRoute;
  19. private Regex _pattern;
  20. public RouteElements(object instance, MethodInfo mi, Route r) {
  21. this._instance = instance;
  22. this._route = r;
  23. this._method = mi;
  24. this._t = instance.GetType();
  25. this._baseRoute = "";
  26. this.createPattern();
  27. }
  28. public RouteElements(Type instance, MethodInfo mi, Route r) {
  29. this._t = instance;
  30. this._route = r;
  31. this._method = mi;
  32. this._baseRoute = "";
  33. this.createPattern();
  34. }
  35. public RouteElements(object instance, MethodInfo mi, Route r, string BaseRoute) {
  36. this._instance = instance;
  37. this._route = r;
  38. this._method = mi;
  39. this._t = instance.GetType();
  40. this._baseRoute = BaseRoute;
  41. this.createPattern();
  42. }
  43. public RouteElements(Type instance, MethodInfo mi, Route r, string BaseRoute) {
  44. this._t = instance;
  45. this._route = r;
  46. this._method = mi;
  47. this._baseRoute = BaseRoute;
  48. this.createPattern();
  49. }
  50. private void createPattern() {
  51. string x = this.Path;
  52. string fin = "";
  53. int round = 0;
  54. int eckig = 0;
  55. int geschweift = 0;
  56. bool bIsEscaped = false;
  57. for (int i = 0; i < x.Length; i++) {
  58. if (!bIsEscaped) {
  59. switch (x.Substring(i, 1)) {
  60. case "{":
  61. geschweift++;
  62. fin += "{";
  63. break;
  64. case "}":
  65. geschweift--;
  66. fin += "}";
  67. break;
  68. case "(":
  69. round++;
  70. fin += "(";
  71. break;
  72. case ")":
  73. round--;
  74. fin += ")";
  75. break;
  76. case "[":
  77. eckig++;
  78. fin += "[";
  79. break;
  80. case "]":
  81. eckig--;
  82. fin += "]";
  83. break;
  84. case ".":
  85. case "/":
  86. if (eckig == 0 && geschweift == 0 && round == 0) {
  87. fin += "\\" + x.Substring(i, 1);
  88. } else {
  89. fin += x.Substring(i, 1);
  90. }
  91. break;
  92. default:
  93. fin += x.Substring(i, 1);
  94. break;
  95. }
  96. }
  97. bIsEscaped = x.Substring(i, 1) == "\\";
  98. }
  99. this._pattern = new Regex(fin, RegexOptions.Singleline);
  100. }
  101. public object Instance {
  102. get {
  103. return this._instance;
  104. }
  105. }
  106. public Type Type {
  107. get {
  108. return this._t;
  109. }
  110. }
  111. public Route Route {
  112. get {
  113. return this._route;
  114. }
  115. }
  116. public MethodInfo Function {
  117. get {
  118. return this._method;
  119. }
  120. }
  121. public string Path {
  122. get {
  123. if (this._baseRoute == "") {
  124. return (this._route.Path.StartsWith("/") ? "" : "/") + this._route.Path;
  125. } else {
  126. return (this._baseRoute.StartsWith("/") ? "" : "/") + this._baseRoute + (!this._baseRoute.EndsWith("/") && !this._route.Path.StartsWith("/") ? "/" : "") + this._route.Path;
  127. }
  128. }
  129. }
  130. public Regex Pattern {
  131. get {
  132. return this._pattern;
  133. }
  134. }
  135. public HttpMethod Method {
  136. get {
  137. return this._route.Method;
  138. }
  139. }
  140. public string BaseRoute {
  141. get {
  142. return this._baseRoute;
  143. }
  144. }
  145. }
  146. }