Cgilite

CGI programs, are executables run by a web server to produce a dynamic website. In most cases CGI programs are expected to produce an HTML document on stdout. They can retreive input and environment variables from the webserver, and thus react to the input of a web site user.

CGI programs are commonly written in languages like PHP, Perl, Pyhon, Ruby, NodeJS, etc. It is of course possible to write CGI programs in any programming language.

Cgilite aims to enable production and testing of CGI programs in Unix shell scipt. Because a shell interpreter is available on virtually all Unix-like systems, this is practical to write web applications with very minimalistic requirements. Cgilite based applications can run on busybox based systems, which are common on embedded hardware. Even on a "proper" webserver they have the advantage of having low dependencies in regard to the system installation, interpreter programs, and libraries.

Cgilite aims to be portable, so applications can run with different implementations of shell and userland tools (e.g. GNU, BSD, Busybox, etc.)

Usage

Cgilite should be included as part of your project. For GIT-Projects I suggest using the git subtree feature. I.e. to include the software in a cgilite/ subfolder within your own project use:

git subtree add --squash -P cgilite https://git.plutz.net/git/cgilite master

If you are not using git, just clone:

git clone https://git.plutz.net/git/cgilite

You can also clone to /opt and include files from there.

For the rest of this documentation we will assume, cgilite resides in a subfolder of your own project.

Writing a program

#!/bin/sh

##  Include CGIlite
. cgilite/cgilite.sh 

name="$(POST name)"

##  CGI programs should produce necessary header fields
printf 'Content-Type: text/htmlrn'
printf 'rn'  ##  An additional newline starts the output content

##  Heredocs are a convenient way to produce longer output containing shell expansions
cat <<END
<!DOCTYPE HTML>
<HTML><HEAD>
  <TITLE>Demo</TITLE>
</HEAD><BODY>
  $([ "$name" ] && printf '<P>Hello my dear %s</P>' "$(HTML "${name}")")
  <FORM METHOD="POST" ACTION="${PATH_INFO}">
    <LABEL>What's your name? <INPUT TYPE="TEXT" NAME="name"></LABEL>
    <BUTTON TYPE="submit">
  </FORM>
</BODY></HTML>
END

Serving the program

... need to be written ...