home *** CD-ROM | disk | FTP | other *** search
/ MacFormat 1996 January / macformat-033.iso / mac / Shareware City / Developers / CGIshell Folder / CGI shell docs.txt next >
Encoding:
Text File  |  1995-09-24  |  8.5 KB  |  300 lines  |  [TEXT/ALFA]

  1.  
  2. CGI shell, version 1.1
  3.  
  4.  
  5. (c) Ronald T. Kneusel, Sept. 1995
  6. rkneusel@post.its.mcw.edu
  7.  
  8. ---------------------------------------------------------------------------------
  9.  
  10.  
  11. *****************************
  12. *                           *
  13. *      INTRODUCTION         *
  14. *                           *
  15. *****************************
  16.  
  17.  
  18. DISCLAIMER
  19.  
  20.  
  21.     This is a tool for programmers; Forth programmers in particular.  Therefore,
  22. this documentation is intentionally sparse.  After all, real programmers don't
  23. need no steenkin' documentation, right? :)
  24.  
  25.  
  26.  
  27. WHAT IS IT?
  28.  
  29.  
  30.     CGI shell works with Chris Heilman's Pocket Forth programming language to 
  31. provide a skeleton for creating custom CGI applications for WebSTAR/MacHTTP.  
  32. The shell code handles AppleEvents, the interpretation of forms data, and
  33. supplies a set of words for building a reply string.  Pocket Forth, version 6.4
  34. is included in the release.
  35.  
  36.  
  37. WHAT DO I NEED TO RUN IT?
  38.  
  39.     • A Mac running WebSTAR (commercial) or MacHTTP (shareware)
  40.     • This code
  41.     • A text editor (Teachtext or Simpletext will do)
  42.     • Some Forth and HTML programming experience
  43.     
  44.  
  45. A FIRST EXAMPLE
  46.  
  47.     This version of the shell gives you easy access to the direct argument, 
  48. the client's IP address, the client's browser type, and the field data.  A
  49. minimal CGI using the shell is:
  50.  
  51.  
  52.    \ Simple CGI
  53.    
  54.    --> CGIshell.4th    ( load the CGI code )
  55.     
  56.    message[ s1 You are: ]                 ( strings used in the reply )
  57.    message[ s2 <br>Using: ]
  58.    message[ s3 <br>And your favorite flavor is: ]
  59.    
  60.    message[ fname flavor]        ( name of the field )
  61.    
  62.    512 String>> out    ( return string goes here, 512 char max )
  63.    out newstr          ( clear it )
  64.    
  65.    ,s sdoc  ,s WWWΩ  ae:   ( begin the AppleEvent reply handler )
  66.    
  67.       out startstring          ( add HTML header )
  68.       
  69.       s1 out strcpy            ( add 'You are: ' )
  70.       out APPEND @Addr         ( add client's IP address )
  71.       
  72.       s2 out strcpy            ( add '<br>Using: ' )
  73.       out APPEND @Browser      ( add browser type  )
  74.       
  75.       s3 out strcpy            ( add '<br>And your favorite flavor is: ' )
  76.       out fname APPEND @Field  ( add flavor field data )
  77.       
  78.       out endstring            ( add HTML ending )
  79.       
  80.       out REPLY  bye           ( send the reply and quit )
  81.       
  82.    ;ae                    ( end reply handler )
  83.    
  84.    
  85.  
  86. This CGI will respond to an HTML form like this one:
  87.  
  88.  
  89.    <h1> Simple CGI test </h1>
  90.    <form method=post action="simple.cgi">
  91.      What is your favorite ice cream flavor?
  92.      <select name="flavor"
  93.        <option>Vanilla <option>Chocolate <option>Strawberry
  94.      </select>
  95.      <input type=submit value="Send it">
  96.    </form> 
  97.    
  98.    
  99. To create 'simple.cgi' load the Forth code above into a *copy* of Pocket Forth
  100. and then enter  save bye  to save the dictionary.  Use a web browser to bring
  101. up the HTML file, call it 'simple.html', and click 'send it'.  If all went well
  102. you will receive a reply indicating who you are, what you are using, and 
  103. (presumably) your favorite type of ice cream.
  104.  
  105.  
  106.  
  107. *****************************
  108. *                           *
  109. *   Technical Description   *
  110. *                           *
  111. *****************************
  112.  
  113.  
  114. MAIN USER LEVEL WORDS
  115.  
  116.  
  117.     The most important words defined by the shell are @Direct, @Addr, @Browser,
  118. and @Field.  These words fetch information sent by the web server to the CGI
  119. application.
  120.  
  121.  
  122.  @Direct  (e.g.  out APPEND @Direct )
  123.  
  124.     Direct returns the direct argument attached to the action component of the
  125.     form definition.  If the HTML file contains:
  126.     
  127.         <form method=post action="my.cgi$3.141592">
  128.         
  129.     Then the phrase  out APPEND @Direct  will append the string 3.141592 to
  130.     the string contained in  out  .  If NEW is specified in place of APPEND
  131.     the direct argument replaces the previous contents of the string.  All
  132.     strings are terminated with a '\0' character.
  133.     
  134.     
  135.  @Addr  (e.g.   out APPEND @Addr )
  136.   
  137.     Addr returns the IP address string of the client.  If the address has a
  138.     DNS entry its name will be returned.  If there was no DNS entry the reversed
  139.     in-arpa numerical IP address is returned.  NEW and APPEND work as above.
  140.     
  141.     
  142.  @Browser  (e.g.  out NEW @Browser )
  143.  
  144.     Browser returns a string indicating what type of web browser the client
  145.     is using.  This can be useful for deciding when to return a graphically
  146.     intensive page and when not to.  @Browser works as @Addr and @Direct.
  147.     
  148.     
  149.  @Field  (e.g.  out f1 APPEND @Field )
  150.  
  151.     Field places the contents of the field whose name is in the string  f1
  152.     into the string  out  .  NEW and APPEND work as before.  @Field will handle
  153.     all translation of characters.
  154.     
  155.     
  156.  REPLY   (e.g.  out REPLY )
  157.  
  158.     REPLY returns the string on the stack to the web server which then sends
  159.     it down to the client.  This word must be called only from within a
  160.     ae: ... ;ae pair.
  161.     
  162.     
  163. SETTING UP THE APPLE EVENT HANDLER
  164.  
  165.   The heart of a CGI application is the AppleEvent handler that responds to the
  166.   AppleEvent sent by the web server.  In Pocket Forth an AppleEvent handler is
  167.   first defined and then saved to the dictionary.  When the application is then
  168.   launched the handler is added to the list of handlers and the application 
  169.   will respond to the event whenever it is listening for events.  Pocket Forth
  170.   listens to system events when waiting for input so it is unnecessary to
  171.   be concerned about handling local events (i.e. mouse clicks, keypresses, etc.)
  172.   In fact, Pocket Forth can be used normally while still listening for
  173.   AppleEvents.  If you write the CGI to quit when finished you do not need to
  174.   write any code for dealing with the server Mac.  This is unlike the C based
  175.   Responder CGI shell.
  176.   
  177.   In Pocket Forth an Apple Event handler is defined using ae: and ;ae,
  178.   
  179.   ,s sdoc  ,s WWWΩ  ae:   \ begin the handler for the sdoc,WWWΩ event
  180.   
  181.      ...some code here... 
  182.      
  183.   ;ae
  184.   
  185.   The general process, then, is:
  186.   
  187.   ,s sdoc  ,s WWWΩ  ae:
  188.   
  189.     <get data from the apple event using @Direct, @Field, etc.>
  190.     
  191.     <manipulate the data and create an output string>
  192.     
  193.     <reply to the event with  <output-string> REPLY>
  194.     
  195.     <quit or re-initialize the application>
  196.     
  197.   ;ae
  198.   
  199.   See the included examples for more information.
  200.   
  201.   
  202. STRING SUPPORT WORDS
  203.  
  204.   The CGI shell provides a number of words for manipulating null terminated 
  205. strings.  These are summarized below:
  206.  
  207.  
  208.    message[  ( compile: <name> <text>] )
  209.              ( run: -- address )
  210.              
  211.              Create <name>, a null terminated string.  E.g.
  212.                message[ s0 <h1>Result...</h1>]
  213.    
  214.    String>>  ( size -- (<name>) )
  215.    
  216.              Create a string, <name>, <size> bytes long.  E.g.
  217.                512 String>> theReply
  218.                
  219.    strcpy    ( s1 s2 -- )
  220.    
  221.              Append s1 to s2.  Appending is useful for creating reply
  222.              strings, use  strncpy  to avoid appending.
  223.              
  224.    length    ( s -- n )
  225.    
  226.              Return the length of string s.
  227.              
  228.    newstr    ( s -- )
  229.    
  230.              Zero the string s.  Equal to  0 s c!
  231.              
  232.    0type     ( s -- )
  233.    
  234.              Type the null terminated string s on the screen.  Useful for
  235.              debugging.
  236.              
  237.    accept    ( s length -- )
  238.    
  239.              Identical to  expect  but adds a terminating '\0'.
  240.              
  241.    startstring  ( s -- )
  242.    
  243.              Add <HTML> to the string s.
  244.              
  245.    endstring ( s -- )
  246.    
  247.              Append </HTML> to the string s.
  248.              
  249.              
  250. NUMERICAL CONVERSION
  251.  
  252.    Pocket Forth has complete support for floating point numbers making it useful
  253.    for scientific applications.  These words will convert between floating point
  254.    numbers and strings.
  255.    
  256.    
  257.    f>str  ( f s -- )
  258.    
  259.           Put the floating point number f into the string using the current
  260.           settings for FIX or SCI.
  261.           
  262.    str>f  ( s -- f )
  263.    
  264.           Make s into a floating point number on the stack.
  265.           
  266.           
  267. NOTES
  268.  
  269.    The best way to learn to use the shell is to experiment.  See the 'Deflect'
  270. example to learn how to force the client's browser to another URL.  When 
  271. developing a CGI keep the following in mind:
  272.  
  273.  
  274.    *  turn off the processor cache if using an '040 based Mac.  Some code will
  275.       compile with it on but often the application quits part way through. The
  276.       compiled code seems to run just fine with the cache on.
  277.  
  278.       
  279.    *  always use a fresh copy of the Pocket Forth application when re-compiling.
  280.    
  281.    
  282.    *  remember to do  save bye  after loading the code.  Amazing how many times
  283.       I forget to do that!
  284.       
  285.       
  286. The software carries no warranty of any kind.  Caveat Emptor.
  287.  
  288. Ron Kneusel
  289. 8725 West Burdick Ave.
  290. Milwaukee, WI  53227  USA
  291. (414) 545-7557
  292. rkneusel@post.its.mcw.edu
  293.  
  294. The latest version of CGIshell can always be found at:
  295.  
  296. http://kreeft.intmed.mcw.edu/pf.html
  297. ftp://kreeft.intmed.mcw.edu/q/pub/forth/
  298.  
  299. September 1995, AMDG
  300.