cscvs to tla changeset 1
[onak.git] / getcgi.c
1 /*
2  * getcgivars.c - routine to read CGI input variables into an array.
3  *
4  * Jonathan McDowell <noodles@earth.li>
5  *
6  * The x2c() and unescape_url() routines were lifted directly
7  * from NCSA's sample program util.c, packaged with their HTTPD.
8  */
9
10 #include <stdio.h>
11 #include <string.h>
12 #include <stdlib.h>
13
14 #include "getcgi.h"
15
16 /**
17  *      txt2html - Takes a string and converts it to HTML.
18  *      @string: The string to HTMLize.
19  *
20  *      Takes a string and escapes any HTML entities.
21  */
22 char *txt2html(const char *string)
23 {
24         static char buf[1024];
25         char *ptr = NULL;
26         char *nextptr = NULL;
27
28         buf[0] = 0;
29
30         ptr = strchr(string, '<');
31         if (ptr != NULL) {
32                 nextptr = ptr + 1;
33                 *ptr = 0;
34                 strncpy(buf, string, 1023);
35                 strncat(buf, "&lt;", 1023 - strlen(buf));
36                 string = nextptr;
37         }
38
39         ptr = strchr(string, '>');
40         if (ptr != NULL) {
41                 nextptr = ptr + 1;
42                 *ptr = 0;
43                 strncat(buf, string, 1023 - strlen(buf));
44                 strncat(buf, "&gt;", 1023 - strlen(buf));
45                 string = nextptr;
46         }
47         
48         /*
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.
53          */
54
55         strncat(buf, string, 1023 - strlen(buf));
56
57         return buf;
58 }
59
60 /* Convert a two-char hex string into the char it represents */
61 char x2c(char *what) 
62 {
63         register char digit;
64
65         digit = (what[0] >= 'A' ? ((what[0] & 0xdf) - 'A')+10 :
66                                         (what[0] - '0'));
67         digit *= 16;
68         digit += (what[1] >= 'A' ? ((what[1] & 0xdf) - 'A')+10 :
69                                         (what[1] - '0'));
70         
71         return(digit);
72 }
73
74 /* Reduce any %xx escape sequences to the characters they represent */
75 void unescape_url(char *url) 
76 {
77         register int i,j;
78
79         for(i=0,j=0; url[j]; ++i,++j) {
80                 if((url[i] = url[j]) == '%') {
81                         url[i]=x2c(&url[j+1]);
82                         j+=2;
83                 }
84         }
85         
86         url[i] = '\0';
87 }
88
89
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[]) 
93 {
94         int i;
95         char *request_method;
96         int content_length, paircount;
97         char *cgiinput;
98         char **cgivars;
99         char **pairlist;
100         char *nvpair,*eqpos;
101
102         /* Depending on the request method, read all CGI input into cgiinput */
103         /* (really should produce HTML error messages, instead of exit()ing) */
104
105         request_method=getenv("REQUEST_METHOD");
106         
107         if (request_method == NULL) {
108                 if (argc > 1) {
109                         cgiinput = argv[1];
110                 } else {
111                         return NULL;
112                 }
113         } else if (strlen(request_method)==0) {
114                 return NULL;
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");
122                         exit(1);
123                 }
124                 
125                 if (!(content_length = atoi(getenv("CONTENT_LENGTH")))) {
126                         printf("getcgivars(): No Content-Length was sent with the POST request.\n");
127                         exit(1);
128                 }
129                 
130                 if (!(cgiinput= (char *) malloc(content_length+1))) {
131                         printf("getcgivars(): Could not malloc for cgiinput.\n");
132                         exit(1);
133                 }
134                 
135                 if (!fread(cgiinput, content_length, 1, stdin)) {
136                         printf("Couldn't read CGI input from STDIN.\n");
137                         exit(1);
138                 }
139                 
140                 cgiinput[content_length]='\0';
141                 
142         } else {
143                 printf("getcgivars(): unsupported REQUEST_METHOD\n");
144                 exit(1);
145         }
146
147         /* Change all plusses back to spaces */
148
149         for(i=0; cgiinput[i]; i++) if (cgiinput[i]=='+') cgiinput[i] = ' ';
150
151         /* First, split on "&" to extract the name-value pairs into pairlist */
152         pairlist=(char **) malloc(256*sizeof(char **));
153         paircount=0;
154         nvpair=strtok(cgiinput, "&");
155         while (nvpair) {
156                 pairlist[paircount++]= strdup(nvpair) ;
157                 if (!(paircount%256)) pairlist=(char **) realloc(pairlist,(paircount+256)*sizeof(char **));
158                 nvpair=strtok(NULL, "&") ;
159         }
160
161         pairlist[paircount]=0;          /* terminate the list with NULL */
162
163         /* Then, from the list of pairs, extract the names and values */
164         
165         cgivars=(char **) malloc((paircount*2+1)*sizeof(char **));
166         
167         for (i=0; i<paircount; i++) {
168                 if ((eqpos=strchr(pairlist[i], '='))!=NULL) {
169                         *eqpos='\0';
170                         unescape_url(cgivars[i*2+1]=strdup(eqpos+1));
171                 } else {
172                         unescape_url(cgivars[i*2+1]=strdup(""));
173                 }
174                 unescape_url(cgivars[i*2]= strdup(pairlist[i])) ;
175         }
176
177         cgivars[paircount*2]=NULL;      /* terminate the list with NULL */
178     
179         /* Free anything that needs to be freed */
180         free(cgiinput);
181         for (i=0; pairlist[i]; i++) free(pairlist[i]);
182         free(pairlist);
183
184         /* Return the list of name-value strings */
185         return cgivars;
186 }