To Previous Chapter To Table of Contents To Bottom of Page To Next Chapter

CS 335/535 Web Programming - Week 5 - CGI and the Internet

  1. CGI and the Internet

    • What is CGI and What Can It Do?
      Common Gateway Interface (CGI) is a standard for interfacing external applications with information servers (i.e. Web servers)

      Web Browser ->HTTP Daemon <--Standard Output-->CGI Program<--TCP/IP Socket-->Database Engine

      • Terminology
        • environment variable - named parameter that carries information from the server to the script. Often a variable in the OS's environment
        • Script - software that is invoked by the server via the CGI
        • Server - application program that involes the script in order to service requests
    • What are the Benefits of Using CGI?
      • can retrieve and update real-time data and format dynamic pages (including images & audio)
      • can be used to store and retrieve cookies
      • can write CGI programs in Perl, C/C++, TCL, Visual BASIC, etc.
      • supported by most web servers and platforms
    • What Are the Negatives of Using CGI?
      • security - programs run on host
      • uses resources on host
    • The Protocols communications is via stdout; CGI program needs to decode information, process it, and return the information
      • Environment Variables
        Environment Variable Purpose/Comment
        GATEWAY_INTERFACE Revision of CGI Spec to which the server complies
        CGI/revision
        SERVER_NAME server's host name, DNS alias, IP address
        SERVER_SOFTWARE name/version of information server
        SERVER_PROTOCOL protocol/version of the information protocol this request has
        SERVER_PORT port # to which the request was sent
        REQUEST_METHOD method to which the request was made (GET, HEAD, POST, ...
        PATH_INFO extra path information
        PATH_TRANSLATED translated version of PATH_INFO (virtual to physical)
        SCRIPT_NAME virtual path to the script being executed
        QUERY_STRING query information that follows ?
        REMOTE_HOST host name making the request
        REMOTE_ADDR IP address of the remote host
        AUTH_TYPE protocol specific authentication method
        REMOTE_USER authenticated user name
        REMOTE_IDENT remote user name
        CONTENT_TYPE content type
        CONTENT_LENGTH content length

      • Getting Information from the Server
        set QUERY_STRING=mydata+is+here and is passed as string following ? on URL
      • Getting Form Data
        1. <FORM ACTION=CGI URL METHOD=GET>
          GET places the data in QUERY_STRING
        2. <FORM ACTION=CGI URL METHOD=POST>
          POST places the data in STDIN with the # of bytes in CONTENT_LENGTH
        Example
      • Returning Information to the Client
        This information must be sent back in proper HTML format wht the first line containing - Content-Type: text/html followed by a blank line and then the HTML code
  2. Using CGI in Internet Applications
    • PseudoCode
    • The CGI Program
      • Initialization
      • Determining the Request Method
      • Sending the Form
      • Receiving Information from the User
      • System Processing
      • Sending a Reply to the User

Examples

  1. Hello World in C
  2. Hello World in Perl
  3. Hello World with sh
  4. More Sample Programs

Further Information on CGI

To Previous Chapter To Table of Contents To top of page To Next Chapter