Skip navigation

CGI — just too much

I spent several hours today trying to feed a CGI script form stdin before I realized you had to set the CONTENT_TYPE, CONTENT_LENGTH and REQUEST_METHOD environment variables in order to emulate a POST correctly. It’d be simpler if:

  • All content (GET or POST) were available from stdin — even the ‘variables’.
  • The CONTENT_TYPE were available in the environment, but CONTENT_LENGTH were simply ignored — the web server should be handling “request validation”, anyhow.

Many command line programs could be called directly with the interface I suggest.

Here’s a shell script that talks to a CGI:

#!/bin/sh
##
##  Testing CGIs from the command line is a little tricky.
##
##


if [ $# -gt 1 ]
then
  content="$1"
else
  content='
    some rubbish,numbers
    other rubbish,more numbers
    '
fi


export CONTENT_TYPE="text/csv"
export REQUEST_METHOD="POST"
export CONTENT_LENGTH=`echo "$content" | wc -c`


echo "$content" | some-cgi.py

Potential improvements include reading from stdin, accepting an option to define the program to be run, &c.

Advertisements

Post a Comment

You must be logged in to post a comment.
%d bloggers like this: