Current time: 12-12-2019, 08:28 AM Hello There, Guest! (LoginRegister)

Post Reply 
 
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Coldfusion Keep-Alives and GZip Tutorial
05-11-2010, 04:45 AM
Post: #1
Coldfusion Keep-Alives and GZip Tutorial
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
Find all posts by this user
Quote this message in a reply
05-28-2010, 01:19 AM
Post: #2
RE: Coldfusion Keep-Alives and GZip Tutorial
Thx for the tut, mate Smile



------------------------
generic viagra soft
buy acomplia
Quote this message in a reply
05-31-2010, 02:45 AM
Post: #3
RE: Coldfusion Keep-Alives and GZip Tutorial
great tut! helped me a lot Smile thanks.
Find all posts by this user
Quote this message in a reply
06-16-2010, 04:35 PM
Post: #4
RE: Coldfusion Keep-Alives and GZip Tutorial
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
Find all posts by this user
Quote this message in a reply
Post Reply 


Forum Jump:


User(s) browsing this thread: 1 Guest(s)