Online Book Reader

Home Category

AppleScript_ The Definitive Guide - Matt Neuburg [227]

By Root 1593 0
CGIs in the traditional Mac OS manner, you can continue using it directly with an applet. However, you are more likely to be using the web server that comes with Mac OS X, namely Apache, which doesn't work this way. Apache is a Unix web server, and Unix doesn't have Apple events. In Unix, environment variables, along with stdin and stdout, are used as the communication medium between the server and the CGI process.

If you really want to, you can still use an AppleScript applet as a CGI application with Apache, but in order to do so, you need some intermediary application that swings both ways, as it were. On the one hand, this intermediary application must behave as an Apache-style CGI process, so that Apache has someone to talk to. On the other hand, this intermediary application must translate a CGI request from Apache into an Apple event and forward this Apple event to the appropriate applet; when the result comes back from the applet, it must then translate that result into the form Apache expects, and pass it back to Apache.

So, where will you get this intermediary application? Mac OS X Server, I believe, comes with one; but ordinary Mac OS X does not. Instead, you can use James Sentman's acgi dispatcher utility. Here is a description of how to write and implement a basic CGI applet in AppleScript using acgi dispatcher. (What's documented here is version 2.5, the latest version at the time this was written; these instructions will not work for earlier versions.)

For purposes of the example, we'll write an "echo" CGI, whose job is to return a web page simply describing the original request. This is always a valuable thing to have on hand when you're doing CGI, because it can be used for testing and debugging, and in any case it exemplifies the two basic tasks of a CGI applet, namely to receive the Apple event and to respond by constructing and returning a web page.

The code is straightforward. The main requirements are that we implement the handle CGI request event as defined in StandardAdditions, and that we return a valid page of HTML preceded by some minimal HTTP headers. (acgi dispatcher provides an extra parameter, a list of URL-decoded form elements, saving your applet the tedious job of parsing the form information; you can capture this by adding an extra parameter, given «class TraL», to the parameters, but this example doesn't.)

property crlf : "\r\n"

property http_header : "MIME-Version: 1.0" & crlf & ¬

"Content-type: text/html" & crlf & crlf

property s : ""

on makeLine(whatName, whatValue)

return "

" & whatName & ": " & whatValue & "

" & return

end makeLine

on addLine(whatName, whatValue)

set s to s & makeLine(whatName, whatValue)

end addLine

on handle CGI request path_args ¬

from virtual host virtual_host ¬

searching for http_search_args ¬

with posted data post_args ¬

using access method method ¬

from address client_address ¬

from user username ¬

using password pword ¬

with user info from_user ¬

from server server_name ¬

via port server_port ¬

executing by script_name ¬

of content type content_type ¬

referred by referer ¬

from browser user_agent ¬

of action type action_path ¬

from client IP address client_ip ¬

with full request full_request ¬

with connection ID connection_id ¬

using action action

set s to http_header

set s to s & ¬

"Echo Page" & return

set s to s & "

Echo Page

" & return

addLine("virtual_host", POSIX path of virtual_host)

addLine("path_args", path_args)

addLine("http_search_args", http_search_args)

addLine("post_args", post_args)

addLine("method", method)

addLine("client_address", client_address)

addLine("username", username)

addLine("password", pword)

addLine("from_user", from_user)

addLine("server_name", server_name)

addLine("server_port", server_port)

addLine("script_name", script_name)

addLine("content_type", content_type)

addLine("referer", referer)

addLine("user_agent", user_agent)

addLine("action_path", action_path)

addLine("client_ip", client_ip)

addLine("full_request",

Return Main Page Previous Page Next Page

®Online Book Reader