public object ExecuteControllerAction(Dictionary <string, object> actionToCall) { bool methodNotAllowed = false; foreach (Route route in _routes) { string[] pathParts = actionToCall["Path"].ToString().Split('/'); string path = "/" + pathParts[1] + "/" + pathParts[2]; if (route.Path == path) { if (route.Method != actionToCall["Method"].ToString()) { methodNotAllowed = true; } else { methodNotAllowed = false; BaseController app = new BaseController(); DirectoryInfo di = new DirectoryInfo(PhysicalPath); FileInfo[] fis = di.GetFiles("*.dll"); foreach (FileInfo fls in fis) { try { var assembly = Assembly.LoadFrom(fls.FullName); foreach (var type in assembly.GetTypes()) { if (type != typeof(Mvc.BaseController) && typeof(Mvc.BaseController).IsAssignableFrom(type)) { app = (Mvc.BaseController)Activator.CreateInstance(type); app.HttpRequest = new Request( (string)actionToCall["Path"], (NameValueCollection)actionToCall["Parameters"], (Dictionary <string, HttpFile>)actionToCall["Files"], (NameValueCollection)actionToCall["Headers"], (NameValueCollection)actionToCall["QueryParams"] ); app.Route = route; app.HttpContext.PhysicalPath = PhysicalPath; app.HttpContext.ConfigurationManager = ConfigurationManager; if (app.GetType().Name == route.Controller) { MethodInfo method = app.GetType().GetMethod(route.Action); AuthorizeAttribute attribute = (AuthorizeAttribute)method.GetCustomAttribute(typeof(AuthorizeAttribute)); User user = null; if (attribute != null) { switch (ConfigurationManager.ApplicationAuthenticationDriver) { case "session": user = attribute.AuthorizeSession(Session.GetCurrentSession()); break; case "jwt": user = attribute.AuthorizeAuthToken(app.HttpRequest, ConfigurationManager.ApplicationSecretKey); Headers["Auth-Token"] = user.Token; break; default: throw new NotImplementedException("Not implemented for other authentication drivers"); } if (user != null) { app.SetUser(user); IActionResult result = (IActionResult)method.Invoke(app, new object[] { }); for (int i = 0; i < app.HttpContext.Headers.Count; i++) { Headers.Set( app.HttpContext.Headers.Keys[i], app.HttpContext.Headers[i] ); } return(result); } return((int)401); } else { IActionResult result = (IActionResult)method.Invoke(app, new object[] { }); for (int i = 0; i < app.HttpContext.Headers.Count; i++) { Headers.Set( app.HttpContext.Headers.Keys[i], app.HttpContext.Headers[i] ); } return(result); } } } } } catch (Exception e) { Console.WriteLine(e.StackTrace); continue; } } } } } if (methodNotAllowed) { return((int)405); } return((int)404); }
/// <summary> /// Execute action of Request. /// </summary> /// <param name="ActionRequest">Dictionary: Request action to execute.</param> /// <returns>Object: Action with to precessed.</returns> public object ExecuteAction(Dictionary <string, object> RequestAction) { AResult result; if (Routes == null) { ///TODO: resolver esto. return("500"); } else { foreach (Routes route in Routes) { if (route.UrlPath == RequestAction["URLPath"].ToString()) { /// TODO: si no hay metodo definido, aceptarlocomo si fuera un Get por defecto //if (route.ControllerName != RequestAction["HttpMethod"].ToString()) // { // return "500"; // } // else { ControllerBase baseController = new ControllerBase(); DirectoryInfo directoryInfo = new DirectoryInfo(AppSite.physicalPath); FileInfo[] fileInfo = directoryInfo.GetFiles("*.dll"); foreach (FileInfo fi in fileInfo) { Assembly assembly = Assembly.LoadFrom(fi.FullName); foreach (var type in assembly.GetTypes()) { if (type != typeof(ControllerBase) && typeof(ControllerBase).IsAssignableFrom(type)) { baseController = (ControllerBase)Activator.CreateInstance(type); //for test. string urlPath = (string)RequestAction["URLPath"]; string mehod = (string)RequestAction["HttpMethod"]; NameValueCollection header = (NameValueCollection)RequestAction["Header"]; NameValueCollection parameters = (NameValueCollection)RequestAction["Params"]; Dictionary <string, HttpFile> files = (Dictionary <string, HttpFile>)RequestAction["Files"]; NameValueCollection queryStrings = (NameValueCollection)RequestAction["QueryString"]; baseController.Request = new Request(urlPath, mehod, header, parameters, files, queryStrings); baseController.route = route; baseController.httpContext.Site.physicalPath = AppSite.physicalPath; if (baseController.GetType().Name == route._controller) { MethodInfo method = baseController.GetType().GetMethod(route._action); AuthorizeAttribute attribute = (AuthorizeAttribute)method.GetCustomAttribute(typeof(AuthorizeAttribute)); User user = null; if (attribute != null) { if (!attribute.IsAuthorized(baseController.Request)) { Console.WriteLine("user no authorized.."); /// TODO: deberia de cargar la configuracion del context /// la configuracion del usuario que crea la app. ServerConfiguration conf = new ServerConfiguration(); conf.Load(); } else { result = (AResult)method.Invoke(baseController, new object[] { }); Headers = baseController.httpContext.Headers; return(result); } } result = (AResult)method.Invoke(baseController, new object[] { }); Headers = baseController.httpContext.Headers; return(result); } } } } // } } } } return(null); }