Survey
* Your assessment is very important for improving the work of artificial intelligence, which forms the content of this project
* Your assessment is very important for improving the work of artificial intelligence, which forms the content of this project
Fundamentals of Web Programming Lecture 7: HTTP & CGI 20-753: Fundamentals of Web Programming Lecture 7: HTTP and CGI Copyright © 1999, Carnegie Mellon. All Rights Reserved. 1 Today’s Topics • Hypertext Transfer Protocol (HTTP) • Common Gateway Interface (CGI) 20-753: Fundamentals of Web Programming Lecture 7: HTTP and CGI Copyright © 1999, Carnegie Mellon. All Rights Reserved. 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) 20-753: Fundamentals of Web Programming Lecture 7: HTTP and CGI Copyright © 1999, Carnegie Mellon. All Rights Reserved. 3 Anatomy of a URL • http://host[:port][/path][?search] • Examples: – Host • http://localhost • http://www.cnn.com – Port • http://localhost:80 20-753: Fundamentals of Web Programming Lecture 7: HTTP and CGI Copyright © 1999, Carnegie Mellon. All Rights Reserved. 4 Anatomy of a URL • Examples – Path • http://localhost/new.html – Search • http://localhost/mirror.cgi?arg=val 20-753: Fundamentals of Web Programming Lecture 7: HTTP and CGI Copyright © 1999, Carnegie Mellon. All Rights Reserved. 5 HTTP • Client-Server Communication – The Browser is the Client – The Web Site is the Server • Client Request: HTTP request • Server Response: HTTP response 20-753: Fundamentals of Web Programming Lecture 7: HTTP and CGI Copyright © 1999, Carnegie Mellon. All Rights Reserved. 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 Browser Content/type: text/html <html><body> <h1>Hello!</h1>... 6. Render Response Web Server 20-753: Fundamentals of Web Programming Lecture 7: HTTP and CGI Copyright © 1999, Carnegie Mellon. All Rights Reserved. 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 20-753: Fundamentals of Web Programming Lecture 7: HTTP and CGI Copyright © 1999, Carnegie Mellon. All Rights Reserved. 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. 20-753: Fundamentals of Web Programming Lecture 7: HTTP and CGI Copyright © 1999, Carnegie Mellon. All Rights Reserved. 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 Web Server 20-753: Fundamentals of Web Programming <html><body> <h1>Hello!</h1> </body></html> Lecture 7: HTTP and CGI Copyright © 1999, Carnegie Mellon. All Rights Reserved. 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) 20-753: Fundamentals of Web Programming Lecture 7: HTTP and CGI Copyright © 1999, Carnegie Mellon. All Rights Reserved. 11 CGI Pitfalls • Resource Requirements • Resource Contention • Where to Store Scripts? – Stored in one directory, managed centrally – Stored in several directories, distributed management 20-753: Fundamentals of Web Programming Lecture 7: HTTP and CGI Copyright © 1999, Carnegie Mellon. All Rights Reserved. 12 CGI Pitfalls • Portability – Yes: Perl, C, C++ – No: VBasic, Unix shell scripts • Server Independence – File paths (data) – Program location (support functions) 20-753: Fundamentals of Web Programming Lecture 7: HTTP and CGI Copyright © 1999, Carnegie Mellon. All Rights Reserved. 13 CGI Methods • GET: Information is sent directly in the URL • POST: Information is sent via STDIN stream 20-753: Fundamentals of Web Programming Lecture 7: HTTP and CGI Copyright © 1999, Carnegie Mellon. All Rights Reserved. 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 20-753: Fundamentals of Web Programming Lecture 7: HTTP and CGI Copyright © 1999, Carnegie Mellon. All Rights Reserved. 15 Standard Variables • See list on p. 875 of the text • Try the mirror.cgi program on the course server 20-753: Fundamentals of Web Programming Lecture 7: HTTP and CGI Copyright © 1999, Carnegie Mellon. All Rights Reserved. 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 20-753: Fundamentals of Web Programming Lecture 7: HTTP and CGI Copyright © 1999, Carnegie Mellon. All Rights Reserved. 17 Complete Example • Simple Shopping Basket • See script basket.cgi on the course server 20-753: Fundamentals of Web Programming Lecture 7: HTTP and CGI Copyright © 1999, Carnegie Mellon. All Rights Reserved. 18