SlideShare una empresa de Scribd logo
1 de 25
Descargar para leer sin conexión
CGI Programming
         Phil Spector
 Statistical Computing Facility
    Department of Statistics
University of California, Berkeley




                1
How a Web Browser Works
When a user enters a URL in a browser, the browser sends a
request to a server for a particular resource, such as an HTML page
or an image, and the server responds by sending some headers that
describe what it’s sending followed by the actual content.
If an HTML page contains images, flash animations, etc. the
process is repeated for each item – the browser then displays
everything in the appropriate way.
If the web server is configured to allow it, requests made to URLs
in certain directories will run a program which generates the
appropriate headers and content instead of just sending a static
page. The mechanism that allows this is known as CGI (Common
Gateway Interface), and such programs are known as CGI
programs.


                                 2
Browser to Server Communication
When a browser makes a request for a resource from a web server,
what information is actually transmitted? We can use a simple
program that sits will echo any information sent to it, and then
point our browser at the program and see what happens.
In python, here’s such a program, which I’ll call webecho:
#!/usr/bin/python
import socket,os,sys

srvsocket = socket.socket(socket.AF_INET,socket.SOCK_STREAM)
srvsocket.bind(("",1888))
srvsocket.listen(5)
while 1:
     clisocket,addr = srvsocket.accept()
     now = clisocket.recv(1024)
     clisocket.send(now)
     clisocket.close()


                                 3
A Simple Request
With the webecho program running, let’s make a simple request
and see what happens. If I point a web browser at
http://localhost:1888/something, here’s what’s displayed:
GET /something HTTP/1.1
Host: localhost:1888
User-Agent: Mozilla/5.0 (X11; U; Linux x86_64; en-US; rv:1.7.12)
            Gecko/20051010 Firefox/1.0.7 (Ubuntu package 1.0.7)
Accept: text/xml,application/xml,application/xhtml+xml,text/html;
        q=0.9,text/plain;q=0.8,image/png,*/*;q=0.5
Accept-Language: en-us,en;q=0.5
Accept-Encoding: gzip,deflate
Accept-Charset: ISO-8859-1,utf-8;q=0.7,*;q=0.7
Keep-Alive: 300
Connection: keep-alive


                               4
HTML Forms
A variety of form elements are available through HTML, and are
presented in an interactive learning tool at:
http://www.w3schools.com/html/html_forms.asp
While I’ll show examples of some form elements, I suggest
consulting the w3schools web page for additional details.
One important type of form element is the type=hidden form.
When you have a multi-screen CGI program, using this type of
form allows you to pass information between the different screens
without the information being visible to the user.




                                5
Getting information to CGI Programs
What makes CGI programming interesting is that it can get
information from users through various HTML form elements, like
entry fields, drop-down menus, and file upload dialogs. How is that
information transmitted to the server? The first step is producing a
page that contains a form. Here’s the html for a simple form that
will talk to the webecho program:
<form action=’http://localhost:1888’ method=get>
<input type=text name=postvar><br>
<input type=submit value=’GET’>
</form>
Here’s what shows up in a web browser:




                                  6
Getting information to CGI Programs (cont’d)
When the phrase ”Hello, world” is entered in the form, and the
submit button pressed, here’s what appears in the browser window:
GET /?getvar=Hello%2C+world HTTP/1.1
Host: localhost:1888
User-Agent: Mozilla/5.0 (X11; U; Linux x86_64; en-US; rv:1.7.12)
            Gecko/20051010 Firefox/1.0.7 (Ubuntu package 1.0.7)
Accept: text/xml,application/xml,application/xhtml+xml,text/html;
        q=0.9,text/plain;q=0.8,image/png,*/*;q=0.5
Accept-Language: en-us,en;q=0.5
Accept-Encoding: gzip,deflate
Accept-Charset: ISO-8859-1,utf-8;q=0.7,*;q=0.7
Keep-Alive: 300
Connection: keep-alive
Referer: http://springer/~s133ar/cform.html


                                7
Getting information to CGI Programs (cont’d)
The GET method sends information to the browser by adding
name/value pairs, possibly separated by ampersands (&), to the
URL after a question mark. These URLs are encoded to handle
characters not allowed in URLs. URLs constructed this way will
communicate with the web server regardless of the method used to
send the information, and are the only way to invoke a CGI
program without a surrounding form.
An alternative method of sending information to a web server that
doesn’t display the information in the web server’s address bar is
known as POST. Here’s a form that uses this method:
<form action=’http://localhost:1888’ method=post>
<input type=text name=postvar><br>
<input type=submit value=’POST’>
</form>


                                8
The POST method
Here’s the result of submitting Hello, world through the form
using the POST method:
POST / HTTP/1.1
Host: localhost:1888
User-Agent: Mozilla/5.0 (X11; U; Linux x86_64; en-US; rv:1.7.12)
            Gecko/20051010 Firefox/1.0.7 (Ubuntu package 1.0.7)
Accept: text/xml,application/xml,application/xhtml+xml,text/html;
        q=0.9,text/plain;q=0.8,image/png,*/*;q=0.5
Accept-Language: en-us,en;q=0.5
Accept-Encoding: gzip,deflate
Accept-Charset: ISO-8859-1,utf-8;q=0.7,*;q=0.7
Keep-Alive: 300
Connection: keep-alive
Referer: http://springer/~s133ar/cform1.html
Content-Type: application/x-www-form-urlencoded
Content-Length: 22

postvar=Hello%2C+world


                                  9
The POST method (cont’d)
The URL is no longer changed, and the name/value pair
information is sent by the browser after it has sent the headers. The
POST method can also be used for file uploads. Here’s the HTML
for a form which will accept a file name and upload it to the server:
<form action=’http://localhost:1888’ method=post
               enctype=’multipart/form-data’>
<input type=file name=’myfile’>
<input type=submit value=’Upload’>
</form>

Here’s how it looks in a browser (the Browse button is added
automatically by the browser):




                                 10
File Upload
Suppose I upload a small text file – here’s what the browser sends
to the server:
POST / HTTP/1.1
Host: localhost:1888
User-Agent: Mozilla/5.0 (X11; U; Linux x86_64; en-US; rv:1.7.12) Gecko/20051010 Firefox/1.0.7 (Ubuntu package 1.0.7)
Accept: text/xml,application/xml,application/xhtml+xml,text/html;q=0.9,text/plain;q=0.8,image/png,*/*;q=0.5
Accept-Language: en-us,en;q=0.5
Accept-Encoding: gzip,deflate
Accept-Charset: ISO-8859-1,utf-8;q=0.7,*;q=0.7
Keep-Alive: 300
Connection: keep-alive
Referer: http://springer/~s133ar/cform2.html
Content-Type: multipart/form-data; boundary=---------------------------1741722292794821883444112406
Content-Length: 687

-----------------------------1741722292794821883444112406
Content-Disposition: form-data; name="myfile"; filename="small.txt"
Content-Type: text/plain

When you type the address of a web page (i.e. a URL or Universal
Resource Locator) into a browser,or click a link which refers to
a URL, a request is made to a computer on the internet to send the
contents of a web page to your browser. Web pages are written
in a language known as HTML (Hypertext Markup Language), and your
web browser knows how to translate HTML into text, pictures, links,
animations or whatever else the designer of the web page had in mind.

-----------------------------1741722292794821883444112406--



                                                      11
File Upload (cont’d)
Uploading the file resulted in the addition of some headers, similar
to those used to send attachments in email messages. The main
difference is that even binary files can be transfered in this way,
since the HTTP protocol does not disturb the eighth bit of its
input the way that SMTP does.
Now that we’ve seen how information is transmitted from the
browser to the server, let’s consider the communication in the
opposite direction.




                                12
Server to Browser Communication
The underlying principle behind CGI programs is that the standard
output of your CGI program is sent to the browser, so your CGI
programs must generate HTML to produce your desired output.
But just as we saw that the browser inserts some headers before it
sends information (if there is any) to the server, the server needs to
send at least one header line to the browser, to let it know that
HTML will follow. Thus, the first thing that any CGI program
should do is to output a header like this:
Content-type: text/html
It’s also the CGI program’s responsibility to send a completely
empty line to signal the end of the headers.



                                  13
Cookies
Cookies are text stored on a remote computer when they access
your CGI program. Cookies can serve as an alternative to hidden
variables, or as way to remember users when they access your CGI
program again.
Cookies are sent through outgoing headers as follows:
Set-Cookie: cookiename=cookievalue

This produces a cookie that expires when the user closes their
browser.
To make persistent cookies, provide an expiration date as follows
after the above specification:
; expires=Monday, 01-May-06 00:00:00 GMT

The cookie, sent as a name/value pair, will be passed to your CGI
program automatically whenever a browser storing your cookie
returns to your HTML page or CGI program.
                                14
The CGI Standard
The CGI standard insures that information is properly transfered
between the browser and the web server, and between the web
server and the browser, by running CGI programs in an
appropriate environment.
 1. Headers from the browser are converted to environmental
    variables.
 2. Information from the browser that was after the headers (if
    any) is placed in standard input for the CGI program.
 3. Standard output from the CGI program is directed to the web
    browser.
Each language that is appropriate for CGI scripting will provide
tools to make this information available to your CGI program in a
convenient form.

                                15
What do CGI Interfaces Provide?
The most important feature that a CGI interface provides is a
means of getting the values of the CGI form variables into the
programming environment. All programs with a CGI interface
provide a way of getting this information regardless of the method
(GET or POST) that was used.
For file uploads, there should an option to avoid reading the entire
file into memory.
Since additional information is provided through environmental
variables, there should be a simple way of accessing these variables.
Since standard output from the CGI program is directed to the
browser, many CGI interfaces provide helper functions to generate
HTML, as well as convenience functions to generate the required
header lines, although it’s often easier to do this by yourself.

                                 16
Some Useful CGI Environmental Variables
Name              Contents
HTTP COOKIE       Persistent data stored in cookies
HTTP REFERER      URL of refering document
HTTP USER AGENT   Type of browser being used
QUERY STRING      URL Fragment after ?
REMOTE ADDR       IP Address of user
REMOTE HOST       Hostname of user
REMOTE USER       Username, if authentication was used
SERVER NAME       Hostname of server
SERVER PORT       Port number of server
SERVER SOFTWARE   Name and version of server software


                             17
Security of CGI Programs
Remember that CGI programs are accessible to anyone who can
access the web server that hosts the program, so extra care is
necessary to make sure your programs are secure.
The cardinal rule is to make sure that any user input that gets
passed to the operating system does not contain any unusual
characters.
In fact, it may be wise to avoid calling any operating system
commands that include user input.
It is not uncommon for CGI programs to set their command path
(through environmental variables) to one limited to just those
directories necessary for execution of the CGI program. On a
UNIX system, an example would be a path of /bin:/usr/bin.


                                 18
A Simple Example
A form to allow the user to choose an ice cream flavor is generated
by the following code:
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 3.2//EN">
<head title=’Pick a Flavor’>
<html><body>
<h1>Ice Cream Flavors</h1>
Please choose your favorite flavor:<br>
<form action=’http://localhost/~spector/cgi-bin/icecream.py’ method=’post’>
<select name="flavor" >
<option value="Chocolate Chip">Chocolate Chip</option>
<option value="Strawberry">Strawberry</option>
<option value="Rum Raisin">Rum Raisin</option>
<option value="Vanilla">Vanilla</option>
</select>
<input type=’submit’ name=’Submit’>
</form>
</body></html>




                                                      19
Processing the Form: Perl
Here’s a perl script to process the ice cream form:
#!/usr/bin/perl

use CGI;

$q = new CGI();
$flavor = $q->param(’flavor’);

print   $q->header();
print   $q->start_html(’Ice Cream Example’);
print   $q->h1(’Ice Cream Flavor’);
print   "$flavor is a good choice, I like it too";
print   $q->end_html();


                                 20
Processing the Form: Python
#!/usr/bin/python

import cgi

f = cgi.FieldStorage()
flavor = f[’flavor’].value

print   ’Content-type: text/htmlnn’
print   ’<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 3.2//EN">’
print   ’<head title="Ice Cream Example">’
print   ’<html><body>’
print   ’<h1>Ice Cream Flavors</h1>’
print   ’%s is a good choice, I like it too’ % flavor
print   ’</body></html>’

                             21
Combo Forms
In the previous example, the proper action had to be coded into
the HTML file that presented the form. In general, separating the
form from the code may make it more difficult to maintain your
program. In addition, it’s often useful to generate HTML forms
through a program, instead of hardcoding the form.
Combo forms are programs that first check to see if form data has
been received – if not, they generate the necessary form, refering
back to themselves as the action. They are especially handy for
multi-form transactions, since they put all of the programs in a
single file, and eliminate the need for separate static HTML pages.




                                22
Combo Form in Perl
#!/usr/bin/perl
use CGI;

$q = new CGI();

@flavors = (’Chocolate Chip’,’Strawberry’,’Rum Raisin’,’Vanilla’);

if (not defined $q->param()){    # called directly
   print $q->header(),
         $q->start_html(’Ice Cream Example’),
         $q->h1(’Ice Cream Flavors’);
   print $q->startform({action=>’icecream1.pl’,method=>’POST’}),
         $q->popup_menu(-name=>’flavor’,-values=>@flavors),
         $q->submit(),
         $q->end_form(),$q->end_html();
}
else{

    $flavor = $q->param(’flavor’);

    print   $q->header();
    print   $q->start_html(’Ice Cream Example’);
    print   $q->h1(’Ice Cream Flavor’);
    print   "$flavor is a good choice, I like it too";
    print   $q->end_html();
}




                                                         23
Combo Form in Python
#!/usr/bin/python
import cgi

f = cgi.FieldStorage()

print ’Content-type: text/htmlnn’
print ’<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 3.2//EN">’

flavors = (’Chocolate Chip’,’Strawberry’,’Rum Raisin’,’Vanilla’)

if len(f.keys()) == 0:   # called directly
        print ’’’<head><title>Pick a Flavor</title></head>
<html><body>
<h1>Ice Cream Flavors</h1>
Please choose your favorite flavor:<br>
<form action=’icecream1.py’ method=’post’>
<select name="flavor" > ’’’
        for f in flavors:
              print ’<option value="%s">%s</option>’ % (f,f)
        print ’’’</select>
<input type=’submit’ name=’Submit’>
</form>
</body></html>’’’
else:
        flavor = f[’flavor’].value
        print ’’’<head><title>Ice Cream Example</title></head>
<html><body>
<h1>Ice Cream Flavors</h1>
%s is a good choice, I like it too’’’ % flavor
        print ’</body></html>’



                                                      24
Debugging CGI Scripts
1. Make sure the Content-type header line is being generated.
2. Make sure that the first line of the CGI program indicates the
   location of the executable that will run the program.
3. Make sure that the script is executable, and any files which it
   accesses have appropriate permissions for the user under which
   the CGI program will run.
4. Execute the script from the command line to find syntax errors
5. Errors are usually redirected to the server’s error log, which
   may not be accessible. Redirecting standard error to standard
   output will display messages in the browser.
6. There may be special debugging facilities in the language of
   your choice.

                               25

Más contenido relacionado

La actualidad más candente

Pushing the web — WebSockets
Pushing the web — WebSocketsPushing the web — WebSockets
Pushing the web — WebSocketsRoland M
 
Bandwidth limiting howto
Bandwidth limiting howtoBandwidth limiting howto
Bandwidth limiting howtoDien Hien Tran
 
Pushing Datatothe Browserwith Comet Ajax W
Pushing Datatothe Browserwith Comet Ajax WPushing Datatothe Browserwith Comet Ajax W
Pushing Datatothe Browserwith Comet Ajax Wrajivmordani
 
From zero to almost rails in about a million slides...
From zero to almost rails in about a million slides...From zero to almost rails in about a million slides...
From zero to almost rails in about a million slides...david_e_worth
 
PHP Training: Module 1
PHP Training: Module 1PHP Training: Module 1
PHP Training: Module 1hussulinux
 
HTTP by Hand: Exploring HTTP/1.0, 1.1 and 2.0
HTTP by Hand: Exploring HTTP/1.0, 1.1 and 2.0HTTP by Hand: Exploring HTTP/1.0, 1.1 and 2.0
HTTP by Hand: Exploring HTTP/1.0, 1.1 and 2.0Cory Forsyth
 
16network Programming Servers
16network Programming Servers16network Programming Servers
16network Programming ServersAdil Jafri
 
15network Programming Clients
15network Programming Clients15network Programming Clients
15network Programming ClientsAdil Jafri
 
WebSockets wiith Scala and Play! Framework
WebSockets wiith Scala and Play! FrameworkWebSockets wiith Scala and Play! Framework
WebSockets wiith Scala and Play! FrameworkFabio Tiriticco
 
Evolution of HTTP - Miran Al Mehrab
Evolution of HTTP - Miran Al MehrabEvolution of HTTP - Miran Al Mehrab
Evolution of HTTP - Miran Al MehrabCefalo
 
Using Websockets in Play !
Using Websockets in Play !Using Websockets in Play !
Using Websockets in Play !Knoldus Inc.
 
Using Websockets with Play!
Using Websockets with Play!Using Websockets with Play!
Using Websockets with Play!Andrew Conner
 
Realtime Communication Techniques with PHP
Realtime Communication Techniques with PHPRealtime Communication Techniques with PHP
Realtime Communication Techniques with PHPWaterSpout
 

La actualidad más candente (20)

Pushing the web — WebSockets
Pushing the web — WebSocketsPushing the web — WebSockets
Pushing the web — WebSockets
 
SPDY - or maybe HTTP2.0
SPDY - or maybe HTTP2.0SPDY - or maybe HTTP2.0
SPDY - or maybe HTTP2.0
 
Bandwidth limiting howto
Bandwidth limiting howtoBandwidth limiting howto
Bandwidth limiting howto
 
Pushing Datatothe Browserwith Comet Ajax W
Pushing Datatothe Browserwith Comet Ajax WPushing Datatothe Browserwith Comet Ajax W
Pushing Datatothe Browserwith Comet Ajax W
 
From zero to almost rails in about a million slides...
From zero to almost rails in about a million slides...From zero to almost rails in about a million slides...
From zero to almost rails in about a million slides...
 
PHP Training: Module 1
PHP Training: Module 1PHP Training: Module 1
PHP Training: Module 1
 
HTTP by Hand: Exploring HTTP/1.0, 1.1 and 2.0
HTTP by Hand: Exploring HTTP/1.0, 1.1 and 2.0HTTP by Hand: Exploring HTTP/1.0, 1.1 and 2.0
HTTP by Hand: Exploring HTTP/1.0, 1.1 and 2.0
 
Web sockets in Java
Web sockets in JavaWeb sockets in Java
Web sockets in Java
 
ZN27112015
ZN27112015ZN27112015
ZN27112015
 
16network Programming Servers
16network Programming Servers16network Programming Servers
16network Programming Servers
 
15network Programming Clients
15network Programming Clients15network Programming Clients
15network Programming Clients
 
WebSockets in JEE 7
WebSockets in JEE 7WebSockets in JEE 7
WebSockets in JEE 7
 
WebSockets wiith Scala and Play! Framework
WebSockets wiith Scala and Play! FrameworkWebSockets wiith Scala and Play! Framework
WebSockets wiith Scala and Play! Framework
 
Evolution of HTTP - Miran Al Mehrab
Evolution of HTTP - Miran Al MehrabEvolution of HTTP - Miran Al Mehrab
Evolution of HTTP - Miran Al Mehrab
 
HTTP2 is Here!
HTTP2 is Here!HTTP2 is Here!
HTTP2 is Here!
 
Using Websockets in Play !
Using Websockets in Play !Using Websockets in Play !
Using Websockets in Play !
 
Http request&response
Http request&responseHttp request&response
Http request&response
 
Using Websockets with Play!
Using Websockets with Play!Using Websockets with Play!
Using Websockets with Play!
 
Http Protocol
Http ProtocolHttp Protocol
Http Protocol
 
Realtime Communication Techniques with PHP
Realtime Communication Techniques with PHPRealtime Communication Techniques with PHP
Realtime Communication Techniques with PHP
 

Destacado

Computer generated imaginary
Computer generated imaginaryComputer generated imaginary
Computer generated imaginaryPratik Gondaliya
 
CGI Technology Research
CGI Technology ResearchCGI Technology Research
CGI Technology Researchshanni77
 
Cgi animation
Cgi animationCgi animation
Cgi animationNazish
 
Computer generated images in movies
Computer generated images in moviesComputer generated images in movies
Computer generated images in moviesStephen Halley
 
Cg film and animation presentation
Cg film and animation presentationCg film and animation presentation
Cg film and animation presentationMeng Eu
 
Computer Generated Imagery (CGI)
Computer Generated Imagery (CGI)Computer Generated Imagery (CGI)
Computer Generated Imagery (CGI)Pribhat Sharma
 
digital watermarking
digital watermarkingdigital watermarking
digital watermarkingBharath
 
Perl ウェブ開発の中世〜CGI と Plack の間〜
Perl ウェブ開発の中世〜CGI と Plack の間〜Perl ウェブ開発の中世〜CGI と Plack の間〜
Perl ウェブ開発の中世〜CGI と Plack の間〜鉄次 尾形
 

Destacado (16)

CGI Sucks
CGI SucksCGI Sucks
CGI Sucks
 
HTTP
HTTPHTTP
HTTP
 
Computer generated imaginary
Computer generated imaginaryComputer generated imaginary
Computer generated imaginary
 
CGI Technology Research
CGI Technology ResearchCGI Technology Research
CGI Technology Research
 
Kvotu teleradio
Kvotu teleradioKvotu teleradio
Kvotu teleradio
 
CGI
CGICGI
CGI
 
CGI technology in movie
CGI technology in movieCGI technology in movie
CGI technology in movie
 
Cgi animation
Cgi animationCgi animation
Cgi animation
 
Computer generated images in movies
Computer generated images in moviesComputer generated images in movies
Computer generated images in movies
 
Cg film and animation presentation
Cg film and animation presentationCg film and animation presentation
Cg film and animation presentation
 
Cgi
CgiCgi
Cgi
 
Computer Generated Imagery (CGI)
Computer Generated Imagery (CGI)Computer Generated Imagery (CGI)
Computer Generated Imagery (CGI)
 
digital watermarking
digital watermarkingdigital watermarking
digital watermarking
 
CGI Presentation
CGI PresentationCGI Presentation
CGI Presentation
 
Perl ウェブ開発の中世〜CGI と Plack の間〜
Perl ウェブ開発の中世〜CGI と Plack の間〜Perl ウェブ開発の中世〜CGI と Plack の間〜
Perl ウェブ開発の中世〜CGI と Plack の間〜
 
robotics ppt
robotics ppt robotics ppt
robotics ppt
 

Similar a How CGI Programming Works (20)

CGI by rj
CGI by rjCGI by rj
CGI by rj
 
Web Services 2009
Web Services 2009Web Services 2009
Web Services 2009
 
Web Services 2009
Web Services 2009Web Services 2009
Web Services 2009
 
WebSockets Everywhere: the Future Transport Protocol for Everything (Almost)
WebSockets Everywhere: the Future Transport Protocol for Everything (Almost)WebSockets Everywhere: the Future Transport Protocol for Everything (Almost)
WebSockets Everywhere: the Future Transport Protocol for Everything (Almost)
 
Python cgi programming
Python cgi programmingPython cgi programming
Python cgi programming
 
Unit 02: Web Technologies (2/2)
Unit 02: Web Technologies (2/2)Unit 02: Web Technologies (2/2)
Unit 02: Web Technologies (2/2)
 
Web II - 02 - How ASP.NET Works
Web II - 02 - How ASP.NET WorksWeb II - 02 - How ASP.NET Works
Web II - 02 - How ASP.NET Works
 
5-WebServers.ppt
5-WebServers.ppt5-WebServers.ppt
5-WebServers.ppt
 
Fm 2
Fm 2Fm 2
Fm 2
 
Building interactivity with websockets
Building interactivity with websocketsBuilding interactivity with websockets
Building interactivity with websockets
 
V2 peter-lubbers-sf-jug-websocket
V2 peter-lubbers-sf-jug-websocketV2 peter-lubbers-sf-jug-websocket
V2 peter-lubbers-sf-jug-websocket
 
HTML 5
HTML 5HTML 5
HTML 5
 
Browser Security
Browser SecurityBrowser Security
Browser Security
 
Introduction about-ajax-framework
Introduction about-ajax-frameworkIntroduction about-ajax-framework
Introduction about-ajax-framework
 
Servletv1 nt
Servletv1 ntServletv1 nt
Servletv1 nt
 
Cgi
CgiCgi
Cgi
 
maXbox Arduino Tutorial
maXbox Arduino TutorialmaXbox Arduino Tutorial
maXbox Arduino Tutorial
 
CSU33012-I-microservices.pdf
CSU33012-I-microservices.pdfCSU33012-I-microservices.pdf
CSU33012-I-microservices.pdf
 
Taking a Quantum Leap with Html 5 WebSocket
Taking a Quantum Leap with Html 5 WebSocketTaking a Quantum Leap with Html 5 WebSocket
Taking a Quantum Leap with Html 5 WebSocket
 
Maxbox starter18
Maxbox starter18Maxbox starter18
Maxbox starter18
 

Más de AkramWaseem

Mseduebookexcitinglearningweb Final 120914022330 Phpapp02
Mseduebookexcitinglearningweb Final 120914022330 Phpapp02Mseduebookexcitinglearningweb Final 120914022330 Phpapp02
Mseduebookexcitinglearningweb Final 120914022330 Phpapp02AkramWaseem
 
Xml Messaging With Soap
Xml Messaging With SoapXml Messaging With Soap
Xml Messaging With SoapAkramWaseem
 
Html5 Cheat Sheet
Html5 Cheat SheetHtml5 Cheat Sheet
Html5 Cheat SheetAkramWaseem
 
Ajax Tags Advanced
Ajax Tags AdvancedAjax Tags Advanced
Ajax Tags AdvancedAkramWaseem
 
Ascii Table Characters
Ascii Table CharactersAscii Table Characters
Ascii Table CharactersAkramWaseem
 
Www Kitebird Com Articles Pydbapi Html Toc 1
Www Kitebird Com Articles Pydbapi Html Toc 1Www Kitebird Com Articles Pydbapi Html Toc 1
Www Kitebird Com Articles Pydbapi Html Toc 1AkramWaseem
 
Scripts Python Dbapi
Scripts Python DbapiScripts Python Dbapi
Scripts Python DbapiAkramWaseem
 
Random And Dynamic Images Using Python Cgi
Random And Dynamic Images Using Python CgiRandom And Dynamic Images Using Python Cgi
Random And Dynamic Images Using Python CgiAkramWaseem
 
Python And My Sq Ldb Module
Python And My Sq Ldb ModulePython And My Sq Ldb Module
Python And My Sq Ldb ModuleAkramWaseem
 
Internet Programming With Python Presentation
Internet Programming With Python PresentationInternet Programming With Python Presentation
Internet Programming With Python PresentationAkramWaseem
 
Docs Python Org Howto Webservers Html
Docs Python Org Howto Webservers HtmlDocs Python Org Howto Webservers Html
Docs Python Org Howto Webservers HtmlAkramWaseem
 

Más de AkramWaseem (20)

Mseduebookexcitinglearningweb Final 120914022330 Phpapp02
Mseduebookexcitinglearningweb Final 120914022330 Phpapp02Mseduebookexcitinglearningweb Final 120914022330 Phpapp02
Mseduebookexcitinglearningweb Final 120914022330 Phpapp02
 
Xml Messaging With Soap
Xml Messaging With SoapXml Messaging With Soap
Xml Messaging With Soap
 
Xhtml Basics
Xhtml BasicsXhtml Basics
Xhtml Basics
 
Uml Tutorial
Uml TutorialUml Tutorial
Uml Tutorial
 
Xhtml Basics
Xhtml BasicsXhtml Basics
Xhtml Basics
 
Html5 Cheat Sheet
Html5 Cheat SheetHtml5 Cheat Sheet
Html5 Cheat Sheet
 
Ajax Tags Advanced
Ajax Tags AdvancedAjax Tags Advanced
Ajax Tags Advanced
 
Ascii Table Characters
Ascii Table CharactersAscii Table Characters
Ascii Table Characters
 
Www Kitebird Com Articles Pydbapi Html Toc 1
Www Kitebird Com Articles Pydbapi Html Toc 1Www Kitebird Com Articles Pydbapi Html Toc 1
Www Kitebird Com Articles Pydbapi Html Toc 1
 
Scripts Python Dbapi
Scripts Python DbapiScripts Python Dbapi
Scripts Python Dbapi
 
Random And Dynamic Images Using Python Cgi
Random And Dynamic Images Using Python CgiRandom And Dynamic Images Using Python Cgi
Random And Dynamic Images Using Python Cgi
 
Python And My Sq Ldb Module
Python And My Sq Ldb ModulePython And My Sq Ldb Module
Python And My Sq Ldb Module
 
Pydbapi
PydbapiPydbapi
Pydbapi
 
My Sq Ldb Tut
My Sq Ldb TutMy Sq Ldb Tut
My Sq Ldb Tut
 
Internet Programming With Python Presentation
Internet Programming With Python PresentationInternet Programming With Python Presentation
Internet Programming With Python Presentation
 
Docs Python Org Howto Webservers Html
Docs Python Org Howto Webservers HtmlDocs Python Org Howto Webservers Html
Docs Python Org Howto Webservers Html
 
Handson Python
Handson PythonHandson Python
Handson Python
 
Python Tutorial
Python TutorialPython Tutorial
Python Tutorial
 
Python 3 Days
Python  3 DaysPython  3 Days
Python 3 Days
 
Tutorial Python
Tutorial PythonTutorial Python
Tutorial Python
 

How CGI Programming Works

  • 1. CGI Programming Phil Spector Statistical Computing Facility Department of Statistics University of California, Berkeley 1
  • 2. How a Web Browser Works When a user enters a URL in a browser, the browser sends a request to a server for a particular resource, such as an HTML page or an image, and the server responds by sending some headers that describe what it’s sending followed by the actual content. If an HTML page contains images, flash animations, etc. the process is repeated for each item – the browser then displays everything in the appropriate way. If the web server is configured to allow it, requests made to URLs in certain directories will run a program which generates the appropriate headers and content instead of just sending a static page. The mechanism that allows this is known as CGI (Common Gateway Interface), and such programs are known as CGI programs. 2
  • 3. Browser to Server Communication When a browser makes a request for a resource from a web server, what information is actually transmitted? We can use a simple program that sits will echo any information sent to it, and then point our browser at the program and see what happens. In python, here’s such a program, which I’ll call webecho: #!/usr/bin/python import socket,os,sys srvsocket = socket.socket(socket.AF_INET,socket.SOCK_STREAM) srvsocket.bind(("",1888)) srvsocket.listen(5) while 1: clisocket,addr = srvsocket.accept() now = clisocket.recv(1024) clisocket.send(now) clisocket.close() 3
  • 4. A Simple Request With the webecho program running, let’s make a simple request and see what happens. If I point a web browser at http://localhost:1888/something, here’s what’s displayed: GET /something HTTP/1.1 Host: localhost:1888 User-Agent: Mozilla/5.0 (X11; U; Linux x86_64; en-US; rv:1.7.12) Gecko/20051010 Firefox/1.0.7 (Ubuntu package 1.0.7) Accept: text/xml,application/xml,application/xhtml+xml,text/html; q=0.9,text/plain;q=0.8,image/png,*/*;q=0.5 Accept-Language: en-us,en;q=0.5 Accept-Encoding: gzip,deflate Accept-Charset: ISO-8859-1,utf-8;q=0.7,*;q=0.7 Keep-Alive: 300 Connection: keep-alive 4
  • 5. HTML Forms A variety of form elements are available through HTML, and are presented in an interactive learning tool at: http://www.w3schools.com/html/html_forms.asp While I’ll show examples of some form elements, I suggest consulting the w3schools web page for additional details. One important type of form element is the type=hidden form. When you have a multi-screen CGI program, using this type of form allows you to pass information between the different screens without the information being visible to the user. 5
  • 6. Getting information to CGI Programs What makes CGI programming interesting is that it can get information from users through various HTML form elements, like entry fields, drop-down menus, and file upload dialogs. How is that information transmitted to the server? The first step is producing a page that contains a form. Here’s the html for a simple form that will talk to the webecho program: <form action=’http://localhost:1888’ method=get> <input type=text name=postvar><br> <input type=submit value=’GET’> </form> Here’s what shows up in a web browser: 6
  • 7. Getting information to CGI Programs (cont’d) When the phrase ”Hello, world” is entered in the form, and the submit button pressed, here’s what appears in the browser window: GET /?getvar=Hello%2C+world HTTP/1.1 Host: localhost:1888 User-Agent: Mozilla/5.0 (X11; U; Linux x86_64; en-US; rv:1.7.12) Gecko/20051010 Firefox/1.0.7 (Ubuntu package 1.0.7) Accept: text/xml,application/xml,application/xhtml+xml,text/html; q=0.9,text/plain;q=0.8,image/png,*/*;q=0.5 Accept-Language: en-us,en;q=0.5 Accept-Encoding: gzip,deflate Accept-Charset: ISO-8859-1,utf-8;q=0.7,*;q=0.7 Keep-Alive: 300 Connection: keep-alive Referer: http://springer/~s133ar/cform.html 7
  • 8. Getting information to CGI Programs (cont’d) The GET method sends information to the browser by adding name/value pairs, possibly separated by ampersands (&), to the URL after a question mark. These URLs are encoded to handle characters not allowed in URLs. URLs constructed this way will communicate with the web server regardless of the method used to send the information, and are the only way to invoke a CGI program without a surrounding form. An alternative method of sending information to a web server that doesn’t display the information in the web server’s address bar is known as POST. Here’s a form that uses this method: <form action=’http://localhost:1888’ method=post> <input type=text name=postvar><br> <input type=submit value=’POST’> </form> 8
  • 9. The POST method Here’s the result of submitting Hello, world through the form using the POST method: POST / HTTP/1.1 Host: localhost:1888 User-Agent: Mozilla/5.0 (X11; U; Linux x86_64; en-US; rv:1.7.12) Gecko/20051010 Firefox/1.0.7 (Ubuntu package 1.0.7) Accept: text/xml,application/xml,application/xhtml+xml,text/html; q=0.9,text/plain;q=0.8,image/png,*/*;q=0.5 Accept-Language: en-us,en;q=0.5 Accept-Encoding: gzip,deflate Accept-Charset: ISO-8859-1,utf-8;q=0.7,*;q=0.7 Keep-Alive: 300 Connection: keep-alive Referer: http://springer/~s133ar/cform1.html Content-Type: application/x-www-form-urlencoded Content-Length: 22 postvar=Hello%2C+world 9
  • 10. The POST method (cont’d) The URL is no longer changed, and the name/value pair information is sent by the browser after it has sent the headers. The POST method can also be used for file uploads. Here’s the HTML for a form which will accept a file name and upload it to the server: <form action=’http://localhost:1888’ method=post enctype=’multipart/form-data’> <input type=file name=’myfile’> <input type=submit value=’Upload’> </form> Here’s how it looks in a browser (the Browse button is added automatically by the browser): 10
  • 11. File Upload Suppose I upload a small text file – here’s what the browser sends to the server: POST / HTTP/1.1 Host: localhost:1888 User-Agent: Mozilla/5.0 (X11; U; Linux x86_64; en-US; rv:1.7.12) Gecko/20051010 Firefox/1.0.7 (Ubuntu package 1.0.7) Accept: text/xml,application/xml,application/xhtml+xml,text/html;q=0.9,text/plain;q=0.8,image/png,*/*;q=0.5 Accept-Language: en-us,en;q=0.5 Accept-Encoding: gzip,deflate Accept-Charset: ISO-8859-1,utf-8;q=0.7,*;q=0.7 Keep-Alive: 300 Connection: keep-alive Referer: http://springer/~s133ar/cform2.html Content-Type: multipart/form-data; boundary=---------------------------1741722292794821883444112406 Content-Length: 687 -----------------------------1741722292794821883444112406 Content-Disposition: form-data; name="myfile"; filename="small.txt" Content-Type: text/plain When you type the address of a web page (i.e. a URL or Universal Resource Locator) into a browser,or click a link which refers to a URL, a request is made to a computer on the internet to send the contents of a web page to your browser. Web pages are written in a language known as HTML (Hypertext Markup Language), and your web browser knows how to translate HTML into text, pictures, links, animations or whatever else the designer of the web page had in mind. -----------------------------1741722292794821883444112406-- 11
  • 12. File Upload (cont’d) Uploading the file resulted in the addition of some headers, similar to those used to send attachments in email messages. The main difference is that even binary files can be transfered in this way, since the HTTP protocol does not disturb the eighth bit of its input the way that SMTP does. Now that we’ve seen how information is transmitted from the browser to the server, let’s consider the communication in the opposite direction. 12
  • 13. Server to Browser Communication The underlying principle behind CGI programs is that the standard output of your CGI program is sent to the browser, so your CGI programs must generate HTML to produce your desired output. But just as we saw that the browser inserts some headers before it sends information (if there is any) to the server, the server needs to send at least one header line to the browser, to let it know that HTML will follow. Thus, the first thing that any CGI program should do is to output a header like this: Content-type: text/html It’s also the CGI program’s responsibility to send a completely empty line to signal the end of the headers. 13
  • 14. Cookies Cookies are text stored on a remote computer when they access your CGI program. Cookies can serve as an alternative to hidden variables, or as way to remember users when they access your CGI program again. Cookies are sent through outgoing headers as follows: Set-Cookie: cookiename=cookievalue This produces a cookie that expires when the user closes their browser. To make persistent cookies, provide an expiration date as follows after the above specification: ; expires=Monday, 01-May-06 00:00:00 GMT The cookie, sent as a name/value pair, will be passed to your CGI program automatically whenever a browser storing your cookie returns to your HTML page or CGI program. 14
  • 15. The CGI Standard The CGI standard insures that information is properly transfered between the browser and the web server, and between the web server and the browser, by running CGI programs in an appropriate environment. 1. Headers from the browser are converted to environmental variables. 2. Information from the browser that was after the headers (if any) is placed in standard input for the CGI program. 3. Standard output from the CGI program is directed to the web browser. Each language that is appropriate for CGI scripting will provide tools to make this information available to your CGI program in a convenient form. 15
  • 16. What do CGI Interfaces Provide? The most important feature that a CGI interface provides is a means of getting the values of the CGI form variables into the programming environment. All programs with a CGI interface provide a way of getting this information regardless of the method (GET or POST) that was used. For file uploads, there should an option to avoid reading the entire file into memory. Since additional information is provided through environmental variables, there should be a simple way of accessing these variables. Since standard output from the CGI program is directed to the browser, many CGI interfaces provide helper functions to generate HTML, as well as convenience functions to generate the required header lines, although it’s often easier to do this by yourself. 16
  • 17. Some Useful CGI Environmental Variables Name Contents HTTP COOKIE Persistent data stored in cookies HTTP REFERER URL of refering document HTTP USER AGENT Type of browser being used QUERY STRING URL Fragment after ? REMOTE ADDR IP Address of user REMOTE HOST Hostname of user REMOTE USER Username, if authentication was used SERVER NAME Hostname of server SERVER PORT Port number of server SERVER SOFTWARE Name and version of server software 17
  • 18. Security of CGI Programs Remember that CGI programs are accessible to anyone who can access the web server that hosts the program, so extra care is necessary to make sure your programs are secure. The cardinal rule is to make sure that any user input that gets passed to the operating system does not contain any unusual characters. In fact, it may be wise to avoid calling any operating system commands that include user input. It is not uncommon for CGI programs to set their command path (through environmental variables) to one limited to just those directories necessary for execution of the CGI program. On a UNIX system, an example would be a path of /bin:/usr/bin. 18
  • 19. A Simple Example A form to allow the user to choose an ice cream flavor is generated by the following code: <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 3.2//EN"> <head title=’Pick a Flavor’> <html><body> <h1>Ice Cream Flavors</h1> Please choose your favorite flavor:<br> <form action=’http://localhost/~spector/cgi-bin/icecream.py’ method=’post’> <select name="flavor" > <option value="Chocolate Chip">Chocolate Chip</option> <option value="Strawberry">Strawberry</option> <option value="Rum Raisin">Rum Raisin</option> <option value="Vanilla">Vanilla</option> </select> <input type=’submit’ name=’Submit’> </form> </body></html> 19
  • 20. Processing the Form: Perl Here’s a perl script to process the ice cream form: #!/usr/bin/perl use CGI; $q = new CGI(); $flavor = $q->param(’flavor’); print $q->header(); print $q->start_html(’Ice Cream Example’); print $q->h1(’Ice Cream Flavor’); print "$flavor is a good choice, I like it too"; print $q->end_html(); 20
  • 21. Processing the Form: Python #!/usr/bin/python import cgi f = cgi.FieldStorage() flavor = f[’flavor’].value print ’Content-type: text/htmlnn’ print ’<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 3.2//EN">’ print ’<head title="Ice Cream Example">’ print ’<html><body>’ print ’<h1>Ice Cream Flavors</h1>’ print ’%s is a good choice, I like it too’ % flavor print ’</body></html>’ 21
  • 22. Combo Forms In the previous example, the proper action had to be coded into the HTML file that presented the form. In general, separating the form from the code may make it more difficult to maintain your program. In addition, it’s often useful to generate HTML forms through a program, instead of hardcoding the form. Combo forms are programs that first check to see if form data has been received – if not, they generate the necessary form, refering back to themselves as the action. They are especially handy for multi-form transactions, since they put all of the programs in a single file, and eliminate the need for separate static HTML pages. 22
  • 23. Combo Form in Perl #!/usr/bin/perl use CGI; $q = new CGI(); @flavors = (’Chocolate Chip’,’Strawberry’,’Rum Raisin’,’Vanilla’); if (not defined $q->param()){ # called directly print $q->header(), $q->start_html(’Ice Cream Example’), $q->h1(’Ice Cream Flavors’); print $q->startform({action=>’icecream1.pl’,method=>’POST’}), $q->popup_menu(-name=>’flavor’,-values=>@flavors), $q->submit(), $q->end_form(),$q->end_html(); } else{ $flavor = $q->param(’flavor’); print $q->header(); print $q->start_html(’Ice Cream Example’); print $q->h1(’Ice Cream Flavor’); print "$flavor is a good choice, I like it too"; print $q->end_html(); } 23
  • 24. Combo Form in Python #!/usr/bin/python import cgi f = cgi.FieldStorage() print ’Content-type: text/htmlnn’ print ’<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 3.2//EN">’ flavors = (’Chocolate Chip’,’Strawberry’,’Rum Raisin’,’Vanilla’) if len(f.keys()) == 0: # called directly print ’’’<head><title>Pick a Flavor</title></head> <html><body> <h1>Ice Cream Flavors</h1> Please choose your favorite flavor:<br> <form action=’icecream1.py’ method=’post’> <select name="flavor" > ’’’ for f in flavors: print ’<option value="%s">%s</option>’ % (f,f) print ’’’</select> <input type=’submit’ name=’Submit’> </form> </body></html>’’’ else: flavor = f[’flavor’].value print ’’’<head><title>Ice Cream Example</title></head> <html><body> <h1>Ice Cream Flavors</h1> %s is a good choice, I like it too’’’ % flavor print ’</body></html>’ 24
  • 25. Debugging CGI Scripts 1. Make sure the Content-type header line is being generated. 2. Make sure that the first line of the CGI program indicates the location of the executable that will run the program. 3. Make sure that the script is executable, and any files which it accesses have appropriate permissions for the user under which the CGI program will run. 4. Execute the script from the command line to find syntax errors 5. Errors are usually redirected to the server’s error log, which may not be accessible. Redirecting standard error to standard output will display messages in the browser. 6. There may be special debugging facilities in the language of your choice. 25