Exemplo n.º 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);
        }
Exemplo n.º 2
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);
            }
        }