next up previous
Next: LP/CLP Scripts for CGI Up: Document: pillow_www6 Previous: Introduction

Writing basic cgi-bin applications

The simplest way of writing WWW applications is through the use of the ``Common Gateway Interface'' (CGI). A CGI executable is a standard executable file but such that the HTTP server can tell it in fact contains a program that is to be run, rather than a document text that is to be sent to the client as usual. The file can be distinguished by belonging to a special directory, commonly named cgi-bin, or by a special filename ending, such as .cgi. The basic idea behind this interface is illustrated in Figure 1. When the user selects an address of a CGI executable in a document, such as http://www.xxx.yyy/cgi-bin/hello_world (or perhaps http://www.xxx.yyy/foo/hello_world.cgi) the browser issues a standard document request (1). The HTTP server, recognizing that it is a CGI executable rather than a document, starts the executable (2), and during such execution stores the output of the executable in a buffer (3). Upon termination of the executable, the contents of the buffer (which should be in a format that the browser can handle, such as HTML) are returned to the browser as if a normal page with that content had been accessed (4).

  figure69
Figure 1: The CGI interface

The following is an example of how a very simple such executable can be written in an LP/CLP language. The source might be as follows:gif

main :-
    write('Content-type: text/html'), nl, nl,
    write('<HTML>'),
    write('Hello world.'),
    write('</HTML>').

And the actual executable could be generated as a saved state at the system prompt in the standard way. E.g., for most Edinburgh-style systems:

?:- compile('hello_world.pl'), 
     save('/usr/local/etc/htppd/cgi-bin/hello_world'), main.

The address of the executable in machine www.xxx.yyy would then be

http://www.xxx.yyy/cgi-bin/hello_world.

In some systems, saved states have the disadvantage of their generally large size, but many systems have other ways of producing reasonably-sized executables. For example, in the &-Prolog/CIAO system compiled executables can be generated which are generally of smaller size than the source program.


next up previous
Next: LP/CLP Scripts for CGI Up: Document: pillow_www6 Previous: Introduction

<herme@fi.upm.es>-< webmaster@clip.dia.fi.upm.es>
Last updated on Mon Mar 31 18:18:15 MET DST 1997