Приглашаем посетить
Брюсов (bryusov.lit-info.ru)

14.2 CGI applications and preprocessors

Table of Contents

Previous Next

14.2 CGI applications and preprocessors

14.2.1 Example of a CGI application

From the discussion above, to develop a server technology is quite straightforward. All you have to do is to generate the CGI header (lines 15 of listing ex14-02.txt) and return a Web page. In fact, for a minimum CGI header the string



Content-type:text/html

with a blank line at the end will be enough. Sometimes, this string is called the "magic line" of CGI applications.

Note that the standard CGI communications between client and server require the standard I/O devices. All CGI correspondence must be echoed or printed to the standard output device such as a screen or a terminal.

To output some strings to the screen is not too difficult. In fact, you can program this with a number of computing languages. The following is a CGI example written in the ANSI/ISO C++ language. Don't worry if you are not familiar with C++: if you know how to write a "Hello World" program, you can modify the program as follows:



Example: ex14-01.cpp - A C++ Program For CGI

 1: #include <iostream>
 2: using namespace std;
 3: int main()
 4: {
 5:   cout <<"HTTP/1.0 200 OK \n";
 6:   cout <<"Server:www.pwt-ex.com\nMIME-Version:1.0\n";
 7:   cout <<"Content-type: text/html\n\n";
 8:
 9:   cout <<"<?xml version=\"1.0\" encoding=\"iso-88591\"?> \n";
10:   cout <<"<!DOCTYPE html PUBLIC \"-//W3C//DTD XHTML 1.0 \n";
11:   cout <<" Transitional//EN\" \n http://www.w3.org/TR/xhtml1/DTD/ \n";
12:   cout <<" xhtml1-transitional.dtd\"> \n";
13:   cout <<"<html xmlns=\"http://www.w3.org/1999/xhtml\" \n";
14:   cout <<" xml:lang=\"en\" lang=\"en\">\n";
15:
16:   cout <<"<head><title></title></head> \n";
17:   cout <<"<body style=\"background:#000088;color:#ffff00;\n";
18:   cout <<" font-weight:bold;\n font-family:arial;font-size:22pt;";
19:   cout <<" text-align:center\"> \n";
20:   cout <<" <br /><br /> \n This page was generated by \n";
21:   cout <<" a C++ program <br /> \n";
22:   cout <<" and run as a CGI script \n</body>\n</html>\n";
23:   return 0;
24: }

The console output statement cout is a frequently used statement in C++ to output a string to the screen. For example, the statement in line 5



cout << "HTTP/1.0 200 OK \n";

will output the string "HTTP/1.0 200 OK" with a line break onto the screen. In fact, lines 57 output the entire CGI header to the screen and the rest (lines 922) return a simple XHTML document.

If you have a compiler capable of compiling this program, you should have an executable program on your machine. Suppose the executable program file is called ex14-01.exe.

For many systems including NT and UNIX/LINUX, CGI programs are usually stored in a default directory called cgi-bin. Other systems may have a different default directory. If the program ex14-01.exe is inside our default directory such as /book/chap14a of www.pwt-ex.com, you can run this program directly from the browser by issuing the following "http" command:

http://www.pwt-ex.com/book/chap14a/ex14-01.exe

Figure 14.2. ex14-01.exe

graphics/14fig02.jpg


By executing this command, the browser requests the document ex14-01.exe from the site www.pwt-ex.com and directory /book/chap14a. Since the document is an executable program, the server will run this file first. The execution result is a CGI header and an XHTML document and therefore can be displayed on the browser window.

A screen shot of this example is shown in Fig. 14.2. If you activate View and then Source from the browser menu, the source code of the page is the same as the output from the executable program ex14-01.exe (except for the CGI header). The output of the executable program is shown in Fig. 14.3.

Figure 14.3. Source generated by program ex14-01.exe

graphics/14fig03.jpg


For UNIX/LINUX systems, you may have a different name for the executable program and may need to change the program status to executable so that a browser can run the program via the Internet.

In fact, you don't need the entire CGI header to make it work. For a working CGI header, all you need is the CGI magic string



Content-type: text/html\n\n

That is, the content type plus a blank new line. If you forget to put this string in your CGI application, the result will be strange. If you delete lines 56 of ex14-01.cpp as in ex14-03.txt, the program will still work on all browsers.



Listing: ex14-03.txt - Minimum CGI Framework

 1: #include <iostream>
 2: using namespace std;
 3: int main()
 4: {
 5:
 6:
 7:   cout <<"Content-type: text/html\n\n";
 x:     xxx xxx xxx xxx
 x:     xxx xxx xxx xxx
21:   cout <<" a C++ program <br /> \n";
22:   cout <<" and run as a CGI script \n</body>\n</html>\n";
23:   return 0;
24: }

If you comment out line 7 and recompile the program, the behavior will be different depending on the browsers. IE6.x may still work, but others such as NS6.x and Opera will open a window to ask what you want to do with the document. For NS communicator or NS4.x, it opens a "Save As" window to help you to save the document. The reason is that if you haven't played by the rules (CGI rules), the browser will not recognize the document and the response can be different.

Many people refer to CGI software as CGI preprocessors due to the fact that documents can be processed on the server. This feature provides a powerful capability and opens up a new area of applications on the Web. Applications such as databases, e-commerce, and Web security are almost impossible without it.

Two of the very first CGI preprocessors that are free and widely available across different platforms are Perl and PHP.

14.2.2 A CGI preprocessor the "perl" program

For Web applications, the searching and extraction power of the Perl language make it an ideal choice for handling CGI applications. In general, the Perl package contains an executable program or preprocessor called "perl" to process documents written in the Perl language (or Perl script).

Basically, the perl program takes a file or a perl file as input and outputs the CGI header and an XHTML document onto the standard screen. Since Perl already includes all the features and many more functionalities than the trivial C++ program listed in ex14-01.cpp, you no longer need to write any C++ programs.

Some advantages of using Perl are that it is

  • system independent;

  • easy to understand;

  • a powerful alternative to C/C++ or any programming languages;

  • able to call system commands and functions;

  • a language with strong text-processing functions such as pattern matching.

If you have a UNIX/LINUX system up and running properly, you may already have the perl preprocessor installed. If you are using Windows XP, 2000, 9.x, and/or NT, a perl preprocessor porting or package called ActivePerl can be downloaded from www.activestate.com. Basically, Perl is free and available to everyone.

As one of the main objectives of this book, we will keep the language features and commands to a minimum and concentrate on applications on the Web. Following Perl practice in Chapter 13, the following format (ex14-04.txt) is used to convert an XHTML page to a Perl application.



Listing: ex14-04.txt - A Framework To Convert XHTML To Perl

 1: #! /usr/bin/perl
 2: print ("Content-type:text/html\n\n");
 4:
 5: print << "mypage";
 6:   **** **** ****
 7:      The XHTML Page
 8:   **** **** ****
 9: mypage

To use this structure, all you have to do is insert an HTML/XHTML document into lines 68. Consider the simple Perl script example ex14-02.pl.



Example: ex14-02.pl - Generating XHTML Page With Perl

 1: #! /usr/bin/perl
 2: print ("Content-type:text/html\n\n");
 3:
 4: print << "mypage";
 5:  <?xml version="1.0" encoding="iso-88591"?>
 6:  <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
 7:  "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
 8:  <html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
 9:  <head>
10:   <title> Generating XHTML Page With Perl - ex1402.pl</title>
11:  </head>
12:  <body style="background:#000088">
13:   <table width="550" border="0" align="center">
14:   <tr>
15:    <td width="80%" align="center">
16:       <span style="font-family:arial;font-size:18pt;color:#ffff00">
17:         <b>Welcome To</b><br /></span>
18:       <span style="font-family:arial;font-size:18pt;color:#00ffff">
19:         <b>Practical Web Technologies</b></span></td>
20:  <td><img alt="pic" src="logo_web.jpg" width="140" height="70" /></td></tr>
21:   <tr align="center"><td colspan="2">
22:     <img alt="pic" src="line1.gif" width="550" height="6" />
23:     <span style="font-family:arial;font-size:18pt;color:#ffff00">
24:         <br /><br /><b>A Simple XHTML Page: From Perl</b><br /></span>
25:     <span style="font-family:arial;font-size:16pt;color:#00ff00">
26:         <b>Some Text Strings and Images</b><br /><br /></span>
27:   </td></tr>
28:  </table>
29:  </body>
30: </html>
31: mypage

This is a Perl file to be interpreted by the perl program. After the location of the perl program (line 1), the statement in line 2 outputs the "magic" string "Content-type:text/html" and a blank new line to the standard output (screen). This is the minimum requirement to instruct the browser to listen for an HTML/XHTML document. The print<<"mypage" command in line 4 specifies a here document and is used to output all messages between lines 4 and 31 to screen.

If you have the perl program installed, you can activate it from a shell window (or MS-DOS). One such command is



shell> perl ex1402.pl

The result of this command is shown in Fig. 14.4. If you run ex14-02.pl as a CGI application, you can use

http://www.pwt-ex.com/book/chap14a/ex14-02.pl

Figure 14.4. Output from Perl

graphics/14fig04.jpg


from the browser. In this case, the Perl file is assumed to be in the /book/chap14a directory. The result is shown in Fig. 14.5. The purpose of this example is not the display but to understand the nature of the perl preprocessor, the Perl source file, and the relationship with Web browsers.

Figure 14.5. Running a Perl script

graphics/14fig05.jpg


14.2.3 The PHP preprocessor

Another well-known CGI preprocessor on the Web is PHP. The PHP software is a program called "php." Perl and PHP are dedicated to device- and platform-independent CGI applications. They both work in a similar fashion in that the CGI header and an XHTML document are output to the browser via the standard output device.

Unlike Perl script, PHP returns the CGI magic string automatically to the browser together with anything that it cannot understand. This feature allows a PHP program to be embedded into existing XHTML documents. To understand how PHP works, consider a date and time example with PHP script:



Example: ex14-03.php - Generating XHTML With PHP

 1: <?PHP echo"<?";?>xml version="1.0" encoding="iso88591"<?PHP echo"?>";?>
 2: <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
 3:     "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
 4: <html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
 5: <head><title>Getting Date & Time Using PHP -- ex1403.php</title></head>
 6: <body style="background:#000088;text-align:center;font-family:arial;
 7: font-size:16pt;color:#ffff00"> Getting Today's Date Using PHP <br /><br />
 8: <?PHP $today = getdate();
 9:    $month = $today['month'];
10:    $mday = $today['mday'];
11:    $year = $today['year'];
12:    $hours = $today['hours'];
13:    $minutes = $today['minutes'];
14:    $seconds = $today['seconds'];
15: ?>
16: <script>
17:  document.write("Today Is <br /><br />")
18:  document.write( "The Current Day =<?PHP echo "$mday"; ?> <br />")
19:  document.write("The Current Month = <?PHP echo "$month"; ?> <br />")
20:  document.write("The Current Year = <?PHP echo "$year"; ?> <br /><br />")
21:  document.write("And The Time Is <br /><br />")
22:  document.write( "The Current Hours =<?PHP echo "$hours"; ?> <br />")
23:  document.write("The Current Minutes= <?PHP echo "$minutes"; ?> <br />")
24:  document.write("The Current Seconds = <?PHP echo "$seconds"; ?> <br />")
25: </script>
26: </body>
27: </html>

Provided you have a proper installation of PHP, you can run this PHP script with the following command:



shell>php ex1403.php

where shell> is the command prompt in a console window. The result is shown in Fig. 14.6. From this figure, you can see that the first three lines are:



X-Powered-By:PHP/4.0.5
Content-type:text/html
xxxx a Blank Line xxxx

Figure 14.6. ex14-03.php

graphics/14fig06.jpg


The first statement is the identity of the PHP preprocessor, while the second and third lines are the "magic" CGI header to the browser. If the following "http" command is issued from the browser

http://www.pwt-ex.com/book/chap14a/ex14-03.php

the XHTML document is rendered and displayed as in Fig. 14.7.

Figure 14.7. Output from PHP

graphics/14fig07.jpg


Now you know how Perl and PHP work, it is time to consider how to perform CGI applications on the Web.

    Table of Contents

    Previous Next