cscvs to tla changeset 37
[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         memset(buf, 0, 1024);
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 /*
61  *      start_html - Start HTML output.
62  *      @title: The title for the HTML.
63  *
64  *      Takes a title string and starts HTML output, including the
65  *      Content-Type header all the way up to <BODY>.
66  */
67 void start_html(const char *title)
68 {
69         puts("Content-Type: text/html; charset=UTF-8\n");
70         puts("<!DOCTYPE HTML PUBLIC '-//W3C//DTD HTML 3.2 Final//EN'>");
71         puts("<HTML>");
72         puts("<HEAD>");
73         printf("<TITLE>%s</TITLE>\n", title);
74         puts("</HEAD>");
75         puts("<BODY>");
76
77         return;
78 }
79
80 /*
81  *      end_html - End HTML output.
82  *
83  *      Ends HTML output - closes the BODY and HTML tags.
84  */
85 void end_html(void)
86 {
87         puts("</BODY>");
88         puts("</HTML>");
89
90         return;
91 }
92
93
94 /* Convert a two-char hex string into the char it represents */
95 char x2c(const char *what) 
96 {
97         register char digit;
98
99         digit = (what[0] >= 'A' ? ((what[0] & 0xdf) - 'A')+10 :
100                                         (what[0] - '0'));
101         digit *= 16;
102         digit += (what[1] >= 'A' ? ((what[1] & 0xdf) - 'A')+10 :
103                                         (what[1] - '0'));
104         
105         return(digit);
106 }
107
108 /* Reduce any %xx escape sequences to the characters they represent */
109 void unescape_url(char *url) 
110 {
111         register int i,j;
112
113         for(i=0,j=0; url[j]; ++i,++j) {
114                 if((url[i] = url[j]) == '%') {
115                         url[i]=x2c(&url[j+1]);
116                         j+=2;
117                 }
118         }
119         
120         url[i] = '\0';
121 }
122
123
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[]) 
127 {
128         int i;
129         char *request_method;
130         int content_length, paircount;
131         char *cgiinput = NULL;
132         char **cgivars = NULL;
133         char **pairlist = NULL;
134         char *nvpair,*eqpos;
135
136         /* Depending on the request method, read all CGI input into cgiinput */
137         /* (really should produce HTML error messages, instead of exit()ing) */
138
139         request_method = getenv("REQUEST_METHOD");
140         
141         if (request_method == NULL) {
142                 if (argc > 1) {
143                         cgiinput = strdup(argv[1]);
144                 } else {
145                         return NULL;
146                 }
147         } else if (strlen(request_method)==0) {
148                 return NULL;
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");
157                         exit(1);
158                 }
159                 
160                 if (!(content_length = atoi(getenv("CONTENT_LENGTH")))) {
161                         printf("getcgivars(): No Content-Length was sent with"
162                                         " the POST request.\n");
163                         exit(1);
164                 }
165                 
166                 if (!(cgiinput= (char *) malloc(content_length+1))) {
167                         printf("getcgivars(): Could not malloc for "
168                                         "cgiinput.\n");
169                         exit(1);
170                 }
171                 
172                 if (!fread(cgiinput, content_length, 1, stdin)) {
173                         printf("Couldn't read CGI input from STDIN.\n");
174                         exit(1);
175                 }
176                 
177                 cgiinput[content_length]='\0';
178                 
179         } else {
180                 printf("getcgivars(): unsupported REQUEST_METHOD\n");
181                 exit(1);
182         }
183
184         /* Change all plusses back to spaces */
185
186         for(i=0; cgiinput[i]; i++) if (cgiinput[i]=='+') cgiinput[i] = ' ';
187
188         /* First, split on "&" to extract the name-value pairs into pairlist */
189         pairlist=(char **) malloc(256*sizeof(char **));
190         paircount=0;
191         nvpair=strtok(cgiinput, "&");
192         while (nvpair) {
193                 pairlist[paircount++]= strdup(nvpair) ;
194                 if (!(paircount%256)) {
195                         pairlist=(char **) realloc(pairlist,
196                                         (paircount+256)*sizeof(char **));
197                 }
198                 nvpair=strtok(NULL, "&") ;
199         }
200
201         pairlist[paircount]=0;          /* terminate the list with NULL */
202
203         /* Then, from the list of pairs, extract the names and values */
204         
205         cgivars=(char **) malloc((paircount*2+1)*sizeof(char **));
206         
207         for (i=0; i<paircount; i++) {
208                 if ((eqpos=strchr(pairlist[i], '='))!=NULL) {
209                         *eqpos='\0';
210                         unescape_url(cgivars[i*2+1]=strdup(eqpos+1));
211                 } else {
212                         unescape_url(cgivars[i*2+1]=strdup(""));
213                 }
214                 unescape_url(cgivars[i*2]= strdup(pairlist[i])) ;
215         }
216
217         cgivars[paircount*2]=NULL;      /* terminate the list with NULL */
218     
219         /* Free anything that needs to be freed */
220         free(cgiinput);
221         for (i=0; pairlist[i]; i++) free(pairlist[i]);
222         free(pairlist);
223
224         /* Return the list of name-value strings */
225         return cgivars;
226 }
227
228
229 /**
230  *      cleanupcgi - free the memory allocated for our CGI parameters.
231  *      @cgivars: The CGI parameter list to free.
232  *
233  *      Frees up the elements of the CGI parameter array and then frees the
234  *      array.
235  */
236 void cleanupcgi(char **cgivars)
237 {
238         int i;
239
240         if (cgivars != NULL) {
241                 for (i = 0; cgivars[i] != NULL; i++) {
242                         free(cgivars[i]);
243                         cgivars[i] = NULL;
244                 }
245                 free(cgivars);
246                 cgivars = NULL;
247         }
248
249         return;
250 }