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));
60 /* Convert a two-char hex string into the char it represents */
65 digit = (what[0] >= 'A' ? ((what[0] & 0xdf) - 'A')+10 :
68 digit += (what[1] >= 'A' ? ((what[1] & 0xdf) - 'A')+10 :
74 /* Reduce any %xx escape sequences to the characters they represent */
75 void unescape_url(char *url)
79 for(i=0,j=0; url[j]; ++i,++j) {
80 if((url[i] = url[j]) == '%') {
81 url[i]=x2c(&url[j+1]);
90 /* Read the CGI input and place all name/val pairs into list. */
91 /* Returns list containing name1, value1, name2, value2, ... , NULL */
92 char **getcgivars(int argc, char *argv[])
96 int content_length, paircount;
102 /* Depending on the request method, read all CGI input into cgiinput */
103 /* (really should produce HTML error messages, instead of exit()ing) */
105 request_method=getenv("REQUEST_METHOD");
107 if (request_method == NULL) {
113 } else if (strlen(request_method)==0) {
115 } else if (!strcmp(request_method, "GET") || !strcmp(request_method, "HEAD")) {
116 cgiinput=strdup(getenv("QUERY_STRING"));
117 } else if (!strcmp(request_method, "POST")) {
118 if (getenv("CONTENT_TYPE") != NULL &&
119 strcasecmp(getenv("CONTENT_TYPE"),
120 "application/x-www-form-urlencoded")) {
121 printf("getcgivars(): Unsupported Content-Type.\n");
125 if (!(content_length = atoi(getenv("CONTENT_LENGTH")))) {
126 printf("getcgivars(): No Content-Length was sent with the POST request.\n");
130 if (!(cgiinput= (char *) malloc(content_length+1))) {
131 printf("getcgivars(): Could not malloc for cgiinput.\n");
135 if (!fread(cgiinput, content_length, 1, stdin)) {
136 printf("Couldn't read CGI input from STDIN.\n");
140 cgiinput[content_length]='\0';
143 printf("getcgivars(): unsupported REQUEST_METHOD\n");
147 /* Change all plusses back to spaces */
149 for(i=0; cgiinput[i]; i++) if (cgiinput[i]=='+') cgiinput[i] = ' ';
151 /* First, split on "&" to extract the name-value pairs into pairlist */
152 pairlist=(char **) malloc(256*sizeof(char **));
154 nvpair=strtok(cgiinput, "&");
156 pairlist[paircount++]= strdup(nvpair) ;
157 if (!(paircount%256)) pairlist=(char **) realloc(pairlist,(paircount+256)*sizeof(char **));
158 nvpair=strtok(NULL, "&") ;
161 pairlist[paircount]=0; /* terminate the list with NULL */
163 /* Then, from the list of pairs, extract the names and values */
165 cgivars=(char **) malloc((paircount*2+1)*sizeof(char **));
167 for (i=0; i<paircount; i++) {
168 if ((eqpos=strchr(pairlist[i], '='))!=NULL) {
170 unescape_url(cgivars[i*2+1]=strdup(eqpos+1));
172 unescape_url(cgivars[i*2+1]=strdup(""));
174 unescape_url(cgivars[i*2]= strdup(pairlist[i])) ;
177 cgivars[paircount*2]=NULL; /* terminate the list with NULL */
179 /* Free anything that needs to be freed */
181 for (i=0; pairlist[i]; i++) free(pairlist[i]);
184 /* Return the list of name-value strings */