Common Gateway Interface

A web server that supports CGI can be configured to interpret(解析) a URL that it serves as a reference to CGI scripts. A common convention is to have a cgi-bin/ directory at the base of the directory tree and treat all executable files within it as CGI scripts. Another popular convention is to use filename extensions; for instance, if CGI scripts are consistently given the extension .cgi, the web server can be configured to interpret all such files as CGI scripts.

In the case of HTTP PUT or POSTs, the user-submitted data is provided to the program via the standard input. In any case, according to the CGI standard, data is passed into the program using certain, specific environment variables. This is in contrast to typical execution, where command-line arguments are used and the environment is in constant upheaval and cannot be trusted. Apache creates a small, efficient subset of the environment variables passed to it and adds details pertinent to the execution of the program.

[edit]Simple Example

The following CGI program shows all the environment variables passed by the web server:

 #!/usr/local/bin/perl
 ##
 ##  printenv—demo CGI program which just prints its environment
 ##
 #
 print "Content-type: text/plain\n\n";
 foreach $var (sort(keys(%ENV))) {
   $val = $ENV{$var};
   $val =~ s|\n|\\n|g;
   $val =~ s|"|\\"|g;
   print "${var}=\"${val}\"\n";
 }
  • If a web browser request the document at http://example.com/cgi-bin/printenv.pl/ponylove?q=20%C001er&moar=kitties, the following information is returned:
 

From the environment, we see that the web browser is Firefox running on Windows 7 running on a PC, the web server is an Apache running on a system which emulates Unix, and the CGI script is named cgi-bin/printenv.pl.

The program could then generate any content, that written to its standard output, will be transmitted by the web server to the browser.

[edit]Environment variables passed to a CGI program

  • Server specific variables:
    • SERVER_SOFTWARE — name/version of HTTP server.
    • SERVER_NAME — host name of the server, may be dot-decimal IP address.
    • GATEWAY_INTERFACE — CGI/version.
  • Request specific variables:
    • SERVER_PROTOCOL — HTTP/version.
    • SERVER_PORT — TCP port (decimal).
    • REQUEST_METHOD — name of HTTP method (see above).
    • PATH_INFO — path suffix, if appended to URL after program name and a slash.
    • PATH_TRANSLATED — corresponding full path as supposed by server, if PATH_INFO is present.
    • SCRIPT_NAME — relative path to the program, like /cgi-bin/script.cgi.
    • QUERY_STRING — the part of URL after ? character. May be composed of *name=value pairs separated with ampersands (such as var1=val1&var2=val2…) when used to submit form data transferred via GET method as defined by HTML application/x-www-form-urlencoded.
    • REMOTE_HOST — host name of the client, unset if server did not perform such lookup.
    • REMOTE_ADDR — IP address of the client (dot-decimal).
    • AUTH_TYPE — identification type, if applicable.
    • REMOTE_USER used for certain AUTH_TYPEs.
    • REMOTE_IDENT — see ident, only if server performed such lookup.
    • CONTENT_TYPE — MIME type of input data if PUT or POST method are used, as provided via HTTP header.
    • CONTENT_LENGTH — similarly, size of input data (decimal, in octets) if provided via HTTP header.
    • Variables passed by user agent (HTTP_ACCEPTHTTP_ACCEPT_LANGUAGEHTTP_USER_AGENTHTTP_COOKIE and possibly others) contain values of corresponding HTTP headers and therefore have the same sense.

[edit]Output format

The program returns the result to the web server in the form of standard output, prefixed by a header and a blank line.

The header is encoded in the same way as an HTTP header and must include the MIME type of the document returned.[1] The headers are generally forwarded with the response back to the user, supplemented by the web server.

[edit]Example

An example of a CGI program is one implementing a wiki. The user agent requests the name of an entry; the server retrieves the source of that entry's page (if one exists), transforms it into HTML, and sends the result.

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值