WebPagetest Forums

Full Version: Coldfusion Keep-Alives and GZip Tutorial
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
Hey there,

It took me a few days to figure out how to GZIP coldfusion (.cfm) files while making sure keep-alives were enabled.

I across the following article describing how to make HTTP KeepAlives and Coldfusion Servers work together:

http://www.cisco.com/en/US/products/hw/c...409d.shtml

The line that lead me to a solution was packets need to contain a content-length value. So here is my solution to getting coldfusion (.cfm) files GZIPPED and Keep-Alives enabled:

Code:
<cfscript>
function HtmlCompressFormat(sInput)
{
    var level = 2;
    
    if( arrayLen( arguments ) GTE 2 AND isNumeric(arguments[2]))
    {
        level = arguments[2];
    }

    sInput = trim(sInput);
    
    switch(level)
    {
        case "3":
        {    
            sInput = reReplace( sInput, "[[:space:]]{2,}", " ", "all" );
            sInput = replace( sInput, "> <", "><", "all" );
            sInput = reReplace( sInput, "<!--[^>]+>", "", "all" );
            break;
        }
        case "2":
        {
            sInput = reReplace( sInput, "[[:space:]]{2,}", chr( 13 ), "all" );
            break;
        }
        case "1":
        {
            sInput = reReplace( sInput, "(" & chr( 10 ) & "|" & chr( 13 ) & ")+[[:space:]]{2,}", chr( 13 ), "all" );
            break;
        }
    }
    
    return sInput;
}
</cfscript>

<cfsavecontent variable = "raw">

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Untitled Document</title>
</head>

<body>
</body>
</html>

</cfsavecontent>

<cfif cgi.HTTP_ACCEPT_ENCODING contains "gzip">
  
  <cfset raw = #htmlCompressFormat(raw, 2)#>
  
  <cfscript>
  fileOut = createobject("java", "java.io.ByteArrayOutputStream").init();
  out = createobject("java","java.util.zip.GZIPOutputStream").init(fileOut);
  out.write(raw.getBytes(), 0, len(raw.getBytes()));
  out.finish();
  out.close();
  </cfscript>
  
  <cfheader name="Content-Encoding" value="gzip">
  <cfheader name="Content-Length" value="#len(fileOut.toByteArray())#">
  <cfcontent type="text/html; charset=utf-8" reset="true" variable="#fileOut.toByteArray()#">  
    
<cfelse>
  <cfoutput>#htmlCompressFormat(raw, 2)#</cfoutput>
</cfif>

I hope this comes in handy for other coldfusion developers working on page speed issues.

Sincerely,
Travis Walters
Thx for the tut, mate Smile



------------------------
generic viagra soft
buy acomplia
great tut! helped me a lot Smile thanks.
Hey Guys,

Something else I recommend having in my script:

<cfheader name="Expires" value="#GetHttpTimeString(DateAdd('m', 1, Now()))#">

This helps caching a bit. You can set the time for whatever you want as long as it follows the produced output in the line above.

Sincerely,
Travis Walters
Reference URL's