7 Web Programming

7.3 Server-Side Web Scripts

We have seen so far how to write programs that create HTML documents. Such programs could be useful to transform existing data into a static set of HTML pages. However, this is not sufficient to create dynamic web pages, i.e., web pages whose contents is computed at the time they are requested by a client. The creation of dynamic web pages is supported by most web servers by so-called CGI (Common Gateway Interface) programs. If a web server is asked for a document with the suffix “.cgi” instead of “.html” (the exact behavior is defined in the configuration of the web server; see also Section 7.4 below), then the server does not return the contents of the corresponding file but executes the file (the “CGI program”) and returns the standard output produced by this program. Thus, a CGI program must write an HTML document on its standard output. The CGI program can also take user input in an HTML form into account; this is described in Section 7.5.

To support the creation of dynamic HTML documents, the library HTML.Base has a definition for HTML forms as follows:

data HtmlForm = HtmlForm String [FormParam] [HtmlExp] \pindex{HtmlForm}

The first argument is the title of the form (as in HTML documents) and the third argument is the contents of the form which can also contain elements for user input, as we will see later (see Section 7.5). The second argument is a list of optional parameters to extend the functionality of forms, like cookies, style sheets etc (see Section 7.8). Since they are seldom used in standard forms, the HTML library contains also the following function to specify forms without optional parameters:

form :: String -> [HtmlExp] -> HtmlForm
form title hexps = HtmlForm title [] hexps

Usually, the contents of dynamic HTML documents depend on the environment of the web server, e.g., information stored in the file system or databases. Thus, a dynamic web page is allowed to perform some I/O operations in order to compute the requested HTML document. As a consequence, a function to compute a dynamic web page must have the type “IO HtmlForm”. For instance, if we want to write a CGI program that computes the above multiplications of digits on demand, we define the following function multForm (the right-associate operator “$” is defined with a low precedence in the prelude and denotes function application; it is often used to avoid brackets, e.g., the expression “f $ g $ 3+4” is equivalent to “f (g (3+4))”) [Browse Program][Download Program]:

multForm :: IO HtmlForm
multForm = return $ form "Multiplication of Digits" htmlMultiplications

To see an application of accessing the server environment, we define a form that shows the current date and time of the server (the IO action “getLocalTime”, defined in the standard library Time, returns the local date and time in some internal representation which can be converted into a readable string by the function “calendarTimeToString”) [Browse Program][Download Program]:

timeForm :: IO HtmlForm
timeForm = do
 time <- getLocalTime
 return $ form "Current Server Time"
          [h1 [htxt $ "Current date and time: " ++ calendarTimeToString time]]

The installation of such web programs on a web server is described in the following section.