コード例 #1
0
        public static IServiceStackHandler GetHandlerForPathInfo(IHttpRequest httpReq, string filePath)
        {
            var appHost = HostContext.AppHost;

            var pathInfo   = httpReq.PathInfo;
            var httpMethod = httpReq.Verb;

            var pathParts = pathInfo.TrimStart('/').Split('/');

            if (pathParts.Length == 0)
            {
                return(NotFoundHttpHandler);
            }

            var restPath = RestHandler.FindMatchingRestPath(httpReq, out string contentType);

            if (restPath != null)
            {
                return new RestHandler {
                           RequestName = restPath.RequestType.GetOperationName()
                }
            }
            ;

            var catchAllHandler = GetCatchAllHandlerIfAny(httpMethod, pathInfo, filePath);

            if (catchAllHandler != null)
            {
                return(catchAllHandler);
            }

            var isFile      = httpReq.IsFile();
            var isDirectory = httpReq.IsDirectory();

            if (!isFile && !isDirectory && Env.IsMono)
            {
                isDirectory = StaticFileHandler.MonoDirectoryExists(filePath, filePath.Substring(0, filePath.Length - pathInfo.Length));
            }

            if (isFile || isDirectory)
            {
                //If pathInfo is for Directory try again with redirect including '/' suffix
                if (appHost.Config.RedirectDirectoriesToTrailingSlashes && isDirectory && !httpReq.OriginalPathInfo.EndsWith("/"))
                {
                    return new RedirectHttpHandler {
                               RelativeUrl = pathInfo + "/"
                    }
                }
                ;

                return(isDirectory || ShouldAllow(pathInfo)
                    ? StaticFilesHandler
                    : ForbiddenHttpHandler);
            }

            restPath = appHost.Config.FallbackRestPath?.Invoke(httpReq);
            if (restPath != null)
            {
                httpReq.SetRoute(restPath);
                return(new RestHandler {
                    RequestName = restPath.RequestType.GetOperationName()
                });
            }

            return(null);
        }
コード例 #2
0
        public static IHttpHandler GetHandlerForPathInfo(IHttpRequest httpReq, string filePath)
        {
            var appHost = HostContext.AppHost;

            var pathInfo   = httpReq.PathInfo;
            var httpMethod = httpReq.Verb;

            if (pathInfo.AsSpan().TrimStart('/').Length == 0)
            {
                return(NotFoundHttpHandler);
            }

            var restPath = RestHandler.FindMatchingRestPath(httpReq, out var contentType);

            if (restPath != null)
            {
                return new RestHandler {
                           RestPath = restPath, RequestName = restPath.RequestType.GetOperationName(), ResponseContentType = contentType
                }
            }
            ;

            var catchAllHandler = GetCatchAllHandlerIfAny(appHost, httpMethod, pathInfo, filePath);

            if (catchAllHandler != null)
            {
                return(catchAllHandler);
            }

            var isFile      = httpReq.IsFile();
            var isDirectory = httpReq.IsDirectory();

            if (!isFile && !isDirectory && Env.IsMono)
            {
                isDirectory = StaticFileHandler.MonoDirectoryExists(filePath, filePath.Substring(0, filePath.Length - pathInfo.Length));
            }

            if (isFile || isDirectory)
            {
                //If pathInfo is for Directory try again with redirect including '/' suffix
                if (appHost.Config.RedirectDirectoriesToTrailingSlashes && isDirectory && !httpReq.OriginalPathInfo.EndsWith("/"))
                {
                    return new RedirectHttpHandler {
                               RelativeUrl = pathInfo + "/"
                    }
                }
                ;

                if (isDirectory)
                {
                    return(StaticFilesHandler);
                }

                return(ShouldAllow(pathInfo)
                    ? StaticFilesHandler
                    : ForbiddenHttpHandler);
            }

            // Check for PagedBasedRouting before wildcard Fallback Service
            foreach (var httpHandlerResolver in appHost.FallbackHandlersArray)
            {
                var httpHandler = httpHandlerResolver(httpMethod, pathInfo, filePath);
                if (httpHandler != null)
                {
                    return(httpHandler);
                }
            }

            if (appHost.Config.FallbackRestPath != null)
            {
                restPath = appHost.Config.FallbackRestPath(httpReq);
                if (restPath != null)
                {
                    return new RestHandler {
                               RestPath = restPath, RequestName = restPath.RequestType.GetOperationName(), ResponseContentType = contentType
                    }
                }
                ;
            }

            return(null);
        }