In this section a method of overriding the "Stay Open" option is presented.
All of the CGI/ACGI scripts described so far, once launched, will never quit. This is not bad since each one of these scripts takes time to load, and once loaded they will execute faster. If the scripts you write are intended to be used often, then keeping them open is a good idea, provided you have the RAM to spare. In those cases where you don't have enough RAM to spare and/or the CGI/ACGI scripts you write are intended to be used infrequently, then you may want to consider including in your scripts a method for quitting.
The following script, hello-world-07.cgi works just most of the other "Hello, World!" scripts here. It simply displays the text "Hello, World!", but unlike the other scripts, this one will quit after 30 ticks (one tick = 1/60th of a second) of idle time. Not incidentally, this technique was first described by Jon Wiederspan in his essay "Extending MacHTTP"
The first part of this script declares two global variables (
-- set up automatic quitting global theIdletime global theDateStamp set theIdletime to 30 set theDateStamp to current date -- process the AppleEvent sent to this script by the server on «event WWW*sdoc» -- practice good error trapping try -- re-initialize the date stamp for .acgi purposes set theDateStamp to current date -- define the standard HTTP header set LF to ASCII character (10) set CR to return set CRLF to CR & LF set http_10_header to "HTTP/1.0 200 OK" & CRLF & ¬ "Server: MacHTTP" & CRLF & ¬ "MIME-Version: 1.0" & CRLF & ¬ "Content-type: text/html" & CRLF & CRLF -- return the results as an HTML file return http_10_header & ¬ "<html>" & ¬ "<head>" & ¬ "<title>Hello, World</title>" & ¬ "</head>" & ¬ "<body>Hello, World!</body" & ¬ "</html>" on error msg -- return the error as an HTML document return http_10_header & ¬ "</html><head><title>Error</title></head>" & ¬ "<body><h1>Error</h1>" & ¬ "An error occured: " & msg & ¬ "</body></html>" end try end «event WWW*sdoc» on idle -- check to see if it is time to quit if (current date) > (theDateStamp + theIdletime) then quit -- wait a few more ticks return 5 end idle on quit -- exit the program continue quit end quit
theIdletime). These variables have to be global in nature because they will be used in other handlers of the script.
Next, the script assigns values to these variables. The variable,
theDateStamp is self-evident. The second variable,
theIdleTime defines how long the script should stay open and wait for more requests for processing. The value of
theIdleTime must be long enough for your script to execute, but not so long that it hangs around in RAM too long.
Third, within the WWW*sdoc handler,
theDateStamp is reinitialized since the program may have been called after it was initially launched. By reinitializing
theDateStamp you can accommodate ACGI scripts.
Fourth, the program does its processing and returns the result back to the server.
Now, the script receives idle calls automatically. This is a feature of AppleScript and works just like the
on idle calls in HyperCard's HyperTalk. It is in the
on idle handler that
theIdletime and the date stamp are compared to the current time. If the current time is greater than the sum of the date stamp and
theIdletime, then the quit routine is called. Otherwise, a few more ticks are spent waiting.
This page was first published on September 26, 1995. Feel free to send comments.