CGI Variables and Their Respective ColdFusion/Java Servlet Alternative Methods

Are you a ColdFusion developer? Have you ever wanted to get away from using CGI variables such as CGI.SCRIPT_NAME but didn't quite know what else to use? Then you might find the following list of CGI variables and their respective ColdFusion/Java Servlet Alternative methods to be helpful because I couldn't find much (if any) documentation on this:

CGI Variable ColdFusion / Java Servlet Alternative Method
CGI.AUTH_TYPE getPageContext().getRequest().getAuthType()
CGI.CONTENT_LENGTH getPageContext().getRequest().getContentLength()
CGI.CONTEXT_PATH getPageContext().getRequest().getContextPath()
CGI.CONTENT_TYPE getPageContext().getRequest().getContentType()
CGI.PATH_INFO getPageContext().getRequest().getPathInfo()
CGI.PATH_TRANSLATED getPageContext().getRequest().getPathTranslated()
CGI.QUERY_STRING getPageContext().getRequest().getQueryString()
CGI.REMOTE_ADDR getPageContext().getRequest().getRemoteAddr()
CGI.REMOTE_HOST getPageContext().getRequest().getRemoteHost()
CGI.REMOTE_USER getPageContext().getRequest().getRemoteUser()
CGI.REQUEST_METHOD getPageContext().getRequest().getMethod()
CGI.SCRIPT_NAME getPageContext().getRequest().getServletPath()
CGI.SERVER_NAME getPageContext().getRequest().getServerName()
CGI.SERVER_PORT getPageContext().getRequest().getServerPort()
CGI.SERVER_PORT_SECURE getPageContext().getRequest().isSecure()
CGI.SERVER_PROTOCOL getPageContext().getRequest().getProtocol()
CGI.SERVER_SOFTWARE getPageContext().getRequest().getHeader("Server-Software")
CGI.HTTP_ACCEPT getPageContext().getRequest().getHeader("Accept")
CGI.HTTP_ACCEPT_CHARSET getPageContext().getRequest().getHeader("Accept-Charset")
CGI.HTTP_ACCEPT_ENCODING getPageContext().getRequest().getHeader("Accept-Encoding")
CGI.HTTP_ACCEPT_LANGUAGE getPageContext().getRequest().getHeader("Accept-Language")
CGI.HTTP_CONNECTION getPageContext().getRequest().getHeader("Connection")
CGI.HTTP_COOKIE getPageContext().getRequest().getHeader("Cookie")
CGI.HTTP_HOST getPageContext().getRequest().getHeader("Host")
CGI.HTTP_REFERER getPageContext().getRequest().getHeader("Referer")
CGI.HTTP_USER_AGENT getPageContext().getRequest().getHeader("User-Agent")
 
Other Useful Java Servlets  
Reconstructs the URL the client used to make the request. The returned URL contains a protocol, server name, port number, and server path, but it does not include query string parameters. getPageContext().getRequest().getRequestURL()
The URL string corresponding to the current client request. The string returned does not include the server name or query arguments. getPageContext().getRequest().getRequestURI()
Returns the name of the scheme used to make this request (http, https, or ftp) getPageContext().getRequest().getScheme()

Here's some code you can use to copy and paste to your .cfm file for testing:


<cfoutput>
    <table border="1">
        <tr>
            <th>CGI Variable</th>
            <th>Displays</th>
            <th>ColdFusion / Java Servlet Alternative Method</th>
            <th>Displays</th>
        </tr>
        <tr>
            <td>CGI.AUTH_TYPE</td>
            <td>#CGI.AUTH_TYPE#</td>
            <td>getPageContext().getRequest().getAuthType()</td>
            <td>#getPageContext().getRequest().getAuthType()#</td>
        </tr>
        <tr>
            <td>CGI.CONTENT_LENGTH</td>
            <td>#CGI.CONTENT_LENGTH#</td>
            <td>getPageContext().getRequest().getContentLength()</td>
            <td>#getPageContext().getRequest().getContentLength()#</td>
        </tr>
        <tr>
            <td>CGI.CONTEXT_PATH</td>
            <td>#CGI.CONTEXT_PATH#</td>
            <td>getPageContext().getRequest().getContextPath()</td>
            <td>#getPageContext().getRequest().getContextPath()#</td>
        </tr>
        <tr>
            <td>CGI.CONTENT_TYPE</td>
            <td>#CGI.CONTENT_TYPE#</td>
            <td>getPageContext().getRequest().getContentType()</td>
            <td>#getPageContext().getRequest().getContentType()#</td>
        </tr>
        <tr>
            <td>CGI.PATH_INFO</td>
            <td>#CGI.PATH_INFO#</td>
            <td>getPageContext().getRequest().getPathInfo()</td>
            <td>#getPageContext().getRequest().getPathInfo()#</td>
        </tr>
        <tr>
            <td>CGI.PATH_TRANSLATED</td>
            <td>#CGI.PATH_TRANSLATED#</td>
            <td>getPageContext().getRequest().getPathTranslated() </td>
            <td>#getPageContext().getRequest().getPathTranslated()#</td>
        </tr>
        <tr>
            <td>CGI.QUERY_STRING</td>
            <td>#CGI.QUERY_STRING#</td>
            <td>getPageContext().getRequest().getQueryString()</td>
            <td>#getPageContext().getRequest().getQueryString()#</td>
        </tr>
        <tr>
            <td>CGI.REMOTE_ADDR</td>
            <td>#CGI.REMOTE_ADDR#</td>
            <td>getPageContext().getRequest().getRemoteAddr()</td>
            <td>#getPageContext().getRequest().getRemoteAddr()#</td>
        </tr>
        <tr>
            <td>CGI.REMOTE_HOST</td>
            <td>#CGI.REMOTE_HOST#</td>
            <td>getPageContext().getRequest().getRemoteHost()</td>
            <td>#getPageContext().getRequest().getRemoteHost()#</td>
        </tr>
        <tr>
            <td>CGI.REMOTE_USER</td>
            <td>#CGI.REMOTE_USER#</td>
            <td>getPageContext().getRequest().getRemoteUser()</td>
            <td>#getPageContext().getRequest().getRemoteUser()#</td>
        </tr>
        <tr>
            <td>CGI.REQUEST_METHOD</td>
            <td>#CGI.REQUEST_METHOD#</td>
            <td>getPageContext().getRequest().getMethod()</td>
            <td>#getPageContext().getRequest().getMethod()#</td>
        </tr>
        <tr>
            <td>CGI.SCRIPT_NAME</td>
            <td>#CGI.SCRIPT_NAME#</td>
            <td>getPageContext().getRequest().getServletPath()</td>
            <td>#getPageContext().getRequest().getServletPath()#</td>
        </tr>
        <tr>
            <td>CGI.SERVER_NAME</td>
            <td>#CGI.SERVER_NAME#</td>
            <td>getPageContext().getRequest().getServerName()</td>
            <td>#getPageContext().getRequest().getServerName()#</td>
        </tr>
        <tr>
            <td>CGI.SERVER_PORT</td>
            <td>#CGI.SERVER_PORT#</td>
            <td>getPageContext().getRequest().getServerPort()</td>
            <td>#getPageContext().getRequest().getServerPort()#</td>
        </tr>
        <tr>
            <td>CGI.SERVER_PORT_SECURE</td>
            <td>#CGI.SERVER_PORT_SECURE#</td>
            <td>getPageContext().getRequest().isSecure()</td>
            <td>#getPageContext().getRequest().isSecure()#</td>
        </tr>
        <tr>
            <td>CGI.SERVER_PROTOCOL</td>
            <td>#CGI.SERVER_PROTOCOL#</td>
            <td>getPageContext().getRequest().getProtocol()</td>
            <td>#getPageContext().getRequest().getProtocol()#</td>
        </tr>
        <tr>
            <td>CGI.SERVER_SOFTWARE</td>
            <td>#CGI.SERVER_SOFTWARE#</td>
            <td>getPageContext().getRequest().getHeader("Server-Software")</td>
            <td>#getPageContext().getRequest().getHeader("Server-Software")#</td>
        </tr>
        <tr>
            <td>CGI.HTTP_ACCEPT</td>
            <td>#CGI.HTTP_ACCEPT#</td>
            <td>getPageContext().getRequest().getHeader("Accept")</td>
            <td>#getPageContext().getRequest().getHeader("Accept")#</td>
        </tr>
        <tr>
            <td>CGI.HTTP_ACCEPT_CHARSET</td>
            <td>#CGI.HTTP_ACCEPT_CHARSET#</td>
            <td>getPageContext().getRequest().getHeader("Accept-Charset")</td>
            <td>#getPageContext().getRequest().getHeader("Accept-Charset")#</td>
        </tr>
        <tr>
            <td>CGI.HTTP_ACCEPT_ENCODING</td>
            <td>#CGI.HTTP_ACCEPT_ENCODING#</td>
            <td>getPageContext().getRequest().getHeader("Accept-Encoding")</td>
            <td>#getPageContext().getRequest().getHeader("Accept-Encoding")#</td>
        </tr>
        <tr>
            <td>CGI.HTTP_ACCEPT_LANGUAGE</td>
            <td>#CGI.HTTP_ACCEPT_LANGUAGE#</td>
            <td>getPageContext().getRequest().getHeader("Accept-Language")</td>
            <td>#getPageContext().getRequest().getHeader("Accept-Language")#</td>
        </tr>
        <tr>
            <td>CGI.HTTP_CONNECTION</td>
            <td>#CGI.HTTP_CONNECTION#</td>
            <td>getPageContext().getRequest().getHeader("Connection")</td>
            <td>#getPageContext().getRequest().getHeader("Connection")#</td>
        </tr>
        <tr>
            <td>CGI.HTTP_COOKIE</td>
            <td>#CGI.HTTP_COOKIE#</td>
            <td>getPageContext().getRequest().getHeader("Cookie")</td>
            <td>#getPageContext().getRequest().getHeader("Cookie")#</td>
        </tr>
        <tr>
            <td>CGI.HTTP_HOST</td>
            <td>#CGI.HTTP_HOST#</td>
            <td>getPageContext().getRequest().getHeader("Host")</td>
            <td>#getPageContext().getRequest().getHeader("Host")#</td>
        </tr>
        <tr>
            <td>CGI.HTTP_REFERER</td>
            <td>#CGI.HTTP_REFERER#</td>
            <td>getPageContext().getRequest().getHeader("Referer")</td>
            <td>#getPageContext().getRequest().getHeader("Referer")#</td>
        </tr>
        <tr>
            <td>CGI.HTTP_USER_AGENT</td>
            <td>#CGI.HTTP_USER_AGENT#</td>
            <td>getPageContext().getRequest().getHeader("User-Agent")</td>
            <td>#getPageContext().getRequest().getHeader("User-Agent")#</td>
        </tr>
        <tr>
            <td colspan="4"> </td>
        </tr>
        <tr>
            <td><em><strong>Other Useful Java Servlets</strong></em></td>
            <td> </td>
            <td> </td>
            <td> </td>
        </tr>
        <tr>
            <td>Reconstructs the URL the client used to make the request. The returned URL contains a protocol, server name, port number, and server path, but it <em>does not include</em> query string parameters. </td>
            <td> </td>
            <td>getPageContext().getRequest().getRequestURL()</td>
            <td>#getPageContext().getRequest().getRequestURL()#</td>
        </tr>
        <tr>
            <td>The URL string corresponding to the current client request. The string returned does not include the server name or query arguments.</td>
            <td> </td>
            <td>getPageContext().getRequest().getRequestURI()</td>
            <td>#getPageContext().getRequest().getRequestURI()#</td>
        </tr>
        <tr>
            <td>Returns the name of the scheme used to make this request (http, https, or ftp)</td>
            <td> </td>
            <td>getPageContext().getRequest().getScheme()</td>
            <td>#getPageContext().getRequest().getScheme()#</td>
        </tr>
    </table>
</cfoutput>

Obviously, you should test these on your server before using them in production. Some of the methods may or may not be available. For example, I get empty strings using getPathInfo() and getPathTranslated(). You might want to try something like the following to see what methods are available on your server:


    <cfset availMethods = GetPageContext().getRequest() />
    <cfdump var="#availMethods#" />
    

Here's some more interesting information. Let's say you've defined some variables in a 'globalvariables.cfm' file and included them in your Application.cfc template, such as:


    <cfset REQUEST.RootDirectory = expandpath('/') />
    <cfset REQUEST.CurrentDirectory = GetDirectoryFromPath(GetTemplatePath()) />
    <cfset REQUEST.CurrentPage = GetFileFromPath(GetBaseTemplatePath()) />
    

If you run the following code, you should see what you also expected to see by simply using <cfoutput>#REQUEST.RootDirectory#</cfoutput> or <cfoutput>#REQUEST.CurrentDirectory#</cfoutput>.


    <cfoutput>
    #getPageContext().getRequest().RootDirectory#<br />
    #getPageContext().getRequest().CurrentDirectory#<br />
    #getPageContext().getRequest().CurrentPage#
    </cfoutput>
    

Just for fun, try this too:


    <cfset dirlist = getPageContext().getRequest().CurrentDirectory />
    <ul>
        <cfloop list="#dirlist#" delimiters="\" index="i">
            <cfoutput><li>#i#</li></cfoutput>
        </cfloop>
    </ul>
    

For more information on the Java HttpServletRequest: http://java.sun.com/products/servlet/2.2/javadoc/javax/servlet/http/HttpServletRequest.html

For more informaiton on HTTP Header Field Definitions, see: http://www.w3.org/Protocols/rfc2616/rfc2616-sec14.html

If you catch a typo or have some other insight, please feel free to add to this.

Comments

Interesting that there's helper functions for some of the more common CGI variables. It would probably be more useful if you were needing several of these, or doing it dynamically, to just use the getHeader function for everything.
getPageContext().getRequest().getHeader("whatever")

Also, CF returns true whenever you use IsDefined for any CGI variable, regardless of if it actually exists or not. These Java functions return nulls for non-existent CGI variables, which is probably slightly more useful. I think you could then do:
x = getPageContext().getRequest().getHeader("foobar")
y = IsDefined("x")
# Posted By duncan | 8/26/08 5:24 PM
Duncan, you bring up some interesting thoughts. I tested the getHeader() function for a few other CGI variables and now I'm trying to figure out where getHeader() is actually pulling its information from. Does getHeader() pull the info directly from CGI?

The reason I'm wondering is because as I mentioned in my article, when I use getPathInfo(), I get an empty string. However when I use CGI.PATH_INFO _or_ getHeader("path-info"), I get the value I was looking for.
# Posted By Stephen Withington | 8/27/08 7:24 AM
Hello,

I think your post is neat. It's always fun to read about different ways to use java in CF. I have to ask though why? Just to do it or would their be a situation or two where it would be better to use the java methods vs the CGI methods. Using CGI seems to be a lot less typing.

Thanks!

Randy
# Posted By Randy | 9/4/08 11:58 AM
@Randy,

Great question. One situation in particular where it might make more sense to use java vs. CGI would be if (for whatever reason, and no, I haven't personally encountered this) a web server wouldn't return CGI vars. I've seen a few posts by Dan G. Switzer, II (http://blog.pengoworks.com/) where he warns against using (or relying on) CGI. For example, see his comments (and other developers) in a post by Ben Nadel at http://www.bennadel.com/index.cfm?dax=blog:362.vie.... I'm sure there are several other examples as well.

Other than that, it's just fun to learn about the ins-and-outs of ColdFusion. I feel like I'm "looking under the hood" so-to-speak (which might be a bad analogy, but oh well). It always amazes me at how many different ways there are to accomplish something in ColdFusion. It seems like there's a method to fit most any programmer's preferred style.
# Posted By Stephen Withington | 9/4/08 1:31 PM
This was a pretty fun post. Well researched and nicely put together.


I also like to learn more about the Java underpinnings so please share other bits of info as you find it.

DW
# Posted By Dan Wilson | 9/4/08 8:31 PM
@Dan,

Thanks for the comments, and ditto.
# Posted By Stephen Withington | 9/5/08 7:43 AM
One other standard method is GetHTTPRequestData(); returns a structure, and member .headers is a struct of all HTTP headers.

Another HTTP header that doesn't show up in CGI scope, is if you have mod_rewrite or an ISAPI rewrite that sends the original path. For example, Ionics ISAPI rewrite has a rule option U, which sends the original path in header "X-Rewrite-Url"
# Posted By David | 9/9/08 6:05 PM
@David,

Thanks for sharing the information. I'll have to play around with that.
# Posted By Stephen Withington | 9/12/08 7:02 AM
Thanks for this article. It provided me with enough information to determine when a web requests passed a bogus "blank" CGI.Content-Type versus not passing on at all. I believe that CGI.Content_Type should always have content when performing a POST and be [Empty String] when performing a GET.

GET = isNull(getPageContext().getRequest().getContentType())
POST = LEN(TRIM(CGI.Content_Type))

[CFIF NOT POST AND NOT GET]
[Log Request for later review]
[cfheader statuscode="204" statustext="No Content"][CFABORT]
[CFIF]

I had to resort to testing this because the CF9 server was throwing a "String index out of range: -1" error when using GetHttpRequestData() to test headers for an ajax request. PCI compliance testing was causing this and the only difference I could identify between the requests was CGI.Content_Type = '' versus CGI.Content_Type = [Empty String].
# Posted By James Moberg | 2/26/13 8:33 PM

© 2024, Stephen J. Withington, Jr.  |  Hosted by Hostek.com

Creative Commons License   |   This work is licensed under a Creative Commons Attribution 3.0 Unported License.