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.
17 * txt2html - Takes a string and converts it to HTML.
18 * @string: The string to HTMLize.
20 * Takes a string and escapes any HTML entities.
22 char *txt2html(const char *string)
24 static char buf[1024];
30 ptr = strchr(string, '<');
34 strncpy(buf, string, 1023);
35 strncat(buf, "<", 1023 - strlen(buf));
39 ptr = strchr(string, '>');
43 strncat(buf, string, 1023 - strlen(buf));
44 strncat(buf, ">", 1023 - strlen(buf));
49 * TODO: We need to while() this really as each entity may appear more
50 * than once. We need to start with & and ; as we replace with those
51 * throughout. Fuck it for the moment though; it's Easter and < & > are
52 * the most common and tend to only appear once.
55 strncat(buf, string, 1023 - strlen(buf));
61 * start_html - Start HTML output.
62 * @title: The title for the HTML.
64 * Takes a title string and starts HTML output, including the
65 * Content-Type header all the way up to <BODY>.
67 void start_html(const char *title)
69 puts("Content-Type: text/html; charset=UTF-8\n");
70 puts("<!DOCTYPE HTML PUBLIC '-//W3C//DTD HTML 3.2 Final//EN'>");
73 printf("<TITLE>%s</TITLE>\n", title);
81 * end_html - End HTML output.
83 * Ends HTML output - closes the BODY and HTML tags.
94 /* Convert a two-char hex string into the char it represents */
95 char x2c(const char *what)
99 digit = (what[0] >= 'A' ? ((what[0] & 0xdf) - 'A')+10 :
102 digit += (what[1] >= 'A' ? ((what[1] & 0xdf) - 'A')+10 :
108 /* Reduce any %xx escape sequences to the characters they represent */
109 void unescape_url(char *url)
113 for(i=0,j=0; url[j]; ++i,++j) {
114 if((url[i] = url[j]) == '%') {
115 url[i]=x2c(&url[j+1]);
124 /* Read the CGI input and place all name/val pairs into list. */
125 /* Returns list containing name1, value1, name2, value2, ... , NULL */
126 char **getcgivars(int argc, char *argv[])
129 char *request_method;
130 int content_length, paircount;
131 char *cgiinput = NULL;
132 char **cgivars = NULL;
133 char **pairlist = NULL;
136 /* Depending on the request method, read all CGI input into cgiinput */
137 /* (really should produce HTML error messages, instead of exit()ing) */
139 request_method = getenv("REQUEST_METHOD");
141 if (request_method == NULL) {
143 cgiinput = strdup(argv[1]);
147 } else if (strlen(request_method)==0) {
149 } else if (!strcmp(request_method, "GET") ||
150 !strcmp(request_method, "HEAD")) {
151 cgiinput=strdup(getenv("QUERY_STRING"));
152 } else if (!strcmp(request_method, "POST")) {
153 if (getenv("CONTENT_TYPE") != NULL &&
154 strcasecmp(getenv("CONTENT_TYPE"),
155 "application/x-www-form-urlencoded")) {
156 printf("getcgivars(): Unsupported Content-Type.\n");
160 if (!(content_length = atoi(getenv("CONTENT_LENGTH")))) {
161 printf("getcgivars(): No Content-Length was sent with"
162 " the POST request.\n");
166 if (!(cgiinput= (char *) malloc(content_length+1))) {
167 printf("getcgivars(): Could not malloc for "
172 if (!fread(cgiinput, content_length, 1, stdin)) {
173 printf("Couldn't read CGI input from STDIN.\n");
177 cgiinput[content_length]='\0';
180 printf("getcgivars(): unsupported REQUEST_METHOD\n");
184 /* Change all plusses back to spaces */
186 for(i=0; cgiinput[i]; i++) if (cgiinput[i]=='+') cgiinput[i] = ' ';
188 /* First, split on "&" to extract the name-value pairs into pairlist */
189 pairlist=(char **) malloc(256*sizeof(char **));
191 nvpair=strtok(cgiinput, "&");
193 pairlist[paircount++]= strdup(nvpair) ;
194 if (!(paircount%256)) {
195 pairlist=(char **) realloc(pairlist,
196 (paircount+256)*sizeof(char **));
198 nvpair=strtok(NULL, "&") ;
201 pairlist[paircount]=0; /* terminate the list with NULL */
203 /* Then, from the list of pairs, extract the names and values */
205 cgivars=(char **) malloc((paircount*2+1)*sizeof(char **));
207 for (i=0; i<paircount; i++) {
208 if ((eqpos=strchr(pairlist[i], '='))!=NULL) {
210 unescape_url(cgivars[i*2+1]=strdup(eqpos+1));
212 unescape_url(cgivars[i*2+1]=strdup(""));
214 unescape_url(cgivars[i*2]= strdup(pairlist[i])) ;
217 cgivars[paircount*2]=NULL; /* terminate the list with NULL */
219 /* Free anything that needs to be freed */
221 for (i=0; pairlist[i]; i++) free(pairlist[i]);
224 /* Return the list of name-value strings */
230 * cleanupcgi - free the memory allocated for our CGI parameters.
231 * @cgivars: The CGI parameter list to free.
233 * Frees up the elements of the CGI parameter array and then frees the
236 void cleanupcgi(char **cgivars)
240 if (cgivars != NULL) {
241 for (i = 0; cgivars[i] != NULL; i++) {