private List <object> ExtractParameters(MethodInfo method, RESTPath path, string requestPath, HttpRequest request) { var parameters = new List <object>(); var methodParams = method.GetParameters(); var requestParams = path.PathReMatch.Matches(requestPath)[0]; foreach (var param in methodParams) { if (param.GetCustomAttribute(typeof(JsonBody)) != null) { dynamic jsonObj = JsonConvert.DeserializeObject(request.Content); parameters.Add(jsonObj); } else if (param.GetCustomAttribute(typeof(Body)) != null) { parameters.Add(request.Content); } else { parameters.Add(requestParams.Groups[param.Name].Value); } } return(parameters); }
public async Task <HttpResponse> Handle(HttpRequest request) { var url = request.Path; var httpMethod = request.Method; foreach (var route in RoutingMethods) { var requestPath = url.AbsolutePath; var routingMethod = ((HttpRequestMethod)route.GetCustomAttribute(typeof(HttpRequestMethod)))?.Method ?? HttpMethod.Get; var routingPath = RESTPath.Combine(Prefix, ((Route)route.GetCustomAttribute(typeof(Route))).Path); bool sameMethod = String.Equals(routingMethod.Method, httpMethod.Method); bool matchingUrl = routingPath.Matches(requestPath); if (sameMethod && matchingUrl) { var method = route; var parameters = ExtractParameters(method, routingPath, request); if (method.GetCustomAttribute(typeof(AsyncStateMachineAttribute)) != null) { return(await(Task <HttpResponse>) method.Invoke(this, parameters.ToArray())); } else { return((HttpResponse)method.Invoke(this, parameters.ToArray())); } } } return(NotFound($"Couldn't find a fitting method on the on matched controller '{ GetType().Name }' for path '{ url }'")); }
private List <object> ExtractParameters(MethodInfo method, RESTPath path, HttpRequest request) { var parameters = new List <object>(); var methodParams = method.GetParameters(); var requestSegments = request.Path.AbsolutePath.Split(new char[] { '/' }, StringSplitOptions.RemoveEmptyEntries); foreach (var param in methodParams) { if (param.GetCustomAttribute(typeof(Body)) != null) { parameters.Add(request.Content); } else { parameters.Add( requestSegments[path.Parameters.Single(p => p.Value.Equals(param.Name)).Key]); } } return(parameters); }
public async Task <HttpResponseBase> Handle(HttpRequest request) { var url = request.Path; var httpMethod = request.Method; var requestPath = Uri.UnescapeDataString(url.AbsolutePath); foreach (var method in PreMethods) { var parameters = new object[] { request }; // Debug.WriteLine($"{requestPath}: Calling pre-execution method ({method.Name})"); if (method.GetCustomAttribute(typeof(AsyncStateMachineAttribute)) != null) { await(Task) method.Invoke(this, parameters); } else { method.Invoke(this, parameters); } } try { foreach (var route in RoutingMethods) { var routingMethod = ((HttpRequestMethod)route.GetCustomAttribute(typeof(HttpRequestMethod)))?.Method ?? HttpMethod.Get; var routingPath = RESTPath.Combine(Prefix, ((Route)route.GetCustomAttribute(typeof(Route))).Path); bool sameMethod = String.Equals(routingMethod.Method, httpMethod.Method); bool matchingUrl = routingPath.PathReMatch.IsMatch(requestPath); if (sameMethod && matchingUrl) { var method = route; var parameters = ExtractParameters(method, routingPath, requestPath, request); Debug.WriteLine($"{requestPath}: Calling handler method ({method.Name})"); if (method.GetCustomAttribute(typeof(AsyncStateMachineAttribute)) != null) { return(await(Task <HttpResponseBase>) method.Invoke(this, parameters.ToArray())); } else { return((HttpResponseBase)method.Invoke(this, parameters.ToArray())); } } } foreach (var route in DefaultMethods) { var routingMethod = ((HttpRequestMethod)route.GetCustomAttribute(typeof(HttpRequestMethod)))?.Method ?? HttpMethod.Get; bool sameMethod = String.Equals(routingMethod.Method, httpMethod.Method); if (sameMethod) { var method = route; // Debug.WriteLine($"{requestPath}: Calling default handler method ({method.Name})"); if (method.GetCustomAttribute(typeof(AsyncStateMachineAttribute)) != null) { return(await(Task <HttpResponseBase>) method.Invoke(this, new[] { requestPath })); } else { return((HttpResponseBase)method.Invoke(this, new[] { requestPath })); } } } } finally { foreach (var method in PostMethods) { var parameters = new object[] { request }; // Debug.WriteLine($"{requestPath}: Calling post-execution method ({method.Name})"); if (method.GetCustomAttribute(typeof(AsyncStateMachineAttribute)) != null) { await(Task) method.Invoke(this, parameters); } else { method.Invoke(this, parameters); } } } return(NotFound($"Couldn't find a suitable method on controller '{GetType().Name}' for path '{url}'")); }