using System; using System.IO; using System.Net; using System.Net.Sockets; using System.Text; using System.Threading; using System.Collections; using System.Collections.Generic; using System.Reflection; using System.Linq; using System.Text.RegularExpressions; namespace System.Net.ApiService { public class RouteElements { private object _instance; private Route _route; private MethodInfo _method; private Type _t; private string _baseRoute; private Regex _pattern; public RouteElements(object instance, MethodInfo mi, Route r) { this._instance = instance; this._route = r; this._method = mi; this._t = instance.GetType(); this._baseRoute = ""; this.createPattern(); } public RouteElements(Type instance, MethodInfo mi, Route r) { this._t = instance; this._route = r; this._method = mi; this._baseRoute = ""; this.createPattern(); } public RouteElements(object instance, MethodInfo mi, Route r, string BaseRoute) { this._instance = instance; this._route = r; this._method = mi; this._t = instance.GetType(); this._baseRoute = BaseRoute; this.createPattern(); } public RouteElements(Type instance, MethodInfo mi, Route r, string BaseRoute) { this._t = instance; this._route = r; this._method = mi; this._baseRoute = BaseRoute; this.createPattern(); } private void createPattern() { string x = this.Path; string fin = ""; int round = 0; int eckig = 0; int geschweift = 0; bool bIsEscaped = false; for (int i = 0; i < x.Length; i++) { if (!bIsEscaped) { switch (x.Substring(i, 1)) { case "{": geschweift++; fin += "{"; break; case "}": geschweift--; fin += "}"; break; case "(": round++; fin += "("; break; case ")": round--; fin += ")"; break; case "[": eckig++; fin += "["; break; case "]": eckig--; fin += "]"; break; case ".": case "/": if (eckig == 0 && geschweift == 0 && round == 0) { fin += "\\" + x.Substring(i, 1); } else { fin += x.Substring(i, 1); } break; default: fin += x.Substring(i, 1); break; } } bIsEscaped = x.Substring(i, 1) == "\\"; } this._pattern = new Regex(fin, RegexOptions.Singleline); } public object Instance { get { return this._instance; } } public Type Type { get { return this._t; } } public Route Route { get { return this._route; } } public MethodInfo Function { get { return this._method; } } public string Path { get { if (this._baseRoute == "") { return (this._route.Path.StartsWith("/") ? "" : "/") + this._route.Path; } else { return (this._baseRoute.StartsWith("/") ? "" : "/") + this._baseRoute + (!this._baseRoute.EndsWith("/") && !this._route.Path.StartsWith("/") ? "/" : "") + this._route.Path; } } } public Regex Pattern { get { return this._pattern; } } public HttpMethod Method { get { return this._route.Method; } } public string BaseRoute { get { return this._baseRoute; } } } }