views:

120

answers:

3

I have a script that I wrote that can either be used on the command line or as a CGI script, and need to determine how the script was called so I can output a content-type header for web requests (and maybe some anti-cache headers too). My first thought is to check for the existance of http environment variables:

my $js = build_javascript();

if ( exists $ENV{HTTP_HOST} ) {
   print "Content-type: text/javascript\n\n";
}
print $js;

Is there a better way?

+9  A: 

According to the CGI specification in RFC3875 (section 4.1.4.), the GATEWAY_INTERFACE environment variable would be the authoritative thing to check whether you are running in a CGI context:

4.1.4. GATEWAY_INTERFACE

The GATEWAY_INTERFACE variable MUST be set to the dialect of CGI being used by the server to communicate with the script.

VoidPointer
Why? Because it MUST be set?
innaM
I suppose because there could possibly be other types of interfaces that are not CGI which might set a `HTTP_HOST` environment variable.
friedo
There are other variables that MUST be set. But HTTP_HOST, indeed, is not one of them.
innaM
That still doesn't really help because you can set any environment variables you like from the command line.
brian d foy
brian, by setting GATEWAY_INTERFACE you are saying "I'm a CGI host and all programs that care should behave accordingly". I don't see any reason for "accidentally" setting this in the environment. Otherwise, no solution would ever be valid because I could confuse the program by attaching a debugger and mess around with information the implementation relies on.
VoidPointer
A: 

Seems like a good enough approach. Also, it allows you to test the output from the script from the command line by passing it the HTTP_HOST.

Another idea might be to add an argument /path/to/script runfromcli and check if that exists, otherwise assume it is running as a CGI.

Martin Wickman
+3  A: 

There's really no way good way to tell if your script was started by a web server or from the command line. Any of the environment variables can be set in both situations. I often run CGI programs straight from the command line to test them, for instance.

Knowing that, if you want to pick one environment variable to use, it just has to be one that you won't set in the other situation, or one that you set in both but give different values to. In that case, choose any environment variable that you like.

If you want to get more sophisicated, you can use something like IO::Interactive to determine if you're connected to a terminal. If you aren't, the filehanandle that is_interactive returns is a null filehandle and the output goes nowhere:

 print { is_interactive() } $http_header;

If you don't like how IO::Interactive decides, you can reimplement is_interactive. It's a very short piece of code and the higher-level interface is very nice.

brian d foy