2 * getcgivars.c - routine to read CGI input variables into an array.
4 * Jonathan McDowell <noodles@earth.li>
6 * The x2c() and unescape_url() routines were lifted directly
7 * from NCSA's sample program util.c, packaged with their HTTPD.
9 * $Id: getcgi.c,v 1.5 2003/06/04 20:57:07 noodles Exp $
19 * txt2html - Takes a string and converts it to HTML.
20 * @string: The string to HTMLize.
22 * Takes a string and escapes any HTML entities.
24 char *txt2html(const char *string)
26 static char buf[1024];
32 ptr = strchr(string, '<');
36 strncpy(buf, string, 1023);
37 strncat(buf, "<", 1023 - strlen(buf));
41 ptr = strchr(string, '>');
45 strncat(buf, string, 1023 - strlen(buf));
46 strncat(buf, ">", 1023 - strlen(buf));
51 * TODO: We need to while() this really as each entity may appear more
52 * than once. We need to start with & and ; as we replace with those
53 * throughout. Fuck it for the moment though; it's Easter and < & > are
54 * the most common and tend to only appear once.
57 strncat(buf, string, 1023 - strlen(buf));
63 * start_html - Start HTML output.
64 * @title: The title for the HTML.
66 * Takes a title string and starts HTML output, including the
67 * Content-Type header all the way up to <BODY>.
69 void start_html(const char *title)
71 puts("Content-Type: text/html; charset=UTF-8\n");
72 puts("<!DOCTYPE HTML PUBLIC '-//W3C//DTD HTML 3.2 Final//EN'>");
75 printf("<TITLE>%s</TITLE>\n", title);
83 * end_html - End HTML output.
85 * Ends HTML output - closes the BODY and HTML tags.
96 /* Convert a two-char hex string into the char it represents */
97 char x2c(const char *what)
101 digit = (what[0] >= 'A' ? ((what[0] & 0xdf) - 'A')+10 :
104 digit += (what[1] >= 'A' ? ((what[1] & 0xdf) - 'A')+10 :
110 /* Reduce any %xx escape sequences to the characters they represent */
111 void unescape_url(char *url)
115 for(i=0,j=0; url[j]; ++i,++j) {
116 if((url[i] = url[j]) == '%') {
117 url[i]=x2c(&url[j+1]);
126 /* Read the CGI input and place all name/val pairs into list. */
127 /* Returns list containing name1, value1, name2, value2, ... , NULL */
128 char **getcgivars(int argc, char *argv[])
131 char *request_method;
132 int content_length, paircount;
133 char *cgiinput = NULL;
134 char **cgivars = NULL;
135 char **pairlist = NULL;
138 /* Depending on the request method, read all CGI input into cgiinput */
139 /* (really should produce HTML error messages, instead of exit()ing) */
141 request_method = getenv("REQUEST_METHOD");
143 if (request_method == NULL) {
145 cgiinput = strdup(argv[1]);
149 } else if (strlen(request_method)==0) {
151 } else if (!strcmp(request_method, "GET") ||
152 !strcmp(request_method, "HEAD")) {
153 cgiinput=strdup(getenv("QUERY_STRING"));
154 } else if (!strcmp(request_method, "POST")) {
155 if (getenv("CONTENT_TYPE") != NULL &&
156 strcasecmp(getenv("CONTENT_TYPE"),
157 "application/x-www-form-urlencoded")) {
158 printf("getcgivars(): Unsupported Content-Type.\n");
162 if (!(content_length = atoi(getenv("CONTENT_LENGTH")))) {
163 printf("getcgivars(): No Content-Length was sent with"
164 " the POST request.\n");
168 if (!(cgiinput= (char *) malloc(content_length+1))) {
169 printf("getcgivars(): Could not malloc for "
174 if (!fread(cgiinput, content_length, 1, stdin)) {
175 printf("Couldn't read CGI input from STDIN.\n");
179 cgiinput[content_length]='\0';
182 printf("getcgivars(): unsupported REQUEST_METHOD\n");
186 /* Change all plusses back to spaces */
188 for(i=0; cgiinput[i]; i++) if (cgiinput[i]=='+') cgiinput[i] = ' ';
190 /* First, split on "&" to extract the name-value pairs into pairlist */
191 pairlist=(char **) malloc(256*sizeof(char **));
193 nvpair=strtok(cgiinput, "&");
195 pairlist[paircount++]= strdup(nvpair) ;
196 if (!(paircount%256)) {
197 pairlist=(char **) realloc(pairlist,
198 (paircount+256)*sizeof(char **));
200 nvpair=strtok(NULL, "&") ;
203 pairlist[paircount]=0; /* terminate the list with NULL */
205 /* Then, from the list of pairs, extract the names and values */
207 cgivars=(char **) malloc((paircount*2+1)*sizeof(char **));
209 for (i=0; i<paircount; i++) {
210 if ((eqpos=strchr(pairlist[i], '='))!=NULL) {
212 unescape_url(cgivars[i*2+1]=strdup(eqpos+1));
214 unescape_url(cgivars[i*2+1]=strdup(""));
216 unescape_url(cgivars[i*2]= strdup(pairlist[i])) ;
219 cgivars[paircount*2]=NULL; /* terminate the list with NULL */
221 /* Free anything that needs to be freed */
223 for (i=0; pairlist[i]; i++) free(pairlist[i]);
226 /* Return the list of name-value strings */
232 * cleanupcgi - free the memory allocated for our CGI parameters.
233 * @cgivars: The CGI parameter list to free.
235 * Frees up the elements of the CGI parameter array and then frees the
238 void cleanupcgi(char **cgivars)
242 if (cgivars != NULL) {
243 for (i = 0; cgivars[i] != NULL; i++) {