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