2 * getcgivars.c - routine to read CGI input variables into an array.
4 * Copyright 2002 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 * This program is free software: you can redistribute it and/or modify it
10 * under the terms of the GNU General Public License as published by the Free
11 * Software Foundation; version 2 of the License.
13 * This program is distributed in the hope that it will be useful, but WITHOUT
14 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
15 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
18 * You should have received a copy of the GNU General Public License along with
19 * this program; if not, write to the Free Software Foundation, Inc., 51
20 * Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
31 * txt2html - Takes a string and converts it to HTML.
32 * @string: The string to HTMLize.
34 * Takes a string and escapes any HTML entities.
36 char *txt2html(const char *string)
38 static char buf[1024];
44 ptr = strchr(string, '<');
48 strncpy(buf, string, 1023);
49 strncat(buf, "<", 1023 - strlen(buf));
53 ptr = strchr(string, '>');
57 strncat(buf, string, 1023 - strlen(buf));
58 strncat(buf, ">", 1023 - strlen(buf));
63 * TODO: We need to while() this really as each entity may appear more
64 * than once. We need to start with & and ; as we replace with those
65 * throughout. Fuck it for the moment though; it's Easter and < & > are
66 * the most common and tend to only appear once.
69 strncat(buf, string, 1023 - strlen(buf));
75 * start_html - Start HTML output.
76 * @title: The title for the HTML.
78 * Takes a title string and starts HTML output, including the
79 * Content-Type header all the way up to <BODY>.
81 void start_html(const char *title)
83 puts("Content-Type: text/html; charset=UTF-8\n");
84 puts("<!DOCTYPE HTML PUBLIC '-//W3C//DTD HTML 3.2 Final//EN'>");
87 printf("<TITLE>%s</TITLE>\n", title);
95 * end_html - End HTML output.
97 * Ends HTML output - closes the BODY and HTML tags.
108 /* Convert a two-char hex string into the char it represents */
109 char x2c(const char *what)
113 digit = (what[0] >= 'A' ? ((what[0] & 0xdf) - 'A')+10 :
116 digit += (what[1] >= 'A' ? ((what[1] & 0xdf) - 'A')+10 :
122 /* Reduce any %xx escape sequences to the characters they represent */
123 void unescape_url(char *url)
127 for(i=0,j=0; url[j]; ++i,++j) {
128 if((url[i] = url[j]) == '%') {
129 url[i]=x2c(&url[j+1]);
138 /* Read the CGI input and place all name/val pairs into list. */
139 /* Returns list containing name1, value1, name2, value2, ... , NULL */
140 char **getcgivars(int argc, char *argv[])
143 char *request_method;
144 int content_length, paircount;
145 char *cgiinput = NULL;
146 char **cgivars = NULL;
147 char **pairlist = NULL;
150 /* Depending on the request method, read all CGI input into cgiinput */
151 /* (really should produce HTML error messages, instead of exit()ing) */
153 request_method = getenv("REQUEST_METHOD");
155 if (request_method == NULL) {
157 cgiinput = strdup(argv[1]);
161 } else if (strlen(request_method)==0) {
163 } else if (!strcmp(request_method, "GET") ||
164 !strcmp(request_method, "HEAD")) {
165 cgiinput=strdup(getenv("QUERY_STRING"));
166 } else if (!strcmp(request_method, "POST")) {
167 if (getenv("CONTENT_TYPE") != NULL &&
168 strcasecmp(getenv("CONTENT_TYPE"),
169 "application/x-www-form-urlencoded")) {
170 printf("getcgivars(): Unsupported Content-Type.\n");
174 if (!(content_length = atoi(getenv("CONTENT_LENGTH")))) {
175 printf("getcgivars(): No Content-Length was sent with"
176 " the POST request.\n");
180 if (!(cgiinput= (char *) malloc(content_length+1))) {
181 printf("getcgivars(): Could not malloc for "
186 if (!fread(cgiinput, content_length, 1, stdin)) {
187 printf("Couldn't read CGI input from STDIN.\n");
191 cgiinput[content_length]='\0';
194 printf("getcgivars(): unsupported REQUEST_METHOD\n");
198 /* Change all plusses back to spaces */
200 for(i=0; cgiinput[i]; i++) if (cgiinput[i]=='+') cgiinput[i] = ' ';
202 /* First, split on "&" to extract the name-value pairs into pairlist */
203 pairlist=(char **) malloc(256*sizeof(char **));
205 nvpair=strtok(cgiinput, "&");
207 pairlist[paircount++]= strdup(nvpair) ;
208 if (!(paircount%256)) {
209 pairlist=(char **) realloc(pairlist,
210 (paircount+256)*sizeof(char **));
212 nvpair=strtok(NULL, "&") ;
215 pairlist[paircount]=0; /* terminate the list with NULL */
217 /* Then, from the list of pairs, extract the names and values */
219 cgivars=(char **) malloc((paircount*2+1)*sizeof(char **));
221 for (i=0; i<paircount; i++) {
222 if ((eqpos=strchr(pairlist[i], '='))!=NULL) {
224 unescape_url(cgivars[i*2+1]=strdup(eqpos+1));
226 unescape_url(cgivars[i*2+1]=strdup(""));
228 unescape_url(cgivars[i*2]= strdup(pairlist[i])) ;
231 cgivars[paircount*2]=NULL; /* terminate the list with NULL */
233 /* Free anything that needs to be freed */
235 for (i=0; pairlist[i]; i++) free(pairlist[i]);
238 /* Return the list of name-value strings */
244 * cleanupcgi - free the memory allocated for our CGI parameters.
245 * @cgivars: The CGI parameter list to free.
247 * Frees up the elements of the CGI parameter array and then frees the
250 void cleanupcgi(char **cgivars)
254 if (cgivars != NULL) {
255 for (i = 0; cgivars[i] != NULL; i++) {