Ejemplo n.º 1
0
        public virtual ICacheStorage CreateStorage(WebPageContext context)
        {
            var value = context.GetConfigValue <string>("Page", "CacheStorage", string.Empty).ToLower();

            if (!string.IsNullOrEmpty(value))
            {
                return(CacheStorageFactory.Create(value));                              //通过存储器名称获取
            }
            //通过后缀名获取
            ICacheStorage storage = null;

            if (_storages.TryGetValue(context.PathExtension, out storage))
            {
                return(storage);
            }

            return(StorageEmpty.Instance);
        }
Ejemplo n.º 2
0
        public void Send(WebPageContext context, object result)
        {
            DTObject dto = result as DTObject;

            if (dto != null)
            {
                context.Response.Write(dto.GetCode(false, false));
                return;
            }
            var code = result as string;

            if (code != null)
            {
                context.Response.Write(code);
                return;
            }
            throw new DTOSenderException("DTOSender对象仅能发送 DTObject 或者 string 类型的对象!当前被发送的对象类型为:" + result.GetType().FullName);
        }
Ejemplo n.º 3
0
        /// <summary>
        /// 压缩流
        /// </summary>
        /// <param name="source">需要压缩的流</param>
        /// <param name="target">需要写入压缩内容的目标流</param>
        public void Compress(WebPageContext context, Stream source, Stream target)
        {
            var compressionType = context.CompressionType;

            int bufferSize = 8000;

            Stream compressStream = null;

            try
            {
                if (compressionType == HttpCompressionType.GZip)
                {
                    compressStream = new GZipStream(target, CompressionMode.Compress, true);
                }
                else if (compressionType == HttpCompressionType.Deflate)
                {
                    compressStream = new DeflateStream(target, CompressionMode.Compress, true);
                }
                else
                {
                    compressStream = target;
                }

                byte[] buffer = new byte[bufferSize];
                int    retval = source.ReadPro(buffer, 0, bufferSize);
                while (retval == bufferSize)
                {
                    compressStream.Write(buffer, 0, bufferSize);
                    compressStream.Flush();
                    retval = source.ReadPro(buffer, 0, bufferSize);
                }
                // 写入剩余的字节。
                compressStream.Write(buffer, 0, retval);
                compressStream.Close();//一定要close才有效,否则gzip的字节数出错
            }
            catch (Exception ex)
            {
                if (compressStream != null)
                {
                    compressStream.Close();
                }
                throw ex;
            }
        }
Ejemplo n.º 4
0
        public static IServerCache Create(WebPageContext context)
        {
            IServerCache serverCache = null;
            var          config      = WebPagesConfiguration.Global.PageConfig;

            if (config == null || config.ServerCacheFactory == null)
            {
                serverCache = InjectionServerCacheFactory.Instance.Create(context);
            }
            else
            {
                IServerCacheFactory factory = config.ServerCacheFactory.GetInstance <IServerCacheFactory>();
                serverCache = factory.Create(context);
                if (serverCache == null)
                {
                    serverCache = NonServerCache.Instance;                      //配置文件没有设置的就由系统自动设置
                }
            }
            return(serverCache);
        }
Ejemplo n.º 5
0
        public virtual object[] ParseArguments(WebPageContext context)
        {
            HttpRequest request = context.Request;

            string code = null;

            using (StreamReader reader = new StreamReader(request.InputStream))
            {
                code = reader.ReadToEnd();
            }

            if (IsJSON(code))
            {
                return new object[] { DTObject.Create(code) }
            }
            ;
            else
            {
                return(FormExtractor.Instance.ParseArguments(context));
            }
        }
Ejemplo n.º 6
0
        private static void ProcessGET(WebPageContext context, Exception ex, int statusCode)
        {
            context.Response.StatusCode = statusCode;
            var msg = ex.GetCompleteInfo();

            if (context.IsErrorPage)
            {
                //如果错误页本身发生错误
                context.Response.Write(msg);
            }
            else
            {
#if (DEBUG)
                context.Response.Write(msg);
#endif

#if (!DEBUG)
                context.Redirect(string.Format("/error.htm?error={0}", HttpUtility.UrlEncode(msg)));
#endif
            }
        }
Ejemplo n.º 7
0
        public static ICompressor Create(WebPageContext context)
        {
            ICompressor cpor   = null;
            var         config = WebPagesConfiguration.Global.PageConfig;

            if (config == null || config.CompressorFactory == null)
            {
                //没有配置文件设置,那么查看程序集级别的注入
                cpor = InjectionCompressorFactory.Instance.Create(context);
            }
            else
            {
                var factory = config.CompressorFactory.GetInstance <ICompressorFactory>();
                cpor = factory.Create(context);
                if (cpor == null)
                {
                    cpor = InjectionCompressorFactory.Instance.Create(context);               //配置文件没有设置的就由系统自动设置
                }
            }
            return(cpor);
        }
        public static IClientCache Create(WebPageContext context)
        {
            IClientCache clientCache = null;
            var          config      = WebPagesConfiguration.Global.PageConfig;

            if (config == null || config.ClientCacheFactory == null)
            {
                //没有配置文件设置,那么查看程序集级别的注入
                clientCache = InjectionClientCacheFactory.Instance.Create(context);
            }
            else
            {
                IClientCacheFactory factory = config.ClientCacheFactory.GetInstance <IClientCacheFactory>();
                clientCache = factory.Create(context);
                if (clientCache == null)
                {
                    clientCache = InjectionClientCacheFactory.Instance.Create(context);                      //配置文件没有设置的就由系统自动设置
                }
            }
            return(clientCache);
        }
        private static WebPage CreatePage(WebPageContext context)
        {
            IWebPageLocator locator = WebPageLocatorFactory.CreateLocator(context.PathExtension);
            IHttpHandler    handler = locator.GetHandler(context.VirtualPath);//利用资源定位器得到资源的Handler类型

            if (handler == null)
            {
                if (!context.IsValidPath())
                {
                    //如果既没有后台文件,也没有前台文件,那么资源不存在
                    throw new HttpException(404, string.Format(Strings.RequestResourceNotExist, context.VirtualPath));
                }
                handler = locator.GetDefaultHandler(); //如果有前台文件那么用默认的处理
            }
            WebPage page = handler as WebPage;

            if (page == null)
            {
                throw new TypeMismatchException(handler.GetType(), typeof(WebPage));
            }
            return(page);
        }
Ejemplo n.º 10
0
        private static HttpCompressionType GetCompressionMode(WebPageContext context)
        {
            var    request        = context.Request;
            string acceptEncoding = request.Headers["Accept-Encoding"];

            if (string.IsNullOrEmpty(acceptEncoding))
            {
                return(HttpCompressionType.None);
            }
            acceptEncoding = acceptEncoding.ToUpperInvariant();
            if (acceptEncoding.Contains("GZIP"))
            {
                return(HttpCompressionType.GZip);
            }
            else if (acceptEncoding.Contains("DEFLATE"))
            {
                return(HttpCompressionType.Deflate);
            }
            else
            {
                return(HttpCompressionType.None);
            }
        }
Ejemplo n.º 11
0
        public ICompressor Create(WebPageContext context)
        {
            var value = context.GetConfigValue <string>("Page", "compressor", string.Empty).ToLower();

            if (!string.IsNullOrEmpty(value))
            {
                switch (value)
                {
                case "gzip": return(HttpCompressor.Instance);

                default: return(NonCompressor.Instance);
                }
            }

            //如果没有页面级配置,那么根据扩展名查看配置
            ICompressor compressor = null;

            if (_compressors.TryGetValue(context.PathExtension, out compressor))
            {
                return(compressor);
            }

            return(NonCompressor.Instance);
        }
Ejemplo n.º 12
0
 public void SetEncoding(WebPageContext context)
 {
 }
Ejemplo n.º 13
0
 public bool IsAccepted(WebPageContext context)
 {
     return(false);
 }
 public IHttpHandler CreateHandler(WebPageContext context)
 {
     return(CreateFromVirtualPath(context, context.VirtualPath));
 }
Ejemplo n.º 15
0
 /// <summary>
 /// 判断浏览器是否支持压缩模式
 /// </summary>
 /// <param name="context"></param>
 /// <returns></returns>
 public bool IsAccepted(WebPageContext context)
 {
     return(context.CompressionType != HttpCompressionType.None);
 }
Ejemplo n.º 16
0
 public void Write(WebPageContext context, Stream content)
 {
     WriteContent(context, content);
     UpdateCache(context, content);
 }
Ejemplo n.º 17
0
 public abstract bool IsExpired(WebPageContext context, ICacheStorage storage);
Ejemplo n.º 18
0
 private int GetCacheMinutes(WebPageContext context)
 {
     return(context.GetConfigValue <int>("Page", "delay", 0));
 }
Ejemplo n.º 19
0
 internal ResolveRequestCache(WebPageContext context)
 {
     _context = context;
 }
Ejemplo n.º 20
0
 public abstract void SetCache(WebPageContext context);
Ejemplo n.º 21
0
 public abstract bool IsExpired(WebPageContext context);
Ejemplo n.º 22
0
 /// <summary>
 /// 向缓存区中写入信息
 /// </summary>
 /// <param name="content"></param>
 public abstract void Write(WebPageContext context, Stream content, ICacheStorage storage);
Ejemplo n.º 23
0
 /// <summary>
 /// 读取缓存区中的流信息
 /// </summary>
 /// <returns></returns>
 public abstract Stream Read(WebPageContext context, ICacheStorage storage);
Ejemplo n.º 24
0
 public void Compress(WebPageContext context, Stream source, Stream target)
 {
 }
Ejemplo n.º 25
0
        /// <summary>
        /// 读取缓存区中的流信息
        /// </summary>
        /// <returns></returns>
        public override Stream Read(WebPageContext context, ICacheStorage storage)
        {
            var variable = new CacheVariable(GetUrl(context), context.CompressionType, context.Device);

            return(storage.Read(variable));
        }
Ejemplo n.º 26
0
 public override void SetCache(WebPageContext context)
 {
     SetClientCache(context.Response, 525600);
 }
Ejemplo n.º 27
0
        protected bool TryGetCacheTime(WebPageContext context, out DateTime cacheTime, ICacheStorage storage)
        {
            var variable = new CacheVariable(GetUrl(context), context.CompressionType, context.Device);

            return(storage.TryGetLastModified(variable, out cacheTime));
        }
Ejemplo n.º 28
0
 protected virtual string GetUrl(WebPageContext context)
 {
     return(context.Request.Url.AbsoluteUri);
 }
Ejemplo n.º 29
0
        /// <summary>
        /// 向缓存区中写入信息
        /// </summary>
        /// <param name="content"></param>
        public override void Write(WebPageContext context, Stream content, ICacheStorage storage)
        {
            var variable = new CacheVariable(GetUrl(context), context.CompressionType, context.Device);

            storage.Update(variable, content);
        }
Ejemplo n.º 30
0
 /// <summary>
 /// 检查缓存是否过期
 /// </summary>
 /// <param name="context"></param>
 /// <returns>true:缓存已过期;false:缓存未过期</returns>
 public override bool IsExpired(WebPageContext context, ICacheStorage storage)
 {
     return(false);
 }