home *** CD-ROM | disk | FTP | other *** search
/ OS/2 Shareware BBS: 35 Internet / 35-Internet.zip / gosrv250.zip / goremote.80 < prev    next >
Text File  |  1995-11-07  |  13KB  |  255 lines

  1. /* GoServe filter program for remote control, usually called from   */
  2. /* GoFilter.80; requires GoServe 2.25 or later.                     */
  3. /* Authorization (which may be on either GET or POST track) should  */
  4. /* be handled by the primary filter.                                */
  5.  
  6. /* Arguments:                                                       */
  7. /*   VERB -- the verb (GET or POST) from the HTTP request           */
  8. /*   URI  -- the partial URI (after leading directory has been      */
  9. /*           removed).  This must be of 'action' form (that is,     */
  10. /*           no file extension, even if a page request).            */
  11. /* Returned is a GoServe command, as from a filter.                 */
  12.  
  13. /* This routine is packaged as a single file for simplicity of      */
  14. /* distribution.  The code is generic, except for the form-specific */
  15. /* code at the bottom.                                              */
  16. /* Form pages are held in the data directory, as usual; this code   */
  17. /* lives in the GoServe directory tree.                             */
  18.  
  19. parse arg verb, action
  20.  
  21. /* Actions are one of two types: page requests and form responses.  */
  22. /* The latter have a verb of POST.  Page requests refer to files    */
  23. /* in the datadir called xxxx'.htm' (where xxxx is the name of the  */
  24. /* page, and the '.htm' is added by the PAGE function).             */
  25.  
  26. if verb='POST' then return form(action)  /* form processing */
  27. return page(action)                      /* page fill and display */
  28.  
  29. /* ---------------------------------------------------------------- */
  30. /* PAGE: return a page, dynamically modified                        */
  31. /* ---------------------------------------------------------------- */
  32. /* ACTION is name of the page                                       */
  33. /* MESSAGE is passed as ARG(2) to EVAL                              */
  34. page: procedure
  35.   parse arg action, message
  36.   pagefile=datadir()action'.htm'                  /* map to file */
  37.   if stream(pagefile, 'c', 'query exists')='' then /* not found */
  38.     return response('notfound',,
  39.       'asked for remote action page "'action'", which could not be found')
  40.   /* File exists.  Now we do the 'server side includes' on the file;  */
  41.   /* each {zzzz} is replaced by the result of the expression zzzz     */
  42.   data=charin(pagefile, 1, chars(pagefile))  /* read whole file */
  43.   out=''                                     /* result */
  44.   do forever
  45.     parse var data pre '{' expr '}' data     /* find expression */
  46.     out=out''pre                             /* pre-stuff */
  47.     if expr='' then if data='' then leave    /* no more '{' */
  48.     out=out''eval(expr, message)             /* add evaluation */
  49.     end
  50.   'VAR TYPE text/html AS' pagefile 'NAME out'/* send it */
  51.   return ''                                  /* [called as function] */
  52.  
  53. /* ---------------------------------------------------------------- */
  54. /* EVAL: return an expression evaluation [protected]                */
  55. /* ---------------------------------------------------------------- */
  56. /* ARG(1) is expression to evaluate                                 */
  57. /* ARG(2) is passed from PAGE, usually an arbitrary text message    */
  58. /* An expression starting with '?' is a direct EXTRACT, special-    */
  59. /* cased for simplicity.  ({?foo} == {extract(foo)})                */
  60. eval: procedure                         /* no variables visible */
  61.   if left(strip(arg(1)),1)='?' then return extract(substr(strip(arg(1)),2))
  62.   /* arbitrary expression expected -- assume CRLF and tabs are whitespace */
  63.   signal on syntax name evaloops                       /* handle errors */
  64.   interpret 'result='translate(arg(1),' ','0d0a09'x)   /* evaluate */
  65.   return result                                        /* done */
  66.  
  67. evaloops: return '[?]'
  68.  
  69. /* ---------------------------------------------------------------- */
  70. /* IF: select string based on ARG(1)                                */
  71. /* ---------------------------------------------------------------- */
  72. /* Rather like C's "?" operation */
  73. if: if arg(1) then return arg(2); else return arg(3)
  74.  
  75. /* ---------------------------------------------------------------- */
  76. /* QFLAG:  return 'checked' for a flag that's ON,  null otherwise   */
  77. /* QFLAGN: return 'checked' for a flag that's OFF, null otherwise   */
  78. /* ---------------------------------------------------------------- */
  79. qflag:  if extract(arg(1))='ON'  then return 'checked'; else return ''
  80. qflagn: if extract(arg(1))='OFF' then return 'checked'; else return ''
  81.  
  82. /* ---------------------------------------------------------------- */
  83. /* GMTWARN: return warning if GMT unavailable, null otherwise       */
  84. /* ---------------------------------------------------------------- */
  85. gmtwarn: if extract('GMTSET')='ON' then return ''
  86. return '[TZ was not set - GMT is <strong>not</strong> available]'
  87.  
  88. /* ----------------------------------------------------------------------- */
  89. /* RESPONSE: Standard [mostly error] responses.                            */
  90. /* ----------------------------------------------------------------------- */
  91. /* This is the same as the one in the sample main filter file.             */
  92. /* Arguments are: response type and extended message information.          */
  93. /* It returns the GoServe command to handle the result file.               */
  94. response: procedure
  95.   parse arg request, message
  96.   select
  97.     when request='badreq'   then use='400 Bad Request Syntax'
  98.     when request='notfound' then use='404 Not found'
  99.     when request='forbid'   then use='403 Forbidden'
  100.     when request='unauth'   then use='401 Unauthorized'
  101.     end  /* Add others to this list as needed */
  102.   /* Now set the response and build the response file */
  103.   'RESPONSE HTTP/1.0' use     /* Set HTTP response line */
  104.   parse var use code text
  105.   crlf='0d0a'x; out=''
  106.   out=out||'<!doctype html public "-//IETF//DTD HTML 2.0//EN">'crlf
  107.   out=out||"<html><head><title>"text"</title></head>"crlf
  108.   out=out||"<body><h2>Sorry...</h2>"crlf
  109.   out=out||"<p>The request from your Web client" message"."crlf
  110.   out=out||"<hr><em>HTTP response code:</em>" code '['text']'crlf
  111.   out=out||"<br><em>From server at:</em>" servername()crlf
  112.   out=out||"<br><em>Running:</em>" server()crlf
  113.   out=out||"</body></html>"crlf
  114.   'VAR TYPE text/html AS Response NAME out'  /* send it */
  115.   return ''                                  /* [called as function] */
  116.  
  117. /* ---------------------------------------------------------------- */
  118. /* FORM: process a form action.  Get here only if verb is POST.     */
  119. /* ---------------------------------------------------------------- */
  120. form: procedure
  121.   parse arg action
  122.   'read body var data'                       /* get the incoming data */
  123.   if rc=-4 then                              /* body too large */
  124.     return response('badreq', 'sent too much data')
  125.   if rc<>0 then                              /* e.g., invalid HTTP header */
  126.     return response('badreq', 'sent data that could not be read')
  127.  
  128.   /* set VAR.x variables from the incoming data stream */
  129.   VAR.=''                                    /* null string unless set */
  130.   data=data'&'                               /* set end condition */
  131.   do until data=''                           /* each value */
  132.     parse var data assign '&' data           /* split off name=value */
  133.     parse var assign name '=' value          /* separate */
  134.     value=translate(value, ' ', '2b090a0d'x) /* handle '+', tabs, and CRLF */
  135.     name='?'translate(packur(name))          /* caseless name, with leading '?' */
  136.     var.name=packur(value)                   /* set, after URI decoding */
  137.     end
  138.  
  139.   /* ----- The above is generic.  Now get form-specific. ----- */
  140.  
  141.   /* There are several ways of handling various types of form  */
  142.   /* shown here.  Compare with the various .HTM files to see   */
  143.   /* how they work.                                            */
  144.  
  145.   message='Changes applied.'                 /* Default message */
  146.   select
  147.     when action='gormdata' then do
  148.       dd=var.?datadir
  149.       if right(dd,1)<>'/' then dd=dd'/' /* be nice */
  150.       if dd='' then rc=1; else 'set datadir' dd
  151.       if rc<>0 then
  152.         return response('badreq', 'asked for "'dd'", which is not a directory')
  153.       end
  154.     when action='gormfilt' then do
  155.       ff=var.?filter
  156.       if pos('.',ff)=0 then ff=ff'.'extract('serverport')
  157.       if ff='' then rc=1; else 'set filter' ff
  158.       if rc<>0 then
  159.         return response('badreq', 'asked for "'ff'", which cannot be a filter')
  160.       end
  161.     when action='gormtest' then do
  162.       if var.?test='ON'    then 'set test on';    else 'set test off'
  163.       if var.?diag='ON'    then 'set diag on';    else 'set diag off'
  164.       if var.?diag2='ON'   then 'set diag2 on';   else 'set diag2 off'
  165.       if var.?trace='ON'   then 'set trace on';   else 'set trace off'
  166.       if var.?diagmsg='ON' then 'set diagmsg on'; else 'set diagmsg off'
  167.       end
  168.     when action='gormaudi' then do
  169.       if var.?auditaccept  ='ON' then 'set auditaccept   on'
  170.                                  else 'set auditaccept   off'
  171.       if var.?auditcomplete='ON' then 'set auditcomplete on'
  172.                                  else 'set auditcomplete off'
  173.       if var.?auditerror   ='ON' then 'set auditerror    on'
  174.                                  else 'set auditerror    off'
  175.       if var.?auditfail    ='ON' then 'set auditfail     on'
  176.                                  else 'set auditfail     off'
  177.       if var.?auditgmt     ='ON' then 'set auditgmt      on'
  178.                                  else 'set auditgmt      off'
  179.       if var.?auditinfo    ='ON' then 'set auditinfo     on'
  180.                                  else 'set auditinfo     off'
  181.       if var.?auditlimit   ='ON' then 'set auditlimit    on'
  182.                                  else 'set auditlimit    off'
  183.       if var.?auditselect  ='ON' then 'set auditselect   on'
  184.                                  else 'set auditselect   off'
  185.       if var.?audituser    ='ON' then 'set audituser     on'
  186.                                  else 'set audituser     off'
  187.       if var.?auditlazy    ='ON' then 'set auditlazy     on'
  188.                                  else 'set auditlazy     off'
  189.       end
  190.     when action='gormlimi' then do
  191.       err=''
  192.       limits='LimitClients LimitWarning LimitFiles',
  193.              'LimitTimeInactive LimitTimeTotal LimitTimeWait LimitStartWait',
  194.              'LimitHeader LimitBody'
  195.       'extract' limits
  196.       do until limits=''
  197.         parse var limits limit limits
  198.         value=value('var.?'limit)
  199.         if value(limit)<>value then do; 'set' limit value
  200.           if rc<>0 then err=err'"SET' limit value'" failed [rc='rc'].<br>'
  201.           end
  202.         end /* limits */
  203.       if err<>'' then message=err
  204.       end
  205.     when action='gormgene' then do
  206.       err=''
  207.       flags='menubar surface hidden sounds soundall fastfile fastfilter'
  208.       'extract' flags
  209.       do until flags=''
  210.         parse var flags flag flags
  211.         value=value('var.?'flag); if value='' then value='OFF'
  212.         if value(flag)<>value then do; 'set' flag value
  213.           if rc<>0 then err=err'"SET' flag value'" failed [rc='rc'].<br>'
  214.           end
  215.         end /* flags */
  216.       if err<>'' then message=err
  217.       end
  218.     when action='gormresp' then do  /* Mixed input and flags */
  219.       err=''
  220.       items='responsegoal responsegoalshow'
  221.       'extract' items
  222.       do until items=''
  223.         parse var items item items
  224.         value=value('var.?'item); if value='' then value='OFF'
  225.         if value(item)<>value then do; 'set' item value
  226.           if rc<>0 then err=err'"SET' item value'" failed [rc='rc'].<br>'
  227.           end
  228.         end /* items */
  229.       if err<>'' then message=err
  230.       end
  231.     when action='gormacts' then do
  232.       n=0                          /* actions count */
  233.       if var.?moveaudit='ON' then do; n=n+1; 'CONTROL var s.n moveaudit'; end
  234.       if var.?resetall ='ON' then do; n=n+1; 'CONTROL var s.n reset all'; end
  235.       if var.?reseterr ='ON' then do; n=n+1; 'CONTROL var s.n reset errors'; end
  236.       if var.?resetlim ='ON' then do; n=n+1; 'CONTROL var s.n reset limits'; end
  237.       if var.?resettran='ON' then do; n=n+1; 'CONTROL var s.n reset transactions'; end
  238.       if var.?resetbyte='ON' then do; n=n+1; 'CONTROL var s.n reset bytes'; end
  239.       if var.?resetpeak='ON' then do; n=n+1; 'CONTROL var s.n reset peak'; end
  240.       if var.?resetresp='ON' then do; n=n+1; 'CONTROL var s.n reset response'; end
  241.       /* Build and return result string */
  242.       if n=0 then message='No actions selected'
  243.        else do
  244.         message=''; do i=1 to n; message=message''s.i'<br>'; end
  245.         end
  246.       end
  247.     otherwise
  248.       return response('badreq', 'asked to process the form "'action'", which is unknown')
  249.     end /* select */
  250.  
  251.   /* Return an 'OK' page here.  Sophisticated users may prefer a
  252.      re-display of the current page, or a direct return to the index page. */
  253.   return page('gormok', message)
  254.  
  255.