Presentation is loading. Please wait.

Presentation is loading. Please wait.

20-753: Fundamentals of Web Programming Copyright © 1999, Carnegie Mellon. All Rights Reserved. 1 Lecture 7: HTTP and CGI Fundamentals of Web Programming.

Similar presentations


Presentation on theme: "20-753: Fundamentals of Web Programming Copyright © 1999, Carnegie Mellon. All Rights Reserved. 1 Lecture 7: HTTP and CGI Fundamentals of Web Programming."— Presentation transcript:

1 20-753: Fundamentals of Web Programming Copyright © 1999, Carnegie Mellon. All Rights Reserved. 1 Lecture 7: HTTP and CGI Fundamentals of Web Programming Lecture 7: HTTP & CGI

2 20-753: Fundamentals of Web Programming Copyright © 1999, Carnegie Mellon. All Rights Reserved. 2 Lecture 7: HTTP and CGI Today’s Topics Hypertext Transfer Protocol (HTTP) Common Gateway Interface (CGI)

3 20-753: Fundamentals of Web Programming Copyright © 1999, Carnegie Mellon. All Rights Reserved. 3 Lecture 7: HTTP and CGI 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)

4 20-753: Fundamentals of Web Programming Copyright © 1999, Carnegie Mellon. All Rights Reserved. 4 Lecture 7: HTTP and CGI Anatomy of a URL http://host[:port][/path][?search] Examples: –Host http://localhost http://www.cnn.com –Port http://localhost:80

5 20-753: Fundamentals of Web Programming Copyright © 1999, Carnegie Mellon. All Rights Reserved. 5 Lecture 7: HTTP and CGI Anatomy of a URL Examples –Path http://localhost/new.html –Search http://localhost/mirror.cgi?arg=val

6 20-753: Fundamentals of Web Programming Copyright © 1999, Carnegie Mellon. All Rights Reserved. 6 Lecture 7: HTTP and CGI HTTP Client-Server Communication –The Browser is the Client –The Web Site is the Server Client Request: HTTP request Server Response: HTTP response

7 20-753: Fundamentals of Web Programming Copyright © 1999, Carnegie Mellon. All Rights Reserved. 7 Lecture 7: HTTP and CGI HTTP Communication 1. Extract Host Part of URL 6. Render Response Browser 4. Send HTTP Request 5. Wait for Response GET /index.html Content/type: text/html Hello!... Hello! http://www.cnn.com/index.html 207.25.71.282. Get IP address from DNS 3. Establish TCP/IP connection to Host Web Server

8 20-753: Fundamentals of Web Programming Copyright © 1999, Carnegie Mellon. All Rights Reserved. 8 Lecture 7: HTTP and CGI 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

9 20-753: Fundamentals of Web Programming Copyright © 1999, Carnegie Mellon. All Rights Reserved. 9 Lecture 7: HTTP and CGI 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.

10 20-753: Fundamentals of Web Programming Copyright © 1999, Carnegie Mellon. All Rights Reserved. 10 Lecture 7: HTTP and CGI CGI Protocol Browser Web Server CGI Program 1. Notice URL is a program 2. Prepare the environment 3. Launch script/program 6. Exit 7. Closes Connection 8. Display Content Hello! 4. Send Header Content-type: text/html 5. Send Content Hello!

11 20-753: Fundamentals of Web Programming Copyright © 1999, Carnegie Mellon. All Rights Reserved. 11 Lecture 7: HTTP and CGI 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)

12 20-753: Fundamentals of Web Programming Copyright © 1999, Carnegie Mellon. All Rights Reserved. 12 Lecture 7: HTTP and CGI CGI Pitfalls Resource Requirements Resource Contention Where to Store Scripts? –Stored in one directory, managed centrally –Stored in several directories, distributed management

13 20-753: Fundamentals of Web Programming Copyright © 1999, Carnegie Mellon. All Rights Reserved. 13 Lecture 7: HTTP and CGI CGI Pitfalls Portability –Yes: Perl, C, C++ –No: VBasic, Unix shell scripts Server Independence –File paths (data) –Program location (support functions)

14 20-753: Fundamentals of Web Programming Copyright © 1999, Carnegie Mellon. All Rights Reserved. 14 Lecture 7: HTTP and CGI CGI Methods GET: Information is sent directly in the URL POST: Information is sent via STDIN stream

15 20-753: Fundamentals of Web Programming Copyright © 1999, Carnegie Mellon. All Rights Reserved. 15 Lecture 7: HTTP and CGI 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

16 20-753: Fundamentals of Web Programming Copyright © 1999, Carnegie Mellon. All Rights Reserved. 16 Lecture 7: HTTP and CGI Standard Variables See list on p. 875 of the text Try the mirror.cgi program on the course servermirror.cgi

17 20-753: Fundamentals of Web Programming Copyright © 1999, Carnegie Mellon. All Rights Reserved. 17 Lecture 7: HTTP and CGI 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

18 20-753: Fundamentals of Web Programming Copyright © 1999, Carnegie Mellon. All Rights Reserved. 18 Lecture 7: HTTP and CGI Complete Example Simple Shopping Basket See script basket.cgi on the course serverbasket.cgi


Download ppt "20-753: Fundamentals of Web Programming Copyright © 1999, Carnegie Mellon. All Rights Reserved. 1 Lecture 7: HTTP and CGI Fundamentals of Web Programming."

Similar presentations


Ads by Google