next up previous contents
Next: Server Performance Up: Form Processing Previous: The cgiparse approach

Using Tokenize on MacHTTP

There are as many ways to handle forms with MacHTTP and with all the other servers. In fact, there is a version of Perl for the Mac which can be used with MacHTTP in the same way AppleScript can be used, and as MacHTTP is CGI compliant, the above Perl scripts should be relatively easily modified for the Macintosh.

If you don't want to install yet another scripting language, AppleScript can also be used to handle forms, though it is made much easier by a few AppleScript extensions. Two extensions in particular are recommended - the Tokenize extension from Wayne Walrath which simplifies splitting strings up and the ScriptTools extensions from Mark Alldrit which provide enhanced file handling capabilities (amongst other things).

The example below uses tokenize to split the form data into fields at the ``&'' characters, and then to split the fields into (key, value) pairs at the ``='' character. This version doesn't actually create any files on the server, but how to do so is described in detail in the ScriptTools documentation and examples. This example used the GET method. (The ... pair indicates this should be all one line).

  set crlf to (ASCII character 13)  & (ASCII character 10)
 
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
 

-- split up the form data at the "\&" characters set form_list to tokenize http_search_args with delimiters "\&"

-- run through each of the form fields repeat with i from 1 to count of form_list -- split each field at the "=" character into a key and a value set tmp_list to tokenize (item i of form_list) with delimiters "=" set tmp_key to first item of tmp_list set tmp_value to second item of tmp_list

-- extract the data we want from the fields if tmp_key = "pubname" then set title to "<title>" & tmp_value & "<title>" & crlf set h1 to "<h1>" & tmp_value & "</h1>" & crlf else if tmp_key = "pubaddress" then set addr to "<I>" & tmp_value & "</I><P>" & crlf else if tmp_key = "description" then set desc to "<H2>Description:</H2>" & crlf & "<P>" & tmp_value & "<P>" & crlf else if tmp_key = "grade" then set grde to "<H2>Grade: " & tmp_value & "</H2>" & crlf set expl to "<P>1=Average, 2=Worth going to, " & "3=Worth a detour, 4=Worth a long detour!<P>" else if tmp_key = "username" then set cred1 to "<hr><I>Information supplied by " & tmp_value & "</i>" else if tmp_key = "useremail" then set cred2 to " <i>(" & tmp_value & ")</i>" end if end repeat

-- and send it back to the user. return http_10_header & title & h1 & addr & desc & grde & expl & cred1 & cred2



next up previous contents
Next: Server Performance Up: Form Processing Previous: The cgiparse approach



Jon Crowcroft
Wed May 10 11:46:29 BST 1995