Esempio n. 1
0
        public HttpController(Type controller)
        {
            this.type    = controller;
            this.actions = new Dictionary <string, MethodInfo>();

            foreach (object obj in controller.GetCustomAttributes(false))
            {
                if (obj is IsPrimaryController)
                {
                    HttpController.primary = this;
                }
            }

            foreach (MethodInfo method in controller.GetMethods())
            {
                if (!method.IsSpecialName && method.IsStatic && method.IsPublic && method.ReturnType == typeof(HttpContent) && method.GetParameters().Length == 1 && method.GetParameters()[0].ParameterType == typeof(HttpRequest))
                {
                    this.actions.Add(method.Name, method);
                    foreach (object obj in method.GetCustomAttributes(false))
                    {
                        if (obj is IsPrimaryAction)
                        {
                            this.primaryAction = method;
                        }
                        if (obj is ViewAlias)
                        {
                            this.actions.Add(((ViewAlias)obj).Alias, method);
                        }
                    }
                }
            }
        }
Esempio n. 2
0
        public static HttpContent RequestAction(string controllerName, string actionName, HttpRequest request)
        {
            if (!controllers.ContainsKey(controllerName))
            {
                throw new FileNotFoundException("controller " + @"""" + controllerName + @"""" + " not found");
            }
            HttpController controller = controllers[controllerName];

            if (!controller.actions.ContainsKey(actionName))
            {
                throw new FileNotFoundException("action " + @"""" + actionName + @"""" + " not found in controller " + @"""" + controllerName + @"""");
            }
            MethodInfo action = controller.actions[actionName];

            object contentObject = action.Invoke(null, new object[] { request });

            return((HttpContent)contentObject);
        }
Esempio n. 3
0
        private void Start(List <Assembly> assemblies)
        {
            if (this.root != null && !Directory.Exists(this.root))
            {
                throw new DirectoryNotFoundException("root folder not found");
            }

            this.Connections = new List <HttpConnection>();
            this.Sessions    = new Dictionary <string, HttpSession>();

            this.Cache = new HttpCache(this);
            HttpController.LoadControllers(assemblies);
            HttpWebSocket.LoadControllers(assemblies);

            this.connectionWaitHandle = new AutoResetEvent(false);

            this.listenerLoopThread = new Thread(this.ListenerLoop)
            {
                Name         = "HttpServer Listener",
                IsBackground = true
            };
            this.listenerLoopThread.Start();
        }
Esempio n. 4
0
        public HttpResponse(HttpRequest request)
        {
            this.request = request;

            try
            {
                if (this.request.Path == @"")
                {
                    if (HttpController.Primary == null)
                    {
                        throw new ApplicationException("Primary Controller Not Defined");
                    }
                    if (HttpController.Primary.PrimaryAction == null)
                    {
                        throw new ApplicationException("Primary Action Not Defined");
                    }

                    this.content = HttpController.RequestPrimary(this.request);
                }
                else if (this.request.IsWebSocket && HttpWebSocket.WebSocketControllerExists(this.request))
                {
                    var type = HttpWebSocket.GetWebSocketController(this.request);
                    this.request.WebSocket = (HttpWebSocket)Activator.CreateInstance(type, this.request);
                }
                else if (HttpController.ActionExists(this.request))
                {
                    this.content = HttpController.RequestAction(this.request);
                }
                else
                {
                    this.content = HttpContent.Read(this.request.Server, this.request.Path);
                }

                if (this.content is HttpExceptionContent)
                {
                    this.bytes = this.ConstructResponse(HttpStatusCode.SERVERERROR);
                }
                else if (this.request.IsWebSocket)
                {
                    this.bytes = this.ConstructResponse(HttpStatusCode.SWITCHING_PROTOCOLS);
                }
                else if (this.content.ETag == this.request.ETag && !this.content.ParsingRequired)
                {
                    this.bytes = this.ConstructResponse(HttpStatusCode.NOT_MODIFIED);
                }
                else
                {
                    this.bytes = this.ConstructResponse(HttpStatusCode.OK);
                }
            }
            catch (FileNotFoundException ex)
            {
                this.content = new HttpExceptionContent(ex);
                this.bytes   = this.ConstructResponse(HttpStatusCode.SERVERERROR);
            }
            catch (Exception ex)
            {
                this.content = new HttpExceptionContent(ex);
                this.bytes   = this.ConstructResponse(HttpStatusCode.SERVERERROR);
            }
        }