Using C for CGI Programming(1)
2010-09-18 12:16 shuisheng 阅读(268) 评论(0) 收藏 举报Perl, Python and PHP are the holy trinity of CGI applicationprogramming. Stores have shelves full of books about these languages,they're covered well in the computer press and there's plenty on theInternet about them. A distinct lack of information exists, however, on usingC to write CGI applications. In this article, I show how touse C for CGI programming and lay out some situations in which itprovides significant advantages.
I use C in my applications for three reasons: speed, features andstability. Although conventional wisdom says otherwise, my own benchmarks havefound that C and PHP are equivalent in speed when the processing to bedone is simple. When there is any complexityto the processing, C wins hands-down.
In addition, C provides an excellent feature set. The language itself comes with abare-bones set of features, but a staggering number oflibraries are available for nearly any job for which a computeris used. Perl, of course, is no slouch in this area, and I don't contend thatC offers more extensibility, but both can fill nearly any bill.
Furthermore, CGI programs written in C are stable. Because the program iscompiled, it is not as susceptible to changes in the operatingenvironment as PHP is. Also, because the language is stable,it does not experience the dramatic changes to which PHP usershave been subjected over the past few years.
My application is a simple event listing suitable for abusiness to list upcoming events, say, the meeting schedule for a dayor the events at a church. It provides an administrative interfaceintended to be password-protected and a public interface that listsall upcoming events (but only upcoming events). This application alsoprovides forruntime configuration and interface independence.
I use a database, rather than write my own datastore, and a configuration file contains the databaseconnection information. A collection of files is usedto provide interface/code separation.
The administrative interface allows events to be listed, edited,saved and deleted. Listing events is the default action if noother action is provided. Both new and existing events can be saved.The interface consists of a grid screen that displays the list ofevents and a detail screen that contains the full record of a singleevent.
The database schema for this application consists of a single table,defined in Listing 1. This schema is MySQL-specific, but an equivalentschema can be created for any database engine.
The following functions are the minimum necessary to implement thefunctionality of the administrative interface: list_events(),show_event(), save_event() and delete_event().I also am going to abstract the reading and writing of database datainto their own group of functions. This keeps each function simpler,which makes debugging easier. The functions that I need for thedata-storage interface are event_create(), event_destroy(),event_read(), event_write and event_delete. To make my life easier,I'm also going to add event_fetch_range(), so I can choose a rangeof events—something I need to do in at least two places.
Next, I need to abstract my records to C structures and abstract databaseresult sets to linked lists. Abstraction lets me change databaseengines or data representation with relatively little expense, becauseonly a little part of my code deals directly with the data store.
There isn't room here to print all of my source code.Complete source code and my Makefile can be downloaded from my Website (see the on-line Resources).
The first hurdle to overcome when using C is acquiring the set of tools youneed. At bare minimum, you need a CGI parser to break out theCGI information for you. Chances are good that you're alsolooking for some database connectivity. A little bit oflogic/interface independence is good too, so you aren'trewriting code every time the site needs a makeover.
For CGI parsing, I recommend the cgic library from Thomas Boutell (seeResources). It's shockingly easy to use andprovides access to all parts of the CGI interface. If you're a C++person, the cgicc libraries also are suitable (see Resources),although I found the Boutell library to be easier to use.
MySQL is pretty much the standard for UNIX Web development,so I stick with it for my sample application. Every significantdatabase engine has a functional C interface library, though, so youcan use whatever database you like.
I'm going to provide my own interface-independence routines, but youcould use libxml and libxslt to do the same thing with a good dealmore sophistication.
 
                     
                    
                 
                    
                 
                
            
         浙公网安备 33010602011771号
浙公网安备 33010602011771号