220 likes | 502 Views
Common Gateway Interface (CGI). Joe Komar. Common Gateway Interface (CGI). HTTP typically requests HTML pages Link (URL) can identify a non-HTML page WWW server invokes the resource named in the URL (a program, batch file, etc.) and lets it do its thing
E N D
Common Gateway Interface (CGI) Joe Komar Komar Associates
Common Gateway Interface (CGI) • HTTP typically requests HTML pages • Link (URL) can identify a non-HTML page • WWW server invokes the resource named in the URL (a program, batch file, etc.) and lets it do its thing • Results of the CGI program are returned to the server in the form of HTML • Server then passes the HTML to the client Komar Associates
HyperText Transfer Protocol (HTTP) • Provides “stateless” connection: • Client establishes connection • Client issues a request • Sever processes the request and returns a response • Server closes the connection • Web’s Remote Procedure Call (RPC) on top of TCP/IP Komar Associates
HTTP Request • Request Line -- method (GET), relative path to resource (/temp/index.html), protocol version (HTTP/1.0) • Request header fields -- additional parameters in the format name, colon (:), value • Entity body -- sometimes used to pass bulk information to the server Komar Associates
HTTP Request Example GET /path/file.html HTTP/1.0 (header line) Accept: text/html (request header fields) Accept: audio/x “ User-agent: MacWeb “ Komar Associates
HTTP Response • Response header line -- HTTP version, status of the response, explanation of status • Response header fields -- server attributes and document returned metadata • Entity body -- typically an HTML document Komar Associates
HTTP Response Example HTTP/1.0 200 OK (response header) Server: NCSA/1.3 (response header fields) Mime_Version: 1.0 “ Content_type: text/html “ Content_length: 2000 “ <HTML> (entity body) . “ </HTML> “ Komar Associates
Common Gateway Interface • HTML FORMs allow user input • Input is packaged up in a known format and send as an HTTP request (POST or GET) and sent on through a CGI protocol • The program or resource named is given the data and left to do with it what it will • Results are returned in the form of an HTML document that the server passes on to the client • Three-tier client/server model Komar Associates
Using FORMs and CGI • FORM’s submit button is pushed • Browser collects data and forms one long string of name/value pairs separated by an ampersand (&) with plus signs (+) for spaces • Browser forms a standard HTTP request with the long string as the “entity body” • Server receives request via a socket and starts a CGI interaction Komar Associates
Using FORMs and CGI continued • Server sets up “environment variables” • shared “bulletin board” with cgi program • server_name, request_method, path_info, script_name, content_type, content_length, etc. • Server starts the CGI program • CGI program reads environment variables • CGI program receives message body (long string) via standard input and parses Komar Associates
Using FORMs and CGI continued • CGI program does its thing and formats results in HTML • CGI program returns results via standard output • Server returns the results to the Browser • ALL DONE !! • Since the connection is “stateless” hidden fields are used in sequences of forms Komar Associates
Example FORM <HTML> <BODY> <H1>Sending POST Data to a CGI Program</H1> <FORM ACTION= "http://140.209.124.107/cgi-bin/CgiPost.bat" METHOD="POST"> Enter some data: <INPUT TYPE="TEXT" NAME="TextField1"><BR> Enter some more: <INPUT TYPE="TEXT" NAME="TextField2"><BR> <INPUT TYPE="SUBMIT" VALUE="Send It"> </FORM> </BODY> </HTML> Link Komar Associates
Count CGI Version (Client Applet) import java.awt.*; import java.applet.*; import java.io.*; import java.net.*; import java.util.*; public class CountCGIClientApplet extends Applet { Komar Associates
Count CGI Version (Client Applet) try { String sum = "0"; showStatus("Incrementing"); int count = new Integer(textField1.getText()).intValue(); long startTime = System.currentTimeMillis(); for (int i = 0 ; i < count ; i++ ) { sum = increment(sum); } long stopTime = System.currentTimeMillis(); list1.addItem("Avg Ping = " + ((stopTime - startTime) / (float)count) + " msecs"); showStatus("Sum = " + sum); } catch(Exception e) { showStatus("System Exception" + e); } Komar Associates
Count CGI Version (Client Applet) public String increment(String currentSum) { String script = "/cgi-bin/Count.bat"; Socket socket = null; String rdata = ""; String line = ""; String lastLine = ""; Komar Associates
Count CGI Version (Client Applet) try { socket = new Socket(getCodeBase().getHost(), 80); PrintWriter ostream = new PrintWriter(socket.getOutputStream()); BufferedReader istream = new BufferedReader( new InputStreamReader(socket.getInputStream())); ostream.print("POST " + script + " HTTP/1.0\r\n" + "Content-type: text/plain\r\n" + "Content-length: " + ("increment " + currentSum).length() + "\r\n\r\n"); ostream.print("increment " + currentSum + "\r\n"); ostream.flush(); Komar Associates
Count CGI Version (Client Applet) try { while ((line = istream.readLine()) != null) { list1.addItem(line); lastLine = line; } } catch ( SocketException e ) { istream.close(); ostream.close(); } Komar Associates
Count CGI Version (Server) import java.io.*; import java.util.*; class CountCGIServer { public static void main(String[] args) { int count; String line; try { // create streams PrintWriter ostream = new PrintWriter(System.out); BufferedReader istream = new BufferedReader( new InputStreamReader(System.in)); line = istream.readLine(); StringTokenizer tokens = new StringTokenizer(line); String myOperation = tokens.nextToken(); Komar Associates
Count CGI Version (Server) if(myOperation.equals("increment")) { String countString = tokens.nextToken(); count = Integer.parseInt(countString); count++; ostream.println( Integer.toString(count) ); ostream.flush(); } istream.close(); ostream.close(); } catch (Exception e) { System.out.println("Error " + e); } } } Komar Associates
Count CGI Html and Batch Files <html> <body> <h1>Count CGI Client Applet</h1> <hr> <center> <applet code=CountCGIClientApplet.class width=500 height=500> </applet> </center> </body> </html> d:\jdk1.1.7a\bin\java CountCGIServer Do it! Komar Associates
Java and CGI Helpful Link Marty Hall’s CGI page Komar Associates
CGI Assignment • Stay tuned…. Komar Associates