CGI.MASTER

From Pickwiki
Jump to navigationJump to search

HomePage>>SourceCode>>PerlSource

A program that listens on a named pipe for requests, converts parameters from key=value pairs into a dynamic array and calls a subroutine to actually create some HTML. Phew! Is it really worth it?

sunset /info[[/DEVEL/BP]].DEV> cat CGI.MASTER
***************************************************************************
* Program: CGI.MASTER
* Author : Ian [[McGowan]]
* Date   : 01/06/1997
* Edited : 16:57:35 Aug 03 2000 By MGC     
* Comment: Klugy web <> unidata interface
***************************************************************************
* 04/29/1998 IAN  Added code to check for already running phantom
* 04/23/1998 IAN  Modified to use named pipes, instead of polling
* 11/08/1998 IAN  Modified to use consistent delimeter (LF)

* @AM seperates records in the input
* @VM seperates fields in a record

ACC=FIELD(@SENTENCE,' ',2)
IF ACC = '' THEN
   PRINT 'MUST SPECIFY AN ACCOUNT!' ; STOP
END

* Check to see if we're already running in this account
MY.PID=@USERNO
OSREAD OLD.PID FROM "/samba_share/web/":ACC THEN
   PRINT "Checking to see if process alive: ":OLD.PID:
   EXECUTE \!ps -ef | awk '{print $2}' | grep \:OLD.PID CAPTURING OUTPUT
   IF OUTPUT # "" THEN
      PRINT 'Phantom is already running as PID ':OLD.PID
      PRINT 'Use stopcgi ':ACC:' to stop it'
      PRINT 'and startcgi ':ACC:' to start it'
      STOP
   END
   PRINT " Not!"
END
PRINT "CGI.MASTER started as PID:":MY.PID
PRINT TIMEDATE()
OSWRITE MY.PID ON "/samba_share/web/":ACC

INPUT.FILE="/samba_share/web/in/":ACC:"/in_from_perl"
OPEN "VOC" TO VOC ELSE STOP 201, "VOC"

LOOP
   OSREAD REQUEST FROM INPUT.FILE ON ERROR REQUEST='' ELSE REQUEST=""
   
   IF REQUEST = '' THEN
      * Slow it down if out of control
      PRINT 'NULL REQUEST - ':TIMEDATE()
      SLEEP 1
   END ELSE
      GOSUB PROCESS.REQUEST
   END
REPEAT
STOP

PROCESS.REQUEST:
   * The calling program sends us the location to put the output file
   OUTPUT.FILE=REQUEST<1>
   OUTPUT='CGI.MASTER: Internal CGI error'
   COMMAND=''
   KEY.REC='' ; VALUE.REC=''
   I=DCOUNT(REQUEST,@AM)
   FOR F=2 TO I
      L=REQUEST<F>
      KEY=FIELD(L,'=',1)
      VALUE=FIELD(L,'=',2,999)
      BEGIN CASE
         CASE KEY='command'
            COMMAND=VALUE
         CASE 1
            KEY.REC<-1>=KEY
            VALUE.REC<-1>=VALUE
      END CASE
   NEXT F
   IF COMMAND = '' THEN
      OUTPUT='CGI.MASTER: No command received'
   END ELSE
      READV DUMMY FROM VOC, COMMAND, 1 THEN
         * Verify that subroutine exists
         IF DUMMY='C' THEN
            * This next line means we will run the new version of any recompiled subs
            * adding a small amount of overhead to each call...
            EXECUTE \NEWPCODE\
            CALL @COMMAND(VALUE.REC,KEY.REC,OUTPUT)
         END ELSE
            OUTPUT='CGI.MASTER: ':COMMAND:" is a VOC item, not a subroutine"
         END
      END ELSE
         OUTPUT='CGI.MASTER: ':COMMAND:" is not a cataloged subroutine"
      END
   END
   OSWRITE OUTPUT ON OUTPUT.FILE
RETURN