3. Routines

[ Next | Up to Table of Contents | Previous ]

3.1. cgi-lib.h

Library functions

int die();

die() should be used in conjunction with C's signal handling libraries. In order to prevent runaway processes, you should send an alarm signal after a certain amount of time and call die() upon receiving this signal.

die() is somewhat primitive at this stage. It will display a simple error message and kill the program gracefully.

short method_get();

method_get() returns a 1 if the data being sent to the CGI script is using the GET method, and a 0 if it is using the POST method.

short accept_image();

accept_image() determines whether the browser will accept pictures. It does so by checking the HTTP_ACCEPT environment variable for an image MIME type. It returns a 1 if the browser will accept graphics, a 0 otherwise.

int read_cgi_input(llist *entries);

This routine parses the raw CGI data passed from the browser to the server and adds each associated name and value to the linked list entries. It will parse information transmitted using both the GET and POST method. If it receives no information, it will return a 0, otherwise it returns a 1.

char* cgi_val(llist l, char *name);

cgi_val() searches the linked list for the value of the entry named name and returns the value if it finds it. If it cannot find an entry with name name, it will return a null string.

void print_entries(llist l);

This is a generic routine which will iterate through the linked list and print each name and associated value in HTML form. It uses the <dl> list format to display the list.

char* escape_input(char *str);

escape_input() "escapes" shell metacharacters in the string. Shell metacharacters include: | & ; ( ) < > ' " * ? . C routines including system() and popen() open up a bourne shell process before running. If you do not escape shell metacharacters in the input (prefix metacharacters with a backslash), then malicious users may be able to take advantage of your system.

More detailed documentation on security will be included in the next release of this package.

3.2. html-lib.h

Library functions

void html_header();

html_header() prints a MIME compliant header which should precede the output of any HTML document from a CGI script. It simply prints to STDOUT:

   Content-Type: text/html
and a blank line.

void mime_header(char *mime);

Allows you to print any MIME header. For example, if you are about to send a GIF image to the standard output from your C CGI program, precede your program with:

   /* now you can send your GIF file to stdout */
mime_header() simply prints Content-Type: followed by your specified MIME header and a blank line.

void no_parse_header();

In order for a CGI program to run without returning any output, it must send an HTTP "no parse" header. (For more information, see the HTTP specification.) The no parse header consists of:

   HTTP/1.0 204 No Response
   Content-Type: text/html
Note that with some servers (notably NCSA), you need to precede the name of CGI programs which will send this header with "nph-".

void show_html_page(char *loc);

Sends a "Location: " header. *loc is the location of the HTML file you wish sent to the browser. For example, if you want to send the root index file from the CGI program:


void html_begin(char *title);

html_begin() sends somewhat standard HTML tags which should generally be at the top of every HTML file. It will send:

   <html> <head>

void html_end();

html_end() is the complement to html_begin(), sending the following HTML:

   </body> </html>
Note that neither html_begin() nor html_end() are necessary for your CGI scripts to output HTML, but they are good style, and I encourage use of these routines.

void h1(char *str);

Surrounds str with the headline 1 tags: <h1>. There are also routines h2(), h3(), h4(), h5(), and h6() which do the same.

3.3. llist.h

For most scripts, with the exception of list_end(), you will most likely never have to use any of the link list routines available, since cgi-lib.h handles most common linked list manipulation almost transparently. However, you may sometimes want to manipulate the information directly or perform special functions on each entry, in which case these routines may be useful.

Library variables

typedef struct {
  char *name;
  char *value;
} entrytype;

typedef struct _node {
  entrytype entry;
  struct _node* next;
} node;

typedef struct {
  node* head;
} llist;
Library functions

void list_create(llist *l);

list_create() creates and initializes the list, and it should be called at the beginning of every CGI script using this library.

node* list_next(node* w);

list_next() returns the next node on the list.

short on_list(llist *l, node* w);

on_list() returns a 1 if the node w is on the linked list l; otherwise, it returns a 0.

short on_list_debug(llist *l, node* w);

The previous routine makes the assumption that my linked list routines are bug-free, a possibly bad assumption. If you are using linked list routines and on_list() isn't returning the correct value, try using on_list_debug() which makes no assumptions, is almost definitely reliable, but is a little slower than the other routine.

void list_traverse(llist *l, void (*visit)(entrytype item));

list_traverse() lets you pass a pointer to a function which will manipulate each entry on the list.

To use, you must create a function that will take as its argument a variable of type entrytype. For example, if you wanted to write your own print_entries() function, you could do the following:

void print_element(entrytype item);
  printf("%s = %s\n",item.name,item.value);

void print_entries(llist entries);
  list_traverse(&stuff, print_element);

node* list_insafter(llist* l, node* w, entrytype item);

list_insafter() adds the entry item after the node w and returns the pointer to the newly created node. I didn't bother writing a function to insert before a node since my CGI functions don't need one.

void list_clear(llist* l);

This routine frees up the memory used by the linked list after you are finished with it. It is imperative that you call this function at the end of every program which calls read_cgi_input().

3.4. string-lib.h

Library functions

char* newstr(char *str);

newstr() allocates memory and returns a copy of str. Use this function to correctly allocate memory and copy strings.

Next | Up to Table of Contents | Previous ]
Last modified: Sun Aug 13 17:24:15 1995