Saturday, May 02, 2009
Saturday, May 02, 2009 10:17:21 PM (GMT Daylight Time, UTC+01:00) (ASP.Net | C#)

I’d previously created a compression processor for dynamic content – using this excellent post for QValues by Dave Transom. Also just just discovered this post on creating an action filter for compression by Kazi Manzur Rashid.

So combining the two and we have:

public class CompressFilter : ActionFilterAttribute
{
    public override void OnActionExecuting(ActionExecutingContext filterContext)
    {
        HttpRequestBase request = filterContext.HttpContext.Request;

        // load encodings from header
        QValueList encodings = new QValueList(request.Headers["Accept-Encoding"]);

        // get the types we can handle, can be accepted and in the defined client preference
        QValue preferred = encodings.FindPreferred("gzip", "deflate", "identity");

        // if none of the preferred values were found, but the
        // client can accept wildcard encodings, we'll default
        // to Gzip.
        if (preferred.IsEmpty && encodings.AcceptWildcard && encodings.Find("gzip").IsEmpty)

            preferred = new QValue("gzip");

        HttpResponseBase response = filterContext.HttpContext.Response;
        // handle the preferred encoding
        switch (preferred.Name)
        {
            case "gzip":
                response.AppendHeader("Content-encoding", "gzip");
                response.Filter = new GZipStream(response.Filter, CompressionMode.Compress);
                break;

            case "deflate":
                response.AppendHeader("Content-encoding", "deflate");
                response.Filter = new DeflateStream(response.Filter, CompressionMode.Compress);
                break;

            case "identity":
                break;
            default:
                break;
        }
    }
}

Which when applied as below to any action method will enable compression if it’s supported by the browser.

[AcceptVerbs(HttpVerbs.Get)]
[CompressFilter]
public ContentResult SiteMap()
{
    return new ContentResult
               {
                   Content = SyndicationHelper.GetAggregateSiteMap(),
                   ContentType = "application/xml; charset=UTF-8"
               };
}

Download here: QValueAndCompressionFilter.zip

Tuesday, May 05, 2009 8:05:12 AM (GMT Daylight Time, UTC+01:00)
maybe you should priorities deflate over gzip as gzip is essentially a padded deflate, eg. more bytes for no use
Anon
Friday, May 08, 2009 10:04:26 PM (GMT Daylight Time, UTC+01:00)
it's wrong. it has no checks for gzip bugs in browsers, especially for ie6
andy
Saturday, May 09, 2009 3:42:15 AM (GMT Daylight Time, UTC+01:00)
Thanks for the feedback Andy and 'Anon'.

I've tested the compression filter with all the major browser version, Safari, Firefox, Chrome, Opera and Internet Explorer 6 SP2 and above, and they all work fine with GZip or Deflate compressed content. Given the current usage statistics on the Web for browser versions I think it's probable safe at this point to serve compressed content without specific user agent checks. Although of course it depends on the application - and implementers may choose otherwise.
Saturday, August 08, 2009 9:22:00 AM (GMT Daylight Time, UTC+01:00)
I test all controll by CompressFile but it has a rand for zip.becuse if size of controll be small compressfill cannot under from controllsize with gzip
Attie
Monday, January 18, 2010 7:32:20 PM (GMT Standard Time, UTC+00:00)
Just wanted to say Thank You
Monday, January 18, 2010 8:48:14 PM (GMT Standard Time, UTC+00:00)
My pleasure :-)
OpenID
Please login with either your OpenID above, or your details below.
Name
E-mail
(will show your gravatar icon)
Home page

Comment (Some html is allowed: a@href@title, b, em, i, strike, strong, u) where the @ means "attribute." For example, you can use <a href="" title=""> or <blockquote cite="Scott">.  

Live Comment Preview

search

categories

on this page

ads

archive

Total Posts: 94
This Year: 0
This Month: 0
This Week: 0
Comments: 77