[svlug] progress indicator for cgi...

George Georgalis george at galis.org
Tue Sep 28 11:42:15 PDT 2010


On Tue 28 Sep 2010 at 08:43:01 AM -0700, Don Marti wrote:
>begin George Georgalis quotation of Mon, Sep 27, 2010 at 06:18:42PM -0700:
>
>> I have a simple task which is invoked by hitting
>> the specific url because of a couple apache directives
>> 
>>         AddHandler cgi-script .cgi
>>         Options +ExecCGI
>> 
>> problem is, it can take 10 to 15 seconds for the
>> page to complete and I want the client to know their
>> request is pending.
>
>Can you fork and build the result page in a temporary
>file, then have the script return a cookie and the
>meta refresh page with a 10-15s delay?  The second
>time the browser hits, serve the temporary file if
>present. (Of course you will have to use file locking
>and temp file naming carefully to avoid building more
>than one temp file per client.)

I sorta did that, the script renders a please wait message and
reloads the workhorse page which is simply a reload to the
results. Your solution would be better in the case of different
client sessions.  This task just updates metric reports.

The first revision of below didn't have the spin case, so it
was quite easy to DOS the server with 5 clicks to the cgi.

-George



#!/bin/sh

# Regex based on http://www.markussipila.info/pub/emailvalidator.php
flatten () { echo "$*" | sed -E -e 's/^[^a-z0-9]/,/' -e 's/[^a-z0-9_.+-]/,/g' ;}

lock="/tmp/$(flatten $SCRIPT_FILENAME)"
query=$(flatten $QUERY_STRING)
[ -e "$lock" ] && query="spin"

mkredirect () { url=$1 ; read body
cat <<EOF
Content-Type: text/html;charset=ISO-8859-1

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
        "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<title></title>
<META HTTP-EQUIV="Refresh" CONTENT="0; URL=$url">
</head><body>
$body
</body></html>
EOF
}

case $query in
 spin)
  while [ -e "$lock" ]; do sleep 1 ; done
  echo | mkredirect result/
 ;;
 result)
  touch $lock
  result.sh result
  echo | mkredirect result/
  rm $lock
 ;;
 *)
  echo '<p>Generating results... please wait 15 seconds.' | mkredirect 'result.cgi?result'
 ;;
esac

exit





More information about the svlug mailing list