Docsity

CGI Programming with Perl: Understanding Environment Variables and User Input, Slides of Web Programming and Technologies

Toggle sidebar
bg1
COMP519: Web Programming
Autumn 2011
The next lectures will teach you how to
use CGI in server-side programming
• CGI and its internal workings
• configuring the server
use environment variables in Perl
• show the environment
• check the client browser
• restrict access for specified domains
• see where the user comes from
access the input from a form with POST and GET methods
• use Query_String and STDIN
• decode the input
use extra path information
make a basic counting code for your web page
docsity.com
pf3
pf4
pf5
pf8
pf9
pfa
pfd
pfe
pff
pf12
pf13
pf14
pf15
pf16
pf17
pf18
pf19
pf1a
pf1b
pf1c
pf1d
pf1e
pf1f
pf20
pf21
pf22
pf23

Partial preview of the text

Download CGI Programming with Perl: Understanding Environment Variables and User Input and more Slides Web Programming and Technologies in PDF only on Docsity!

COMP519: Web Programming

Autumn 2011

The next lectures will teach you how to

^ use CGI in server-side programming

  • CGI and its internal workings• configuring the server

^ use environment variables in Perl

  • show the environment• check the client browser• restrict access for specified domains• see where the user comes from

^ access the input from a form with POST and GET methods

  • use Query_String and STDIN• decode the input

^ use extra path information ^ make a basic counting code for your web page

What Is CGI?

The Common Gateway Interface

(CGI) is a standard (protocol) for interfacing external

applications with information servers, such as HTTP or Web servers. A CGI program isexecuted in real-time, so that it can output dynamic information.

•^ The Web

server

can

call up

a

program

, while passing

user-

specific data

to the program.

•^ The

program

then

processes

that

data

and the

server

passes

the

that Forms Shopping, Booking. Gateways Search Engine, Database Virtual Documents Guestbook, Chat, Bulletin Board,Dictionary.

data

and the

server

passes

the

program's response

back to the

Web

browser

Internal Workings of CGI (cont.)

HTTP/1.

200

OK

Date:

Tue,

22

Nov^

2005

18:25:

GMT

Server:

Apache/2.0.

(Fedora)

Content-Length:91Connection:

close

Content-Type:

text/html;

charset=ISO-8859-

Welcome!Hello

World!

Idea 4. Input to CGI program (UNIX) • either from (

STDIN

), or

• from UNIX

environment variables

(%ENV hash in Perl)

Idea 5

.^ Output from CGI program (UNIX)

• either to the client directly, or •^ to^

STDOUT

as a data stream

•^ to^

STDOUT

as a data stream

Content-type:

text/html

Welcome!Hello

World!

Idea 6. Output as data stream (UNIX) HTTP header

+^ a blank line

+^ body

• if a complete HTTP header, to the client• if not, the server must do it.

Configuring the Server

•^

Before you can run CGI programs on your server, certain parameters in theserver configuration files must be modified (this has been done already for us

•^

For the UoL server you should proceed as follows.^ CGI scripts

must

go in the directory

$HOME/public_html/cgi

- bin/

(so mine are in

martin/public_html/cgi

- bin/ )

•^

Your

cgi-bin

directory must be readable and executable

by everyone, and the

Perl scripts in that directory must be readable and executable

by everyone.

$HOME/public_html/cgi

- bin/

(so mine are in

martin/public_html/cgi

- bin/ )

and are accessed using the

URL:

http://cgi.csc.liv.ac.uk/cgi-bin/cgiwrap/{user}/{script}

If they don't seem to work, try replacing

"cgiwrap

" with "

cgiwrapd

to see debugging output.

CGI Input

The CGI input information can be roughly broken into three groups:

Information about the client, server, and user ^ available in

CGI environment variables

Form data that the user supplied ^ available either in a

CGI environment variable

, or

See a trend here?

Environmental variables are available in Perl in a built-in hash called%ENV (note that, like all Perl variables, it is case-sensitive).

^ in

STDIN

Additional pathname information ^ available in

CGI environment variables

Environment Variables (%ENV)

1.^
AUTH_TYPE
The authentication method used to validate a user.
2.^
CONTENT_LENGTH
The length of the data (in bytes or the number of characters) passed to the CGI
program through standard input.
3.^
CONTENT_TYPE
The MIME type of the query data, such as "text/html".
4.^
DOCUMENT_ROOT
The directory from which Web documents are served.
5.^
GATEWAY_INTERFACE
The revision of the Common Gateway Interface that the server uses.
6.^
HTTP_ACCEPT
A list of the MIME types that the client can accept.
7.^
HTTP_FROM
The email address of the user making the request. Most browsers do not support this
variable.
8.^
HTTP_REFERER
The URL of the document that the client points to before accessing the CGI program.
9.^
HTTP_USER_AGENT
The browser the client is using to issue the request.
10.^
PATH_INFO
Extra path information passed to a CGI program.
11.^
PATH_TRANSLATED
The translated version of the path given by the variable PATH_INFO.

Show the Environment

#!/usr/local/bin/perl#^ CGI02.cgi COMP519print

"Content-type:

text/html

\n\n

";

print

"

\n";

#^ I^

actually

printed

a^ full DTD

here.

#!/usr/local/bin/perl#^ CGI01.pl

COMP foreach

$env_key

(keys

%ENV

){

print

"The

value

of^

$env_key

is^

$ENV

{$env_key

}^ \n";

} exit (0);

Can view in the shell CGI01.pl

in the prompt

Can view in the browser http://cgi.csc.liv.ac.uk/cgi- bin/cgiwrap/martin/CGI02. cgi

print

"

\n";

#^ I^

actually

printed

a^ full DTD

here.

print

"About

Environment

\n";

print

"About

Environment

\n";

print

""; foreach

$env_key

(keys

%ENV

){

print

"The

value

of^

$env_key

is^

$ENV

{$env_key

}^ \n";

} print

"

\n";

print

"\n

\n";

exit (0);

view the output page

Content-type:

text/html

\n\n

bin/cgiwrap/martin/CGI02. cgi in the browser Content-type:

text/plain

\n\n

Output can be different

Check the Client Browser

#!/usr/local/bin/perl# CGI03.cgi$good_sys = 'Linux|Unix';$client_browser
= $ENV{'
HTTP_USER_AGENT

Can check the browser type, and then display specific documents

1. get the name of the browser from the

HTTP_USER_AGENT

variable:

2. identify the files to return depending on the sys:^ $NET_document = "NET.html";

Welcome IE!

$NET_document = "NET.html"; $IE_document = "IE.html"; 3. check if the name is included in the list: if ($client_browser =~ /$good_sys/) {

$html_document = $NET_document;
} else {
$html_document = $IE_document;

IE! Are

you

a^ friend

of

Microsoft? WelcomeNET!You

are

a^ friend

of^

the

NET!

Restricting Access for Specified Domains

You can configure most servers to restrict access to your documents 1. Define the IP domain name and address that are considered local.^ #!/usr/local/bin/perl# CGI04.cgi COMP519$local_host = 'csc.liv.ac.uk';$local_address = '10.40|140.135';2. Find the IP numerical address for the remote user.^ $remote_address = $ENV{'

REMOTE_ADDR

3. Convert the IP address to its corresponding name @subnet_numbers = split(/./, $remote_address);$packed_address = pack("C4", @subnet_numbers);($remote_host)

= gethostbyaddr($packed_address, 2);

Restricting Access for Specified Domains (Cont.) $in_users

= "IN.html"; $out_users

=^ "OUT.html"; if(($remote_host

=~/$local_host$/)

&&^

($remote_address

=~/^($local_address)/)){

$html_document

=^ $in_users;

}^ else

{ $html_document

=^ $out_users;

4. Examine the remote host and check the remote address} 5. Open the document and print the information.^ print

"Content-type:

text/html

\n\n";

if^ (open

(HTML,

"<"

.$html_document))

{

while

()

{ print; } close

(HTML); }^ else

{ print

"Oops!

There

is^

a^ problem

with

the configuration

on^

this

system!

\n";

print

"Please inform

the

Webmaster

of^

the^

problem.

Thanks!

\n"; } exit(0);

view the output page

Where Did You Come From?

Can get information from what server (or document) the remote users came.

#!/usr/local/bin/perl# CGI05.cgi COMP519print "Content-type:
text/plain \n\n
$remote_address = $ENV{'
REMOTE_ADDR
$referral_address = $ENV{'
HTTP_REFERER

print "Hello user from $remote_address! \n";print "The last site you visited was: $referral_address. \n"print "Am I genius or what? \n"; exit (0); Now for the caveats.• First, not all browsers set this variable.• Second, if a user accesses your server first, right at startup, this variable willnot be set.• Third, if someone accesses your site via a bookmark or just by typing in theURL, the referring document is meaningless.

view the output page

Accessing Form Input

The CGI program can access the form input.

A Simple Form

Can use a form to solicit the information from the user.^

Working with GET

The server runs CGI07.cgi and puts"command=fortune

into

QUERY_STRING

#!/usr/local/bin/perl#CGI07.cgi COMP519print "Content-type: text/plain \n\n";$query_string = $ENV{'
QUERY_STRING

First, we should extract the information:($key_name, $value) = split (/=/, $query_string);Now, we're ready to execute the UNIX command: if ($value eq "

fortune
if ($value eq "
fortune
print '/usr/local/bin/fortune';
} elsif ($value eq "
finger
print '/usr/ucb/finger';
} else {
print '/usr/local/bin/date';

} exit (0); Since the GET method is used here, we can directly access by using the following URL: http://cgi.csc.liv.ac.uk/cgi-bin/cgiwrap/martin/CGI07.cgi

?command=fortune

view the output page