Web Server Design Week 16Common Gateway InterfaceCGI InvocationCGI OperationStatusLocationCGI EnvironmentHow to Customize the Environment?ENV & CGI ExamplesAdvanced Topics: “Soft 404s”Web Server DesignWeek 16Old Dominion UniversityDepartment of Computer ScienceCS 495/595 Spring 2006Michael L. Nelson <[email protected]>4/24/06Common Gateway Interface •A method for remotely invoking executable programs on a server–A long-time convention•http://hoohoo.ncsa.uiuc.edu/cgi/–finally defined in RFC 3875clientserverGET /foo.cgi HTTP/1.1foo.cgi200 OKCGI Invocation•How Apache does it:–http://httpd.apache.org/docs/2.0/mod/mod_cgi.html•We’ll live slightly more dangerously:–any executable (non-directory) file can be invoked as CGI with:•POST•GET w/ query string–e.g. /a/b/c.cgi?var1=foo&var2=barCGI Operation•The CGI program is responsible for returning (on STDOUT) some combination of its own headers:–Content-type–Location–Status–and other locally-defined headers•Script-returned headers are:–collected by the server–processed; e.g.:•“Location” -> HTTP/1.1 302 Found•Status -> HTTP response code line–combined with the server’s headers•Resulting headers are returned to the clientStatus% more status.cgi #!/usr/bin/perlprint "Status: 678 This is not a real HTTP status code\n\n";% telnet www.cs.odu.edu 80Trying 128.82.4.2...Connected to xenon.cs.odu.edu.Escape character is '^]'.GET /~mln/teaching/cs595-s06/cgi/status.cgi HTTP/1.1Host: www.cs.odu.eduHTTP/1.1 678 This is not a real HTTP status codeDate: Mon, 24 Apr 2006 14:37:14 GMTServer: Apache/2Content-Length: 0Content-Type: text/plainConnection closed by foreign host.Location% more location.cgi #!/usr/bin/perlprint "Location: http://www.cs.odu.edu/~mln/\n\n";% telnet www.cs.odu.edu 80Trying 128.82.4.2...Connected to xenon.cs.odu.edu.Escape character is '^]'.GET /~mln/teaching/cs595-s06/cgi/location.cgi HTTP/1.1Host: www.cs.odu.eduHTTP/1.1 302 FoundDate: Mon, 24 Apr 2006 14:40:31 GMTServer: Apache/2Location: http://www.cs.odu.edu/~mln/Content-Length: 277Content-Type: text/html; charset=iso-8859-1<!DOCTYPE HTML PUBLIC "-//IETF//DTD HTML 2.0//EN"><html><head><title>302 Found</title></head><body><h1>Found</h1><p>The document has moved <a href="http://www.cs.odu.edu/~mln/">here</a>.</p><hr><address>Apache/2 Server at www.cs.odu.edu Port 80</address></body></html>CGI Environment•Section 4.1, RFC 3875–AUTH_TYPE, CONTENT_LENGTH, CONTENT_TYPE, GATEWAY_INTERFACE, PATH_INFO, PATH_TRANSLATED, QUERY_STRING, REMOTE_ADDR, REMOTE_HOST, REMOTE_IDENT, REMOTE_USER, REQUEST_METHOD, SCRIPT_NAME, SERVER_NAME, SERVER_PORT, SERVER_PROTOCOL, SERVER_SOFTWARE •In practice, slightly different:–http://www.cs.odu.edu/~mln/teaching/cs595-s06/cgi/env.cgiHow to Customize the Environment?•C:–fork() & execve()•Perl:–set %ENV–fork() & exec() • Python:–set %ENV–fork () & execve()•Others???–please share w/ the listENV & CGI Examples#!/usr/bin/perlprint "Content-type: text/html\n\n";foreach $key (keys (%ENV)) { print "$key = $ENV{$key} <br>\n";}while (<STDIN>) { print "$_<br>\n";}http://www.cs.odu.edu/~mln/teaching/cs595-s06/cgi/{GET, POST} X {multipart/form-data, application/x-form-www-urlencoded}Advanced Topics: “Soft 404s”•foo.edu/lookup.php?key=123456•what http status code do you return if:–script “lookup.php” exists and has no syntax errors–but key 123456 is deleted or invalid•Semantic events:–http - 200–database - bad key•reading:–“Sic Transit Gloria Telae: Towards an Understanding of the Web’s
View Full Document