public bool TryRespond(IHttpContext context, bool endResponse = false) { try { if (Etags.CheckEtags(context)) { return(true); } if (!IsInitialized) { Initialize(); } IRequest request = context.Request; IResponse response = context.Response; Session.Init(context); SecureSession.Init(context); bool handled = false; string path = request.Url.AbsolutePath; string commonPath = Path.Combine("/common", path.TruncateFront(1)); byte[] content = new byte[] { }; string appName = ResolveApplicationName(context); string[] checkedPaths = new string[] { }; if (AppContentResponders.ContainsKey(appName)) { handled = AppContentResponders[appName].TryRespond(context, out checkedPaths); } if (!handled && !ShouldIgnore(path)) { bool exists; exists = ServerRoot.FileExists(path, out string absoluteFileSystemPath); if (!exists) { exists = ServerRoot.FileExists(commonPath, out absoluteFileSystemPath); } if (exists) { string ext = Path.GetExtension(absoluteFileSystemPath); if (FileCachesByExtension.ContainsKey(ext)) { FileCache cache = FileCachesByExtension[ext]; if (ShouldZip(request)) { SetGzipContentEncodingHeader(response); content = cache.GetZippedContent(absoluteFileSystemPath); } else { content = cache.GetContent(absoluteFileSystemPath); } handled = true; Etags.SetLastModified(response, request.Url.ToString(), new FileInfo(absoluteFileSystemPath).LastWriteTime); } } if (handled) { SetContentType(response, path); Etags.Set(response, request.Url.ToString(), content); SendResponse(response, content); OnResponded(context); } else { LogContentNotFound(path, appName, checkedPaths); OnNotResponded(context); } } if (!handled && endResponse) { SendResponse(response, "Not Found", 404); } return(handled); } catch (Exception ex) { Logger.AddEntry("An error occurred in {0}.{1}: {2}", ex, this.GetType().Name, MethodBase.GetCurrentMethod().Name, ex.Message); OnNotResponded(context); return(false); } }
public bool TryRespond(IHttpContext context, out string[] checkedPaths) { checkedPaths = new string[] { }; try { if (Etags.CheckEtags(context)) { return(true); } IRequest request = context.Request; IResponse response = context.Response; string path = request.Url.AbsolutePath; string ext = Path.GetExtension(path); string[] split = path.DelimitSplit("/"); byte[] content = new byte[] { }; bool handled = AllRequestHandler.HandleRequest(context, out content); if (!handled) { if (ContentHandlers.ContainsKey(path.ToLowerInvariant())) { handled = ContentHandlers[path.ToLowerInvariant()].HandleRequest(context, out content); } else if (AppContentLocator.Locate(path, out string locatedPath, out checkedPaths)) { handled = true; string foundExt = Path.GetExtension(locatedPath); if (FileCachesByExtension.ContainsKey(foundExt)) { FileCache cache = FileCachesByExtension[ext]; if (ShouldZip(request)) { SetGzipContentEncodingHeader(response); content = cache.GetZippedContent(locatedPath); } else { content = cache.GetContent(locatedPath); } } else { content = File.ReadAllBytes(locatedPath); } Etags.SetLastModified(response, request.Url.ToString(), new FileInfo(locatedPath).LastWriteTime); } else if (string.IsNullOrEmpty(ext) && !ShouldIgnore(path) || (AppRoot.FileExists("~/pages{0}.html"._Format(path), out locatedPath))) { content = RenderLayout(response, path); handled = true; } } if (handled) { SetContentType(response, path); SetContentDisposition(response, path); Etags.Set(response, request.Url.ToString(), content); SetResponseHeaders(response, path); SendResponse(response, content); OnResponded(context); } else { OnNotResponded(context); } return(handled); }