如何确定脚本是从命令行调用的还是作为 cgi 脚本调用的? [英] How can I determine if a script was called from the command line or as a cgi script?
问题描述
我编写了一个脚本,它可以在命令行上使用,也可以作为 CGI 脚本使用,并且需要确定脚本的调用方式,以便我可以为 Web 请求输出一个内容类型的标头(可能还有一些反缓存标头也是).我的第一个想法是检查http环境变量是否存在:
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
";
}
print $js;
有更好的方法吗?
推荐答案
根据RFC3875<中的CGI规范/a>(第 4.1.4 节.),GATEWAY_INTERFACE 环境变量将是检查您是否在 CGI 上下文中运行的权威:
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:
GATEWAY_INTERFACE
变量必须设置为 CGI 的方言被服务器用来与脚本通信.
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.
这篇关于如何确定脚本是从命令行调用的还是作为 cgi 脚本调用的?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!