Web Servers are an important part of the internet and the web. Web servers used to serve web sites, web pages, APIs, documents, video images, etc. Web servers are generally designed to be dynamic to respond to user requests in a proper fashion. CGI (Common Gateway Interface) is a technology designed to provide dynamic programming and response capabilities to the web servers.
Web服务器是Internet和Web的重要组成部分。 用于服务网站,网页,API,文档,视频图像等的Web服务器。Web服务器通常设计为动态的,以适当的方式响应用户请求。 CGI(通用网关接口)是一项旨在为Web服务器提供动态编程和响应功能的技术。
什么是CGI(通用网关接口)? (What Is CGI (Common Gateway Interface)?)
CGI is the short form of the Common Gateway Interface
. CGI provides the programmatic interface to the webserver where the webserver will consume the CGI scripts and applications in order to respond to the user request. It is called common because it provides a generic interface where it is used as a gateway to the different scripts and applications. CGI scripts generally produce web pages that contain HTML, JavaScript, CSS, Image, API, ASCII text, etc.
CGI是Common Gateway Interface
的缩写。 CGI提供了到Web服务器的编程接口,其中Web服务器将使用CGI脚本和应用程序来响应用户请求。 之所以称为通用,是因为它提供了通用接口,在该接口中它用作通向不同脚本和应用程序的网关。 CGI脚本通常会产生包含HTML,JavaScript,CSS,图像,API,ASCII文本等的网页。
CGI is first created as a best practice in NCSA (National Center for Supercomputing Applications) in order to call command-line executables from the web. With wide usage and popularity, the standard RFC 3875 is created in November 1997.
CGI最初是作为NCSA(国家超级计算应用程序中心)中的最佳实践创建的,以便从Web调用命令行可执行文件。 标准RFC 3875具有广泛的用途和流行性,于1997年11月创建。
CGI脚本 (CGI Scripts)
CGI scripts mainly created using C programming language because of its standard uses C programming for definitions. getenv()
function of the C library is used to get environment variables from the requests. CGI scripts are stored in /usr/local/apache/htdocs/cgi-bin
path in general but the path can be changed according to will. CGI scripts can be used in different programming and scripting languages like Bash, Perl, Python, etc. but for easiness and practical development and change scripting languages Bash and Perl are preferred.
CGI脚本主要使用C编程语言创建,因为其标准使用C编程进行定义。 C库的getenv()
函数用于从请求中获取环境变量。 CGI脚本通常存储在/usr/local/apache/htdocs/cgi-bin
路径中,但是可以根据需要更改路径。 CGI脚本可以在Bash,Perl,Python等不同的编程和脚本语言中使用,但为了简便起见和实际开发以及更改脚本语言,Bash和Perl是首选。
示例CGI脚本 (Example CGI Script)
Below we can see an example CGI script which is developed in Perl scripting language.
下面我们可以看到一个用Perl脚本语言开发的示例CGI脚本。
#!/usr/bin/perl
=head1 DESCRIPTION
printenv — This CGI script will print environment variables
=cut
print "Content-Type: text/plain\n\n";
for my $var ( sort keys %ENV ) {
printf "%s = \"%s\"\n", $var, $ENV{$var};
}
- `#!/usr/bin/perl` line is used to set the given file interpreter or programming language. This line will set the interpreter as Perl which is located at `/usr/bin/perl`. `#!/ usr / bin / perl`行用于设置给定的文件解释器或编程语言。 这行代码将解释器设置为Perl,位于/ usr / bin / perl中。
- `=head1 DESCRIPTION` is a comment which simply put a header to the documentation and below the lines are also comment == head1DESCRIPTION是一个注释,它只是将标题放置在文档中,而在行下方也是注释
- `=cut` is the end of the comment.== cut是注释的结尾。
- `print “Content-Type: text/plain\n \n` will output the first line of the HTTP response for the request. This line will output the content type as plain text. `print'Content-Type:text / plain \ n \ n`将输出请求的HTTP响应的第一行。 此行将内容类型输出为纯文本。
- `for my $var ( sort keys %ENV) {` block will iterate over the environment variables and put the value into `$var` and the `$var` will be printed in every step. 对于我的$ var(排序键%ENV){`块将遍历环境变量并将值放入$ var中,并且$ var将在每个步骤中打印。
When we call this Perl CGI script we will get an output or HTTP response as HTML format like below.
当我们调用此Perl CGI脚本时,我们将获得如下HTML格式的输出或HTTP响应。
COMSPEC="C:\Windows\system32\cmd.exe"
DOCUMENT_ROOT="C:/Program Files (x86)/Apache Software Foundation/Apache2.2/htdocs"
GATEWAY_INTERFACE="CGI/1.1"
HOME="/home/SYSTEM"
HTTP_ACCEPT="text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8"
HTTP_ACCEPT_CHARSET="ISO-8859-1,utf-8;q=0.7,*;q=0.7"
HTTP_ACCEPT_ENCODING="gzip, deflate, br"
HTTP_ACCEPT_LANGUAGE="en-us,en;q=0.5"
HTTP_CONNECTION="keep-alive"
HTTP_HOST="example.com"
HTTP_USER_AGENT="Mozilla/5.0 (Windows NT 6.1; WOW64; rv:67.0) Gecko/20100101 Firefox/66.0"
PATH="/home/SYSTEM/bin:/bin:/cygdrive/c/progra~2/php:/cygdrive/c/windows/system32:..."
PATHEXT=".COM;.EXE;.BAT;.CMD;.VBS;.VBE;.JS;.JSE;.WSF;.WSH;.MSC"
PATH_INFO="/foo/bar"
PATH_TRANSLATED="C:\Program Files (x86)\Apache Software Foundation\Apache2.4\htdocs\foo\bar"
QUERY_STRING="var1=value1&var2=with%20percent%20encoding"
REMOTE_ADDR="127.0.0.1"
REMOTE_PORT="63555"
REQUEST_METHOD="GET"
REQUEST_URI="/cgi-bin/printenv.pl/foo/bar?var1=value1&var2=with%20percent%20encoding"
SCRIPT_FILENAME="C:/Program Files (x86)/Apache Software Foundation/Apache2.4/cgi-bin/printenv.pl"
SCRIPT_NAME="/cgi-bin/printenv.pl"
SERVER_ADDR="127.0.0.1"
SERVER_ADMIN="(server admin's email address)"
SERVER_NAME="127.0.0.1"
SERVER_PORT="80"
SERVER_PROTOCOL="HTTP/1.1"
SERVER_SIGNATURE=""
SERVER_SOFTWARE="Apache/2.2.39 (Win32) PHP/7.1.7"
SYSTEMROOT="C:\Windows"
TERM="cygwin"
WINDIR="C:\Windows"
翻译自: https://www.poftut.com/what-is-cgi-common-gateway-interface/