Example #1
0
        public override bool TryRespond(IHttpContext context)
        {
            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 = AppConf.AppNameFromUri(request.Url, BamConf.AppConfigs);

            string[] checkedPaths = new string[] { };
            if (AppContentResponders.ContainsKey(appName))
            {
                handled = AppContentResponders[appName].TryRespond(context, out checkedPaths);
            }

            if (!handled && !ShouldIgnore(path))
            {
                string readFileFromPath;
                bool   exists;
                exists = ServerRoot.FileExists(path, out readFileFromPath);
                if (!exists)
                {
                    exists = ServerRoot.FileExists(commonPath, out readFileFromPath);
                }

                if (exists)
                {
                    string ext = Path.GetExtension(readFileFromPath);
                    if (FileCachesByExtension.ContainsKey(ext))
                    {
                        FileCache cache = FileCachesByExtension[ext];
                        if (ShouldZip(request))
                        {
                            SetGzipContentEncodingHeader(response);
                            content = cache.GetZippedContent(readFileFromPath);
                        }
                        else
                        {
                            content = cache.GetContent(readFileFromPath);
                        }
                        handled = true;
                    }
                }

                if (handled)
                {
                    SetContentType(response, path);
                    SendResponse(response, content);
                    OnResponded(context);
                }
                else
                {
                    LogContentNotFound(path, appName, checkedPaths);
                    OnNotResponded(context);
                }
            }

            return(handled);
        }
        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);
            }
Example #3
0
        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[] { };
            IRequest  request  = context.Request;
            IResponse response = context.Response;
            string    path     = request.Url.AbsolutePath;

            string ext = Path.GetExtension(path);

            path = RemoveBamAppsPrefix(path);

            string[] split   = path.DelimitSplit("/");
            byte[]   content = new byte[] { };
            bool     result  = false;

            string locatedPath;

            if (path.Equals("/upload", StringComparison.InvariantCultureIgnoreCase))
            {
                HandleUpload(context, HttpPostedFile.FromRequest(request));
                string query = request.Url.Query.Length > 0 ? request.Url.Query : string.Empty;
                if (query.StartsWith("?"))
                {
                    query = query.TruncateFront(1);
                }
                content = RenderLayout(response, path, query);
                result  = true;
            }
            else if (string.IsNullOrEmpty(ext) && !ShouldIgnore(path) ||
                     (AppRoot.FileExists("~/pages{0}.html"._Format(path), out locatedPath)))
            {
                content = RenderLayout(response, path);
                result  = true;
            }
            else if (AppContentLocator.Locate(path, out locatedPath, out checkedPaths))
            {
                result = 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);
                }
            }

            if (result)
            {
                SetContentType(response, path);
                SetContentDisposition(response, path);
                SendResponse(response, content);
                OnResponded(context);
            }
            else
            {
                OnNotResponded(context);
            }
            return(result);
        }