Beispiel #1
0
        /// <summary>
        /// Enables processing of HTTP Web requests by a custom HttpHandler that implements the <see cref="IHttpHandler"/> interface.
        /// </summary>
        /// <param name="context">An <see cref="HttpContextBase"/> object that provides references to the intrinsic server objects (for example, Request, Response, Session, and Server) used to service HTTP requests. </param>
        public void ProcessRequest(HttpContextBase context)
        {
            if (context == null)
            {
                throw new ArgumentNullException("context", "context cannot be null.");
            }

            string        cacheKey = this.GetCacheKey(context);
            HandlerOutput output   = context.Cache[cacheKey] as HandlerOutput;

            this.PrepareResponse(context, output);

            if (context.Response.StatusCode == 200)
            {
                if (output == null)
                {
                    try
                    {
                        output = new HandlerOutput()
                        {
                            Data     = this.PerformRequest(context) ?? new byte[0],
                            Modified = DateTime.UtcNow
                        };
                    }
                    catch (HttpException ex)
                    {
                        context.Response.StatusCode = ex.GetHttpCode();
                    }

                    if (context.Response.StatusCode == 200)
                    {
                        if (BlueCollarSection.Section.Dashboard.CachingEnabled &&
                            (this.CacheModes & ResponseCacheModes.Server) == ResponseCacheModes.Server)
                        {
                            context.Cache.Add(
                                cacheKey,
                                output,
                                null,
                                Cache.NoAbsoluteExpiration,
                                Cache.NoSlidingExpiration,
                                CacheItemPriority.Low,
                                null);
                        }
                    }
                }

                if (output != null && output.Data != null && output.Data.Length > 0)
                {
                    context.Response.OutputStream.Write(output.Data, 0, output.Data.Length);
                }
            }
        }
        /// <summary>
        /// Prepares an HTTP context's response.
        /// </summary>
        /// <param name="context">The HTTP context to prepare.</param>
        /// <param name="output">The cached output the response is being prepared for, if applicable.</param>
        protected override void PrepareResponse(HttpContextBase context, HandlerOutput output)
        {
            if (context == null)
            {
                throw new ArgumentNullException("context", "context cannot be null.");
            }

            base.PrepareResponse(context, output);

            if (context.Response.StatusCode == 200)
            {
                context.Response.Charset = "utf-8";
            }
        }
Beispiel #3
0
        /// <summary>
        /// Prepares an HTTP context's response.
        /// </summary>
        /// <param name="context">The HTTP context to prepare.</param>
        /// <param name="output">The cached output the response is being prepared for, if applicable.</param>
        protected override void PrepareResponse(HttpContextBase context, HandlerOutput output)
        {
            if (context == null)
            {
                throw new ArgumentNullException("context", "context cannot be null.");
            }

            base.PrepareResponse(context, output);

            if (context.Response.StatusCode == 200)
            {
                context.Response.Charset = "utf-8";
            }
        }
Beispiel #4
0
        /// <summary>
        /// Prepares an HTTP context's response.
        /// </summary>
        /// <param name="context">The HTTP context to prepare.</param>
        /// <param name="output">The cached output the response is being prepared for, if applicable.</param>
        protected virtual void PrepareResponse(HttpContextBase context, HandlerOutput output)
        {
            if (context == null)
            {
                throw new ArgumentNullException("context", "context cannot be null.");
            }

            context.Response.ContentType = this.ResponseContentType;
            context.Response.StatusCode  = 200;

            if (BlueCollarSection.Section.Dashboard.CachingEnabled &&
                (this.CacheModes & ResponseCacheModes.Client) == ResponseCacheModes.Client)
            {
                context.Response.Cache.SetCacheability(HttpCacheability.Public);
                context.Response.Cache.SetVaryByCustom("Accept-Encoding");

                DateTime modifiedSince = DateTime.UtcNow;
                bool     check304      = false;

                if (!string.IsNullOrEmpty(context.Request.Headers["If-Modified-Since"]))
                {
                    try
                    {
                        modifiedSince = DateTime.Parse(context.Request.Headers["If-Modified-Since"], CultureInfo.InvariantCulture).ToUniversalTime();
                        check304      = true;
                    }
                    catch (FormatException)
                    {
                        context.Response.StatusCode = 400;
                    }
                }

                if (context.Response.StatusCode == 200)
                {
                    if (check304 && (output == null || output.Modified <= modifiedSince))
                    {
                        context.Response.StatusCode = 304;
                    }

                    context.Response.Cache.SetExpires(output != null ? output.Modified.AddYears(1) : DateTime.UtcNow.AddYears(1));
                    context.Response.Cache.SetLastModified(output != null ? output.Modified : DateTime.UtcNow);
                }
            }
            else
            {
                context.Response.Cache.SetCacheability(HttpCacheability.Private);
                context.Response.Cache.SetMaxAge(new TimeSpan());
                context.Response.Cache.SetNoServerCaching();
                context.Response.Cache.SetNoStore();
            }

            if (context.Response.StatusCode == 200 && this.ShouldCompressResponse())
            {
                if (!string.IsNullOrEmpty(context.Request.Headers["accept-encoding"]) &&
                    (canCompressResponses == null ||
                     canCompressResponses.Value))
                {
                    string[] acceptEncodingTypes = context.Request.Headers["accept-encoding"].Split(new char[] { ',' }, StringSplitOptions.RemoveEmptyEntries);

                    if (acceptEncodingTypes != null)
                    {
                        try
                        {
                            if (acceptEncodingTypes.Any(e => "GZIP".Equals(e, StringComparison.OrdinalIgnoreCase)))
                            {
                                context.Response.Headers.Add("Content-Encoding", "gzip");
                                context.Response.Filter = new GZipStream(context.Response.Filter, CompressionMode.Compress);
                            }
                            else if (acceptEncodingTypes.Any(e => "DEFLATE".Equals(e, StringComparison.OrdinalIgnoreCase)))
                            {
                                context.Response.Headers.Add("Content-Encoding", "deflate");
                                context.Response.Filter = new DeflateStream(context.Response.Filter, CompressionMode.Compress);
                            }

                            canCompressResponses = true;
                        }
                        catch (PlatformNotSupportedException)
                        {
                            // Only available on IIS7 in integrated pipeline mode.
                            canCompressResponses = false;
                        }
                    }
                }
            }
        }
        /// <summary>
        /// Prepares an HTTP context's response.
        /// </summary>
        /// <param name="context">The HTTP context to prepare.</param>
        /// <param name="output">The cached output the response is being prepared for, if applicable.</param>
        protected virtual void PrepareResponse(HttpContextBase context, HandlerOutput output)
        {
            if (context == null)
            {
                throw new ArgumentNullException("context", "context cannot be null.");
            }

            context.Response.ContentType = this.ResponseContentType;
            context.Response.StatusCode = 200;

            if (BlueCollarSection.Section.Dashboard.CachingEnabled
                && (this.CacheModes & ResponseCacheModes.Client) == ResponseCacheModes.Client)
            {
                context.Response.Cache.SetCacheability(HttpCacheability.Public);
                context.Response.Cache.SetVaryByCustom("Accept-Encoding");

                DateTime modifiedSince = DateTime.UtcNow;
                bool check304 = false;

                if (!string.IsNullOrEmpty(context.Request.Headers["If-Modified-Since"]))
                {
                    try
                    {
                        modifiedSince = DateTime.Parse(context.Request.Headers["If-Modified-Since"], CultureInfo.InvariantCulture).ToUniversalTime();
                        check304 = true;
                    }
                    catch (FormatException)
                    {
                        context.Response.StatusCode = 400;
                    }
                }

                if (context.Response.StatusCode == 200)
                {
                    if (check304 && (output == null || output.Modified <= modifiedSince))
                    {
                        context.Response.StatusCode = 304;
                    }

                    context.Response.Cache.SetExpires(output != null ? output.Modified.AddYears(1) : DateTime.UtcNow.AddYears(1));
                    context.Response.Cache.SetLastModified(output != null ? output.Modified : DateTime.UtcNow);
                }
            }
            else
            {
                context.Response.Cache.SetCacheability(HttpCacheability.Private);
                context.Response.Cache.SetMaxAge(new TimeSpan());
                context.Response.Cache.SetNoServerCaching();
                context.Response.Cache.SetNoStore();
            }

            if (context.Response.StatusCode == 200 && this.ShouldCompressResponse())
            {
                if (!string.IsNullOrEmpty(context.Request.Headers["accept-encoding"])
                    && (canCompressResponses == null
                    || canCompressResponses.Value))
                {
                    string[] acceptEncodingTypes = context.Request.Headers["accept-encoding"].Split(new char[] { ',' }, StringSplitOptions.RemoveEmptyEntries);

                    if (acceptEncodingTypes != null)
                    {
                        try
                        {
                            if (acceptEncodingTypes.Any(e => "GZIP".Equals(e, StringComparison.OrdinalIgnoreCase)))
                            {
                                context.Response.Headers.Add("Content-Encoding", "gzip");
                                context.Response.Filter = new GZipStream(context.Response.Filter, CompressionMode.Compress);
                            }
                            else if (acceptEncodingTypes.Any(e => "DEFLATE".Equals(e, StringComparison.OrdinalIgnoreCase)))
                            {
                                context.Response.Headers.Add("Content-Encoding", "deflate");
                                context.Response.Filter = new DeflateStream(context.Response.Filter, CompressionMode.Compress);
                            }

                            canCompressResponses = true;
                        }
                        catch (PlatformNotSupportedException)
                        {
                            // Only available on IIS7 in integrated pipeline mode.
                            canCompressResponses = false;
                        }
                    }
                }
            }
        }
        /// <summary>
        /// Enables processing of HTTP Web requests by a custom HttpHandler that implements the <see cref="IHttpHandler"/> interface.
        /// </summary>
        /// <param name="context">An <see cref="HttpContextBase"/> object that provides references to the intrinsic server objects (for example, Request, Response, Session, and Server) used to service HTTP requests. </param>
        public void ProcessRequest(HttpContextBase context)
        {
            if (context == null)
            {
                throw new ArgumentNullException("context", "context cannot be null.");
            }

            string cacheKey = this.GetCacheKey(context);
            HandlerOutput output = context.Cache[cacheKey] as HandlerOutput;

            this.PrepareResponse(context, output);

            if (context.Response.StatusCode == 200)
            {
                if (output == null)
                {
                    try
                    {
                        output = new HandlerOutput()
                        {
                            Data = this.PerformRequest(context) ?? new byte[0],
                            Modified = DateTime.UtcNow
                        };
                    }
                    catch (HttpException ex)
                    {
                        context.Response.StatusCode = ex.GetHttpCode();
                    }

                    if (context.Response.StatusCode == 200)
                    {
                        if (BlueCollarSection.Section.Dashboard.CachingEnabled
                            && (this.CacheModes & ResponseCacheModes.Server) == ResponseCacheModes.Server)
                        {
                            context.Cache.Add(
                                cacheKey,
                                output,
                                null,
                                Cache.NoAbsoluteExpiration,
                                Cache.NoSlidingExpiration,
                                CacheItemPriority.Low,
                                null);
                        }
                    }
                }

                if (output != null && output.Data != null && output.Data.Length > 0)
                {
                    context.Response.OutputStream.Write(output.Data, 0, output.Data.Length);
                }
            }
        }