You copied the Doc URL to your clipboard.

3.8.1. CGI routine inputs

Example 3.5 shows the semantics of the CGI calls:

int setip_cgi(struct httpd *hp,           /* connection on which GET or POST came in */
              struct httpform *form,      /* a list of name-value pairs */
              char **filetext 			            /* optional text or file to return to the */
                                          /* Web browser */
	 	      )

The input to these routines is fairly simple, assuming you are familiar with HTML forms. Before you attempt to code a CGI routine, you should be familiar with the use of name/value pairs from a form. The CGI routine is passed a form structure that contains all the information from the filled in form. The structure is defined as follows:

/* a form reply name-value pair */
struct name_val {
       char *name;
       char *value;
};

struct httpform {
       char   *action; /* the VFS file name */
       int    nv_ct;   /* number of entries in name_val table */
       struct name_val nameval[1];  
                       /* the first of an array of name-value */
                       /* entries */
};

There are actually two structures here:

  • The first contains a single name/value pair. The memory for the strings is obtained by npalloc() before the CGI routine is called, and is freed by way of npfree() after the CGI routine returns, so the CGI routine should copy any data it will need later.

  • The second structure is primarily a list of name/value pairs. The first member is a pointer to the file name, which can be useful if you want to write a CGI routine that is invoked by more than one form. The next member is the number of name/value structures that follow. The rest of the structure is an array of the name/value structures. Although this array is declared (in the structure definition) to contain only one element, the actual structure passed to your CGI routine has been dynamically allocated by npalloc(), and the nameval[] array contains nv_ct entries.

The name/value structures are filled in the order they were received from the browser. Experience shows this has always been the order in which they appeared in the form. However, the HTTP specifications allows them to be in any order. You should check the names before parsing the values.

The CGI routine may block while it is parsing data, but caution should be taken so that calls to http_loop() do not cease. This would stop your webserver from servicing other blocked connections until the CGI routine returns. The ARM Webserver logic keeps a flag in the httpd structure, which lets it know that the connection is blocked in a CGI routine, so it will not call the CGI routine recursively for the same connection and overflow your stack.

Was this page helpful? Yes No