Michells Mesterværk

Vinderen af låge nr. 13

by Administrator 15. December 2009 10:38

Niklas Christoffer Petersen har lavet det lille og fine compression modul i ASP.NET. Tillykke til dig Niklas.

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.IO.Compression;
using System.IO;
using System.Text.RegularExpressions;

public class CompressionModule : IHttpModule
{
private IList<string> acceptedContentTypes;

public CompressionModule()
{
acceptedContentTypes = new List<string>
        { "text/html", "text/ccs", "text/javascript", "image/gif", "image/jpeg", "image/png" };
}

private void OnReleaseRequestState(object sender, EventArgs e)
{
HttpContext context = ((HttpApplication)sender).Context;
string acceptEncoding = context.Request.Headers["Accept-Encoding"];
string contentType = context.Response.ContentType.ToLower();

/* Skal denne Content Type komprimeres og accepterer klienten komprimering? */
if (acceptEncoding != null && acceptedContentTypes.Any
        (x => x.StartsWith(contentType, StringComparison.InvariantCultureIgnoreCase)))
{
acceptEncoding = acceptEncoding.ToLower();

if (acceptEncoding.Contains("deflate") || acceptEncoding.Equals("*"))
{
context.Response.AppendHeader("Content-Encoding", "deflate");
context.Response.Filter = new DeflateStream
                (context.Response.Filter, CompressionMode.Compress);
}
else if (acceptEncoding.Contains("gzip"))
{
context.Response.AppendHeader("Content-Encoding", "gzip");
context.Response.Filter = new GZipStream
                (context.Response.Filter, CompressionMode.Compress);
}
}
}

public void Init(HttpApplication context)
{
context.ReleaseRequestState += new EventHandler(OnReleaseRequestState);
}

public void Dispose() { }
}

Der er et par kilo bolscher, en bog en pizza cutter på vej til dig.

Tags:

Comments

1/21/2010 7:44:17 AM #

same day payday loans

Your writing skill is very serviceable in order to read. Therefore, i like your blog to read in my residual time.

same day payday loans United States

2/14/2010 1:46:10 PM #

اخبار

Thank you very much for the possibility to have a look into the office. Nice boy, he has got the talent to explain.

اخبار Egypt

3/4/2010 8:13:40 AM #

watch movies online

I enjoy watching movies online, it is way easier than going to the theaters.

watch movies online United States

3/10/2010 12:01:35 AM #

WoW Mobile

Do you know which mobile phone services allow tethering?

WoW Mobile United States

3/17/2010 2:07:06 AM #

ssk sorgulama

This is a really good read for me, Must admit that you are one of the best bloggers I ever saw.Thanks for posting this informative article.

ssk sorgulama United States

3/17/2010 11:49:08 AM #

arac sorgulama

I admire the valuable information you offer in your articles. I will bookmark your blog and have my children check up here often. I am quite sure they will learn lots of new stuff here than anybody else!

arac sorgulama United States

3/20/2010 3:28:30 PM #

ehliyet

Excellent post.I want to thank you for this informative read, I really appreciate sharing this great post. Keep up your work.

ehliyet Denmark

Add comment


(Will show your Gravatar icon)

(De 3 specielle karaktere i det danske alfabet?)
  Country flag

biuquote
  • Comment
  • Preview
Loading



Powered by BlogEngine.NET 1.6.0.0
Theme by Mads Kristensen