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, see <https://www.gnu.org/licenses/>.
30 * start_html - Start HTML output.
31 * @title: The title for the HTML.
33 * Takes a title string and starts HTML output, including the
34 * Content-Type header all the way up to <BODY>.
36 void start_html(const char *title)
38 puts("Content-Type: text/html; charset=UTF-8\n");
39 puts("<!DOCTYPE HTML PUBLIC '-//W3C//DTD HTML 3.2 Final//EN'>");
42 printf("<TITLE>%s</TITLE>\n", title);
50 * end_html - End HTML output.
52 * Ends HTML output - closes the BODY and HTML tags.
63 /* Convert a two-char hex string into the char it represents */
64 char x2c(const char *what)
68 digit = (what[0] >= 'A' ? ((what[0] & 0xdf) - 'A')+10 :
71 digit += (what[1] >= 'A' ? ((what[1] & 0xdf) - 'A')+10 :
77 /* Reduce any %xx escape sequences to the characters they represent */
78 void unescape_url(char *url)
82 for(i=0,j=0; url[j]; ++i,++j) {
83 if((url[i] = url[j]) == '%') {
84 url[i]=x2c(&url[j+1]);
93 /* Read the CGI input and place all name/val pairs into list. */
94 /* Returns list containing name1, value1, name2, value2, ... , NULL */
95 char **getcgivars(int argc, char *argv[])
98 char *request_method, *env;
99 int content_length, paircount;
100 char *cgiinput = NULL;
101 char **cgivars = NULL;
102 char **pairlist = NULL;
105 /* Depending on the request method, read all CGI input into cgiinput */
106 /* (really should produce HTML error messages, instead of exit()ing) */
108 request_method = getenv("REQUEST_METHOD");
110 if (request_method == NULL) {
112 cgiinput = strdup(argv[1]);
116 } else if (strlen(request_method)==0) {
118 } else if (!strcmp(request_method, "GET") ||
119 !strcmp(request_method, "HEAD")) {
120 env = getenv("QUERY_STRING");
122 cgiinput = strdup(env);
124 } else if (!strcmp(request_method, "POST")) {
125 env = getenv("CONTENT_TYPE");
126 if ((env != NULL) && strcasecmp(env,
127 "application/x-www-form-urlencoded")) {
128 printf("getcgivars(): Unsupported Content-Type.\n");
132 env = getenv("CONTENT_LENGTH");
133 if ((env == NULL) || !(content_length = atoi(env))) {
134 printf("getcgivars(): No Content-Length was sent with"
135 " the POST request.\n");
139 if (!(cgiinput = (char *) malloc(content_length+1))) {
140 printf("getcgivars(): Could not malloc for "
145 if (!fread(cgiinput, content_length, 1, stdin)) {
146 printf("Couldn't read CGI input from STDIN.\n");
150 cgiinput[content_length]='\0';
153 printf("getcgivars(): unsupported REQUEST_METHOD\n");
157 /* If we didn't get any cgiinput info, nothing to return */
158 if (cgiinput == NULL) {
162 /* Change all plusses back to spaces */
164 for(i=0; cgiinput[i]; i++) if (cgiinput[i]=='+') cgiinput[i] = ' ';
166 /* First, split on "&" to extract the name-value pairs into pairlist */
167 pairlist= malloc(256*sizeof(char *));
169 nvpair=strtok(cgiinput, "&");
171 pairlist[paircount++]= strdup(nvpair) ;
172 if (!(paircount%256)) {
173 pairlist= realloc(pairlist,
174 (paircount+256)*sizeof(char *));
176 nvpair=strtok(NULL, "&") ;
179 pairlist[paircount]=0; /* terminate the list with NULL */
181 /* Then, from the list of pairs, extract the names and values */
183 cgivars= malloc((paircount*2+1)*sizeof(char *));
185 for (i=0; i<paircount; i++) {
186 if ((eqpos=strchr(pairlist[i], '='))!=NULL) {
188 unescape_url(cgivars[i*2+1]=strdup(eqpos+1));
190 unescape_url(cgivars[i*2+1]=strdup(""));
192 unescape_url(cgivars[i*2]= strdup(pairlist[i])) ;
195 cgivars[paircount*2]=NULL; /* terminate the list with NULL */
197 /* Free anything that needs to be freed */
199 for (i=0; pairlist[i]; i++) free(pairlist[i]);
202 /* Return the list of name-value strings */
208 * cleanupcgi - free the memory allocated for our CGI parameters.
209 * @cgivars: The CGI parameter list to free.
211 * Frees up the elements of the CGI parameter array and then frees the
214 void cleanupcgi(char **cgivars)
218 if (cgivars != NULL) {
219 for (i = 0; cgivars[i] != NULL; i++) {