`

Common Gateway Interface

    博客分类:
  • java
阅读更多

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:
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"
HTTP_ACCEPT_LANGUAGE="en-us,en;q=0.5"
HTTP_CONNECTION="keep-alive"
HTTP_COOKIE="ShowBlockedFriends=1; oneswarmView=list; swarmsPerPage=6; Community contacts=true; OneSwarm=Y6XAAAAAX; friend_list_open=true"
HTTP_HOST="example.com"
HTTP_USER_AGENT="Mozilla/5.0 (Windows NT 6.1; WOW64; rv:5.0) Gecko/20100101 Firefox/5.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="/ponylove"
PATH_TRANSLATED="C:\Program Files (x86)\Apache Software Foundation\Apache2.2\htdocs\ponylove"
QUERY_STRING="q=20%C001er&moar=kitties"
REMOTE_ADDR="127.0.0.1"
REMOTE_PORT="63555"
REQUEST_METHOD="GET"
REQUEST_URI="/cgi-bin/printenv.pl/ponylove?q=20%C001er&moar=kitties"
SCRIPT_FILENAME="C:/Program Files (x86)/Apache Software Foundation/Apache2.2/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.19 (Win32) PHP/5.2.17"
SYSTEMROOT="C:\Windows"
TERM="cygwin"
WINDIR="C:\Windows"

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.

 

分享到:
评论

相关推荐

    CGI入门 Common Gateway Interface

    CGI 意思为 Common Gateway Interface, 一种基于浏览器的输入、在Web服务器上运行的程序方法. CGI脚本 使你的浏览器与用户能交互,为了在数据库中 寻找一个名词, 提供你写入的评论,或者从一个表单中选择几个条目...

    The Common Gateway Interface (CGI) Version 1.1.pdf

    有需要的没有积分的,可以发邮件h5ru@qq.com,我发给你。 The Common Gateway Interface (CGI) Version 1.1,CGI规范

    Linux开发基础-c-c -CGI.ppt

    CGI(Common Gateway Interface: 公用网关接口)规定了Web服务器调用其他可执行程序(CGI 程序)的接口协议标准。Web服务器通过调用CGI程序实现和Web浏览器的交互,也就是CGI程序接受Web浏览器发送给Web服务器的信息,进行...

    ASP.NET之表单和控件详解

    action属性指明当前表单提交之后由哪个程序来处理,这个处理程序可以是任何动态网页或者servlet或者CGI(Common Gateway Interface),在asp.net里面一般都是都aspx页面来处理。 method属性指明form表单的提交方式。...

    JAVA教程及实例(Sun公司源码原资料)

    Initially, Common Gateway Interface (CGI) scripts were the main technology used to generate dynamic content. Although widely used, CGI scripting technology has a number of shortcomings, including ...

    ASP技术访问WEB数据库

    公共网关接口CGI(Common Gateway Interface)  CGI是较早实现的技术。适用于多种服务器平台,如UNIX、WINDOWS等,但CGI的开发成本高、维护困难、功能有限、不具备事务处理功能、占用服务器资源较多。  2. INTERNET...

    JavaWeb面试题

    JavaWeb面试题说出Servlet的生命周期,并说出Servlet和CGI[CGI(Common Gateway Interface,公用网关接口)是较早用来建立动态网页的技术。当客户端向Web服务器上指定的CGI程序发出请求时,Web服务器会启动一个新的...

    动态网页技术的发展

    (1)cgi(common gateway interface) 特点: a:cgi技术是早期动态技术使用最多,发展比较成熟并且功能强大 b:效率比较低,编程比较困难 c:cgi可以用不同的语言编写(vb,delphi,c/c++,perl)常用的c/c++和...

    ASP技术在交互式网页设计中的运用

    ASP技术 ASP(Active Server Pages动态网页)是微软公司推出的一种用以取代CGI(Common Gateway Interface通用网关接口)的技术。目前,Internet上的许多基于Windows平台的Web站点已开始应用ASP来替换CGI。 简单地讲...

    Servlet初级学习源代码

    Java Servlet 通常情况下与使用 CGI(Common Gateway Interface,公共网关接口)实现的程序可以达到异曲同工的效果。 ———————————————— 版权声明:本文为CSDN博主「那山的狐狸」的原创文章,遵循CC ...

    ASP技术详解

    ASP(Active Server Pages)动态网页,是微软公司推出的一种用以取代CGI(Common Gateway Interface)通用网关接口的技术。我们可以通过ASP结合HTML语言、ASP指令和ActiveX组件以及数据库等方面知识,使用自己的 Web ...

    C语言“Console Graphic Interface”开发工具包

    此“CGI”并非指“公共网关接口”(Common Gateway Interface),也与相关的技术无关。此处的CGI(Console Graphic Interface),即“控制台图形界面”,是自定义的一套控制台应用程序“图形化”界面开发解决方案。...

    Manning.Ajax.in.Practice.Jun.2007.pdf

    ment retrieval protocol was subverted by the Common Gateway Interface into serving up dynamically-generated documents delivering data from a database back-end, allowing online access to one’s data ...

    ​ActivePerl5.28版本下载、ActivePerl下载

    CGI(Common Gateway Interface)公共网关接口,是外部扩展应用程序与 Web 服务器交互的一个标准接口。服务器端与客户端进行交互的常见方式多,CGI 技术就是其中之一。根据CGI标准,编写外部扩展应用程序,可以对...

    依据ASP华夏文化交流平台的设计与实现(毕业设计源代码+论文)

    ASP(Active Server Page)是微软公司推出的一种用以取代通用网关接口(Common Gateway Interface)的技术。从字面上说,ASP包含三方面含义:(1) Active:ASP使用了Microsoft的ActiveX技术。ActiveX是Microsoft...

    PHP运行模式的深入理解

    PHP运行模式有4钟:1)cgi 通用网关接口(Common Gateway Interface))2) fast-cgi 常驻 (long-live) 型的 CGI3) cli 命令行运行 (Command Line Interface)4)web模块模式 (apache等web服务器运行的模块模式...

    servlet 8个最基础demo

    这些通常是使用公共网关接口(CGI(Common Gateway Interface))应用程序完成的。然而,在服务器上运行Java,这种程序可使用Java编程语言实现。在通信量大的服务器上,Javaservlet的优点在于它们的执行速度更快于CGI...

    asp综合网站设计-课设

    在Internet飞速发展的今天,...ASP技术ASP(Active Server Pages动态网页)是微软公司推出的一种用以取代CGI(Common Gateway Interface通用网关接口)的技术。目前,Internet上的许多基于Windows平台的Web站点已开始

    Web应用安全:web数据库访问技术.pptx

    CGI(Common Gateway Interface,通用网关界面)是一种Web服务器上运行的基于Web浏览器输入程序的方法,是最早的访问数据库的解决方案。CGI程序可以建立网页与数据库之间的连接,将用户的查询要求转换成数据库的查询...

    Python - How to Program, 1e (2002).pdf

    Introduction to the Common Gateway Interface (CGI). 7. Object-Based Programming: Classes and Data Abstraction. 8. Object-Oriented Programming: Inheritance and Polymorphism. 9. Operator Overloading...

Global site tag (gtag.js) - Google Analytics