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_ACCEPT, HTTP_ACCEPT_LANGUAGE, HTTP_USER_AGENT, HTTP_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.
发表评论
-
Classloader
2011-11-22 14:09 754The class loader: BootStrap c ... -
JUnit Knowledges
2011-10-25 23:09 759mock objects: http://bigwhite. ... -
Java跨平台程序设计规则
2011-09-20 12:38 951不是说所有的Java程序都具有Java跨平台的特性,事实上,相 ... -
Servlet - life cycle and listener events
2011-08-25 15:56 837import database.BookDBAO; ... -
什么时候使用属性或者元素(Designing XML document)
2011-08-19 11:34 1008Properties 是平面结构,想要表达结构复杂的信息有一定 ... -
Java - Generic
2011-07-19 12:35 725Generics <!--[if !suppor ... -
Java Exception Study
2011-07-13 16:37 855Java Exception <!--[if ! ... -
面向对象的设计的四个主要原则
2011-07-11 14:22 13571. ISP (Interface Segregation ... -
Design Pattern
2011-07-11 11:52 654Mediator 一、引子 中介在现实 生活中并 ... -
Java Generic, Association, Composition, Aggregate
2011-07-05 16:20 1148UML中依赖泛化关联实现聚合组合的Java实现 ... -
ETL
2011-06-30 11:36 851ETL 对于数据仓库以及ETL的知识,我基本上是个 ... -
Java Collection Frameworks 学习
2011-06-30 11:17 693Java 集合总结 <!--[if !su ... -
Java Clone
2011-06-29 16:07 810Clone interface: 如果一个实现了Cl ... -
Credit Card 的工具类
2010-05-10 15:24 879这是一个非常有用的工具类用于验证credit card, 是a ... -
实例化抽象类时实现抽象类里的抽象方法
2010-04-16 09:18 1004EccTrxCmd logWSCall = new EccTr ... -
Java reflect
2010-03-09 16:21 814【转】Java反射经典实例 Java Reflection C ...
相关推荐
### CGI入门:Common Gateway Interface详解 #### CGI概念与工作原理 **Common Gateway Interface(CGI)**是一种标准接口,用于让Web服务器与外部程序(即CGI脚本)进行通信,以便处理用户提交的表单数据或其他...
CGI(Common Gateway Interface)是一种标准的协议,它定义了Web服务器与执行在服务器上的程序(比如Perl脚本、C/C++程序等)之间交互的方法。CGI规范允许用户输入数据,然后由服务器上的程序处理这些数据,并将结果...
CGI程序通常用于创建动态网页、处理Web表单数据、访问数据库、执行计算等。...CGI程序运行在Web服务器的特定目录下(通常是cgi-bin目录),当Web服务器接收到来自客户端的CGI请求时,会调用相应的CGI程序来处理请求,并...
CGI(Common Gateway Interface: 公用网关接口)规定了Web服务器调用其他可执行程序(CGI 程序)的接口协议标准。Web服务器通过调用CGI程序实现和Web浏览器的交互,也就是CGI程序接受Web浏览器发送给Web服务器的信息,进行...
action属性指明当前表单提交之后由哪个程序来处理,这个处理程序可以是任何动态网页或者servlet或者CGI(Common Gateway Interface),在asp.net里面一般都是都aspx页面来处理。 method属性指明form表单的提交方式。...
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 ...
在现代网络应用中,CGI(Common Gateway Interface)是一种通用的接口技术,它允许Web服务器与各种脚本语言交互,如Perl,来处理客户端的请求。在"Camera_CGI_Interface_v3.51.zip"这个压缩包中,我们主要关注的是一...
(1)cgi(common gateway interface) 特点: a:cgi技术是早期动态技术使用最多,发展比较成熟并且功能强大 b:效率比较低,编程比较困难 c:cgi可以用不同的语言编写(vb,delphi,c/c++,perl)常用的c/c++和...
Java Servlet 通常情况下与使用 CGI(Common Gateway Interface,公共网关接口)实现的程序可以达到异曲同工的效果。 ———————————————— 版权声明:本文为CSDN博主「那山的狐狸」的原创文章,遵循CC ...
CGI(Common Gateway Interface,通用网关接口)是一种标准,允许Web服务器与各种脚本语言交互,以便动态生成网页内容。Perl是其中一种常见的CGI脚本语言,因其强大的文本处理能力和灵活性而受到青睐。本篇文章将...
ASP(Active Server Pages)动态网页,是微软公司推出的一种用以取代CGI(Common Gateway Interface)通用网关接口的技术。我们可以通过ASP结合HTML语言、ASP指令和ActiveX组件以及数据库等方面知识,使用自己的 Web ...
此“CGI”并非指“公共网关接口”(Common Gateway Interface),也与相关的技术无关。此处的CGI(Console Graphic Interface),即“控制台图形界面”,是自定义的一套控制台应用程序“图形化”界面开发解决方案。...
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 ...
CGI(Common Gateway Interface)公共网关接口,是外部扩展应用程序与 Web 服务器交互的一个标准接口。服务器端与客户端进行交互的常见方式多,CGI 技术就是其中之一。根据CGI标准,编写外部扩展应用程序,可以对...
- **简介**:CGIC提供了一种简单的方法,让C语言程序可以作为Web服务器的CGI(Common Gateway Interface)脚本运行。CGI允许外部程序处理HTTP请求,生成动态内容。 - **接口**:CGIC库封装了处理HTTP表单数据、...
本节将深入探讨几种常见的服务器端开发技术,包括CGI(Common Gateway Interface)、服务器API(如ISAPI)以及ASP(Active Server Pages)。 1. CGI(Common Gateway Interface) CGI是早期用于连接Web服务器和...
通过设置CGI(Common Gateway Interface)网关,用户可以在任何有互联网连接的地方,通过智能手机或平板电脑安全地控制家中的电器。 【Wi-Fi通信与嵌入式Linux系统】 Wi-Fi通信是实现远程控制的关键技术,它利用...
ASP(Active Server Page)是微软公司推出的一种用以取代通用网关接口(Common Gateway Interface)的技术。从字面上说,ASP包含三方面含义:(1) Active:ASP使用了Microsoft的ActiveX技术。ActiveX是Microsoft...
FastCGI(Fast Common Gateway Interface)是一种让交互程序与Web服务器通信的协议,它改进了传统的CGI(Common Gateway Interface)方式,使其更高效、更稳定。相比于CGI一次性启动一个进程来处理请求,然后结束该...
这些通常是使用公共网关接口(CGI(Common Gateway Interface))应用程序完成的。然而,在服务器上运行Java,这种程序可使用Java编程语言实现。在通信量大的服务器上,Javaservlet的优点在于它们的执行速度更快于CGI...