1. Web Servers and CGI/Perl Programs

 

 

1-1  Web Servers

 

Web server is a network server that manages and controls access to files, folders, and other resources for Internet and Intranet access via HTTP protocol. The program called the hypertext transfer protocol daemon (httpd), which runs on the Web server computer, handles the requests from client Web browsers and send responding information back to clients. When clients request information from the server, the Web server returns the appropriate Multipurpose Internet Mail Extension (MIME) type data to client Web browsers. Web browsers then use the MIME type to determine the file type and determine if it can process the document directly, or it may require an external helper application, or a plug-in program. Some popular Web servers are Microsoft Personal Web Server (PWS), Microsoft Internet Information Server (IIS), Apache a Web server for both 32-bit Windows and UNIX based operating systems, Netscape's FastTrack and Enterprise servers, Jigsaw Sever (Java environment), Windows 95/ 98/ NT/ Sun Microsystems' Solaris and Linux. In addition, you may search for the free Web servers through Internet search engines such as www.alta-vista.com .

 

The Common Gateway Interface, or CGI, is a standard protocol for external gateway programs to interface with information servers such as HTTP servers. A CGI program, on the other hand, is executed in real-time by the server, so that it can create dynamic information for Web browsers. We will introduce you to the Web Server (OmniHTTPd) and CGI programs, which can be written in Visual Basic, C, Visual C++, Perl, etc.

Perl (Practical Extraction and Report Language) is an interpreted language (actually it both a compiler and an interpreter), but it works a little bit differently from other interpreted languages. Perl can be used to create Web pages, read Usenet News, do system administration and programming, and write network clients and servers applications. This lab will include running a simple Perl program and CGI program (interacting between server and client). Perl for Win32 can be downloaded at http://www.activestate.com , and http://www.perl.com/CPAN.

 

Every file with html, htm in the htdocs folder of the Apache Web server, HtDocs folder in the HTTPd Web Server, and wwwroot folder in the PWS Web server are available to the Web browsers when the Internet connection has been made. After that, the client computer will be able to view all files in the this folder. Therefore, depends on the type of Web server, the files to be published in the Internet has to be in the htdocs (Apache), HtDocs (HTTPd), or wwwroot (PWS). For instance, if the HomePage.html is located in the HtDocs folder (HTTPd), client computers can enter WebServer.com/HomePage.html to  browse the HomePage page in their browser.

All files in the Cgi-bin folder in the Apache server, Cgi-Bin folder in the HTTPd Web server, and Scripts in the PWS server are CGI programs. The CGI program will run when the program is called or linked. The program can be linked in the Form format with Action tag from one of the Web pages (for example) in the HtDocs folder (HTTPd Web server).

Other folders in the Web server root are Pictures, Icons, Web Administrator folders, and Web server defined folders.

 

 

1.2 CGI Protocol

 

The Common Gateway Interface (CGI) is a standard for interfacing Web applications with information servers such as HTTP or Web servers in a platform-independent manner. Some common Web-based client/server tasks that performed by CGI scripts (programs) include

 

A CGI program is an executable program that resided in a special directory such as /cgi-bin. Only a server can interact with a CGI Scripts, which is stored on the server side cgi-bin directory. It can be written in any language: C/C++, Fortran, Perl, TCL, any UNIX shell, Visual Basic, and AppleScript.

 

The CGI has been in use by the World-Wide Web since 1993.  The CGI specification can be found at the following Web sites:

ftp://ftp.ietf.org/internet-drafts/draft-coar-cgi-v11-0x.txt

www.golux.com/coar/cgi/

www.fastcgi.com

perl.apache.org

www.perl.com/CPAN/modules/by-module/Apache/

www.velocigen.com

 


 

1.3 Features of CGI Scripting

 

 

 

 

Figure 1. CGI and HTTP Server


 

 

1.4 Running Perl CGI Script

 

Perl (Practical Extraction Report Language) is a portable, interpreted language used primarily as a tool for writing scripts that perform such functions as:

Perl for Windows is available from www.activestate.com.

 

How to use Perl Scripts

 

Indicate URL as a virtual path of the CGI program into the Web browser:

            http://localhost/cgi-bin/hellow.pl

 

You don’t need a form to pass a parameter to most CGI programs. For example, you can run the CGI program and pass the variable through a question mark and a string.

            http://localhost/cgi-bin/password.pl?pass=tigerusa

 

 

Use a hyperlink to run the CGI:

<A HREF="cgi-bin/hellow.pl"> Click here to run hello.pl CGI program </A>

 

Pass extra path information to a CGI program, for example, to search the /exs/doc directory. The extra path information can be accessed through the PATH_INFO environment variable:

<A HREF="cgi-bin/serach.pl/exs/doc"> Search Document </A>

 

Use a question mark to pass keywords that will be used in a search. The information follows the question mark is available through QUERY_STRING environment variable.

<A HREF="cgi-bin/search.pl?Car+1999"> Search for 1999 Cars</A>

 

 


 

Example 1-1: A simple Perl program prints two lines on a monitor screen. You will need to have an access to a Perl interpreter, either on a Window-based PC or a UNIX machine. You will edit the following program using the Notepad editor and run this program directly under the Microsoft DOS machine without using a Web browser.

 

#!/usr/bin/perl -w

#test1.pl

print("My first Perl Program\n");

print("Hello!\n");

 

OUTPUT:

 

C:\httpd\Cgi-Bin>perl test1.pl

My first Perl Program

Hello!

 

 

Example 1-2: The simple CGI script hello.pl generates an HTML page is already saved in cgi-bin directory of the ECET Lab server with the IP address of 149.164.36.204. To run this CGI program directly from the browser you enter the: http://149.164.36.204/cgi-bin/hello.pl. The first line #!usr/bin/perl informs the UNIX operating system to locate where the Perl interpreter is located.

 

#!/usr/bin/perl  

#  hello.pl   -- generate a header line and an HTML page.

#  Place this file in the Web server's directory -- /cgi-bin/hello.pl

#  or in a user's personal directories

 

 

print "Content-type: text/html\n\n";

 

print "<HTML>\n";

print "<HEAD>\n";

print "<TITLE> Hello World through Perl </TITLE>\n";

print "</HEAD>\n";

print "<BODY>\n";

print "<H1>Hello World!</H1>\n";

print "<P> Howdy, World!</P>\n";

print "</BODY>\n";

print "</HTML>\n";

 

 

 

 

 

 

 

 

 

 

OUTPUT:

 

 

 

If you are using IE5, click on view source code, you will see the source code without print() function from the Perl.

 


 

Example 1-3: Running CGI program thorough ECET lab server at 149.164.36.204. As we mentioned earlier, a CGI program is a program that runs on the server side machine. A Cgi program that is written, and compiled in Visual Basic, C and Visual C++ should have an exe extension. However, a regular Perl CGI program file has a pl or cgi extension. The server is prepared with a Web page for linking to the program; for example, http://www.address.com/LinkPerlMultiForm.html. This page usually has a form format (radio button, text box, send button, and so on) that allows the users enter information to the server. The page will also have Action attribute that links to the CGI program; for example,

  <FORM METHOD=POST ACTION= "http://149.164.36.204/cgi-bin/PerlCgi/MultiForm.pl">.

The CGI program then reads the input values such as (textbox, check box, radio button, text field, passwords, etc) from the MultiForm.html and processes necessary information and returns the result to the client as Web page.