Download as ppt, pdf, or txt
Download as ppt, pdf, or txt
You are on page 1of 18

Fundamentals of

Web Programming
Lecture 7: HTTP & CGI

Lecture 7: HTTP and CGI 1


Today’s Topics
• Hypertext Transfer Protocol (HTTP)
• Common Gateway Interface (CGI)

Lecture 7: HTTP and CGI 2


URLs and URIs
• Used interchangeably:
– URL: Uniform Resource Locator
– URI: Uniform Resource Identifier
• URLs can use one of many
different protocols:
– ftp://…
– news://…
– http://… (our focus today: http URLs)

Lecture 7: HTTP and CGI 3


Anatomy of a URL
• http://host[:port][/path][?search]
• Examples:
– Host
• http://localhost
• http://www.cnn.com
– Port
• http://localhost:80

Lecture 7: HTTP and CGI 4


Anatomy of a URL
• Examples
– Path
• http://localhost/new.html
– Search
• http://localhost/mirror.cgi?arg=val

Lecture 7: HTTP and CGI 5


HTTP
• Client-Server Communication
– The Browser is the Client
– The Web Site is the Server
• Client Request: HTTP request
• Server Response: HTTP response

Lecture 7: HTTP and CGI 6


HTTP Communication
1. Extract Host Part of URL http://www.cnn.com/index.html
2. Get IP address from DNS 207.25.71.28
3. Establish TCP/IP connection to Host

4. Send HTTP Request


Hello!
GET /index.html

5. Wait for Response

Content/type: text/html
<html><body>
<h1>Hello!</h1>...
Browser
6. Render Response

Web Server
Lecture 7: HTTP and CGI 7
HTTP Is Stateless
• A stateless protocol doesn’t
remember anything from one
transaction to another (all
transactions are independent)
• Workarounds:
– Use INPUT with TYPE=HIDDEN to
store state information
– use cookies to store state information

Lecture 7: HTTP and CGI 8


CGI: Common Gateway
Interface
• Standard interface supports server
programming in a variety of ways:
– Unix shell scripts
– PERL scripts
– C, C++ programs
– Java servlets
– ASP
– etc.

Lecture 7: HTTP and CGI 9


CGI Protocol
7. Closes Connection 1. Notice URL is a program
2. Prepare the environment
3. Launch script/program
Hello!

CGI Program
4. Send Header
5. Send Content
6. Exit

Browser
Content-type: text/html

8. Display Content <html><body>


Web Server <h1>Hello!</h1>
</body></html>

Lecture 7: HTTP and CGI 10


CGI Advantages
• Allows dynamic control of “pages”
• Examples:
– counters
– customized pages (user prefs)
– interactions with state
(server ‘remembers’ data from
request to request; e.g., shopping
basket)

Lecture 7: HTTP and CGI 11


CGI Pitfalls
• Resource Requirements
• Resource Contention
• Where to Store Scripts?
– Stored in one directory, managed
centrally
– Stored in several directories,
distributed management

Lecture 7: HTTP and CGI 12


CGI Pitfalls
• Portability
– Yes: Perl, C, C++
– No: VBasic, Unix shell scripts
• Server Independence
– File paths (data)
– Program location (support functions)

Lecture 7: HTTP and CGI 13


CGI Methods
• GET: Information is sent directly in
the URL
• POST: Information is sent via
STDIN stream

Lecture 7: HTTP and CGI 14


CGI Variables
• REQUEST_METHOD: GET | POST
• QUERY_STRING: the data sent
• CONTENT_LENGTH: the amount of
data
• If you use a CGI support library
(e.g., CGI.pm), this is taken care
of automatically

Lecture 7: HTTP and CGI 15


Standard Variables
• See list on p. 875 of the text
• Try the mirror.cgi program on the
course server

Lecture 7: HTTP and CGI 16


URL Encoding
• Special chars (&, +) must be
escaped
– ‘&’ = %25; ‘ ’ = %20
• CONTENT_TYPE indicates the style
of encoding
• If you use a CGI support library,
this is taken care of for you
• Example: basket.cgi with GET
Lecture 7: HTTP and CGI 17
Complete Example
• Simple Shopping Basket
• See script basket.cgi on the course
server

Lecture 7: HTTP and CGI 18

You might also like