World Wide Web and Common Gateway Interface
by Chin-Shiuh Shieh on
- World Wide Web Server
- Common Gateway Interface
- Common Gateway Interface Programming
World Wide Web Server
Client-Server Architecture
- All nodes on the Internet are born to be equal. What kind of services a site can offer depends on what kind of server programs had been installed on it. What kind of services a site can obtain depends on what kind of client programs had been installed on it.
- World Wide Web, likes other services on the Internet, is implemented under the framework of client-server architecture.
- A WWW server program is installed and executed on the server.
- Web pages are prepared on the web server.
- A WWW client program (browser) is installed and executed on the client.
- Client sends requests in URL format.
- Request travels over the Internet and reaches the server.
- Server wakes up and responses to the request.
- Server fetches requested file and send it back to the client via the Internet.
- Client parses received file and displays it on the screen.
- Most of our previous experience is at client-end. That is, we concern that which client program is preferred for a particular Internet service. Here we will turn our attention to server-end. We will focus on WWW service. Readers may refer to Internet.com's ServerWatch for server programs for other services.
Experience Internetworking on Standalone Machines
- If your computer has connection to the Internet, either via LAN or via dialup connection, your machine will be assigned with an unique IP address, static or dynamic. A registered domain name is preferred but not required.
- Within an isolated Intranet, you have the freedom to arbitrary choose the IP addresses for your machines. For example, if you have two PCs connected via Ethernet 10/100BaseT twisted connection, you may assign IP addresses 140.127.112.1 and 140.127.112.2 to them.
- You may still experiment Internetworking on a standalone machine even there is no real connection to the Internet.
- Both the domain name "localhost" and IP address "127.0.0.1" will refer to your local machine.
- In the following paragraphs, we will demonstrate the setting up of Internet servers on a standalone Windows 98/ME machine. However, the experience gained here may port to Windows NT/2000 machines or real Internet environment without any difficulty.
- NetInfo v4.3, by Tsarfin Computing, is a handy diagnosis tool for our subsequent study. It help us to find out wether servers are in work or not.
WWW Server on Unix
- FreeBSD and Linux are free clones of unix for IBM-PC compatible personal computers.
- Apache is a popular web server for FreeBSD, Linux, and other platforms.
- For performance and security reasons, only system administrators have the right to install CGI program in most unix systems. A CGI program, CGIWrap, allows users to install and test their own CGI programs without bothering the system administrators.
- System administrators should also consider PERL interpreter and PHP module to enhance the functionality of their WWW servers.
WWW Server on Windows
Network Configuration for Internet via LAN on Microsoft Windows 98
- Select "開始|設定|控制台|網路" to set up network components for Internet via LAN.
- "D-Link DFE-650 Fast Ethernet PC Card" is the driver for my Ethernet card. It depends on the network card you are using. In general, it is shipped with your network card.
- "TCP/IP" is the protocol driver for the Internet. It is included in Win98 CD. Click "內容" for "TCP/IP" to setup "IP位址", "通訊閘", and "DNS組態". All these setting is essential for network operation. Query your network administrator for correct setting.
- IP address is an unique identifier for a machine on the Internet. It can be statically assigned by network administrator, or dynamically allocated by DHCP server.
- "通訊閘", also known as gateway, is a machine which forwards outgoing packet.
- DNS server is a machine which translates symbolic domain name into numeric IP address, and vice visa.
Common Gateway Interface
Working paradigm of CGI
- Common Gateway Interface is a standard for transferring information between WWW servers and external programs.
- A WWW server program is installed and executed on server.
- Web pages with forms and corresponding CGI programs are prepared on the web server.
- A WWW client program (browser) is installed and executed on client.
- Client sends requests in URL format.
- Request travels over the Internet and reaches the server.
- Server wakes up and responses to the request.
- Server fetches requested file and send it back to the client via the Internet.
- Client parses received file and displays it on screen.
- Users at client-end input data into forms.
- Browser collects and URL-encodes user inputs. Those URL-encoded data are then sent back to the server.
- WWW server invokes specified CGI program and redirect URL-encoded data to its standard input.
- CGI program processes data and returns results to the server.
- Server forwards result to the client.
- Client parses received data and displays it on the screen.
- CGI programming is a common approach for interactive, dynamic web pages.
- CGI programs could be written in any programming language, including C, Perl, Java, or Visual Basic, provided that server platform supports it.
- CGI programs process user input data in form and return results to user on the fly.
- You may download ready-made CGI programs from The CGI Resource Index.
Sample CGI Programs in C
- Although C language is considered to be highly portable, successful porting is not guarantied.
- C source programs should be compiled on the server platform.
- Here are three examples:
- Web Page with User Signature v1.0 by Chin-Shiuh Shieh.
- Installation
"cgiex1.c" is compiled by Borland Turbo C++ 1.01. Compiled program, "cgiex1.exe", is placed in directory "c:\httpd\Cgi-Bin\"
"cgiex1.html" is placed in directory "c:\httpd\HtDocs\"
- Web Page with User Signature v1.0 in action at "http://localhost/cgiex1.html"
- Digital Clock CGI For Windows NT/95 Version 2.0 by Ryan Terry Sammartino.
- Installation
Unzip "clock.zip" to directory "c:\httpd\cgi-bin\"
- Refer to the clock using <IMG SRC="http://localhost/cgi-bin/clock.exe?format=24hr">
- The Visitor Counter Shipped with OmniHTTPd
- Installation
Run "c:\httpd\visadmin.exe" to create a new counter file, such as "csshieh"
- Refer to the counter using <IMG SRC="http://localhost/cgi-bin/visitor.exe?csshieh">
Sample CGI Programs in PERL
- PERL, short for Practical Extraction and Report Language, is a popular language for CGI programming.
- Perl is an an interpretive language. Perl scripts are text files. They are interpreted and executed by Perl interpreter.
- To run Perl scripts in Windows 98, all you need is a Perl Interpreter. We suggest ActivePerl-5.6.1.629-MSWin32.
- Although Perl is considered to be platform independent, successful installation is not always possible.
- Here are two examples:
- WebBBS v2.25 by Darryl C. Burgdorf.
- Installation
Unzip "webbbs.zip" to a temporary directory.
Modify "config.pl" according to your system settings and preference.
Place "config.pl" and "webbbs.pl" in directory "c:\httpd\cgi-bin\"
Create a new sub-directory "c:\httpd\cgi-bin\board\"
- URL "http://localhost/cgi-bin/config.pl" will refer to WebBBS v2.25.
- Guestbook2-1 by Stamp.
- Installation
Unzip "guestbook2-1.zip" to a temporary directory.
Modify "guestbook7.cgi", "repair7.cgi", "search7.cgi", and "define7.dat" according to your system settings and preference, and place them in directory "c:\httpd\cgi-bin\"
Modify "guestbook7.htm" and "addguest7.htm" according to your preference, and place them in directory "c:\httpd\htdocs\"
Place all "*.gif" in "c:\httpd\HtDocs\pic\"
- URL "http://localhost/addguest7.htm" will refer to Guestbook2-1.
Common Gateway Interface Programming
Preparing Form for User Inputs
-
<FORM METHOD="POST" ACTION="http://hoohoo.ncsa.uiuc.edu/cgi-bin/post-query">
Name: <INPUT TYPE="TEXT" NAME="NAME" VALUE="Chin-Shiuh Shieh">
<BR>
Password: <INPUT TYPE="PASSWORD" NAME="PASSWORD" VALUE="csshieh" SIZE="8">
<BR>
Age: <INPUT TYPE="INT" NAME="AGE" VALUE="32" SIZE="3">
<BR>
Sex:
<INPUT TYPE="RADIO" NAME="SEX" VALUE="MALE" CHECKED>Male
<INPUT TYPE="RADIO" NAME="SEX" VALUE="FEMALE">Female
<BR>
Amount of Schooling:
<INPUT TYPE="RADIO" NAME="DEGREE" VALUE="HS">High School
<INPUT TYPE="RADIO" NAME="DEGREE" VALUE="BS">College
<INPUT TYPE="RADIO" NAME="DEGREE" VALUE="MA" CHECKED>Graduate School
<BR>
Favorite hobbies?
<INPUT TYPE="CHECKBOX" NAME="SPORT">sports
<INPUT TYPE="CHECKBOX" NAME="READ" CHECKED>reading
<INPUT TYPE="CHECKBOX" NAME="COMPUTER" CHECKED>computers
<INPUT TYPE="CHECKBOX" NAME="COOK">cooking
<INPUT TYPE="CHECKBOX" NAME="SLEEP">sleeping
<BR>
Eyes color:
<SELECT NAME="COLOR" SIZE=1>
<OPTION>Green
<OPTION>Brown
<OPTION>Blue
<OPTION SELECTED>Black
</SELECT>
<BR>
Comments:
<BR>
<TEXTAREA NAME="COMMENT" ROWS=5 COLS=50>
Hello, world!
</TEXTAREA>
<BR>
<INPUT TYPE="SUBMIT"><INPUT TYPE="RESET">
</FORM>
-
- Modifier "METHOD" specifies the way of data passing and "ACTION" specifies the tentative CGI program.
Form Submission
- Form contents are URL-encoded and sent to web server. For example, above example form has it contents URL-encoded to
NAME=Chin-Shiuh+Shieh&PASSWORD=csshieh&AGE=32&SEX=MALE&DEGREE=MA&READ=on&COMPUTER=on&COLOR=Black&COMMENT=Hello%2C+world%21%0D%0A
- Web server redirects URL-encoded data to the stdin of the specified CGI program, and sets the environment variable CONTENT_LENGTH to data length, some other environments are also set.
Output of CGI Programs
- Invoked CGI program parses the URL-encoded input data and do anything required, such as updating database, issuing I/O actions.
- C language, especially the compiler Borland Turbo C++ 1.01, is recommended for applications involving hardware control.
- tiCGI 0.6, by Tilman Budde, is a C library which simplifies the parsing of URL-encoded data.
- Immediate feedback to the client is necessary for truly interactive WWW.
- Output from the stdout of CGI programs is captured by the web server.
- Output from CGI programs should have a header followed by a blank line and then native data. Header specifies document type and/or location, for example
- web page: Content-type: text/html
- plain text: Content-type: text/plain
- an URL: Location: ftp://bit.kuas.edu.tw
- Web server forwards feedback from the CGI program to its client.
Programming Examples
- Web Page with User Signature v2.0 using tiCGI 0.6 by Chin-Shiuh Shieh.
- Installation
"cgiex2.c" is compiled by Borland Turbo C++ 1.01. Compiled program, "cgiex2.exe", is placed in directory "c:\httpd\Cgi-Bin\"
"cgiex2.html" is placed in directory "c:\httpd\HtDocs\"
- Web Page with User Signature v2.0 in action at "http://localhost/cgiex2.html"
- Web-based Remote Control by Chin-Shiuh Shieh.
- Installation
"wwwrc.c" is compiled by Borland Turbo C++ 1.01. Compiled program, "wwwrc.exe", is placed in directory "c:\httpd\Cgi-Bin\"
"wwwrc.html" is placed in directory "c:\httpd\HtDocs\"
- An 8051 target board (8051.gif, 8051.a51, 8051.hex) is connected to PC via RS-232. Whenever a byte is received from its serial port, the received byte is sent to port P0 and the value of P1 is read and sent to serial port. Readers may refer to 8051 Development Tools for free or low-cost tools for 8051.
- Web-based Remote Control in action at "http://localhost/wwwrc.html"