public override void Write(byte[] buffer, int offset, int count)
            {
                if (m_Response.StatusCode != 200) // 比如301或302跳转的
                {
                    m_Filter.Write(buffer, offset, count);
                    return;
                }
                var item = RouteHelper.GetCurrentConfigRouteItem();

                if (item.ServerCacheExpiredMinutes > 0 && m_HasCacheItem == false) // 需要服务器端缓存,但还没有缓存
                {
                    var citem = m_HttpContext.Items["RouteCacheFilterAttribute.citem"] as CacheItem;
                    if (citem == null) // 这样做目的是为了确保Write方法可能被多次调用时不会出错
                    {
                        citem = new CacheItem {
                            Html = new List <byte>(count)
                        };
                        for (int i = offset; i < count; i++)
                        {
                            citem.Html.Add(buffer[i]);
                        }
                        string key = GenerateServerCacheKey(m_HttpContext);
                        m_HttpContext.Items["RouteCacheFilterAttribute.citem"] = citem;
                        Cache.SetLocalCache(key, citem, true, item.ServerCacheExpiredMinutes); // 此时将缓存数据放入全局Cache中,可以被其他访问请求看到和读取了
                    }
                    else
                    {
                        for (int i = offset; i < count; i++)
                        {
                            citem.Html.Add(buffer[i]);
                        }
                    }
                }
                m_Filter.Write(buffer, offset, count);
            }
Esempio n. 2
0
        protected override bool CheckAuthentication()
        {
            var  routeItem = RouteHelper.GetCurrentConfigRouteItem();
            bool need      = routeItem == null ? false : routeItem.NeedLogin;

            if (need == false)
            {
                return(true);
            }
            return(base.CheckAuthentication());
        }
            public override void Write(byte[] buffer, int offset, int count)
            {
                if (m_Response.StatusCode != 200) // 比如301或302跳转的
                {
                    m_Filter.Write(buffer, offset, count);
                    return;
                }
                string clientToken = m_Request.Headers["If-None-Match"];

                if (m_ETag != clientToken)
                {
                    var item = RouteHelper.GetCurrentConfigRouteItem();
                    if (item.ServerCacheExpiredMinutes > 0 && m_HasCacheItem == false) // 需要服务器端缓存,但还没有缓存
                    {
                        var citem = m_HttpContext.Items["RouteCacheFilterAttribute.citem"] as CacheItem;
                        if (citem == null) // 这样做目的是为了确保Write方法可能被多次调用时不会出错
                        {
                            citem = new CacheItem {
                                Html = new List <byte>(count)
                            };
                            for (int i = offset; i < count; i++)
                            {
                                citem.Html.Add(buffer[i]);
                            }
                            string key = GenerateServerCacheKey(m_HttpContext);
                            m_HttpContext.Items["RouteCacheFilterAttribute.citem"] = citem;
                            Cache.SetLocalCache(key, citem, true, item.ServerCacheExpiredMinutes); // 此时将缓存数据放入全局Cache中,可以被其他访问请求看到和读取了
                        }
                        else
                        {
                            for (int i = offset; i < count; i++)
                            {
                                citem.Html.Add(buffer[i]);
                            }
                        }
                    }
                    m_Response.AddHeader("ETag", m_ETag);
                    m_Filter.Write(buffer, offset, count);
                }
                else
                {
                    m_Response.SuppressContent   = true;
                    m_Response.StatusCode        = 304;
                    m_Response.StatusDescription = "Not Modified";
                    //m_Response.AddHeader("Content-Length", "0");
                }
            }
        public override void OnResultExecuting(ResultExecutingContext filterContext)
        {
            var       item  = RouteHelper.GetCurrentConfigRouteItem();
            CacheItem citem = null;

            if (item.ServerCacheExpiredMinutes > 0) // 需要服务端缓存
            {
                citem = filterContext.HttpContext.Items["RouteCacheFilterAttribute.citem"] as CacheItem;
            }
            if (item == null || item.ClientCache == ClientCacheMode.Default)
            {
                filterContext.HttpContext.Response.Filter = new ServerCacheFilter(filterContext.HttpContext, item.ServerCacheExpiredMinutes, citem != null);
                return;
            }
            if (item.ClientCache == ClientCacheMode.None)
            {
                // 让每一次的etag都不一样,防止iis级别上的Etag缓存设置
                filterContext.HttpContext.Response.AddHeader("ETag", Guid.NewGuid().ToString());
                // abandon cache
                filterContext.HttpContext.Response.Expires         = -1;
                filterContext.HttpContext.Response.ExpiresAbsolute = DateTime.Now.AddSeconds(-1);
                filterContext.HttpContext.Response.CacheControl    = "no-cache";
                filterContext.HttpContext.Response.Cache.SetExpires(DateTime.Now.AddSeconds(-1));
                filterContext.HttpContext.Response.Cache.SetCacheability(HttpCacheability.NoCache);
                filterContext.HttpContext.Response.Cache.SetNoStore();
                filterContext.HttpContext.Response.Filter = new ServerCacheFilter(filterContext.HttpContext, item.ServerCacheExpiredMinutes, citem != null);
            }
            else if (item.ClientCache == ClientCacheMode.Permanent304)
            {
                string etag = item.ETag;
                if (string.IsNullOrWhiteSpace(etag))
                {
                    etag = s_ETag;
                }
                filterContext.HttpContext.Response.Filter = new PermanentNotModifiedFilter(filterContext.HttpContext,
                                                                                           etag, item.ServerCacheExpiredMinutes, citem != null);
            }
            else if (item.ClientCache == ClientCacheMode.Unchanged304)
            {
                filterContext.HttpContext.Response.Filter = new ContentNotModifiedFilter(filterContext.HttpContext, item.ServerCacheExpiredMinutes, citem != null);
            }
            else
            {
                filterContext.HttpContext.Response.Filter = new ServerCacheFilter(filterContext.HttpContext, item.ServerCacheExpiredMinutes, citem != null);
            }
        }
        public override void OnActionExecuting(ActionExecutingContext filterContext)
        {
            var item = RouteHelper.GetCurrentConfigRouteItem();

            if (item.ServerCacheExpiredMinutes > 0)
            {
                string    key   = GenerateServerCacheKey(filterContext.HttpContext);
                CacheItem citem = Cache.GetLocalCache(key) as CacheItem;
                if (citem != null)
                {
                    string c = "1";
                    using (var stream = new MemoryStream(citem.Html.ToArray()))
                    {
                        using (var sr = new StreamReader(stream))
                        {
                            c = sr.ReadToEnd();
                        }
                    }
                    filterContext.HttpContext.Items["RouteCacheFilterAttribute.citem"]    = citem;
                    filterContext.HttpContext.Items["RouteCacheFilterAttribute.FindItem"] = "1";
                    filterContext.Result = new ContentResult()
                    {
                        Content = c
                    };                                                          // 避免再执行Action和View的Render
                    return;
                }
            }
            if (item.ClientCache == ClientCacheMode.Permanent304)
            {
                string etag = item.ETag;
                if (string.IsNullOrWhiteSpace(etag))
                {
                    etag = s_ETag;
                }
                string clientToken = filterContext.HttpContext.Request.Headers["If-None-Match"];
                if (etag == clientToken)
                {
                    filterContext.Result = new ContentResult()
                    {
                        Content = "1"
                    };                                                          // 避免再执行Action和View的Render
                    return;
                }
            }
            base.OnActionExecuting(filterContext);
        }
Esempio n. 6
0
        protected string GetConfiguredSchema()
        {
            var    config = RouteHelper.GetCurrentConfigRouteItem();
            string domain = config == null ? string.Empty : config.Domain;
            object tmp;

            if (string.IsNullOrWhiteSpace(domain) &&
                RouteHelper.TryGetDataTokenOfCurrentRoute("_ibb_domain", out tmp) && tmp != null)
            {
                domain = tmp.ToString();
            }
            if (string.IsNullOrWhiteSpace(domain))
            {
                return("http");
            }
            if (domain.StartsWith("https://", StringComparison.InvariantCultureIgnoreCase))
            {
                return("https");
            }
            return("http");
        }
        protected override bool CheckNeedTrace(HttpContextBase httpContext)
        {
            var item = RouteHelper.GetCurrentConfigRouteItem();

            return(base.CheckNeedTrace(httpContext) && (item == null || item.Trace));
        }