ColdFusion Pre-Flight Proxy
Today’s development blog entry is some coldfusion code that’s helped me make a little app that is basically a proxy.
It takes a URL as input and does a pre-flight check to ensure that the endpoint is available before launching the user to the destination.
Most web developers probably never need to take this extra step, since it’s another layer in the link chain, but this becomes important if you are dealing with a large multi-environment site or external links that are a critical portion of your web site. The trend (at least in the company I work for) seems to be a move move toward outsourced web services, where whole portions of your website functionality is literally outsourced to a vendor. The outsource component is more complex than it seems, because this isn’t just sending the user blindly over to the vendor; we must perform authentication in some fashion, a single sign on, if you will, to allow for a more seamless experience. In the case of a large internal multi-environment web site, you have multiple pieces, which are expected to behave as a single entity, so any failure in one part ruins the cohesive single experience.
The code below also provides a simple mechanism to disable parts of my web site, based upon trigger files which I can set to determine if I should even send the user over, if the destination is available.
Wait – did you say TRIGGER FILES? Yes I did.
There are those that will probably argue with me how poor this concept is and how mundane, but honestly, it works, and it’s pretty quick to put together.
I had about little over an hour to whip the code up to do this, or suffer from manually fudging the site to take links down during an upcoming maintenance window (I won’t go into how we got to this state.. That’s for another story). Of course the more PRO way of doing this would have been to either create a cool Coldfusion application that read a database table or read an XML file to get all sorts of nifty data to show and a cool kick ass admin interface to handle it all! But honestly, I’m not that good at Coldfusion, and I don’t have a guru handy at the moment to help me figure out the CFML to parse XML, nor a DBA available at my beckon call to whip up an awesome Schema for me. So, in the time I’m given, trigger files were easy.
Hopefully someone might find this code handy, or at least parts of it, since I’ve not found anything this specific online. I paraphrased some of the details, like the exact messages, but you should get the idea. Enjoy
<!--- dest is the parameter passed in --->
<cfif isDefined('url.dest')>
<cfhttp url='#url.dest#' method="head" resolveurl="no" throwonerror="no" />
<cfif NOT IsDefined("cfhttp.responseheader.status_code") OR cfhttp.responseheader.status_code EQ "404">
I just checked the header response for a 404.
<pre style="font: normal normal normal 12px/18px Consolas, Monaco, 'Courier New', Courier, monospace;"> Sorry the destination is down</pre>
<cfelse>
<!--- ok the destination is there, can I go to it? --->
<!--- here, I check to see if I'm calling this proxy script from my member section --->
<cfif #CGI.HTTP_REFERER# contains "member">
<cfif fileexists(expandpath("/outage/member.html"))>
<cfset outage = 1>
<cfelse>
<cfset outage = 0>
</cfif>
<!--- I do this for other sections of my site --->
<cfelse>
<cfset outage = 0>
</cfif>
<!--- Now I check to see if there was an outage, if not, we send the user to the destination --->
<cfif outage eq 1>
<p>We're sorry, but due to maintenance, this part of the site is down. </p>
<cfelse>
<cflocation url='#url.dest#' />
</cfif>
</cfif>
<cfelse>
<!--- Do Nothing, since no Parameter came in --->
</cfif>
You can follow any responses to this entry through the RSS 2.0 feed. You can leave a response, or trackback from your own site.