Skip to content

Instantly share code, notes, and snippets.

@prabirshrestha
Forked from dieseltravis/Readme.txt
Created July 27, 2010 07:25
Show Gist options
  • Select an option

  • Save prabirshrestha/491883 to your computer and use it in GitHub Desktop.

Select an option

Save prabirshrestha/491883 to your computer and use it in GitHub Desktop.

Revisions

  1. @dieseltravis dieseltravis revised this gist Feb 18, 2010. 1 changed file with 1 addition and 1 deletion.
    2 changes: 1 addition & 1 deletion YuiCompressor.ashx.cs
    Original file line number Diff line number Diff line change
    @@ -91,7 +91,7 @@ public void ProcessRequest(HttpContext context)
    if (ENABLEHTTPCOMPRESSION)
    {
    string encodingsAccepted = context.Request.Headers("Accept-Encoding");
    if (!(String.IsNullOrEmpty(encodingsAccepted)))
    if (! string.IsNullOrEmpty(encodingsAccepted))
    {
    encodingsAccepted = encodingsAccepted.ToLowerInvariant();
    if (encodingsAccepted.Contains("deflate"))
  2. @dieseltravis dieseltravis revised this gist Feb 18, 2010. 3 changed files with 49 additions and 18 deletions.
    5 changes: 4 additions & 1 deletion Readme.txt
    Original file line number Diff line number Diff line change
    @@ -3,4 +3,7 @@ Example usage:
    <link rel="stylesheet" type="text/css" href="/YuiCompressor.ashx?css=reset,style" />

    ...just before the </body>:
    <script type="text/javascript" src="/YuiCompressor.ashx?js=main,someotherscript"></script>
    <script type="text/javascript" src="/YuiCompressor.ashx?js=main,someotherscript"></script>

    Changelog:
    2010-02-18: Updated to support HTTP compression
    55 changes: 43 additions & 12 deletions YuiCompressor.ashx.cs
    Original file line number Diff line number Diff line change
    @@ -6,33 +6,41 @@
    public class YuiCompressor : IHttpHandler
    {
    public const char DELIMITER = ',';

    public const bool ENABLEHTTPCOMPRESSION = true;
    public TimeSpan CacheDuration = TimeSpan.FromDays(1);

    public class ContentTypes
    {
    public const string CSS = "text/css";
    public const string JS = "application/x-javascript";
    public const string ERROR = "text/plain";
    }

    public class Extensions
    {
    public const string CSS = ".css";
    public const string JS = ".js";
    }


    public class Folders
    {
    // customize these to your site's paths
    public const string CSS = "~/styles/";
    public const string JS = "~/scripts/";
    }


    public class Extensions
    {
    public const string CSS = ".css";
    public const string JS = ".js";
    }

    public void ProcessRequest(HttpContext context)
    {
    context.Response.ContentEncoding = System.Text.Encoding.Default;

    context.Response.Cache.SetExpires(DateTime.Now.Add(CacheDuration));
    context.Response.Cache.SetMaxAge(CacheDuration);
    context.Response.Cache.SetCacheability(HttpCacheability.Private);

    string cssFiles = context.Request.QueryString["css"];
    string jsFiles = context.Request.QueryString["js"];
    string fileName = context.Request.QueryString["f"];
    if (! string.IsNullOrEmpty(cssFiles))
    {
    // A list of CSS files has been passed in, write each file's contents to the response object
    @@ -68,23 +76,46 @@ public void ProcessRequest(HttpContext context)
    {
    // 500?
    //Throw New System.IO.FileNotFoundException("The file specified isn't an allowed type.", fileName)
    context.Response.ContentType = ContentTypes.ERROR;
    }
    }
    else
    {
    // 404?
    //Throw New System.IO.FileNotFoundException("A filename hasn't been specified.")
    context.Response.ContentType = ContentTypes.ERROR;
    context.Response.Write(Environment.NewLine);
    }

    // compress output if enabled
    if (ENABLEHTTPCOMPRESSION)
    {
    string encodingsAccepted = context.Request.Headers("Accept-Encoding");
    if (!(String.IsNullOrEmpty(encodingsAccepted)))
    {
    encodingsAccepted = encodingsAccepted.ToLowerInvariant();
    if (encodingsAccepted.Contains("deflate"))
    {
    context.Response.AppendHeader("Content-Encoding", "deflate");
    context.Response.Filter = New System.IO.Compression.DeflateStream(context.Response.Filter, System.IO.Compression.CompressionMode.Compress);
    }
    else if (encodingsAccepted.Contains("gzip"))
    {
    context.Response.AppendHeader("Content-Encoding", "gzip");
    context.Response.Filter = New System.IO.Compression.GZipStream(context.Response.Filter, System.IO.Compression.CompressionMode.Compress);
    }
    }
    }
    }

    public bool IsReusable
    {
    get
    {
    return false;
    }
    }

    public void WriteCompressedFile(HttpContext context, string fileName, string folder)
    {
    // Add each file's compressed contents to the cache as it is read with a file dependency on itself
    7 changes: 2 additions & 5 deletions YuiCompressor.ashx.vb
    Original file line number Diff line number Diff line change
    @@ -6,9 +6,9 @@ Imports Yahoo.Yui.Compressor

    Public Class YuiCompressor : Implements IHttpHandler
    Public Const Delimiter As String = ","
    'TODO: update C# version with http compression ability
    Public Const EnableHttpCompression As Boolean = True

    Public cacheDuration As TimeSpan = TimeSpan.FromDays(1)

    Public Class ContentTypes
    Public Const Css As String = "text/css"
    Public Const Js As String = "application/x-javascript"
    @@ -28,8 +28,6 @@ Public Class YuiCompressor : Implements IHttpHandler
    Public Sub ProcessRequest(ByVal context As HttpContext) Implements IHttpHandler.ProcessRequest
    context.Response.ContentEncoding = Encoding.Default

    'TODO: update C# version with extra cache params
    Dim cacheDuration As TimeSpan = TimeSpan.FromDays(1)
    context.Response.Cache.SetExpires(DateTime.Now.Add(cacheDuration))
    context.Response.Cache.SetMaxAge(cacheDuration)
    context.Response.Cache.SetCacheability(HttpCacheability.Private)
    @@ -70,7 +68,6 @@ Public Class YuiCompressor : Implements IHttpHandler
    context.Response.Write(Environment.NewLine)
    End If

    'TODO: update C# version with http compression
    ' compress output if enabled
    If EnableHttpCompression Then
    Dim encodingsAccepted As String = context.Request.Headers("Accept-Encoding")
  3. @dieseltravis dieseltravis revised this gist Feb 18, 2010. 1 changed file with 34 additions and 4 deletions.
    38 changes: 34 additions & 4 deletions YuiCompressor.ashx.vb
    Original file line number Diff line number Diff line change
    @@ -6,10 +6,13 @@ Imports Yahoo.Yui.Compressor

    Public Class YuiCompressor : Implements IHttpHandler
    Public Const Delimiter As String = ","

    'TODO: update C# version with http compression ability
    Public Const EnableHttpCompression As Boolean = True

    Public Class ContentTypes
    Public Const Css As String = "text/css"
    Public Const Js As String = "application/x-javascript"
    Public Const Err As String = "text/plain"
    End Class

    Public Class Folders
    @@ -24,6 +27,13 @@ Public Class YuiCompressor : Implements IHttpHandler

    Public Sub ProcessRequest(ByVal context As HttpContext) Implements IHttpHandler.ProcessRequest
    context.Response.ContentEncoding = Encoding.Default

    'TODO: update C# version with extra cache params
    Dim cacheDuration As TimeSpan = TimeSpan.FromDays(1)
    context.Response.Cache.SetExpires(DateTime.Now.Add(cacheDuration))
    context.Response.Cache.SetMaxAge(cacheDuration)
    context.Response.Cache.SetCacheability(HttpCacheability.Private)

    Dim cssFiles As String = context.Request.QueryString("css")
    Dim jsFiles As String = context.Request.QueryString("js")
    Dim fileName As String = context.Request.QueryString("f")
    @@ -51,10 +61,29 @@ Public Class YuiCompressor : Implements IHttpHandler
    Else
    ' 500?
    'Throw New System.IO.FileNotFoundException("The file specified isn't an allowed type.", fileName)
    context.Response.ContentType = ContentTypes.Err
    End If
    Else
    ' 404?
    'Throw New System.IO.FileNotFoundException("A filename hasn't been specified.")
    context.Response.ContentType = ContentTypes.Err
    context.Response.Write(Environment.NewLine)
    End If

    'TODO: update C# version with http compression
    ' compress output if enabled
    If EnableHttpCompression Then
    Dim encodingsAccepted As String = context.Request.Headers("Accept-Encoding")
    If Not (String.IsNullOrEmpty(encodingsAccepted)) Then
    encodingsAccepted = encodingsAccepted.ToLowerInvariant()
    If (encodingsAccepted.Contains("deflate")) Then
    context.Response.AppendHeader("Content-Encoding", "deflate")
    context.Response.Filter = New System.IO.Compression.DeflateStream(context.Response.Filter, System.IO.Compression.CompressionMode.Compress)
    ElseIf (encodingsAccepted.Contains("gzip")) Then
    context.Response.AppendHeader("Content-Encoding", "gzip")
    context.Response.Filter = New System.IO.Compression.GZipStream(context.Response.Filter, System.IO.Compression.CompressionMode.Compress)
    End If
    End If
    End If
    End Sub

    @@ -66,24 +95,25 @@ Public Class YuiCompressor : Implements IHttpHandler

    Public Sub WriteCompressedFile(ByVal context As HttpContext, ByVal fileName As String, ByVal folder As String)
    ' Add each file's compressed contents to the cache as it is read with a file dependency on itself
    Dim output As String = String.Empty
    If context.Cache(fileName) Is Nothing Then
    Dim filePath As String = HttpContext.Current.Server.MapPath(folder & fileName)
    Dim output As String = String.Empty
    Try
    output = System.IO.File.ReadAllText(filePath)
    If folder = Folders.Js Then
    output = JavaScriptCompressor.Compress(output) & vbCrLf
    Else
    output = CssCompressor.Compress(output) & vbCrLf
    End If
    context.Response.Write(output)
    context.Cache.Insert(fileName, output, New CacheDependency(filePath))
    Catch fnf As System.IO.FileNotFoundException
    ' throw 404?
    End Try
    Else
    context.Response.Write(DirectCast(HttpContext.Current.Cache(fileName), String))
    output = DirectCast(HttpContext.Current.Cache(fileName), String)
    End If

    context.Response.Write(output)
    End Sub

    End Class
  4. @dieseltravis dieseltravis revised this gist Sep 7, 2009. 2 changed files with 2 additions and 4 deletions.
    3 changes: 1 addition & 2 deletions Readme.txt
    Original file line number Diff line number Diff line change
    @@ -3,5 +3,4 @@ Example usage:
    <link rel="stylesheet" type="text/css" href="/YuiCompressor.ashx?css=reset,style" />

    ...just before the </body>:
    <script type="text/javascript" src="/YuiCompressor.ashx?js=main,someotherscript"></script>

    <script type="text/javascript" src="/YuiCompressor.ashx?js=main,someotherscript"></script>
    3 changes: 1 addition & 2 deletions YuiCompressor.ashx.cs
    Original file line number Diff line number Diff line change
    @@ -116,5 +116,4 @@ public void WriteCompressedFile(HttpContext context, string fileName, string fol
    context.Response.Write((string) context.Cache[fileName]);
    }
    }
    }

    }
  5. @dieseltravis dieseltravis revised this gist Aug 13, 2009. 1 changed file with 0 additions and 1 deletion.
    1 change: 0 additions & 1 deletion LICENSE.TXT
    Original file line number Diff line number Diff line change
    @@ -1 +0,0 @@
    License: If you use this code, you owe me a beer. :-)
  6. @dieseltravis dieseltravis renamed this gist Aug 10, 2009. 1 changed file with 0 additions and 0 deletions.
    File renamed without changes.
  7. @dieseltravis dieseltravis revised this gist Aug 10, 2009. 2 changed files with 1 addition and 0 deletions.
    1 change: 1 addition & 0 deletions LICENSE.TXT
    Original file line number Diff line number Diff line change
    @@ -0,0 +1 @@
    License: If you use this code, you owe me a beer. :-)
    File renamed without changes.
  8. @dieseltravis dieseltravis revised this gist Jun 17, 2009. 1 changed file with 89 additions and 0 deletions.
    89 changes: 89 additions & 0 deletions YuiCompressor.ashx.vb
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,89 @@
    '<%@ WebHandler Language="VB" Class="YuiCompressor" %>

    Imports System
    Imports System.Web
    Imports Yahoo.Yui.Compressor

    Public Class YuiCompressor : Implements IHttpHandler
    Public Const Delimiter As String = ","

    Public Class ContentTypes
    Public Const Css As String = "text/css"
    Public Const Js As String = "application/x-javascript"
    End Class

    Public Class Folders
    Public Const Css As String = "~/styles/"
    Public Const Js As String = "~/scripts/"
    End Class

    Public Class Extensions
    Public Const Css As String = ".css"
    Public Const Js As String = ".js"
    End Class

    Public Sub ProcessRequest(ByVal context As HttpContext) Implements IHttpHandler.ProcessRequest
    context.Response.ContentEncoding = Encoding.Default
    Dim cssFiles As String = context.Request.QueryString("css")
    Dim jsFiles As String = context.Request.QueryString("js")
    Dim fileName As String = context.Request.QueryString("f")

    If Not String.IsNullOrEmpty(cssFiles) Then
    ' A list of CSS files has been passed in, write each file's contents to the response object
    context.Response.ContentType = ContentTypes.Css
    For Each cssFile As String In cssFiles.Split(Delimiter)
    WriteCompressedFile(context, cssFile & Extensions.Css, Folders.Css)
    Next
    ElseIf Not String.IsNullOrEmpty(jsFiles) Then
    ' A list of JS files has been passed in, write each file's contents to the response object
    context.Response.ContentType = ContentTypes.Js
    For Each jsFile As String In jsFiles.Split(Delimiter)
    WriteCompressedFile(context, jsFile & Extensions.Js, Folders.Js)
    Next
    ElseIf Not String.IsNullOrEmpty(fileName) Then
    ' A specific file has been passed in, write that file's contents to the response object
    If fileName.EndsWith(Extensions.Js) Then
    context.Response.ContentType = ContentTypes.Js
    WriteCompressedFile(context, fileName, Folders.Js)
    ElseIf fileName.EndsWith(Extensions.Css) Then
    context.Response.ContentType = ContentTypes.Css
    WriteCompressedFile(context, fileName, Folders.Css)
    Else
    ' 500?
    'Throw New System.IO.FileNotFoundException("The file specified isn't an allowed type.", fileName)
    End If
    Else
    ' 404?
    'Throw New System.IO.FileNotFoundException("A filename hasn't been specified.")
    End If
    End Sub

    Public ReadOnly Property IsReusable() As Boolean Implements IHttpHandler.IsReusable
    Get
    Return False
    End Get
    End Property

    Public Sub WriteCompressedFile(ByVal context As HttpContext, ByVal fileName As String, ByVal folder As String)
    ' Add each file's compressed contents to the cache as it is read with a file dependency on itself
    If context.Cache(fileName) Is Nothing Then
    Dim filePath As String = HttpContext.Current.Server.MapPath(folder & fileName)
    Dim output As String = String.Empty
    Try
    output = System.IO.File.ReadAllText(filePath)
    If folder = Folders.Js Then
    output = JavaScriptCompressor.Compress(output) & vbCrLf
    Else
    output = CssCompressor.Compress(output) & vbCrLf
    End If
    context.Response.Write(output)
    context.Cache.Insert(fileName, output, New CacheDependency(filePath))
    Catch fnf As System.IO.FileNotFoundException
    ' throw 404?
    End Try
    Else
    context.Response.Write(DirectCast(HttpContext.Current.Cache(fileName), String))
    End If
    End Sub

    End Class
  9. @dieseltravis dieseltravis revised this gist Jun 16, 2009. 1 changed file with 7 additions and 0 deletions.
    7 changes: 7 additions & 0 deletions gistfile3.txt
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,7 @@
    Example usage:
    ...in <head>:
    <link rel="stylesheet" type="text/css" href="/YuiCompressor.ashx?css=reset,style" />

    ...just before the </body>:
    <script type="text/javascript" src="/YuiCompressor.ashx?js=main,someotherscript"></script>

  10. @dieseltravis dieseltravis revised this gist Jun 16, 2009. 1 changed file with 2 additions and 1 deletion.
    3 changes: 2 additions & 1 deletion YuiCompressor.ashx.cs
    Original file line number Diff line number Diff line change
    @@ -21,7 +21,8 @@ public class Extensions

    public class Folders
    {
    public const string CSS = "~/themes/travis/";
    // customize these to your site's paths
    public const string CSS = "~/styles/";
    public const string JS = "~/scripts/";
    }

  11. @dieseltravis dieseltravis created this gist Jun 16, 2009.
    1 change: 1 addition & 0 deletions YuiCompressor.ashx
    Original file line number Diff line number Diff line change
    @@ -0,0 +1 @@
    <%@ WebHandler Language="C#" CodeBehind="YuiCompressor.ashx.cs" Class="YuiCompressor" %>
    119 changes: 119 additions & 0 deletions YuiCompressor.ashx.cs
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,119 @@
    using System;
    using System.Collections.Generic;
    using System.Web;
    using Yahoo.Yui.Compressor;

    public class YuiCompressor : IHttpHandler
    {
    public const char DELIMITER = ',';

    public class ContentTypes
    {
    public const string CSS = "text/css";
    public const string JS = "application/x-javascript";
    }

    public class Extensions
    {
    public const string CSS = ".css";
    public const string JS = ".js";
    }

    public class Folders
    {
    public const string CSS = "~/themes/travis/";
    public const string JS = "~/scripts/";
    }

    public void ProcessRequest(HttpContext context)
    {
    context.Response.ContentEncoding = System.Text.Encoding.Default;
    string cssFiles = context.Request.QueryString["css"];
    string jsFiles = context.Request.QueryString["js"];
    string fileName = context.Request.QueryString["f"];

    if (! string.IsNullOrEmpty(cssFiles))
    {
    // A list of CSS files has been passed in, write each file's contents to the response object
    context.Response.ContentType = ContentTypes.CSS;
    foreach (string cssFile in cssFiles.Split(DELIMITER))
    {
    WriteCompressedFile(context, cssFile + Extensions.CSS, Folders.CSS);
    }
    }
    else if (! string.IsNullOrEmpty(jsFiles))
    {
    // A list of JS files has been passed in, write each file's contents to the response object
    context.Response.ContentType = ContentTypes.JS;
    foreach (string jsFile in jsFiles.Split(DELIMITER))
    {
    WriteCompressedFile(context, jsFile + Extensions.JS, Folders.JS);
    }
    }
    else if (! string.IsNullOrEmpty(fileName))
    {
    // A specific file has been passed in, write that file's contents to the response object
    if (fileName.EndsWith(Extensions.JS))
    {
    context.Response.ContentType = ContentTypes.JS;
    WriteCompressedFile(context, fileName, Folders.JS);
    }
    else if (fileName.EndsWith(Extensions.CSS))
    {
    context.Response.ContentType = ContentTypes.CSS;
    WriteCompressedFile(context, fileName, Folders.CSS);
    }
    else
    {
    // 500?
    //Throw New System.IO.FileNotFoundException("The file specified isn't an allowed type.", fileName)
    }
    }
    else
    {
    // 404?
    //Throw New System.IO.FileNotFoundException("A filename hasn't been specified.")
    }
    }

    public bool IsReusable
    {
    get
    {
    return false;
    }
    }

    public void WriteCompressedFile(HttpContext context, string fileName, string folder)
    {
    // Add each file's compressed contents to the cache as it is read with a file dependency on itself
    if (context.Cache[fileName] == null)
    {
    string filePath = HttpContext.Current.Server.MapPath(folder + fileName);
    string output = string.Empty;
    try
    {
    output = System.IO.File.ReadAllText(filePath);
    if (folder == Folders.JS)
    {
    output = JavaScriptCompressor.Compress(output) + Environment.NewLine;
    }
    else
    {
    output = CssCompressor.Compress(output) + Environment.NewLine;
    }
    context.Response.Write(output);
    context.Cache.Insert(fileName, output, new System.Web.Caching.CacheDependency(filePath));
    }
    catch (System.IO.FileNotFoundException)
    {
    // throw 404?
    }
    }
    else
    {
    context.Response.Write((string) context.Cache[fileName]);
    }
    }
    }