Overview• A plain HTML document is static• A CGI program is executed in real-time, so
that it can output dynamic information.• CGI (Common Gateway Interface) is the
language or protocol that the browser uses to communicate the data from the form to the web server.
• A standard for interfacing external applications with information servers, such as HTTP or Web servers.
• When the user submits his/her answers on a form, the browser bundles them up and sends them to the web server, which passes them on to your CGI script/program for processing. – The web page itself does not process the dat
a entered on the form. Neither does the web server.
• A CGI script/program is any program which knows how to read that bundle of data.– The script/program must build up and return t
he html source for a web page– Shell and Perl scripts are easier and are mor
e commonly used for CGI scripts.
CGI specification
• Define how these data are passed from the server to the gateway program, and vice versa
Browser
CGIPorgram
Network
CGI
HTTP server
Server Machine
CGI relativescript
stdout
stdincmd lineenv var
<Form Action=/cgi-bin/php.cgi/~jlwang/php/test.html>. . .</Form>
CGI program: /cgi-bin/php.cgi
Script program: /~jlwang/php/test.html
<Form Action=/cgi-bin/pw.pl>. . .</Form>
CGI program: /cgi-bin/pw.pl
Communication with gateway programs
• Client to Server– URL query string– Extra path information in the URL– Message body (in HTML Forms)
• Server to Gateway program– Command-line argument– Standard input– Environment variables
• Gateway program to Server– Standard output
Client to Server• URL query string
– http://www.mcu.edu.tw/cgi-bin/ex_prog?query_info– Pass the query string query_info to the server– The server launches the gateway program ex_prog and
passes it the query string• Extra path information in the URL
– http://www.mcu.edu.tw/cgi-bin/ex_prog/dir/file?query_info– /dir/file: extra path information– Pass information using environment variables
• Message body– HTTP POST method within HTML FORM
– Pass information using standard I/O
Server to Gateway program• Command-line argument
– GET method within ISINDEX query
• Standard input– POST method
• Environment variables– Everything sent by the client, except POST
data– The query string – The extra path information – The request header
Gateway program to Server
• Standard output– The only way that gateway programs can
pass data to a client– Two parts
• A collection of server directives– Be used to compose the response header
• The data
CGI Environment Variables
• Server CGI
• About the Server
• Request-Specific Variables
• About the user
Server CGI
• The server uses command line arguments as well as environment variables.
• These environment variables are set when the server executes the gateway program.
About the Server• SERVER_SOFTWARE
– The name and version of the information server software answering the request (and running the gateway). Format: name/version
• SERVER_NAME – The server's hostname, DNS alias, or IP
address as it would appear in self-referencing URLs.
• GATEWAY_INTERFACE– The revision of the CGI specification to which
this server complies. Format: CGI/revision
Request-Specific Variables• SERVER_PROTOCOL
– The name and revision of the information protcol this request came in with.
– Format: protocol/revision (HTTP:1.0)• SERVER_PORT
– The port number to which the request was sent. • REQUEST_METHOD
– The method with which the request was made. For HTTP, this is "GET", "HEAD", "POST", etc.
• PATH_INFO – The extra path information, as given by the client.
• PATH_INFO – The extra path information, as given by the client.
• PATH_TRANSLATED – The server provides a translated version of PATH_IN
FO, which takes the path and does any virtual-to-physical mapping to it.
– http://www.mcu.edu.tw/cgi/cgiprogram.cgi/x/y/z– (Assume the document root path:/home1/www/docs)
• PATH_INFO = /a/b/c• PATH_TRANSLATED = /home1/www/docs/x/y/
z• SCRIPT_NAME
– A virtual path to the script being executed, used for self-referencing URLs.
• QUERY_STRING – The information which follows the ? in the URL whi
ch referenced this script. – http://www.mcu.edu.tw/cgi/test.cgi?mcu.edu.tw– QUERY_STRING= mcu.edu.tw
• AUTH_TYPE
– The protocol-specific authentication method used to validate the user.
• REMOTE_USER – If the server supports user authentication, and the
script is protected, this is the username they have authenticated as.
• REMOTE_IDENT – Remote user name retrieved from the server. Usage
of this variable should be limited to logging only. • CONTENT_TYPE
– For queries which have attached information, such as HTTP POST and PUT, this is the content type of the data.
• CONTENT_LENGTH
– The length of the said content as given by the client.
About the user• HTTP_ACCEPT
– The MIME types which the client will accept, as given by HTTP headers.
– Format: type/subtype, type/subtype • HTTP_USER_AGENT
– The browser the client is using to send the request.– Format: software/version library/version.
• REMOTE_HOST– Domain name of the user
• REMOTE_ADDR– IP address of the user
CGI: Output• Standard output
– Document generated by the script– Instructions to the server
• Scripts return – Output a document to their standard output– Output the location of the result document
(either a full URL or a local virtual path)
• The server normally adds the needed HTTP headers to the back-end's results
• Data stream– Header
• One or more lines of text, and is separated from the body by a blank line
– Body• Contains MIME-conforming data whose content
type must be reflected in the header• The server does not interpret or modify the
body in any way
CGI Output Example
print <<EOT;
Content-type: text/html
<HTML> <HEAD>
<TITLE> Search Results</TITLE>
</HEAD>
<BODY> <H1> Search Results </H1>
EOT
CGI and Database
Browser Database server
GatewayPorgram
DatabaseInterface
Network CGI
HTTP server
Server Machine
Gateway Program
Web BrowserHTML Form
HTTP Server
Common Gateway Interface
Access List
Information Entered by the User
User Authentication Information Entered by the User
GatewayProgram
Database Interface
WebBrowser
HTTP Server
Common Gateway Interface
Error Message
GatewayProgram
ProcessesInformation
Relays ErrorMessage
Database Query
Error Message
DatabaseInterface
ParserDiscoveries
Syntax Error
Interface Error Message
Gateway program transfersthe received data into the database query, which then is passed to database interface.
Database interface parses the received database query
Database Server
Web Browser
Common Gateway Interface
HTTP Server
Formatted Results
GatewayProgram
ProcessesInformation
FormatsResults
Database Query
Query Results
DatabaseInterface
VerifiesQuery
QueryResults
Database Query
Query Results
DatabaseServer
PorcessesQuery
Results From Database
Database interface passes the query to the database server
After handling the query,the database server feed the query result back to the database interface
Local Database Server
PC
Database
Server machineBrowser Network
gateway
user request
output
stringkey tosearch
matcheddata
matcheddata
CGI
Web server
The database server and the Web server are in the same site
Remote Database Server (homogeneous)
Web Browser
Database Server
Database Server
Database Server
GatewayDatabaseInterface
NetworkNetwork
Network
Network
Web server Machine
¹Ï1-6
HTTP server
CGI
Remote Database Server ( heterogeneous )
PC
gatewaydatabaseinterface
DatabasemSQL
Databasesybase
Databaseoracle
Ethe
rnet
user request
output
string
key tosearch key to
search
matcheddata
matcheddata
matcheddata
Browser Network Server machine NetworkDatabasemachine
CGI
Web server