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.
18 * txt2html - Takes a string and converts it to HTML.
19 * @string: The string to HTMLize.
21 * Takes a string and escapes any HTML entities.
23 char *txt2html(const char *string)
25 static char buf[1024];
31 ptr = strchr(string, '<');
35 strncpy(buf, string, 1023);
36 strncat(buf, "<", 1023 - strlen(buf));
40 ptr = strchr(string, '>');
44 strncat(buf, string, 1023 - strlen(buf));
45 strncat(buf, ">", 1023 - strlen(buf));
50 * TODO: We need to while() this really as each entity may appear more
51 * than once. We need to start with & and ; as we replace with those
52 * throughout. Fuck it for the moment though; it's Easter and < & > are
53 * the most common and tend to only appear once.
56 strncat(buf, string, 1023 - strlen(buf));
62 * start_html - Start HTML output.
63 * @title: The title for the HTML.
65 * Takes a title string and starts HTML output, including the
66 * Content-Type header all the way up to <BODY>.
68 void start_html(const char *title)
70 puts("Content-Type: text/html; charset=UTF-8\n");
71 puts("<!DOCTYPE HTML PUBLIC '-//W3C//DTD HTML 3.2 Final//EN'>");
74 printf("<TITLE>%s</TITLE>\n", title);
82 * end_html - End HTML output.
84 * Ends HTML output - closes the BODY and HTML tags.
95 /* Convert a two-char hex string into the char it represents */
96 char x2c(const char *what)
100 digit = (what[0] >= 'A' ? ((what[0] & 0xdf) - 'A')+10 :
103 digit += (what[1] >= 'A' ? ((what[1] & 0xdf) - 'A')+10 :
109 /* Reduce any %xx escape sequences to the characters they represent */
110 void unescape_url(char *url)
114 for(i=0,j=0; url[j]; ++i,++j) {
115 if((url[i] = url[j]) == '%') {
116 url[i]=x2c(&url[j+1]);
125 /* Read the CGI input and place all name/val pairs into list. */
126 /* Returns list containing name1, value1, name2, value2, ... , NULL */
127 char **getcgivars(int argc, char *argv[])
130 char *request_method;
131 int content_length, paircount;
132 char *cgiinput = NULL;
133 char **cgivars = NULL;
134 char **pairlist = NULL;
137 /* Depending on the request method, read all CGI input into cgiinput */
138 /* (really should produce HTML error messages, instead of exit()ing) */
140 request_method = getenv("REQUEST_METHOD");
142 if (request_method == NULL) {
144 cgiinput = strdup(argv[1]);
148 } else if (strlen(request_method)==0) {
150 } else if (!strcmp(request_method, "GET") ||
151 !strcmp(request_method, "HEAD")) {
152 cgiinput=strdup(getenv("QUERY_STRING"));
153 } else if (!strcmp(request_method, "POST")) {
154 if (getenv("CONTENT_TYPE") != NULL &&
155 strcasecmp(getenv("CONTENT_TYPE"),
156 "application/x-www-form-urlencoded")) {
157 printf("getcgivars(): Unsupported Content-Type.\n");
161 if (!(content_length = atoi(getenv("CONTENT_LENGTH")))) {
162 printf("getcgivars(): No Content-Length was sent with"
163 " the POST request.\n");
167 if (!(cgiinput= (char *) malloc(content_length+1))) {
168 printf("getcgivars(): Could not malloc for "
173 if (!fread(cgiinput, content_length, 1, stdin)) {
174 printf("Couldn't read CGI input from STDIN.\n");
178 cgiinput[content_length]='\0';
181 printf("getcgivars(): unsupported REQUEST_METHOD\n");
185 /* Change all plusses back to spaces */
187 for(i=0; cgiinput[i]; i++) if (cgiinput[i]=='+') cgiinput[i] = ' ';
189 /* First, split on "&" to extract the name-value pairs into pairlist */
190 pairlist=(char **) malloc(256*sizeof(char **));
192 nvpair=strtok(cgiinput, "&");
194 pairlist[paircount++]= strdup(nvpair) ;
195 if (!(paircount%256)) {
196 pairlist=(char **) realloc(pairlist,
197 (paircount+256)*sizeof(char **));
199 nvpair=strtok(NULL, "&") ;
202 pairlist[paircount]=0; /* terminate the list with NULL */
204 /* Then, from the list of pairs, extract the names and values */
206 cgivars=(char **) malloc((paircount*2+1)*sizeof(char **));
208 for (i=0; i<paircount; i++) {
209 if ((eqpos=strchr(pairlist[i], '='))!=NULL) {
211 unescape_url(cgivars[i*2+1]=strdup(eqpos+1));
213 unescape_url(cgivars[i*2+1]=strdup(""));
215 unescape_url(cgivars[i*2]= strdup(pairlist[i])) ;
218 cgivars[paircount*2]=NULL; /* terminate the list with NULL */
220 /* Free anything that needs to be freed */
222 for (i=0; pairlist[i]; i++) free(pairlist[i]);
225 /* Return the list of name-value strings */
231 * cleanupcgi - free the memory allocated for our CGI parameters.
232 * @cgivars: The CGI parameter list to free.
234 * Frees up the elements of the CGI parameter array and then frees the
237 void cleanupcgi(char **cgivars)
241 if (cgivars != NULL) {
242 for (i = 0; cgivars[i] != NULL; i++) {