My co-worker was going through some of my code and spotted an error that I had made when using a CGI variable. I had misspelled the key "script_name". The reason that I had not caught it was because ColdFusion did not throw any error. Strange. When I did some more testing on this, it seems that you can basically access any key in CGI and will not throw an error:
<!--- Access invalid key. ---> <cfdump var="#CGI.hot_n_sexy#" />
Works totally fine. Returns an empty string (probably because Java is returning a NULL value).
I know this sort of thing usually doesn't work, but I tested it in another scope:
<!--- Access invalid key. ---> <cfdump var="#URL.hot_n_sexy#" />
Ok, this throws the error:
Element HOT_N_SEXY is undefined in URL.
Now, both of these are ColdFusion scopes, so I am curious as to why one throws an error and one does not. To investigate, I dumped out the class chain that makes up the CGI and the URL scopes:
<!--- Output CGI class chain. ---> CGI: #CGI.GetClass().GetName()# #CGI.GetClass().GetSuperClass().GetName()# #CGI.GetClass().GetSuperClass().GetSuperClass().GetName()# <!--- Output the URL class chain. ---> URL: #URL.GetClass().GetName()# #URL.GetClass().GetSuperClass().GetName()# #URL.GetClass().GetSuperClass().GetSuperClass().GetName()# #URL.GetClass().GetSuperClass().GetSuperClass().GetSuperClass().GetName()#
This gives us the output:
As you can see, both ColdFusion scope ultimately extend the AbstractMap class. If you look at the Get() method of the java.util.AbstractMap, you will see that it can either return a null value or thrown an error depending on wether the Map allows null values. The ColdFusion wrappers (that extends the AbstractMap) must determine wether null values are allowed.
And just to make sure I am not going crazy, I ran the ContainsKey() method on both the URL and the CGI scopes to see if the invalid key shows up as existing:
<!--- Check CGI key. ---> #CGI.ContainsKey( JavaCast( "string", "hot_n_sexy" ) )# <!--- Check URL key. ---> #URL.ContainsKey( JavaCast( "string", "hot_n_sexy" ) )#
This gives us:
So, it seems that neither scopes have the key (which we pretty much knew already), but apparently, the coldfusion.runtime.CgiScope class allows NULL entries while coldfusion.filter.UrlScope does not.
Want to use code from this post? Check out the license.