cscvs to tla changeset 77
[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  * $Id: getcgi.c,v 1.5 2003/06/04 20:57:07 noodles Exp $
10  */
11
12 #include <stdio.h>
13 #include <string.h>
14 #include <stdlib.h>
15
16 #include "getcgi.h"
17
18 /**
19  *      txt2html - Takes a string and converts it to HTML.
20  *      @string: The string to HTMLize.
21  *
22  *      Takes a string and escapes any HTML entities.
23  */
24 char *txt2html(const char *string)
25 {
26         static char buf[1024];
27         char *ptr = NULL;
28         char *nextptr = NULL;
29
30         memset(buf, 0, 1024);
31
32         ptr = strchr(string, '<');
33         if (ptr != NULL) {
34                 nextptr = ptr + 1;
35                 *ptr = 0;
36                 strncpy(buf, string, 1023);
37                 strncat(buf, "&lt;", 1023 - strlen(buf));
38                 string = nextptr;
39         }
40
41         ptr = strchr(string, '>');
42         if (ptr != NULL) {
43                 nextptr = ptr + 1;
44                 *ptr = 0;
45                 strncat(buf, string, 1023 - strlen(buf));
46                 strncat(buf, "&gt;", 1023 - strlen(buf));
47                 string = nextptr;
48         }
49         
50         /*
51          * TODO: We need to while() this really as each entity may appear more
52          * than once. We need to start with & and ; as we replace with those
53          * throughout. Fuck it for the moment though; it's Easter and < & > are
54          * the most common and tend to only appear once.
55          */
56
57         strncat(buf, string, 1023 - strlen(buf));
58
59         return buf;
60 }
61
62 /*
63  *      start_html - Start HTML output.
64  *      @title: The title for the HTML.
65  *
66  *      Takes a title string and starts HTML output, including the
67  *      Content-Type header all the way up to <BODY>.
68  */
69 void start_html(const char *title)
70 {
71         puts("Content-Type: text/html; charset=UTF-8\n");
72         puts("<!DOCTYPE HTML PUBLIC '-//W3C//DTD HTML 3.2 Final//EN'>");
73         puts("<HTML>");
74         puts("<HEAD>");
75         printf("<TITLE>%s</TITLE>\n", title);
76         puts("</HEAD>");
77         puts("<BODY>");
78
79         return;
80 }
81
82 /*
83  *      end_html - End HTML output.
84  *
85  *      Ends HTML output - closes the BODY and HTML tags.
86  */
87 void end_html(void)
88 {
89         puts("</BODY>");
90         puts("</HTML>");
91
92         return;
93 }
94
95
96 /* Convert a two-char hex string into the char it represents */
97 char x2c(const char *what) 
98 {
99         register char digit;
100
101         digit = (what[0] >= 'A' ? ((what[0] & 0xdf) - 'A')+10 :
102                                         (what[0] - '0'));
103         digit *= 16;
104         digit += (what[1] >= 'A' ? ((what[1] & 0xdf) - 'A')+10 :
105                                         (what[1] - '0'));
106         
107         return(digit);
108 }
109
110 /* Reduce any %xx escape sequences to the characters they represent */
111 void unescape_url(char *url) 
112 {
113         register int i,j;
114
115         for(i=0,j=0; url[j]; ++i,++j) {
116                 if((url[i] = url[j]) == '%') {
117                         url[i]=x2c(&url[j+1]);
118                         j+=2;
119                 }
120         }
121         
122         url[i] = '\0';
123 }
124
125
126 /* Read the CGI input and place all name/val pairs into list.        */
127 /* Returns list containing name1, value1, name2, value2, ... , NULL  */
128 char **getcgivars(int argc, char *argv[]) 
129 {
130         int i;
131         char *request_method;
132         int content_length, paircount;
133         char *cgiinput = NULL;
134         char **cgivars = NULL;
135         char **pairlist = NULL;
136         char *nvpair,*eqpos;
137
138         /* Depending on the request method, read all CGI input into cgiinput */
139         /* (really should produce HTML error messages, instead of exit()ing) */
140
141         request_method = getenv("REQUEST_METHOD");
142         
143         if (request_method == NULL) {
144                 if (argc > 1) {
145                         cgiinput = strdup(argv[1]);
146                 } else {
147                         return NULL;
148                 }
149         } else if (strlen(request_method)==0) {
150                 return NULL;
151         } else if (!strcmp(request_method, "GET") ||
152                         !strcmp(request_method, "HEAD")) {
153                 cgiinput=strdup(getenv("QUERY_STRING"));
154         } else if (!strcmp(request_method, "POST")) {
155                 if (getenv("CONTENT_TYPE") != NULL &&
156                                 strcasecmp(getenv("CONTENT_TYPE"),
157                                         "application/x-www-form-urlencoded")) {
158                         printf("getcgivars(): Unsupported Content-Type.\n");
159                         exit(1);
160                 }
161                 
162                 if (!(content_length = atoi(getenv("CONTENT_LENGTH")))) {
163                         printf("getcgivars(): No Content-Length was sent with"
164                                         " the POST request.\n");
165                         exit(1);
166                 }
167                 
168                 if (!(cgiinput= (char *) malloc(content_length+1))) {
169                         printf("getcgivars(): Could not malloc for "
170                                         "cgiinput.\n");
171                         exit(1);
172                 }
173                 
174                 if (!fread(cgiinput, content_length, 1, stdin)) {
175                         printf("Couldn't read CGI input from STDIN.\n");
176                         exit(1);
177                 }
178                 
179                 cgiinput[content_length]='\0';
180                 
181         } else {
182                 printf("getcgivars(): unsupported REQUEST_METHOD\n");
183                 exit(1);
184         }
185
186         /* Change all plusses back to spaces */
187
188         for(i=0; cgiinput[i]; i++) if (cgiinput[i]=='+') cgiinput[i] = ' ';
189
190         /* First, split on "&" to extract the name-value pairs into pairlist */
191         pairlist=(char **) malloc(256*sizeof(char **));
192         paircount=0;
193         nvpair=strtok(cgiinput, "&");
194         while (nvpair) {
195                 pairlist[paircount++]= strdup(nvpair) ;
196                 if (!(paircount%256)) {
197                         pairlist=(char **) realloc(pairlist,
198                                         (paircount+256)*sizeof(char **));
199                 }
200                 nvpair=strtok(NULL, "&") ;
201         }
202
203         pairlist[paircount]=0;          /* terminate the list with NULL */
204
205         /* Then, from the list of pairs, extract the names and values */
206         
207         cgivars=(char **) malloc((paircount*2+1)*sizeof(char **));
208         
209         for (i=0; i<paircount; i++) {
210                 if ((eqpos=strchr(pairlist[i], '='))!=NULL) {
211                         *eqpos='\0';
212                         unescape_url(cgivars[i*2+1]=strdup(eqpos+1));
213                 } else {
214                         unescape_url(cgivars[i*2+1]=strdup(""));
215                 }
216                 unescape_url(cgivars[i*2]= strdup(pairlist[i])) ;
217         }
218
219         cgivars[paircount*2]=NULL;      /* terminate the list with NULL */
220     
221         /* Free anything that needs to be freed */
222         free(cgiinput);
223         for (i=0; pairlist[i]; i++) free(pairlist[i]);
224         free(pairlist);
225
226         /* Return the list of name-value strings */
227         return cgivars;
228 }
229
230
231 /**
232  *      cleanupcgi - free the memory allocated for our CGI parameters.
233  *      @cgivars: The CGI parameter list to free.
234  *
235  *      Frees up the elements of the CGI parameter array and then frees the
236  *      array.
237  */
238 void cleanupcgi(char **cgivars)
239 {
240         int i;
241
242         if (cgivars != NULL) {
243                 for (i = 0; cgivars[i] != NULL; i++) {
244                         free(cgivars[i]);
245                         cgivars[i] = NULL;
246                 }
247                 free(cgivars);
248                 cgivars = NULL;
249         }
250
251         return;
252 }