Simple Content Autonegotiation (F)CGI

This is a simple (Fast)CGI application that performs HTTP auto-negotiation and serves files staticly. It can be installed in the usual way with one of:

% pip install autoneg
% easy_install autoneg

It is intended to facilitate serving of different variants of an HTTP resource according to the requested content type. In this way one can pre-render RDF/XML, N3, HTML, plain text, etc variants of a document and save them to the filesystem to be served directly rather than relying on complicated databases or middleware.

This work was sponsored by the Open Knowledge Foundation for use in

Scripts

autoneg_fcgi

autoneg_cgi

The script need to know the following:

  • mime_type -> extension mapping
  • base directory to look for the files in the filesystem
  • name of the script to strip from request URIs

There is a default set of mime_types which might be good for some purposes but it will usually be desirable to put them in a configuration file.

If you have a file called conf.py with:

{ "mime_types" : [ ("text/plain", ["txt"]), ("text/html", ["html"]) ] }

And then run the cgi with:

% autoneg_cgi -c conf.py

It will look for text files with the extension .txt and html files with the extension .html.

Autonegotiation is done by first taking into account the client’s preferences as expressed in the HTTP Accept header and then the server’s preferences as configured.

Parameters such as base and script may be configured either in the configuration file or passed on the command line.

Running a fast-cgi service can be done with spawn-fcgi which should be available for most operating systems. A content negotiation layer over /var/www might be started with

% spawn-fcgi -P /tmp/test.pid -s /tmp/test.sock -M 0666 – autoneg_fcgi -c conf.py -b /var/www

And then the web server would be configured to pass requests which it couldn’t handle to this script over the /tmp/test.sock socket.

NGINX Configuration

Example configuration for a global installation:

location / {
    index index.html index.htm
    try_files $uri $uri/ /autoneg$uri;
    autoindex on;
}

location ~ ^/autoneg {
    fastcgi_pass   unix:/var/run/autoneg.sock;
    include        fastcgi_params;
}

And then the script could be launched with a command like:

% spawn-fcgi -P /var/run/autoneg.pid -s /var/run/autoneg.sock -M 0666 \
    -- autoneg_fcgi -c conf.py -b /var/www

the try_files means that if the file exists on disk it will be served correctly. Otherwise the request will be passed internally to the autonegotiation service.

Table Of Contents

This Page