CMP 217 - C Programming Language:
C.G.I. Programs:

Last Update:

Table of Contents:


Background

The popularity of the Web has suddenly caused a resurgence in programming languages such as C. In order to create an interactive web site, a program is needed on the server that can both interact with the operating system and at the same time, stay within the guidelines of web interaction.

Programs that meet these needs are known as Common Gateway Interface (CGI) programs. Although CGI can be written in a variety of languages (based on server support), C still remains a popular choice.


To make HTML output (dynamic web page).

Your CGI program simply outputs HTML tags that will be interpreted by the browser. For example, The C statement:

printf("<h1>Hello world</h1>");

would produce Hello world as the H1 HTML format.



Prior to printing ANY output, a CGI requirement called a content header must be output. This content header defines what type of data (text, graphics, etc) is coming and how the server is to process and pass it along to your browser
The content header for this assignment is:

printf("Content-type: text/html\n\n\n");

This must be done prior to having you program perform ANY output.

If you deviate from the EXACT format or fail to perform this, a server error will occur.

Some additional CGI notes


Program Testing

Writing CGI is easy but frustrating. If a program contains some type of error, either program or failure to obey CGI rules, a server error occurs with very little (usually no) specific error message.

Therefore, it essential that you test your program locally before ever attempting to install and run on a server.

In order to test properly, your program must run both on your development platform and on the server you are installing on. C provides several mechanisms allowing you develop programs code that can be compiled and executed on multiple platforms.

One such mechanism is the

#ifdef name
code/definitions block 1
#else
code/definitions block 2
#endif

If there is a #define name value prior to the #ifdef, the resulting block 1 is compiled into the code. Otherwise, the optional #else block is compiled in.

For these assignments, programs on the server will have a #define SERVER 1 set so that within that block, server/CGI specific code will take effect.

For example:


#ifdef SERVER
printf("Content-type: text/html\n\n\n"); /* server specific stuff */
#else
clrscr();/* development platform (such as PC) stuff, if needed */
#endif

All C compilers allow one to specify #define(s) at compile time as opposed to them appearing in the source. For more information on pre-processor commands, consult your textbook.


© R. Glass
glassr@sunynassau.edu