180 likes | 307 Views
Fundamentals of Web Programming. Lecture 7: HTTP & CGI. Today’s Topics. Hypertext Transfer Protocol (HTTP) Common Gateway Interface (CGI). URLs and URIs. Used interchangeably: URL : Uniform Resource Locator URI : Uniform Resource Identifier URLs can use one of many different protocols:
E N D
Fundamentals ofWeb Programming Lecture 7: HTTP & CGI Lecture 7: HTTP and CGI
Today’s Topics • Hypertext Transfer Protocol (HTTP) • Common Gateway Interface (CGI) 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) 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 Lecture 7: HTTP and CGI
Anatomy of a URL • Examples • Path • http://localhost/new.html • Search • http://localhost/mirror.cgi?arg=val 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 Lecture 7: HTTP and CGI
Browser Web Server 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>... 6. Render Response 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 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. Lecture 7: HTTP and CGI
7. Closes Connection Hello! CGI Program 4. Send Header 5. Send Content Content-type: text/html Browser <html><body> <h1>Hello!</h1> </body></html> 8. Display Content Web Server CGI Protocol 1. Notice URL is a program 2. Prepare the environment 3. Launch script/program 6. Exit 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) 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 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) Lecture 7: HTTP and CGI
CGI Methods • GET: Information is sent directly in the URL • POST: Information is sent via STDIN stream 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 Lecture 7: HTTP and CGI
Standard Variables • See list on p. 875 of the text • Try the mirror.cgi program on the course server 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 Lecture 7: HTTP and CGI
Complete Example • Simple Shopping Basket • See script basket.cgi on the course server Lecture 7: HTTP and CGI