public UriTreeNode Find(string[] path, HttpMethod httpMethod, Dictionary <string, object> param) { var cur = treeNodes; int i = 0; UriTreeNode node = null; if (path.Length > 0) { while (i < path.Length) { node = cur.Find(x => ((!x.IsParam && x.Name == path[i]) || (x.IsParam)) && (x.Method != null ? x.HttpMethod == httpMethod : x.Method == null && (x.HttpMethod == HttpMethod.none || x.HttpMethod == httpMethod))); if (node != null) { if (node.IsParam) { param.Add(node.Name, path[i]); } i++; cur = node.treeNodesChild; } else { return(null); } } } return(node); }
static void AddChildNodeRecursive(int depth, UriTreeNode node, UriTreeNode currentNode) { if (depth > 1) { AddChildNodeRecursive(depth - 1, node, currentNode.treeNodesChild[0]); } else { currentNode.treeNodesChild.Add(node); } }
public static UriTree Parse(string uriTemplate, HttpMethod httpMethod = HttpMethod.none, System.Reflection.MethodInfo methodInfo = null) { UriTree res = new UriTree(); var nodesStr = uriTemplate.Contains("?") ? uriTemplate.Remove(uriTemplate.IndexOf('?')).Split(new char[] { '/' }, StringSplitOptions.RemoveEmptyEntries) : uriTemplate.Split(new char[] { '/' }, StringSplitOptions.RemoveEmptyEntries); var firstNode = new UriTreeNode() { Name = nodesStr[0], HttpMethod = httpMethod }; if (nodesStr.Length == 1) { firstNode.Method = methodInfo; } res.treeNodes.Add(firstNode); for (int i = 1; i < nodesStr.Length; i++) { UriTreeNode node = new UriTreeNode() { HttpMethod = HttpMethod.none }; if (nodesStr[i][0] == '{' && nodesStr[i][nodesStr[i].Length - 1] == '}') { node.IsParam = true; node.Name = nodesStr[i].Substring(1, nodesStr[i].Length - 2); } else { node.Name = nodesStr[i]; } AddChildNodeRecursive(i, node, res.treeNodes[0]); //метод должен иметь лишь последний элемент в дереве по определенному пути if (i == nodesStr.Length - 1) { node.HttpMethod = httpMethod; node.Method = methodInfo; } } return(res); }
void InvokeWrapper(HttpListenerContext context, Dictionary <string, object> param, UriTreeNode node) { var p = node.Method.GetParameters(); object[] par = new object[p.Length]; for (int i = 0; i < p.Length; i++) { par[i] = param.ContainsKey(p[i].Name) ? Convert.ChangeType(param[p[i].Name], p[i].ParameterType) : (p[i].ParameterType.IsValueType ? Activator.CreateInstance(p[i].ParameterType) : (IsPostPutExistContent(node.HttpMethod, context.Request) ? DeserializeRequest(context.Request, node.Method, p[i].ParameterType) : null)); } try { var res = node.Method.Invoke(Activator.CreateInstance(node.Method.DeclaringType), par); object respObj = null; if (node.Method.ReturnType != typeof(void)) { respObj = new { status = 200, result = res }; } else { respObj = new { status = 200 }; } context.Response.ContentEncoding = Encoding.Default; var typeResp = node.Method.CustomAttributes. First(x => x.AttributeType == typeof(WebInvokeAttribute)).NamedArguments.FirstOrDefault(y => y.MemberName == "ResponseType"); SerializeResponse(context.Response, typeResp.MemberInfo != null ? (RequestResponseType)typeResp.TypedValue.Value : RequestResponseType.Json, respObj); context.Response.StatusCode = 200; } catch (Exception ex) { Console.WriteLine(ex.Message); context.Response.StatusCode = (int)HttpStatusCode.Forbidden; context.Response.Close(); } }