]> the.earth.li Git - onak.git/blob - getcgi.c
Bump debhelper compat level to 13
[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;
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                 cgiinput=strdup(getenv("QUERY_STRING"));
165         } else if (!strcmp(request_method, "POST")) {
166                 if (getenv("CONTENT_TYPE") != NULL &&
167                                 strcasecmp(getenv("CONTENT_TYPE"),
168                                         "application/x-www-form-urlencoded")) {
169                         printf("getcgivars(): Unsupported Content-Type.\n");
170                         exit(1);
171                 }
172                 
173                 if (!(content_length = atoi(getenv("CONTENT_LENGTH")))) {
174                         printf("getcgivars(): No Content-Length was sent with"
175                                         " the POST request.\n");
176                         exit(1);
177                 }
178                 
179                 if (!(cgiinput= (char *) malloc(content_length+1))) {
180                         printf("getcgivars(): Could not malloc for "
181                                         "cgiinput.\n");
182                         exit(1);
183                 }
184                 
185                 if (!fread(cgiinput, content_length, 1, stdin)) {
186                         printf("Couldn't read CGI input from STDIN.\n");
187                         exit(1);
188                 }
189                 
190                 cgiinput[content_length]='\0';
191                 
192         } else {
193                 printf("getcgivars(): unsupported REQUEST_METHOD\n");
194                 exit(1);
195         }
196
197         /* Change all plusses back to spaces */
198
199         for(i=0; cgiinput[i]; i++) if (cgiinput[i]=='+') cgiinput[i] = ' ';
200
201         /* First, split on "&" to extract the name-value pairs into pairlist */
202         pairlist= malloc(256*sizeof(char *));
203         paircount=0;
204         nvpair=strtok(cgiinput, "&");
205         while (nvpair) {
206                 pairlist[paircount++]= strdup(nvpair) ;
207                 if (!(paircount%256)) {
208                         pairlist= realloc(pairlist,
209                                         (paircount+256)*sizeof(char *));
210                 }
211                 nvpair=strtok(NULL, "&") ;
212         }
213
214         pairlist[paircount]=0;          /* terminate the list with NULL */
215
216         /* Then, from the list of pairs, extract the names and values */
217         
218         cgivars= malloc((paircount*2+1)*sizeof(char *));
219         
220         for (i=0; i<paircount; i++) {
221                 if ((eqpos=strchr(pairlist[i], '='))!=NULL) {
222                         *eqpos='\0';
223                         unescape_url(cgivars[i*2+1]=strdup(eqpos+1));
224                 } else {
225                         unescape_url(cgivars[i*2+1]=strdup(""));
226                 }
227                 unescape_url(cgivars[i*2]= strdup(pairlist[i])) ;
228         }
229
230         cgivars[paircount*2]=NULL;      /* terminate the list with NULL */
231     
232         /* Free anything that needs to be freed */
233         free(cgiinput);
234         for (i=0; pairlist[i]; i++) free(pairlist[i]);
235         free(pairlist);
236
237         /* Return the list of name-value strings */
238         return cgivars;
239 }
240
241
242 /**
243  *      cleanupcgi - free the memory allocated for our CGI parameters.
244  *      @cgivars: The CGI parameter list to free.
245  *
246  *      Frees up the elements of the CGI parameter array and then frees the
247  *      array.
248  */
249 void cleanupcgi(char **cgivars)
250 {
251         int i;
252
253         if (cgivars != NULL) {
254                 for (i = 0; cgivars[i] != NULL; i++) {
255                         free(cgivars[i]);
256                         cgivars[i] = NULL;
257                 }
258                 free(cgivars);
259                 cgivars = NULL;
260         }
261
262         return;
263 }