public static IHttpHandler GetHandlerForPathInfo(string httpMethod, string pathInfo, string requestPath, string filePath)
{
var appHost = HostContext.AppHost;
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.GetOperationName(), ResponseContentType = contentType };
var existingFile = pathParts[0].ToLower();
if (WebHostRootFileNames.Contains(existingFile))
{
var fileExt = System.IO.Path.GetExtension(filePath);
var isFileRequest = !string.IsNullOrEmpty(fileExt);
if (!isFileRequest && !HostAutoRedirectsDirs)
{
//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 = Host.Handlers.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 appHost.VirtualFileSources.DirectoryExists(pathInfo)
? StaticFilesHandler
: NotFoundHttpHandler;
}
return ShouldAllow(requestPath)
? StaticFilesHandler
: ForbiddenHttpHandler;
}
var handler = GetCatchAllHandlerIfAny(httpMethod, pathInfo, filePath);
if (handler != null) return handler;
if (appHost.Config.FallbackRestPath != null)
{
restPath = appHost.Config.FallbackRestPath(httpMethod, pathInfo, filePath);
if (restPath != null)
{
return new RestHandler { RestPath = restPath, RequestName = restPath.RequestType.GetOperationName(), ResponseContentType = contentType };
}
}
return null;
}