Click here to Skip to main content
Click here to Skip to main content
Articles » Web Development » ASP.NET » General » Downloads
 
Add your own
alternative version

HTTP Compression Module

, 20 Mar 2008 CPOL
A compression module for ASP.NET that works with WebResource.axd, JavaScript, and CSS
CompressionModule.zip
HttpCompress
bin
Debug
DC.Web.HttpCompress.dll
DC.Web.HttpCompress.pdb
obj
Debug
DC.Web.HttpCompress.dll
Refactor
DC.Web.HttpCompress.dll
TempPE
DC.Web.HttpCompress.pdb
Properties
HttpCompressWeb
App_Data
Bin
DC.Web.HttpCompress.dll
DC.Web.HttpCompress.pdb
test
js
App_Themes
MyTheme
favicon.PNG
compressionmoduleold.zip
DC.Web.HttpCompress.dll
DC.Web.HttpCompress.pdb
cache
cache-128213046009839691-D7D3ACCE12AC6EE21F3A2C4853FB330B-gzip.javascript.gzip
cache-128214849328112387-B5AD6B0BF0A02267A16F6E8BF93F8B31-gzip.javascript.gzip
cache-128220044775252020-05454B988893FDC5F7F1FBFD32D5DC37-gzip.css.gzip
cache-128220044775252020-B5AD6B0BF0A02267A16F6E8BF93F8B31-gzip.css.gzip
webResource-632962478152244369-sgQWzX90hPzyZo1xqcGnaUgWtfXwxfhN9aII-2myGJY1-gzip.javascript.gzip
webResource-632962478152244369-vcuzFf2Ilw1XPlD2WrjlQg2-gzip.javascript.gzip
css
javascript
NoCompress
DC.Web.HttpCompress.dll

using System;
using System.Collections.Generic;
using System.Text;
using System.Web;
using System.IO;
using System.IO.Compression;
using System.Security.Cryptography;
using System.Net;

namespace DC.Web.HttpCompress
{
    public class CompressWebResource : Compress
    {
        protected string assemblyName, assemblyTime;

        public CompressWebResource()
            : base()
        {
        }

        public void CompressResource(HttpContext context)
        {            
            this.context = context;
            // initialize the data
            Init();
            // get the query data
            assemblyName = context.Request.QueryString["d"];
            assemblyTime = context.Request.QueryString["t"];
            // get the encoding we want to use
            SetEncoding();

            // get the string for the Etag
            hash = string.Format("{0}-{1}", assemblyTime, assemblyName);

            // check the 'HTTP_IF_NONE_MATCH' string to see if it is cached on the browser
            // this is for content types other than javascript and css
            if (IsCachedOnBrowser())
                return;

            // get Etag to see if javascript or css is cached on browser with the type of encoding
            hash = string.Format("{0}-{1}-{2}", assemblyTime, assemblyName, encoding);

            // check the 'HTTP_IF_NONE_MATCH' string to see if it is cached on the browser
            if (IsCachedOnBrowser())
                return;

            // it's not cached on browser, so check if cached to disk.  We don't know the 
            // content-type since no processing has been done.  So we try javascript, if none found
            // try css.

            // check if chache files is true
            if (Settings.CacheSettings.CacheFiles)
            {
                type = FileType.javascript;
                SetCachefile();
                if (File.Exists(cachefile))
                {
                    SendCachedFile();
                    return;
                }
                type = FileType.css;
                SetCachefile();
                if (File.Exists(cachefile))
                {
                    SendCachedFile();
                    return;
                }
            }

            // resource was not chached on disk, so we need to get the data.
            // Tag a '&u=1' on the url and get the data.  The 'u' will be checked in the beginRequest
            // method in the HttpModule, if it is present, it won't be sent through here.
            HttpWebRequest request = (HttpWebRequest)WebRequest.Create(context.Request.Url.OriginalString + "&u=1");
            HttpWebResponse resp = request.GetResponse() as HttpWebResponse;
            string content_type = resp.ContentType.ToLower();
            Stream recDataStream = resp.GetResponseStream();
            // if the retrieved data is not javascript or css
            if (content_type != "application/x-javascript" && content_type != "text/javascript" && content_type != "text/css")
            {
                // set the Etag so the webresource will be cached by the browser and send it off.
                hash = string.Format("{0}-{1}", assemblyTime, assemblyName);
                context.Response.ClearHeaders();
                context.Response.AppendHeader("Etag", hash);
                context.Response.ContentType = resp.ContentType;
                CopyToStream(recDataStream, context.Response.OutputStream);
                return;
            }
            else
            {
                // Set the content type
                if (content_type.Contains("javascript"))
                    type = FileType.javascript;
                else if (content_type.Contains("text/css"))
                    type = FileType.css;

                // set the cach file
                SetCachefile();
                // get the data
                CopyToStream(recDataStream, dataStream);
                // sometimes when compressed, the data is cut off, not sure why.
                // but this assures all data is present.
                byte[] LFCR = StrToByteArray(Environment.NewLine + Environment.NewLine);
                dataStream.Write(LFCR, 0, LFCR.Length);
                dataStream.Write(StrToByteArray("    "), 0, 4);

                // compresses the data, cache it on disk, send it.
                SendFile();

            }
        }

        // copy one stream to another
        private void CopyToStream(Stream inStream, Stream outStream)
        {
            byte[] buff = new byte[1000];
            int read;
            int totalRead = 0;
            do
            {
                read = inStream.Read(buff, totalRead, 1000);
                outStream.Write(buff, 0, read);
            } while (read > 0);
        }
        // gets the file to be cached or has been cached
        protected override void SetCachefile()
        {
            if (string.IsNullOrEmpty(encoding))
                SetEncoding();
            cachefile = Cachedir + string.Format("webResource-{0}.{1}{2}", hash, type, encoding != "none" ? "." + encoding : "");
        }
    }
}

By viewing downloads associated with this article you agree to the Terms of Service and the article's licence.

If a file you wish to view isn't highlighted, and is a text file (not binary), please let us know and we'll add colourisation support for it.

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

Share

About the Author

darick_c
Web Developer
United States United States
No Biography provided

| Advertise | Privacy | Terms of Use | Mobile
Web04 | 2.8.141223.1 | Last Updated 20 Mar 2008
Article Copyright 2007 by darick_c
Everything else Copyright © CodeProject, 1999-2014
Layout: fixed | fluid