The following warnings occurred:
Warning [2] count(): Parameter must be an array or an object that implements Countable - Line: 802 - File: showthread.php PHP 7.2.19-0ubuntu0.18.04.2 (Linux)
File Line Function
/showthread.php 802 errorHandler->error



Current time: 10-19-2019, 07:22 PM 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
Post Reply 


Messages In This Thread
Coldfusion Keep-Alives and GZip Tutorial - green-watch.org - 05-11-2010 04:45 AM

Forum Jump:


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