如何确定脚本是从命令行调用还是作为 cgi 脚本调用的? [英] How can I determine if a script was called from the command line or as a cgi script?

查看:18
本文介绍了如何确定脚本是从命令行调用还是作为 cgi 脚本调用的?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我编写了一个脚本,既可以在命令行上使用,也可以作为 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(第 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屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆