0% found this document useful (0 votes)
61 views25 pages

Unit 3 CGI Using PERL

Uploaded by

umesh701
Copyright
© Attribution Non-Commercial (BY-NC)
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
61 views25 pages

Unit 3 CGI Using PERL

Uploaded by

umesh701
Copyright
© Attribution Non-Commercial (BY-NC)
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPT, PDF, TXT or read online on Scribd
You are on page 1/ 25

UNIT 3

CGI using PERL


Introduction to CGI
GI is very lightweight interface that a program uses to handle requests from
a web server.

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 script share a common directory like /cgi or filename extension .cgi

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

RL provides the standard way to locate the resources on the web.

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

RL that include hostname are called the absolute URL’s.

g http://locahost/cgi/script.cgi

elative URL:

RL’s without scheme,host or port are called relative URL’s.


What happens when we follow a link?

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.

he request and response cycle Request


(includes header & sometimes body)

browser Web server


Response
(includes header & usually body)

Two important features of HTTP

1. It is request/response protocol.
2. The request & response contains the different information but the
structure of header and body remains the same.

header: contains the meta information i.e information about information


body: contains the content of the message.
he HTTP header /body message structure
Request line/status line

[METH] [REQUEST-URI] HTTP/[VER] 1. The first line in the header is


Known as request line in request
/ and status line in response.
HTTP/[VER] [CODE] [TEXT]
header Field1:
Value1
Header 2. The remainder is called header
Field2:
fields Value2 Fields .it contains name-value
Information.

3. If header fields contains multiple


Values then they are separated by
<html>
Commas.
.
.
body 4. Field names are not case sensitive.
.

</html> 5. Header fields don’t have to appear


In special order.

6. Every line in the header field must


Be terminated by carriage return.
Browser request

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

GET /index.html HTTP/1.1


HOST: localhost
.ACCEPT-LANGUAGE: en
USER-AGENT:Mozilla/4.0
.
.
The Request line

The first line of


an HTTP request includes the request method,a URL to the resource
Being
requested and the verison string of protocol. Request methods are case
sensitive and
uppercase.

GET: Ask the


server for given resource
HEAD: Used in
the same cases that a GET is used but only returns HTTP header and no content
POST: Ask the
server to modify information stored on the server
PUT: Ask the
server to create or replace a resource on the server
DELETE: Ask the
server to delete a resource on the server
CONNECT: Used
to allow secure SSL connections to tunnel through HTTP connections
OPTIONS: Asks
the server to list the request methods available to given resource
TRACE: Asks the
server to echo back the request headers as it received them
The request header
field lines
[fieldname]: [field-
value]

Fieldnames:

ost: specifies the target host name

ontent-length: specifies the length on bytes of the request content.

ontent-type: specifies the media type of the request.

uthentication: specifies the username & password of the user requesting the resource.

ser-agent: specifies the name, version,and platform of the client.

eferer: specifies the URL that referred the user to the current resource.

ookie: returns a name/value pair set by the server on a previous response


Server Responses

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

HTTP/ 1.1 200 OK

Version : 1.1 or 1.0


Status line: 1xx-low level transaction .not used in CGI scripts
2xx- all well with the request
3xx-some kind of redirections
4xx-indicates error.here server blames browser
5xx-indicates error.here server considers cgi script at fault
Server Headers

ontent-Base: Specifies the base URL for resolving all relative URLs within the
document.

ontent-length: specifies the length in bytes of the body

ontent-type: specifies the media type

ate: specifies the date and time when the response was sent.

tag:specifies the entity tag for the requested resource

ast-modified: specifies the lat date and time when the resource was last modified.

ocation: specifies the new location for the resource.

erver: Specifies the name and version of the web server


Proxies
roxy act as a Client as well as server.

roxy affects Request/Response cycle.


 It make impossible for web server to reliably identify the browser.
 Proxies often cache content.

dentifying Client

aching
Content Negotiation

t allows the client and servers to negotiate in best possible format for each
given response.

or Example: Document availability in multiple languages

our primary forms of content negotiation:


 Language
 Character set
 Media Type
 Encoding
The CGI environment

GI establishes a particular environment in which CGI script operate. This environment


includes such things as what current working directory the script starts in, what
variables are preset for it, where the standard file handles are directed and so on.

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.

UTH_TYPE: The authentication method used to validate a user.

ONTENT_LENGTH: The length of data passed to the


CGI
program via STDIN.

ONTENT_TYPE: The media type.

OCUMENT_ROOT: The directory from which static document are served

ATEWAY_INTERFACE: The revision of the CGI that the server uses.

ATH_INFO: Extra path information passed to a CGI program.

ATH_TRANSLATED: The translated version of the path given by PATH_INFO

UERY_STRING: The query information from the requested


URL.
EMOTE_ADDR: The remote IP address of the client.

EMOTE_HOST: The remote hostname of the client

EMOTE_IDENT: The user making the request

EMOTE_USER: The user’s login, authenticated by the web server

EQUSET_METHOD: The HTTP request method used for this request.

CRIPT_NAME: The URL path of the script being executed

ERVER_NAME: The server’s hostname or IP address.

ERVER_PORT: The port number of the host on which the server is listening

ERVER_PROTOCOL: The name and revision of the request protocol e.g.,”HTTP/1.1”

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_ACCEPT_CHARSET: A list of the character sets the client can accept.

TTP_ACCEPT_ENCODING: A list of the encodings the client can accept.

TTP_ACCEPT_LANGUAGE: A list of the languages the client can accept.

TTP_COOKIE: A name-value pair previously set by the server.

TTP_FROM: The email address of the user making the request.

TTP_HOST: The hostname of the server from the requested URL

TTP_REFERER: The URL of the document that directed the user to this CGI program

TTP_USER_AGENT: The name and version of the client’s browser.


CGI output

CGI script Web server


CGI output

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.

wo encoding scheme: application/x-www-form-urlencoded

multipart/form-data
Decoding Form Input
ead the query string from $ENV{QUERY_STRING}

f the $ENV{REQUEST_METHOD} is POST, determine the size of the request using


$ENV{CONTENT_LENGTH} and read the amount of data read from the query string, if present
(this should be joined with”&”).

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.

ecode the URL-encoded character in the name and value.

ssociate each name with its value(s)


A form sends
its parameters as the body of the POST request, or as the query string of a GET request.
ASSIGNMENT
rchitectural guidelines

oding Guidelines

fficiency and Optimization

Submission Last Date: 20th Oct 2010


HANK YOU!

You might also like