無投影片標題 - National Chi Nan University

Download Report

Transcript 無投影片標題 - National Chi Nan University

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
data 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
the html source for a web page
– Shell and Perl scripts are easier and are
more commonly used for CGI scripts.
CGI specification
• Define how these data are passed from the
server to the gateway program, and vice
versa
stdout
Network
CGI
Browser
stdin
cmd line
env var
CGI
Porgram
HTTP server
Server Machine
CGI relative
script
Relative Programs and Files
Client
Web server
Script
Workstation
IBM Compatible
Form
CGI program
<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_INFO, 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
which 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
Examples
CGI and Database
Network
Browser
CGI
Gateway
Porgram
Database
Interface
Database server
HTTP server
Server Machine
Gateway Program
Web Browser
HTML Form
HTTP Serv er
Information Entered by the User
Common Gateway Interface
User Authentication
Information Entered by the User
Access List
Gateway
Program
Database Interface
Web
Browser
Database interface parses
the received database query
HTTP Server
Interface Error Message
Common Gateway Interface
Error Message
Gateway program transfers
the received data into the
database query, which
then is passed to
database interface.
Gateway
Program
Database
Interface
Processes
Information
Database Query
Parser
Discoveries
Relays Error
Message
Error Message
Syntax Error
Database ServerDatabase interface
passes the query to
the database server
Web Browser
Results From Database
HTTP Server
After handling the query,
the database server feed
the query result back to
the database interface
Common Gateway Interface
Formatted Results
Gateway
Program
Processes
Information
Formats
Results
Database
Interface
Database Query
Verifies
Query
Query Results
Query
Results
Database
Server
Database Query
Query Results
Porcesses
Query
Local Database Server
user request
CGI
output
Web server
PC
Brow ser
Netw ork
key to
search
string
gateway
matched
data
Database
matched
data
Server machine
The database server and the Web server are in the same site
Remote Database Server
(homogeneous)
Network
CGI
Network
Web Browser
Gateway
HTTP server
Database
Interface
Database Serv er
Network
Network
Database Serv er
Web server Machine
¹Ï1-6
Database Serv er
Remote Database Server
( heterogeneous )
string
CGI
output
Web server
PC
Brow ser
Netw ork
matched
data
gateway
matched
data
Server machine
key to
search
Ethernet
user request
Database
oracle
key to
search
database
interf ace
Database
sybase
matched
data
Database
mSQL
Netw ork
Database
machine
Third party model
user request
CGI
output
PC
Brow ser
key to
search
string
gateway
matched
data
matched
data
Server Machine
matched
data
3rd-Party
Network Software
Server
Web server
Netw ork
key to
search
Netw ork
Database independent
Database
Server
Database Machine