Unit 3 CGI Using PERL
Unit 3 CGI Using PERL
hen a web server gets a request for CGI Script, the web server executes the
CGI script as another process the server passes this process some
parameters and collects its output which it then return to the client.
GI programs get their input from standard input (STDIN) and environment
variables.
nce the CGI program starts running it sends its output back to the server via
standard output (STDOUT).
CGI Application Execution
Alternative Technologies
Why PERL?
•Easy to learn
•fast
•Rapid development
•Portable
•Powerful string manipulation operator, regular expression.
•Handle binary data
•Strict variable type does not require
•Interface with external applications.
Alternatives to CGI
SP
HP
oldFusion
ava Servlets
astCGI
od_perl
URL
lements of URL
t consist of scheme,host name,a port number ,a path ,query string and a fragment identifier any of which can be omitted under certain circumstances.
host path Fragment
http://www.abc.com :80/cgi/calender.cgi?month=july#week
Query
scheme Port
bsolute URL
g http://locahost/cgi/script.cgi
elative URL:
S
tep 1: Parsing the URL
T
he first thing the browser has to do is to look at the URL of the new document to find out how to get hold of the new
document. Most URLs have this basic form: "protocol://server/request-URI.
S
tep 2: Sending the request
U
sually, the protocol is "http". To retrieve a document via HTTP the browser transmits the following request to the server:
"GET /request-URI HTTP/version", where version tells the server which HTTP version is used .
S
tep 3: The server response
W
hen the server receives the HTTP request it locates the appropriate document and returns it. However, an
HTTP response is required to have a particular form
HTTP
t is the common language that web browsers and web servers use to communicate with each
other on the internet.
1. It is request/response protocol.
2. The request & response contains the different information but the
structure of header and body remains the same.
he user provides the browser with URL and browser create a HTTP request .
he structure
[METH] of HTTP request
[REQUEST-URI] HTTP/[VER] Request line
[fieldname1]: [field-value1]
Header line
[fieldname2]: [field-value2]
for eg
Web browser generated the following request when it was asked to fetch the URL.
http://localhost/index.html
Fieldnames:
uthentication: specifies the username & password of the user requesting the resource.
eferer: specifies the URL that referred the user to the current resource.
Status line
TTP/[VER] [CODE] [TEXT]
Header fields
ield1: Value1
Body
Theield2:
status line
Value2
It includes the protocol and the version just as in HTTP request,3 digit status code,as
Well..as a text version
Document of ...
contents the status
ontent-Base: Specifies the base URL for resolving all relative URLs within the
document.
ate: specifies the date and time when the response was sent.
ast-modified: specifies the lat date and time when the resource was last modified.
dentifying Client
aching
Content Negotiation
t allows the client and servers to negotiate in best possible format for each
given response.
ile Handles
TDIN
TDOUT
TDERR
Environment Variables
CGI
scripts are given predefined environment variables that provide information about the web server as well
as the client. Environment variables are available to script via global hash %ENV.
ERVER_PORT: The port number of the host on which the server is listening
ERVER_SOFTWARE: The name and version of the server software that is answering the client request.
Additional CGI Environment Variables
TTP_ACCEPT: A list of the media types the client can accept.
TTP_REFERER: The URL of the document that directed the user to this CGI program
200 Ok
201 No Response
Content Location Status code 301 moved permanently
type header 302 Found
303 See other
Specify the media 307 Temporary redirect
Specifies a URL 400 Bad request
type of the content
to redirect the client. 401 Unauthorized
that will follow.
here 403 Forbidden
Usually its HTML
internal Redirection 404 Not found
format
occurs 405 Not allowed
408 Request timed out
500 Internal server error
503 Service unavailable
504 Gateway timed out
Sending data to the server
Input type=”checkbox” NAME=“send_email” VALUE=“yes”>
f this chck box is checked, then the option send_email with a value of
yes is send to the server.
efore the browser can send form option data to the server, the
browser must encode it.
multipart/form-data
Decoding Form Input
ead the query string from $ENV{QUERY_STRING}
plit the result on the “&” character, which seperates name-value pairs( the format is
name=value&name=value…)
plit each name-value pair on the “s” character to get the name and value.
oding Guidelines