FindMatchingRestPath() public static method

public static FindMatchingRestPath ( string httpMethod, string pathInfo ) : IRestPath
httpMethod string
pathInfo string
return IRestPath
Example #1
0
        public static IHttpHandler GetHandlerForPathInfo(string httpMethod, string pathInfo, string requestPath, string filePath)
        {
            var pathParts = pathInfo.TrimStart('/').Split('/');

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

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

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

            var existingFile = pathParts[0].ToLower();

            if (WebHostRootFileNames.Contains(existingFile))
            {
                var fileExt = Path.GetExtension(filePath);

                var isFileRequest = !string.IsNullOrEmpty(fileExt);

                if (!isFileRequest && !AutoRedirectsDirs)
                {
                    //If pathInfo is for Directory try again with redirect including '/' suffix
                    if (!pathInfo.EndsWith("/"))
                    {
                        var appFilePath = filePath.Substring(0, filePath.Length - requestPath.Length);
                        var redirect    = Support.StaticFileHandler.DirectoryExists(filePath, appFilePath);
                        if (redirect)
                        {
                            return(new RedirectHttpHandler
                            {
                                RelativeUrl = pathInfo + "/",
                            });
                        }
                    }
                }

                //e.g. CatchAllHandler to Process Markdown files
                var catchAllHandler = GetCatchAllHandlerIfAny(httpMethod, pathInfo, filePath);
                if (catchAllHandler != null)
                {
                    return(catchAllHandler);
                }

                if (!isFileRequest)
                {
                    return(NotFoundHttpHandler);
                }

                return(ShouldAllow(requestPath) ? StaticFileHandler : ForbiddenHttpHandler);
            }

            return(GetCatchAllHandlerIfAny(httpMethod, pathInfo, filePath));
        }