コード例 #1
0
ファイル: HttpProcessor.cs プロジェクト: val4o89/CSharpWeb
        private HttpResponse RouteRequest()
        {
            var routes = this.Routes
                         .Where(x => Regex.Match(Request.Url, x.UrlRegex).Success)
                         .ToList();

            if (!routes.Any())
            {
                return(HttpResponseBuilder.NotFound());
            }

            var route = routes.FirstOrDefault(x => x.Method == Request.Method);

            if (route == null)
            {
                return new HttpResponse()
                       {
                           StatusCode = ResponseStatusCode.MethodNotAllowed
                       }
            }
            ;

            #region FIleSystemHandler
            // extract the path if there is one
            //var match = Regex.Match(request.Url, route.UrlRegex);
            //if (match.Groups.Count > 1)
            //{
            //    request.Path = match.Groups[1].Value;
            //}
            //else
            //{
            //    request.Path = request.Url;
            //}
            #endregion


            // trigger the route handler...
            try
            {
                return(route.Callable(Request));
            }
            catch (Exception ex)
            {
                Console.WriteLine(ex.Message);
                Console.WriteLine(ex.StackTrace);
                return(HttpResponseBuilder.InternalServerError());
            }
        }
コード例 #2
0
        public HttpResponse Handle(HttpRequest request)
        {
            this.getParams = WebUtilities.RetrieveGetParameters(request.Url);

            this.postParams = WebUtilities.RetrievePostParameters(request.Content);

            this.requestMethod = request.Method.ToString();

            Tuple <string, string> controllerAndActionNames = this.GetControllerAndActionName(request.Url);

            if (controllerAndActionNames == null)
            {
                return(HttpResponseBuilder.NotFound());
            }

            this.controllerName = controllerAndActionNames.Item1;
            this.actionName     = controllerAndActionNames.Item2;

            MethodInfo method = this.GetControllerMethod(this.controllerName, this.actionName, this.requestMethod);

            if (method == null)
            {
                throw new NotSupportedException("Method not supported!");
            }

            this.SetMethodParameters(method);

            IInvocable actionResult = (IInvocable)this.GetControllerMethod(
                this.controllerName,
                this.actionName,
                this.requestMethod)
                                      .Invoke(
                this.GetController(this.GetControllerTypeName(this.controllerName)),
                this.methodParams);

            string content = actionResult.Invoke();

            HttpResponse response = new HttpResponse()
            {
                ContentAsUTF8 = content
            };

            return(response);
        }
コード例 #3
0
        public HttpResponse Handle(HttpRequest request)
        {
            this.Request = request;
            string urlPart;
            // extract the path if there is one

            var match = Regex.Match(request.Url, this.RouteUrlRegex);

            if (match.Groups.Count > 1)
            {
                urlPart = match.Groups[1].Value;
            }
            else
            {
                urlPart = request.Url;
            }
            //var urlPart = request.Path;

            urlPart = SanitarizePath(urlPart);

            // make sure the first part of the path is not "/" or "\"
            if (urlPart.Length > 0)
            {
                var firstChar = urlPart.ElementAt(0);
                if (firstChar == '/' || firstChar == '\\')
                {
                    urlPart = "." + urlPart;
                }
            }
            var localPath = Path.Combine(this.BasePath, urlPart);

            if (Directory.Exists(localPath))
            {
                return(this.HandleDirectory(localPath));
            }
            else if (File.Exists(localPath))
            {
                return(HandleFile(localPath));
            }
            else
            {
                return(HttpResponseBuilder.NotFound());
            }
        }
コード例 #4
0
        public HttpResponse Handle(HttpRequest request, string fileDirectory)
        {
            try
            {
                HttpFile     file     = this.ServeFile(request, fileDirectory);
                HttpResponse response = new HttpResponse
                {
                    Content = file.Content,
                    Header  = { ContentType = file.MimeType },
                };

                return(response);
            }
            catch (FileNotFoundException fnfe)
            {
                Console.WriteLine(fnfe.ToString());

                return(HttpResponseBuilder.NotFound());
            }
        }
コード例 #5
0
        private HttpResponse RouteRequest()
        {
            var routes = this.Routes
                         .Where(x => Regex.Match(Request.Url, x.UrlRegex).Success)
                         .ToList();


            if (!routes.Any())
            {
                return(HttpResponseBuilder.NotFound());
            }


            var route = routes.FirstOrDefault(x => x.Method == Request.Method);

            if (route == null)
            {
                return new HttpResponse()
                       {
                           StatusCode = ResponseStatusCode.MethodNotAllowed
                       }
            }
            ;


            // trigger the route handler...
            try
            {
                return(route.Callable(Request));
            }
            catch (Exception ex)
            {
                Console.WriteLine(ex.Message);
                Console.WriteLine(ex.StackTrace);
                return(HttpResponseBuilder.InternalServerError());
            }
        }