home *** CD-ROM | disk | FTP | other *** search
/ The Datafile PD-CD 5 / DATAFILE_PDCD5.iso / utilities / n / netplex / !Netplex / !Help / HTML / 13-cgi < prev    next >
Encoding:
Text File  |  1997-01-25  |  12.8 KB  |  231 lines

  1. <HTML>
  2.  
  3. <HEAD>
  4. <TITLE>Netplex - CGI Programs</TITLE>
  5. </HEAD>
  6.  
  7. <BODY TEXT="#000000" BGCOLOR="#ffffff" LINK="#004499" VLINK="#00224c" ALINK="#00cc00">
  8.  
  9. <H1><IMG SRC="images/world" ALIGN=ABSMIDDLE>  CGI Programs</H1>
  10. <HR>
  11.  
  12. <A NAME="introduction"><H2>Introduction</H2></A>
  13.  
  14. <P>Netplex allows you to write 'CGI' (<I>Common Gateway Interface</I>) programs which dynamically generate a response when a client accesses them.  By using CGI programs you can provide interactive facilities for the users accessing your server.  You may have seen access counters on WWW pages which display a small graphic showing how many times the page has been accessed - this is an example of a CGI program.</P>
  15.  
  16. <P>The CGI program is either a normal program (BASIC, Absolute, Utility) or an OS script (Obey, Command, etc.) kept in a special directory, which once invoked produce output that the client can interpret.  In the case of the access counter program mentioned above, the output is a GIF image.</P>
  17.  
  18. <P>CGI programs are usually referenced from a normal, static, WWW page, acting as a utility to the master page.  For example, a form, when the user presses the 'Submit' button, will ask the server for a URL similar to :</P>
  19.  
  20. <P><CENTER>http://bronze.netplex.net/cgi-bin/form?name=Zaphod+Beeblebrox</CENTER></P>
  21.  
  22. <P>This means that the CGI program specified by the URL "http://bronze.netplex.net/cgi-bin/form" will be invoked with the parameters "name=Zaphod+Beeblebrox".  The '?' character separates the URL of the program and the parameters to be passed to the program.  Also, several named parameters can be passed by separating them with the '&' symbol.  For example : </P>
  23.  
  24. <P><CENTER>http://bronze.netplex.net/cgi-bin/form?firstname=Zaphod&lastname=Beeblebrox</CENTER></P>
  25.  
  26. <P>Once invoked, the program will generate its output, which the server will then despatch to the client.  This type of retrieval is the 'GET' method.</P>
  27.  
  28. <P>The other method used is 'POST', this is different to GET in that instead of appending the parameters to the URL, it sends them as a 'packet' of data (officially called an 'entity') accompanying the request.  This has the advantage of allowing much larger amounts of data to be passed, since URLs are limited to a fixed number of characters.  POSTs are the preferred method of submitting form data.</P>
  29.  
  30. <P>In the case of the access counter program mentioned above, the parameters passed to it will be an ID for the user's WWW page.  The counter program will look up the ID in its database, find the number of times the counter has been requested, increment it, and then generate a GIF file of the number of requests.</P>
  31.  
  32. <HR WIDTH="50%">
  33.  
  34. <A NAME="writing"><H2>Writing CGI Programs</H2></A>
  35.  
  36. <P>Writing CGI programs for use with Netplex is fairly easy, as BASIC can be used to implement them quickly.  To start with an example, here's a web-enabled Hello World program :</P>
  37.  
  38. <PRE><P>
  39. REM >cgi-bin.helloworld  (Netplex Example 1)
  40.  
  41. PRINT "Content-Type: text/html";CHR$(13);CHR$(10);
  42. PRINT                           CHR$(13);CHR$(10);
  43.  
  44. PRINT "<HTML>"
  45. PRINT "<BODY>"
  46. PRINT "<H1>Hello World!</H1>"
  47. PRINT "</BODY>"
  48. PRINT "</HTML>"
  49.  
  50. END
  51. </P></PRE>
  52.  
  53. <P>The first two PRINT statements are part of the header that is sent to the client.  The first line tells the client that the document is HTML text and the second, empty line, terminates the header.  Both lines are terminated by 'CHR$(13);CHR$(10);' as the usual PRINT line ending is inapropriate for header fields which must be terminated by CR,LF.  The other lines are a simple HTML 'page' which will be displayed in the browser.</P>
  54.  
  55. <P>As far as things go, this program isn't very interesting - it doesn't actually do anything <I>different</I> every time you run it, which is the point of the CGI system.  An easy modification to make it produce different output every time is to replace "Hello World!" with TIME$, which returns the current time :</P>
  56.  
  57. <PRE><P>
  58. REM >cgi-bin.time  (Netplex Example 2)
  59.  
  60. PRINT "Content-Type: text/html";CHR$(13);CHR$(10);
  61. PRINT                           CHR$(13);CHR$(10);
  62.  
  63. PRINT "<HTML>"
  64. PRINT "<BODY>"
  65. PRINT "<H1>"+TIME$+"</H1>"
  66. PRINT "</BODY>"
  67. PRINT "</HTML>"
  68.  
  69. END
  70. </P></PRE>
  71.  
  72. <P>Now, when the program is fetched, it will return the current time.</P>
  73.  
  74. <P>To learn more about CGI programming, examine the <A HREF="#examples">examples</A> given below.</P>
  75.  
  76.  
  77. <H3>The CGI Library</H3>
  78. <P>A library of BASIC functions for use in CGI programs is provided, referenced by the file <Netplex$Dir>.CGILibrary.  In order to use them, you should use the following instructions at the start of your program :</P>
  79.  
  80. <PRE><P>
  81. LIBRARY "<Netplex$Dir>.CGILibrary"
  82. PROCcgi_init
  83. </P></PRE>
  84.  
  85. <P>This will set up an error handler, dimension a 256-byte block of memory called misc% and will set the variables cgi_in% and cgi_out% to the handles of the input and output data streams respectively.</P>
  86.  
  87. <P>The library provides the following routines :</P>
  88. <DL>
  89.  
  90. <DT><B>PROCcgi_header(content_type$,content_length%)</B>
  91. <DD>Given the Content-Type and Content-Length, this routine generates an appropriate response header.  Content_type$ is given as 'image/gif', 'text/html', etc.  Content_length% is the amount of data you are going to be sending, or -1 if you don't know.  Note that you <EM>must</EM> use this routine, as it handles compatibility with HTTP/1.1 requests.
  92.  
  93. <DT><B>PROCcgi_statusline(code%,phrase$)</B>
  94. <DD>If you need to, you may generate an entire HTTP response yourself, including headers.  Cgi_statusline outputs the initial HTTP status line, which informs the recipient of the version of HTTP in use, the response status code and a short phrase indicating the server's response.  You must call cgi_statusline before anything else that will cause output.
  95.  
  96. <DT><B>PROCcgi_headerline(name$,value$)</B>
  97. <DD>This routine takes care of outputting HTTP headers with the correct line endings.  Name$ is the name of the header field and value$ is its value, so cgi_headerline("Cookie","name=cookie_monster") will generate the header line "Cookie: name=cookie_monster".
  98.  
  99. <DT><B>PROCcgi_error</B>
  100. <DD>This is automatically called when an error occurs.  It cleans up any temporary files and generates an HTML page describing the error.
  101.  
  102. <DT><B>FNcgi_nextelement</B>
  103. <DD>Returns the next element passed to the program (POST only).
  104.  
  105. <DT><B>FNcgi_nextelementdecoded</B>
  106. <DD>As above, but translates all of the escape codes first, by calling FNcgi_decodeelement.
  107.  
  108. <DT><B>FNcgi_decodeelement(e$)</B>
  109. <DD>Translates all the escape characters (e.g. '%7e') in e$, and returns the translated string.
  110.  
  111. <DT><B>FNstring_readtoctrl(RETURN p%)</B>
  112. <DD>Returns the string pointed to by p%, and updates p% to point to the first unread character.
  113.  
  114. <DT><B>FNcase_lower(s$)</B>
  115. <DD>Returns the given string, in lower case.
  116.  
  117. <DT><B>FNcase_upper(s$)</B>
  118. <DD>Returns the given string, in upper case.
  119.  
  120. <DT><B>FNsystem_getvar(var$)</B>
  121. <DD>Returns the value of the named system variable as a string.
  122.  
  123. <DT><B>FNmime(filetype%)</B>
  124. <DD>Converts a filetype to a MIME Content-Type.
  125.  
  126. </DL>
  127.  
  128.  
  129. <H3>CGI System Variables</H3>
  130.  
  131. <P>Netplex passes information to CGI programs via a set of system variables, which use the prefixes 'CGI$' and 'HTTP$'.  Note that this is different to CGI on non-RISC OS servers which have different rules regarding system variables.  For example, CGI$ServerSoftware is used rather than SERVER_SOFTWARE.  This means that anyone porting CGI software or scripts from other platforms must take this into account, and adjust all references to system variables.</P>
  132.  
  133. <P>Only some of the following variables may be set, depending on the method the client used and the information it passed.</P>
  134.  
  135. <H5>Static Variables</H5>
  136. <I>Variables which are constant for the duration of the server.</I>
  137. <DL>
  138. <DT>CGI$ServerSoftware
  139. <DD>Contains the name and version of the server software, e.g. 'Netplex/0.09  (05 Oct 1996)'.
  140. <DT>CGI$ServerName
  141. <DD>Is the fully qualified name of the server.  At startup, Netplex will attempt to resolve this from the Inet$LocalIP variable.
  142. <DT>CGI$GatewayInterface
  143. <DD>Always set to "GGI/1.1".
  144. <DT>CGI$ServerPort
  145. <DD>Set to the port that the server is operating on.
  146. </DL>
  147.  
  148. <H5>Dynamic Variables</H5>
  149. <I>Variables which change according to the request.</I>
  150. <DL>
  151. <DT>CGI$ServerProtocol
  152. <DD>HTTP/1.0 or HTTP/0.9 depending on the request received.
  153. <DT>CGI$RequestMethod
  154. <DD>This is set to the request method that was used to retrieve the file, e.g. GET or POST.
  155. <DT>CGI$PathInfo
  156. <DD>If you specify the URL http://mercury.netplex.net/cgi-bin/imagemap/dthomas then if the program imagemap exists in the cgi-bin directory, PathInfo will be set to 'dthomas'.
  157. <DT>CGI$PathTranslated
  158. <DD>This is the same as above but is prefixed with the path of the web page directory.  With the above example it would be something like, 'ADFS::IDEDisc4.$.Internet.WWW.dthomas'.
  159. <DT>CGI$ScriptFilename
  160. <DD>The full RISC OS filename of the program.  For example 'ADFS::IDEDisc4.$.Internet.WWW.cgi-bin.process'.
  161. <DT>CGI$ScriptName
  162. <DD>The URL path of the program.  For example '/cgi-bin/process'.
  163. <DT>CGI$QueryString
  164. <DD>The data passed in the URL, which followed the '?'.  Used by GET requests to pass parameters to CGI programs.
  165. <DT>CGI$RemoteHost
  166. <DD>The name of the client machine, or the dotted-IP address if unknown.
  167. <DT>CGI$RemoteAddr
  168. <DD>The dotted-IP address of the client machine.
  169. <DT>CGI$AuthType
  170. <DD>If Client Authentication is in use then this will be set to 'Basic'.
  171. <DT>CGI$AuthUser
  172. <DD>If Client Authentication is in use then this will be set to the username of the client 'logged in'.
  173. <DT>CGI$ContentType
  174. <DD>The type of data that has been submitted to the program. (Applies to POST only).
  175. <DT>CGI$ContentLength
  176. <DD>The length of the data that that has been submitted to the program. (Applies to POST only).
  177. </DL>
  178.  
  179. <H5>HTTP Header Fields</H5>
  180. <I>The header fields, as submitted by the client.</I>
  181. <DL>
  182. <DD>It depends entirely on the client as to what you will find set in the HTTP variables.
  183. </DL>
  184.  
  185. <P>There is also another system variable, although not strictly linked to CGI programs, which is useful to have.  It is Netplex$WebPagesDir which gives the name of the root web page directory.</P>
  186.  
  187.  
  188. <H3>Notes</H3>
  189. <UL>
  190. <LI>Netplex checks the requested URL for '/cgi-bin/' and will invoke the program <B>only</B> if it is inside a 'cgi-bin' directory <B>and</B> is of type Command, Utility, BASIC, Absolute or Obey, otherwise it will be served as a file.
  191. <LI>CGI directories do not need to be in the root web page directory, e.g. you could have http://iron.netplex.net/~bob/cgi-bin/ which would still work OK.  You can have multiple cgi-bin directories if you want (in different directories, of course).
  192. <LI>In order for CGI$PathInfo to be correct, you must <B>not</B> use subdirectories within the cgi-bin directories.
  193. <LI>The first line of CGI output is parsed, if it begins 'HTTP/1' then it is assumed that the CGI program has generated the entire header itself and Netplex will not attach any header fields of its own.
  194. </UL>
  195.  
  196. <HR WIDTH="50%">
  197.  
  198. <A NAME="examples"><H2>Examples</H2></A>
  199.  
  200. <P><DL>
  201. <DT><B><A HREF="cgi-bin/time">Time</A></B>
  202. <DD>A CGI program which when retrieved displays the time.
  203. <DT><B><A HREF="cgi-bin/timeobey">TimeObey</A></B>
  204. <DD>The same as above, but implemented as an Obey script.
  205. <DT><B><A HREF="cgi-bin/query?This+is+the+query+example">Query</A></B>
  206. <DD>A CGI program which when retrieved displays the query string that was passed to the program.  In this case the query string is 'This+is+the+query+example'.
  207. <DT><B><A HREF="form">Form</A></B>
  208. <DD>An HTML page containing a Form which when submitted is sent to a CGI program called 'form' which displays the results.  This uses the POST method rather than GET.
  209. <DT><B><A HREF="cgi-bin/tasks">Tasks</A></B>
  210. <DD>Dynamically builds an HTML page of information about all running tasks on your machine.
  211. <DT><B><A HREF="cgi-bin/info">Info</A></B>
  212. <DD>Provides information on the system variables used by Netplex to inform CGI programs of their parameters.
  213. <DT><B><A HREF="cgi-bin/dir">Dir</A></B>
  214. <DD>An advanced version of the Query program which uses self-references to enable the user to traverse a directory structure and serve files.  It defaults to using 'ADFS::4.$', which may not be applicable to your system.  It will only list Public-readable files.
  215. <BR>(Note: ArcWeb isn't fond of this program, due to its slightly non-standard use of URLs).
  216. </DL></P>
  217.  
  218. <P><B>Note :</B> These programs are provided purely for demonstration purposes.  You should not allow open access to your machine with these programs present - particularly Dir as it allows unrestricted access to any filing system on your machine.</P>
  219.  
  220. <HR>
  221.  
  222. <P><CENTER>
  223. <A HREF="12-auth"><IMG SRC="images/buttons/bak" WIDTH=32 HEIGHT=32 BORDER=0 ALT="[ Previous ]"></A>
  224. <A HREF="index"><IMG SRC="images/buttons/up" WIDTH=32 HEIGHT=32 BORDER=0 ALT="[ Index ]"></A>
  225. <A HREF="14-image"><IMG SRC="images/buttons/fwd" WIDTH=32 HEIGHT=32 BORDER=0 ALT="[ Next ]"></A>
  226. </CENTER></P>
  227.  
  228. </BODY>
  229.  
  230. </HTML>
  231.