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

            if (controller.Assembly == AssemblyInfo.Assembly)
            {
                foreach (object obj in controller.GetCustomAttributes(false))
                {
                    if (obj is PrimaryController)
                    {
                        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 PrimaryView)
                        {
                            this.primaryAction = method;
                        }
                        if (obj is ViewAlias)
                        {
                            this.actions.Add(((ViewAlias)obj).Alias, method);
                        }
                    }
                }
            }
        }
示例#2
0
        private void Start()
        {
            if (this.root.EndsWith(@"\", StringComparison.Ordinal))
            {
                this.root = StringHelper.TrimEnd(this.root);
            }

            if (!Directory.Exists(this.root))
            {
                Directory.CreateDirectory(this.root);
            }

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

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

            this.connectionWaitHandle = new AutoResetEvent(false);

            this.listenerLoopThread              = new Thread(this.ListenerLoop);
            this.listenerLoopThread.Name         = "HttpServer Listener";
            this.listenerLoopThread.IsBackground = true;
            this.listenerLoopThread.Start();
        }
示例#3
0
        public HttpController(Type controller)
        {
            this.type = controller;
            this.actions = new Dictionary<string, MethodInfo>();

            if (controller.Assembly == Assembly.GetEntryAssembly())
            {
                foreach (object obj in controller.GetCustomAttributes(false))
                {
                    if (obj is PrimaryController) 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 PrimaryView) this.primaryAction = method;
                        if (obj is ViewAlias) this.actions.Add(((ViewAlias)obj).Alias, method);
                    }
                }
            }
        }
示例#4
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);
        }
示例#5
0
        public HttpResponse(HttpRequest request)
        {
            this.request = request;
            const string PDF_IDENT = "-pdf";

            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.Path.ToLower().EndsWith(PDF_IDENT, StringComparison.CurrentCulture))
                {
                    string url = "http://localhost:" + request.Server.Port.ToString() + this.request.FullUrl;

                    url = url.Replace(this.request.Path, StringHelper.TrimEnd(this.request.Path, PDF_IDENT.Length));

                    this.content = new HttpContent(HtmlToPdfHelper.ReadPDF(url), "application/pdf");
                }
                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 HttpErrorContent)
                {
                    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)
            {
                Logger.Log(ex);
                this.content = ErrorController.Display(this.request, ex, HttpStatusCode.NOTFOUND);
                this.bytes   = this.ConstructResponse(HttpStatusCode.SERVERERROR);
            }
            catch (Exception ex)
            {
                Logger.Log(ex);
                this.content = ErrorController.Display(this.request, ex, HttpStatusCode.SERVERERROR);
                this.bytes   = this.ConstructResponse(HttpStatusCode.SERVERERROR);
            }
        }