词条 | Common Gateway Interface |
释义 |
In computing, Common Gateway Interface (CGI) offers a standard protocol for web servers to execute programs that execute like console applications (also called command-line interface programs) running on a server that generates web pages dynamically. Such programs are known as CGI scripts or simply as CGIs. The specifics of how the script is executed by the server are determined by the server. In the common case, a CGI script executes at the time a request is made and generates HTML.[1] In brief, an HTTP POST request from the client will send the CGI program HTML form data via standard input. Other data, such as URL paths, and HTTP header data, are presented as process environment variables. HistoryIn 1993 the National Center for Supercomputing Applications (NCSA) team wrote the specification for calling command line executables on the www-talk mailing list.[2][3] The other Web server developers adopted it, and it has been a standard for Web servers ever since. A work group chaired by Ken Coar started in November 1997 to get the NCSA definition of CGI more formally defined.[4] This work resulted in RFC 3875, which specified CGI Version 1.1. Specifically mentioned in the RFC are the following contributors:[1]
Historically CGI scripts were often written using the C language. RFC 3875 "The Common Gateway Interface (CGI)" partially defines CGI using C,[1] as in saying that environment variables "are accessed by the C library routine getenv() or variable environ". The name CGI comes from the early days of the web, where users wanted to connect databases to their web servers. The CGI was a program executed by the server that provided a common "gateway" between the web server and the database. Purpose of the CGI standardEach web server runs HTTP server software, which responds to requests from web browsers. Generally, the HTTP server has a directory (folder), which is designated as a document collection — files that can be sent to Web browsers connected to this server.[5] For example, if the Web server has the domain name For pages constructed on the fly, the server software may defer requests to separate programs and relay the results to the requesting client (usually, a web browser that displays the page to the end user). In the early days of the web, such programs were usually small and written in a scripting language; hence, they were known as scripts. Such programs usually require some additional information to be specified with the request. For instance, if Wikipedia were implemented as a script, one thing the script would need to know is whether the user is logged in and, if logged in, under which name. The content at the top of a Wikipedia page depends on this information. HTTP provides ways for browsers to pass such information to the web server, e.g. as part of the URL. The server software must then pass this information through to the script somehow. Conversely, upon returning, the script must provide all the information required by HTTP for a response to the request: the HTTP status of the request, the document content (if available), the document type (e.g. HTML, PDF, or plain text), et cetera. Initially, different server software would use different ways to exchange this information with scripts. As a result, it wasn't possible to write scripts that would work unmodified for different server software, even though the information being exchanged was the same. Therefore, it was decided to establish a standard way for exchanging this information: CGI (the Common Gateway Interface, as it defines a common way for server software to interface with scripts). Webpage generating programs invoked by server software that operate according to the CGI standard are known as CGI scripts. This standard was quickly adopted and is still supported by all well-known server software, such as Apache, IIS, and (with an extension) node.js-based servers. An early use of CGI scripts was to process forms. In the beginning of HTML, HTML forms typically had an "action" attribute and a button designated as the "submit" button. When the submit button is pushed the URI specified in the "action" attribute would be sent to the server with the data from the form sent as a query string. If the "action" specifies a CGI script then the CGI script would be executed and it then produces an HTML page. Using CGI scriptsA web server allows its owner to configure which URLs shall be handled by which CGI scripts. This is usually done by marking a new directory within the document collection as containing CGI scripts — its name is often As remarked above, the CGI standard defines how additional information passed with the request is passed to the script. For instance, if a slash and additional directory name(s) are appended to the URL immediately after the name of the script (in this example, ExampleThe following Perl program shows all the environment variables passed by the Web server: If a Web browser issues a request for the environment variables at 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_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="/foo/bar" PATH_TRANSLATED="C:\\Program Files (x86)\\Apache Software Foundation\\Apache2.2\\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.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" Some, but not all, of these variables are defined by the CGI standard. Some, such as From the environment, it can be seen that the Web browser is Firefox running on a Windows 7 PC, the Web server is Apache running on a system that emulates Unix, and the CGI script is named The program could then generate any content, write that to standard output, and the Web server will transmit it to the browser. The following are environment variables passed to CGI programs:
The program returns the result to the Web server in the form of standard output, beginning with 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.[7] The headers, supplemented by the Web server, are generally forwarded with the response back to the user. Here is a simple CGI program written in Python 2 along with the HTML that handles a simple addition problem.[8]
import cgi import cgitb cgitb.enable() input_data = cgi.FieldStorage() print 'Content-Type:text/html' # HTML is following print # Leave a blank line print ' Addition Results'try: num1 = int(input_data["num1"].value) num2 = int(input_data["num2"].value) except: print ' print ' {0} + {1} = {2} '.format(num1, num2, num1 + num2)DeploymentA Web server that supports CGI can be configured to interpret a URL that it serves as a reference to a CGI script. A common convention is to have a In the case of HTTP PUT or POSTs, the user-submitted data are provided to the program via the standard input. The Web server creates a subset of the environment variables passed to it and adds details pertinent to the HTTP environment. UsesCGI is often used to process inputs information from the user and produce the appropriate output. An example of a CGI program is one implementing a Wiki. The user agent requests the name of an entry; the Web server executes the CGI; the CGI program retrieves the source of that entry's page (if one exists), transforms it into HTML, and prints the result. The web server receives the input from the CGI and transmits it to the user agent. If the "Edit this page" link is clicked, the CGI populates an HTML AlternativesCalling a command generally means the invocation of a newly created process on the server. Starting the process can consume much more time and memory than the actual work of generating the output, especially when the program still needs to be interpreted or compiled. If the command is called often, the resulting workload can quickly overwhelm the server. The overhead involved in process creation can be reduced by techniques such as FastCGI that "prefork" interpreter processes, or by running the application code entirely within the web server, using extension modules such as mod_perl or mod_php. Another way to reduce the overhead is to use precompiled CGI programs, e.g. by writing them in languages such as C or C++, rather than interpreted or compiled-on-the-fly languages such as Perl or PHP, or by implementing the page generating software as a custom webserver module. Alternative approaches include:
The optimal configuration for any Web application depends on application-specific details, amount of traffic, and complexity of the transaction; these tradeoffs need to be analyzed to determine the best implementation for a given task and time budget. Web Frameworks offer an alternative to using CGI scripts to interact with user agents. See also
References1. ^1 2 RFC3875: The Common Gateway Interface (CGI) Version 1.1 2. ^Server Scripts, by Rob McCool, www-talk mailing list, Sun, 14 Nov 1993 19:24:47 -0600{{Citation |title=The Common Gateway Interface |url=http://hoohoo.ncsa.uiuc.edu/cgi/ |archiveurl=https://web.archive.org/web/20100127161358/http://hoohoo.ncsa.illinois.edu/cgi/ |archivedate=2010-01-27}} 3. ^CGI: Common Gateway Interface at w3.org 4. ^{{cite web |url=http://ken.coar.org/cgi/ |title=Common Gateway Interface RFC Project Page |archiveurl=https://web.archive.org/web/20130825202222/http://ken.coar.org/cgi/ |archivedate=2013-08-25}} 5. ^Mapping URLs to Filesystem Locations Apache HTTP Server Version 2.2 6. ^Nelson, Anne Fulcher, and Nelson, William Harris Morehead. (2001). Building Electronic Commerce with Web Database Constructions. Boston, MA: Addison Wesley. 7. ^CGI Primer (Mirror at citycat.ru) 8. ^{{Cite web|url=http://www.abacles.com/html-forms.html|title=Abacles HTML Forms|website=www.abacles.com|access-date=2016-04-06}} 9. ^{{cite thesis |author=Enrico Marino |date=11 September 2018 |title=Information Management in the Distributed Web |type=phd |publisher=Roma Tre University |url=http://phd.dia.uniroma3.it/wp-content/uploads/2018/03/Phd-Thesis-Enrico-Marino.pdf |access-date=11 February 2019}} External links
4 : Servers (computing)|Web 1.0|Web technology|World Wide Web |
随便看 |
|
开放百科全书收录14589846条英语、德语、日语等多语种百科知识,基本涵盖了大多数领域的百科知识,是一部内容自由、开放的电子版国际百科全书。