property crlf : (ASCII character 13) & (ASCII character 10) property http_10_header : "HTTP/1.0 200 OK" & crlf & "Server: MacHTTP" & crlf & Ā "MIME-Version: 1.0" & crlf & "Content-type: text/html" & crlf & crlf property idletime : 300 property datestamp : 0 set datestamp to current date on «event WWW½sdoc» path_args ¬ given «class kfor»:http_search_args, ¬ «class post»:post_args, «class meth»:method, ¬ «class addr»:client_address, «class user»:username, ¬ «class pass»:password, «class frmu»:from_user, ¬ «class svnm»:server_name, «class svpt»:server_port, ¬ «class scnm»:script_name, «class ctyp»:content_type set datestamp to current date set return_page to http_10_header ¬ & "<HTML><HEAD><TITLE>Unprocessed Results</TITLE></HEAD>" ¬ & "<BODY><H1>Unprocessed Results</H1>" & return ¬ & "<H4>http_search_args</H4>" & return & http_search_args set return_page to return_page & return ¬ & "<H4>post_args</H4>" & return & post_args & return ¬ & "<H4>method</H4>" & return & method & return ¬ & "<H4>client_address</H4>" & return & client_address & return set return_page to return_page & return ¬ & "<H4>from_user</H4>" & return & from_user & return ¬ & "<H4>server_name</H4>" & return & server_name & return ¬ & "<H4>server_port</H4>" & return & server_port & return set return_page to return_page & return ¬ & "<H4>script_name</H4>" & return & script_name & return ¬ & "<H4>content_type</H4>" & return & content_type & return ¬ & "<H4>username</H4>" & return & username & return ¬ & "<H4>password</H4>" & return set return_page to return_page ¬ & "<I>Results generated at: " & (current date) ¬ & "</I>" & "</BODY></HTML>" return return_page end «event WWW½sdoc» on idle if (current date) > (datestamp + idletime) then quit end if return 5 end idle on quit continue quit end quit
property idletime : 300 property datestamp : 0These are two new properties that are used to control when the CGI will quit itself. idletime tells how long to allow the CGI to remain idle before quitting. It is measured in seconds, so the line above lets the CGI run 5 minutes before quitting. datestamp contains the last date that something happened. As you will see below, it is set at startup and at the start of each AppleEvent. It is important that these are properties, so their value persists between AppleEvents received.
set datestamp to current dateThis line gets the current date (which includes day and time) and stores it in the variable datestamp. This line is used twice; once at the very start and once inside the AppleEvent handler. By putting it outside the AppleEvent handler, it ensures that the CGI will quit as desired even if it was somehow launched without an AppleEvent. The line inside the AppleEvent handler ensures that it will be reset each time an AppleEvent is processed.
on idle if (current date) > (datestamp + idletime) then quit end if return 5 end idleThis new handler is introduced to test whether it is time to quit or not. When "nothing" is happening (meaning, when no AppleEvents are sent to this CGI), an "idle" event is sent to the CGI. The idle handler checks the current time and compares it to the last set time to see if it has been long enough to quit yet. If so, then it calls the quit handler (see below). If not, then it returns a value that tells how long to wait before checking for idle again (you don't want to tie up the whole cpu checking to see if you're idle!).
on quit continue quit end quitThis new handler allows you to do processing before quitting. This might be useful if you wanted to save some information about the current state, wanted to log when the CGI runs, or anything else you can think of. Logging could be very useful to get an idea of whether your idletime is set too high or too low for the use the CGI is getting. Currently all this does is tell the CGI to go ahead and quit.
There is one caveat when working with scripts that are self-quitting. You want to be careful not to quit the script before you are done processing all of the AppleEvents that might be queued up. This is not a problem with regular CGI applications. With the new ability to do asynchronous processing of CGI applications, though, there is the possibility for this to be a problem. For that reason, make sure that idletime is long enough for the AppleEvent to be processed and another one that is waiting to be accepted.
Last Edited: November 26, 1994