CFContent-Variable Response vs. Standard Output Response

Posted November 23, 2009 at 6:56 PM

Tags: ColdFusion

I've been playing around with ColdFusion Builder Extensions lately and I'm running into some very odd behavior. Before I write anything about that, however, I wanted to confirm that a response driven by the CFContent tag and its Variable attribute is identical to a response driven by the standard ColdFusion output. To test this hypothesis, I created a simple ColdFusion page that would take XML data and return it, based on a URL variable, using either the standard ColdFusion output or the CFContent tag:

Content.cfm

 Launch code in new window » Download code as text file »

  • <!---
  • Param the method of content delivery. Basically, whether or
  • not we want to use the CFContent tag to deliver the content
  • back to the client (as a binary stream).
  • --->
  • <cfparam name="url.cfcontent" type="boolean" default="false" />
  •  
  •  
  • <!---
  • Create the content that we are going to stream back. (NOTE:
  • this response is the Tick's war cry!!).
  • --->
  • <cfsavecontent variable="responseData">
  •  
  • <response>
  • <message>Spooooooon!</message>
  • </response>
  •  
  • </cfsavecontent>
  •  
  •  
  • <!---
  • Check to see which streaming method we are using: either
  • CFContent, or the standard output.
  • --->
  • <cfif url.cfcontent>
  •  
  • <!--- Use CFContent to stream back binary data. --->
  •  
  • <!---
  • Convert the raw data into a binary string so we can
  • use it with the CFContent tag's Variable attribute.
  • --->
  • <cfset responseBinary = toBinary(
  • toBase64( trim( responseData ) )
  • ) />
  •  
  • <!--- Tell client how much data to expect. --->
  • <cfheader
  • name="content-length"
  • value="#arrayLen( responseBinary )#"
  • />
  •  
  • <!--- Set a type-flag for debugging. --->
  • <cfheader
  • name="response-type"
  • value="CFCONTENT"
  • />
  •  
  • <!--- Stream the binary data to the client. --->
  • <cfcontent
  • type="text/xml"
  • variable="#responseBinary#"
  • />
  •  
  • <cfelse>
  •  
  • <!--- Use standard output to stream back data. --->
  •  
  • <!--- Tell client how much data to expect. --->
  • <cfheader
  • name="content-length"
  • value="#len( trim( responseData ) )#"
  • />
  •  
  • <!--- Set a type-flag for debugging. --->
  • <cfheader
  • name="response-type"
  • value="STANDARD"
  • />
  •  
  • <!---
  • Forgive this last piece of messiness - I just wanted
  • to make sure that this methodology would finalize the
  • request in the same way that CFContent would.
  • --->
  • <cfcontent
  • type="text/xml"
  • /><cfset writeOutput( trim( responseData ) ) /><cfabort />
  •  
  • </cfif>

As you can see above, starting with a single XML string, I configure the response in such a way that both responses halt immediately after the response data is written to the response output. Theoretically, the only difference between these two responses should be the custom CFHeader value, "response-type", which I am inserting for debugging / sanity-check purposes.

With the content page in place, I then set up a test page which would make two CFHTTP requests to the content.cfm page - one using the standard output response and one using the CFContent-driven response:

 Launch code in new window » Download code as text file »

  • <!--- Build the URL for the standard content page. --->
  • <cfset standardURL = (
  • "http://" &
  • cgi.server_name &
  • getDirectoryFromPath( cgi.script_name ) &
  • "content.cfm"
  • ) />
  •  
  • <!---
  • Build the URL for the cfcontent-driven content page
  • (simply adding the URL-flag to the request).
  • --->
  • <cfset cfcontentURL = (standardURL & "?cfcontent=true") />
  •  
  •  
  •  
  • <!--- Request data from the standard url. --->
  • <cfhttp
  • result="standardGet"
  • method="get"
  • url="#standardURL#"
  • />
  •  
  • <!--- Request data from the cfcontent url. --->
  • <cfhttp
  • result="cfcontentGet"
  • method="get"
  • url="#cfcontentURL#"
  • />
  •  
  •  
  •  
  • <cfoutput>
  •  
  • <!--- Output the standard response. --->
  • <h2>
  • Standard Response
  • </h2>
  •  
  • <cfdump
  • var="#standardGet#"
  • label="Standard Get"
  • />
  •  
  • <p>
  • Message:
  • #xmlParse( standardGet.fileContent ).response.message#
  • </p>
  •  
  • <!--- ------------------------------------------------- --->
  • <!--- ------------------------------------------------- --->
  •  
  • <!--- Output the cfcontent-driven response. --->
  • <h2>
  • CFContent-Driven Response
  • </h2>
  •  
  • <cfdump
  • var="#cfcontentGet#"
  • label="CFContent-Driven Get"
  • />
  •  
  • <p>
  • Message:
  • #xmlParse( cfcontentGet.fileContent ).response.message#
  • </p>
  •  
  • </cfoutput>

As you can see, the only difference between the two CFHTTP requests is the use of the url-variable, "?cfcontent=true". When we run the above code, we get the following page output:

 
 
 
 
 
 
CFContent / Variable Response vs. Standard ColdFusion Output Response. 
 
 
 

Other than the "response-type" header value that I inserted (to make sure that I didn't mess this up), there is zero difference between the two responses; both report the same status code, content type, content length, and actual file content - from the requesting standpoint, these two responses are identical.

This result is not surprising - the CFContent / Variable approach to ColdFusion responses is something that I use often; this exploration and confirmation was meant only to be a lead-up to a following blog post.

Download Code Snippet ZIP File

Post Comment  |  Ask Ben  |  Other Searches  |  Print Page




Learning ColdFusion 9 - ColdFusion 9 tutorials, samples, examples, demos

Reader Comments

Nov 24, 2009 at 1:14 AM // reply »
74 Comments

I always thought you took the streamy approach because of some kind of performance gains or reduced server overhead you secretly knew about. Excited for the follow up.


Nov 24, 2009 at 8:01 AM // reply »
7,207 Comments

@David,

No performance gain (that I know of). I just like the approach because it gives me ultimate control over what data is actually returned, including things like the content-length precision.


Post Comment  |  Ask Ben

Recent Blog Comments
Feb 9, 2010 at 4:14 AM
Ask Ben: Converting a Query to an Array
Exellent script! Tried to make it shorter, gained approx 10% performance improvement :) [CODE] <cffunction name="queryToArray" access="public" returntype="array" output="false"hint="This turns a q ... read »
Feb 9, 2010 at 3:16 AM
Using jQuery's SlideUp() and SlideDown() Methods With Bottom-Positioned Elements
Just a quick fix for the code example above: $('.panel').height() should instead be... $('.panel').outerHeight() ... read »
Feb 9, 2010 at 3:09 AM
Using jQuery's SlideUp() and SlideDown() Methods With Bottom-Positioned Elements
I came across this page because I was searching for a certain behavior. I didn't just want the element to slide up; I wanted it's contents to slide up with it. As if the contents were written on a p ... read »
Feb 9, 2010 at 12:57 AM
Ask Ben: Creating A PDF And Attaching It To An Email Using ColdFusion
Have you got any reference to code explaining the procedure to use cfdocument, file to disk and attach to email? ... read »
Feb 8, 2010 at 11:27 PM
Why NULL Values Should Not Be Used in a Database Unless Required
@Randi, While I can appreciate your specific situation, I personally am not a fan of a situation where people have the option to run their own ad-hoc reports on the database. This is, honestly, a r ... read »
Feb 8, 2010 at 11:15 PM
Creating Microsoft Excel Documents With ColdFusion And XML
@Candice, No problem - glad you got it figured out. @David, To view the XLS document as an XML file, if I remember correctly, I actually opened up the document in Excel and then went "File > ... read »
Feb 8, 2010 at 11:11 PM
Converting An IP Address To An Integer Using MySQL (Thanks Julian Halliwell)
@Rob, As @Julian pointed out, you need to enable multiple queries in order to run queries containing semi-colons. For more info on that, take a look at this post: http://www.bennadel.com/blog/120 ... read »
Feb 8, 2010 at 11:08 PM
Muscle: Confessions Of An Unlikely Bodybuilder By Samuel Wilson Fussell
@Robert, Awesome. When you start reading it, I'd love for you to drop by and share your comments. I happen to love the book and am always happy to have a good conversation about it. ... read »