CGIs in C
Volume Number: 11
Issue Number: 9
Column Tag: Internet Development
Writing CGI Applications in C 
Inject Some Adrenaline into your World Wide Web
Server Applications.
By John O’Fallon, john@maxum.com
Note: Source code files accompanying article are located on MacTech CD-ROM orsource code disks.
For the last two months this column has been teaching you how to write CGI
applications for WebSTAR using AppleScript. AppleScript is a wonderful environment
for tying existing Apple event-aware applications (database engines, for example) to
WebSTAR. It also provides an easy way to get your feet wet with CGI applications.
Inevitably, though, there will come a time when you need a high-throughput,
high-reliability CGI for your WebSTAR server. For that, we turn our attention to the
C language.
Writing CGIs in C will be very comfortable for most experienced Macintosh
programmers, and will provide all of the performance and reliability the most
demanding servers need. In addition, when writing CGIs, you can get these benefits
without the headaches that are often associated with C. For example, developing in C
normally means spending a lot of time on the user interface. But CGI applications don’t
typically need any significant interface, so all you need to worry about is the actual job
that you are trying to accomplish.
If you aren’t already somewhat familiar with programming in C, you should stop
right here and spend some quality time with a C programming primer. You don’t
necessarily need to be a C ace to write a good CGI, but familiarity with the language, the
basic libraries and the Macintosh ToolBox are essential. I will also assume that you
already have some experience with WebSTAR and with the topic of CGI applications in
general (if not, see the July and August issues for “CGI Applications in AppleScript”)
as well as a basic knowledge of HTML. Now, on to the topic at hand.
CGIs exist for Apple events. You will be building an application whose sole purpose in
life is to receive Apple events, look at what they’re asking for, do some processing, and
send back some reasonable responses. So, to begin, we’ll start with an overview of
just how Apple events are used to send and receive information. Please note that I
haven’t read the Inside Macintosh chapter on Apple events in almost a year, so for the
real deal, in all its glorious detail, look at Inside Mac, Volume VI, Chapter 6. [or
Inside Macintosh: Interapplication Communication, Chapter 3 - Assoc. Ed. jaw]
Set It Up
All a CGI really needs to do is respond to Apple events that are originated by
WebSTAR. Luckily for us, responding to Apple events turns out to be quite a bit easier
than sending them.
Responding to Apple events starts by installing a series of event handlers. An
event handler is a procedure that will be called to process a given event, based on the
event class and event ID. Every event is defined by its class (an attribute representing
a group of events) and its ID (within the group, the specific event that needs to occur).