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