Ejemplo n.º 1
0
        private static void RegisterDefaultRoute(ServerRoutingTable routingTable)
        {
            if (!routingTable.Contains(HttpRequestMethod.Get, "/") &&
                routingTable.Contains(HttpRequestMethod.Get, "/Home/Index"))
            {
                routingTable.Add(HttpRequestMethod.Get, "/", (request) =>
                                 routingTable.Get(HttpRequestMethod.Get, "/Home/Index")(request));

                Console.WriteLine($"Route registered: reuse /Home/Index => {HttpRequestMethod.Get} => /");
            }
        }
Ejemplo n.º 2
0
        private static void RegisterDefaultRoute(ServerRoutingTable routingTable)
        {
            if (!routingTable.Routes[HttpRequestMethod.Get].ContainsKey("/") &&
                routingTable.Routes[HttpRequestMethod.Get].ContainsKey("/Home/Index"))
            {
                routingTable.Routes[HttpRequestMethod.Get]["/"] = (request) =>
                                                                  routingTable.Routes[HttpRequestMethod.Get]["/Home/Index"](request);

                Console.WriteLine($"Route registered: reuse /Home/Index => {HttpRequestMethod.Get} => /");
            }
        }
Ejemplo n.º 3
0
        private static void RegisterStaticFiles(ServerRoutingTable routingTable, MvcFrameworkSettings settings)
        {
            var path  = settings.WwwrootPath;
            var files = Directory.GetFiles(path, "*", SearchOption.AllDirectories);

            if (!Directory.Exists(path))
            {
                return;
            }

            foreach (var file in files)
            {
                var url = file.Replace("\\", "/").Replace(settings.WwwrootPath, string.Empty);
                routingTable.Add(HttpRequestMethod.GET, url, (request) =>
                {
                    var content     = File.ReadAllBytes(file);
                    var contentType = "text/plain";

                    if (file.EndsWith(".css"))
                    {
                        contentType = "text/css";
                    }
                    else if (file.EndsWith(".js"))
                    {
                        contentType = "application/javascript";
                    }
                    else if (file.EndsWith(".bmp"))
                    {
                        contentType = "image/bmp";
                    }
                    else if (file.EndsWith(".png"))
                    {
                        contentType = "image/png";
                    }
                    else if (file.EndsWith(".jpg") || file.EndsWith(".jpeg"))
                    {
                        contentType = "image/jpeg";
                    }
                    else if (file.EndsWith(".ico"))
                    {
                        contentType = "image/x-icon";
                    }

                    var result = new TextResult(content, HttpResponseStatusCode.Ok, contentType);
                    return(result);
                });

                Console.WriteLine($"Content registered: {file} => {HttpRequestMethod.GET} => {url}");
            }
        }
Ejemplo n.º 4
0
        public void RegisterRoutes(
            ServerRoutingTable routingTable,
            IMvcApplication application,
            MvcFrameworkSettings settings,
            IServiceCollection serviceCollection)
        {
            //1. Register static files
            RegisterStaticFiles(routingTable, settings);

            //2. Register actions
            RegisterActions(routingTable, application, settings, serviceCollection);

            //3. Register /
            RegisterDefaultRoute(routingTable);
        }
Ejemplo n.º 5
0
        private static void RegisterStaticFiles(
            ServerRoutingTable routingTable,
            MvcFrameworkSettings settings)
        {
            var path  = settings.WwwrootPath;
            var files = Directory.GetFiles(path, "*", SearchOption.AllDirectories);

            foreach (var file in files)
            {
                var filePath = file.Replace("\\", "/").Replace(settings.WwwrootPath, String.Empty);
                routingTable.Routes[HttpRequestMethod.Get][filePath] = (request) =>
                {
                    var content     = System.IO.File.ReadAllText(file);
                    var contentType = "text/plain";

                    if (file.EndsWith(".css"))
                    {
                        contentType = "text/css";
                    }
                    else if (file.EndsWith(".js"))
                    {
                        contentType = "application/json";
                    }
                    else if (file.EndsWith(".png"))
                    {
                        contentType = "image/png";
                    }
                    else if (file.EndsWith(".jpg") || file.EndsWith(".jpeg"))
                    {
                        contentType = "image/jpeg";
                    }
                    else if (file.EndsWith(".bmp"))
                    {
                        contentType = "image/bmp";
                    }
                    else if (file.EndsWith(".ico"))
                    {
                        contentType = "image/x-icon ";
                    }

                    return(new TextResult(content, HttpResponseStatusCode.Ok, contentType));
                };
                Console.WriteLine($"Content registered:{file} => {HttpRequestMethod.Get} => {filePath}");
            }
        }
Ejemplo n.º 6
0
        private static void RegisterActions(ServerRoutingTable routingTable, IMvcApplication application, MvcFrameworkSettings settings, IServiceCollection serviceCollection)
        {
            var userCookieService = serviceCollection.CreateInstance <IUserCookieService>();
            var controllers       = application.GetType().Assembly.GetTypes()
                                    .Where(myType => myType.IsClass &&
                                           !myType.IsAbstract &&
                                           myType.IsSubclassOf(typeof(Controller)));

            foreach (var controller in controllers)
            {
                var getMethods = controller.GetMethods(BindingFlags.Public | BindingFlags.Instance | BindingFlags.DeclaredOnly);

                foreach (var methodInfo in getMethods)
                {
                    var httpAttribute = (HttpAttribute)methodInfo.GetCustomAttributes(true)
                                        .FirstOrDefault(ca => ca.GetType().IsSubclassOf(typeof(HttpAttribute)));

                    var    method = HttpRequestMethod.GET;
                    string path   = null;
                    if (httpAttribute != null)
                    {
                        method = httpAttribute.Method;
                        path   = httpAttribute.Path;
                    }

                    if (path == null)
                    {
                        var controllerName = controller.Name;
                        if (controllerName.EndsWith("Controller"))
                        {
                            controllerName = controllerName.Substring(0, controllerName.Length - "Controller".Length);
                        }

                        var actionName = methodInfo.Name;

                        path = $"/{controllerName.ToLower()}/{actionName.ToLower()}";
                    }
                    else if (!path.StartsWith("/"))
                    {
                        path = "/" + path;
                    }

                    var authorizeAttribute = methodInfo.GetCustomAttributes(true).FirstOrDefault(ca => ca.GetType() == typeof(AuthorizeAttribute)) as AuthorizeAttribute;

                    routingTable.Add(method, path,
                                     (request) =>
                    {
                        if (authorizeAttribute != null)
                        {
                            var userData = Controller.GetUserData(request.Cookies, userCookieService);
                            if (userData == null ||
                                !userData.IsLoggedIn ||
                                (authorizeAttribute.RoleName != null && authorizeAttribute.RoleName != userData.Role))
                            {
                                var response = new HttpResponse();
                                response.Headers.Add(new HttpHeader(HttpHeader.Location, settings.LoginPageUrl));
                                response.StatusCode = HttpResponseStatusCode.SeeOther;
                                return(response);
                            }
                        }
                        return(ExecuteAction(controller, methodInfo, request, serviceCollection));
                    });

                    Console.WriteLine($"Route registered: {controller.Name}.{methodInfo.Name} => {method} => {path}");
                }
            }
        }
Ejemplo n.º 7
0
        private static void RegisterActions(
            ServerRoutingTable routingTable,
            IMvcApplication application,
            MvcFrameworkSettings settings,
            IServiceCollection serviceCollection)
        {
            var userCookieService = serviceCollection.CreateInstance <IUserCookieService>();

            var controllers = application.GetType().Assembly.GetTypes()
                              .Where(t => t.IsClass &&
                                     !t.IsAbstract &&
                                     t.IsSubclassOf(typeof(Controller)));

            foreach (var controller in controllers)
            {
                var getMethods = controller.GetMethods(BindingFlags.Public | BindingFlags.Instance | BindingFlags.DeclaredOnly);

                foreach (var methodInfo in getMethods)
                {
                    var httpAttribute = (HttpAttribute)
                                        methodInfo
                                        .GetCustomAttributes(true)
                                        .FirstOrDefault(ca =>
                                                        ca.GetType().IsSubclassOf(typeof(HttpAttribute)));

                    var    method = HttpRequestMethod.Get;
                    string path   = null;

                    if (httpAttribute != null)
                    {
                        path   = httpAttribute.Path;
                        method = httpAttribute.Method;
                    }

                    if (path == null)
                    {
                        //If path is null => generate path from controller and action /ControllerName/ActionName
                        var controllerName = controller.Name;
                        if (controllerName.EndsWith("Controller"))
                        {
                            controllerName = controllerName.Substring(0, controllerName.Length - "Controller".Length);
                        }

                        var actionName = methodInfo.Name;

                        path = $"/{controllerName}/{actionName}";
                    }
                    else if (!path.StartsWith("/"))
                    {
                        path = "/" + path;
                    }

                    var hasAuthorizeAttribute = methodInfo
                                                .GetCustomAttributes(true)
                                                .Any(ca =>
                                                     ca.GetType() == typeof(AuthorizeAttribute));

                    routingTable.Add(method, path, (request) =>
                    {
                        // if (method has AuthorizeAttribute)
                        if (hasAuthorizeAttribute)
                        {
                            //get username Controller.GetUserData
                            var userData = Controller.GetUserData(request.Cookies, userCookieService);
                            // check if user is logged
                            if (userData == null)
                            {
                                //if not redirect to login page
                                var response = new HttpResponse();
                                response.Headers.Add(new HttpHeader(HttpHeader.Location, settings.LoginPageUrl));
                                response.StatusCode = HttpResponseStatusCode.SeeOther;
                                return(response);
                            }
                        }

                        return(ExecuteAction(controller, methodInfo, request, serviceCollection));
                    });

                    Console.WriteLine($"Route registered:{controller.Name} {methodInfo.Name} => {method} => {path}");
                }
            }
        }