Common Gateway Interface slides modified from Dave Hollinger











- Slides: 11
Common Gateway Interface slides modified from Dave Hollinger slides are modified from Dave Hollinger
Common Gateway Interface • CGI is a standard mechanism for: – Associating URLs with programs that can be run by a web server – A protocol (of sorts) for how the request is passed to the external program – How the external program sends the response to the client CGI 2
CGI Programming t s e qu e r p htt HTTP SERVER CLIENT CGI Program http response CGI 3
CGI URLs • There is mapping between URLs and CGI programs provided by a web sever – The exact mapping is not standardized • web server admin can set it up • Typically: – requests that start with /CGI-BIN/ , /cgi-bin/ or /cgi/, etc. • not to static documents CGI 4
Typical FORM CGI setup • User fills out a form and presses submit • CGI program gets a set of name, value pairs – one for each form field • CGI decides what to do based on the name, value pairs – sometimes creates a new form based on the submission CGI Sessions 5
Sessions • Many web sites allow you to establish a session – you identify yourself to the system – now you can visit lots of pages, add stuff to shopping cart, establish preferences, etc CGI Sessions 6
State Information • Each HTTP request is unrelated to any other – as far as the Web server is concerned • Each new request to a CGI program starts up a brand new copy of the CGI program • Providing sessions requires keeping state information CGI Sessions 7
Session Conversation Client Hi! I'm Joe. Hi Joe (it's him again) Welcome Back. . . Server CGI 1 I wanna buy a cookie. CGI 2 OK Joe, it will be there tomorrow. CGI Sessions 8
HTTP Cookies • A "cookie' is a name, value pair that a CGI program can ask the client to remember • Client sends this name, value pair along with every request to the CGI • We can also use "cookies" to propagate state information CGI Sessions 9
CGI cookie creation • A CGI program can send back any number of HTTP headers – can set multiple cookies • Content-Type is required! printf("Content-Type: text/htmlrn"); printf("Set-Cookie: prefs=nofrmsrn"); printf("Set-Cookie: Java=yesrn"); printf("rn"); • … now sends document content CGI Sessions 10
Getting HTTP Cookies • Browser sends each cookie as a header: Cookie: prefs=nofrms Cookie: Java=OK • Web server gives cookies to CGI program via an environment variable CGI Sessions 11