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.VirtualPathProvider.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); }
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); } if (appHost.Config.FallbackRestPath != null) { restPath = appHost.Config.FallbackRestPath(httpReq); if (restPath != null) { return new RestHandler { RestPath = restPath, RequestName = restPath.RequestType.GetOperationName(), ResponseContentType = contentType } } ; } foreach (var httpHandlerResolver in appHost.FallbackHandlersArray) { var httpHandler = httpHandlerResolver(httpMethod, pathInfo, filePath); if (httpHandler != null) { return(httpHandler); } } return(null); }
public async Task <HotReloadPageResponse> Any(HotReloadPage request) { var pathInfo = request.Path ?? "/"; var page = Pages.GetPage(pathInfo); if (page == null) { var matchingRoute = RestHandler.FindMatchingRestPath( HttpMethods.Get, pathInfo, out var contentType); var feature = HostContext.AppHost.AssertPlugin <SharpPagesFeature>(); if (matchingRoute != null) { page = feature.GetViewPage(matchingRoute.RequestType.Name); if (page == null) { var responseType = HostContext.AppHost.Metadata.GetResponseTypeByRequest(matchingRoute.RequestType); page = feature.GetViewPage(responseType.Name); } } if (page == null) { page = feature.GetRoutingPage(pathInfo, out var args); } } if (page == null) { throw HttpError.NotFound("Page not found: " + request.Path); } if (!page.HasInit) { await page.Init(); } var startedAt = DateTime.UtcNow; var eTagTicks = string.IsNullOrEmpty(request.ETag) ? (long?)null : long.Parse(request.ETag); var maxLastModified = DateTime.MinValue; var shouldReload = false; while (DateTime.UtcNow - startedAt < LongPollDuration) { maxLastModified = Pages.GetLastModified(page); if (eTagTicks == null) { return new HotReloadPageResponse { ETag = maxLastModified.Ticks.ToString() } } ; shouldReload = maxLastModified.Ticks > eTagTicks; if (shouldReload) { await Task.Delay(ModifiedDelay); break; } await Task.Delay(CheckDelay); } return(new HotReloadPageResponse { Reload = shouldReload, ETag = maxLastModified.Ticks.ToString() }); } }
private void ProcessRequest(MessageContext context) { //NOTE: This method is called on a background thread and must be protected by an outer big-try catch var httpReq = new RequestWrapper(context.Request); var httpRes = new ResponseWrapper(); IServiceStackHttpHandler handler = null; string operationName, contentType; //var handler = ServiceStackHttpHandlerFactory.GetHandler(httpReq); var restPath = RestHandler.FindMatchingRestPath(httpReq.HttpMethod, httpReq.PathInfo, out contentType); if (restPath != null) { handler = new RestHandler { RestPath = restPath, RequestName = restPath.RequestType.Name }; httpReq.OperationName = operationName = ((RestHandler)handler).RestPath.RequestType.Name; } else { handler = new NotFoundHttpHandler(); var stream = httpRes.OutputStream; //Bug fix: reading the OutputStream property will cause it to be created if it's null httpReq.OperationName = operationName = null; } HttpResponsePacket resPacket = null; try { handler.ProcessRequest(httpReq, httpRes, operationName); resPacket = CreateResponsePacketFromWrapper(httpRes, subscriber); } catch (Exception exception) { //Send Exception details back to Queue resPacket = CreateResponsePacketFromException(exception); } finally { httpReq.InputStream.Close(); httpRes.Close(); } if (resPacket == null) { //TODO: Not good, Log this //TODO: derive exception from RestBus.Exceptions class resPacket = CreateResponsePacketFromException(new ApplicationException("Unable to get response")); } try { //TODO: Why can't the subscriber append the subscriber id itself from within sendresponse subscriber.SendResponse(context, resPacket); } catch { //TODO: Log SendResponse error } }