6.5 CGI binding for .xq files

Here are the instructions to make .xq files directly executable from a Apache web server.

Note: The code provided below is still in an experimental stage and is not intended to be used in a production environment.

6.5.1 httpd.conf

First you must adapt the httpd.conf configuration file of the Apache web server, in order to a) turn on cgi scripts (if not already the case), and b) add a "handler" for <code>.xq</code> files. To do so, I made the following changes:

$ diff -w httpd.conf httpd.conf.default
 858c858
 &lt;     AddHandler cgi-script .cgi
 ---
 &gt;     #AddHandler cgi-script .cgi
 885,890d884
 &lt; # redirect xquery files to our cgi script
 &lt; AddType    text/xml .xq
 &lt; AddHandler xquery-type .xq
 &lt; Action xquery-type /cgi-bin/xquery.cgi
Don't forget to restart Apache; so it reads httpd.conf

6.5.2 xquery.cgi

In the cgi-bin/ directory, you must also place the script (xquery_cgi) under the name xquery.cgi with executable file permissions:

chmod 755 xquery.cgi

Beware! You must potentially adapt:

  1. the location of bash
  2. WWWDIR(the htdocs directory where Apache stores its content)
  3. MONETDIR (the installation dir of MonetDB/XQuery)
 #!/bin/bash
 echo 'Content-type: text/xml'
 echo 

 MONETDIR=/path_to_monetdb/
 WWWDIR=/var/www/htdocs

 XQFILE=$WWWDIR/$REDIRECT_URL

 if [ x$QUERY_STRING == x ]
 then
   $MONETDIR/bin/mclient --set prefix=$MONETDIR --set exec_prefix=$MONETDIR -fxml -lx $XQFILE
 else
    `echo "sed -e s/%$QUERY_STRING/g $XQFILE" | sed -e "s/&amp;/\/g -e s\/%/g" -e "s/=/%\//g"` &gt; /tmp/$$
   $MONETDIR/bin/mclient --set prefix=$MONETDIR --set exec_prefix=$MONETDIR -fxml -lx /tmp/$$
 fi

6.5.3 passing parameters

The <code>xquery.cgi</code> script allows parameter passing. Suppose we have the following XQuery file (save it as example.xq in the htdocs>-folder):

 for $i in 1 to %max%
 return element { "mult" } {
   $i, " times ", %table%, " is ", $i * %table%
 } 

The table and max have been parametrized. Parameters take the form: %name% The parameters can be used in the usual URL convention:

     <a href="http://localhost/example.xq?table=3&max=10"
     class='external'>http://localhost/example.xq?table=3&max=10</a>

Beware: parameter substitution in this script is very simple; it won't work with special characters in it (that get escaped by the web server) or even spaces. For optimal web server performance, we may want to do all parameter substitution inside mclient; so that we don't need to fork a bash shell process on each web request.