]> the.earth.li Git - onak.git/blob - cgi/getcgi.c
Move the CGI specific routines into cgi/ and split out UID escaping
[onak.git] / cgi / 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  *      start_html - Start HTML output.
31  *      @title: The title for the HTML.
32  *
33  *      Takes a title string and starts HTML output, including the
34  *      Content-Type header all the way up to <BODY>.
35  */
36 void start_html(const char *title)
37 {
38         puts("Content-Type: text/html; charset=UTF-8\n");
39         puts("<!DOCTYPE HTML PUBLIC '-//W3C//DTD HTML 3.2 Final//EN'>");
40         puts("<HTML>");
41         puts("<HEAD>");
42         printf("<TITLE>%s</TITLE>\n", title);
43         puts("</HEAD>");
44         puts("<BODY>");
45
46         return;
47 }
48
49 /*
50  *      end_html - End HTML output.
51  *
52  *      Ends HTML output - closes the BODY and HTML tags.
53  */
54 void end_html(void)
55 {
56         puts("</BODY>");
57         puts("</HTML>");
58
59         return;
60 }
61
62
63 /* Convert a two-char hex string into the char it represents */
64 char x2c(const char *what) 
65 {
66         register char digit;
67
68         digit = (what[0] >= 'A' ? ((what[0] & 0xdf) - 'A')+10 :
69                                         (what[0] - '0'));
70         digit *= 16;
71         digit += (what[1] >= 'A' ? ((what[1] & 0xdf) - 'A')+10 :
72                                         (what[1] - '0'));
73         
74         return(digit);
75 }
76
77 /* Reduce any %xx escape sequences to the characters they represent */
78 void unescape_url(char *url) 
79 {
80         register int i,j;
81
82         for(i=0,j=0; url[j]; ++i,++j) {
83                 if((url[i] = url[j]) == '%') {
84                         url[i]=x2c(&url[j+1]);
85                         j+=2;
86                 }
87         }
88         
89         url[i] = '\0';
90 }
91
92
93 /* Read the CGI input and place all name/val pairs into list.        */
94 /* Returns list containing name1, value1, name2, value2, ... , NULL  */
95 char **getcgivars(int argc, char *argv[]) 
96 {
97         int i;
98         char *request_method, *env;
99         int content_length, paircount;
100         char *cgiinput = NULL;
101         char **cgivars = NULL;
102         char **pairlist = NULL;
103         char *nvpair,*eqpos;
104
105         /* Depending on the request method, read all CGI input into cgiinput */
106         /* (really should produce HTML error messages, instead of exit()ing) */
107
108         request_method = getenv("REQUEST_METHOD");
109         
110         if (request_method == NULL) {
111                 if (argc > 1) {
112                         cgiinput = strdup(argv[1]);
113                 } else {
114                         return NULL;
115                 }
116         } else if (strlen(request_method)==0) {
117                 return NULL;
118         } else if (!strcmp(request_method, "GET") ||
119                         !strcmp(request_method, "HEAD")) {
120                 env = getenv("QUERY_STRING");
121                 if (env != NULL) {
122                         cgiinput = strdup(env);
123                 }
124         } else if (!strcmp(request_method, "POST")) {
125                 env = getenv("CONTENT_TYPE");
126                 if ((env != NULL) && strcasecmp(env,
127                                 "application/x-www-form-urlencoded")) {
128                         printf("getcgivars(): Unsupported Content-Type.\n");
129                         exit(1);
130                 }
131
132                 env = getenv("CONTENT_LENGTH");
133                 if ((env == NULL) || !(content_length = atoi(env))) {
134                         printf("getcgivars(): No Content-Length was sent with"
135                                         " the POST request.\n");
136                         exit(1);
137                 }
138
139                 if (!(cgiinput = (char *) malloc(content_length+1))) {
140                         printf("getcgivars(): Could not malloc for "
141                                         "cgiinput.\n");
142                         exit(1);
143                 }
144
145                 if (!fread(cgiinput, content_length, 1, stdin)) {
146                         printf("Couldn't read CGI input from STDIN.\n");
147                         exit(1);
148                 }
149                 
150                 cgiinput[content_length]='\0';
151                 
152         } else {
153                 printf("getcgivars(): unsupported REQUEST_METHOD\n");
154                 exit(1);
155         }
156
157         /* If we didn't get any cgiinput info, nothing to return */
158         if (cgiinput == NULL) {
159                 return NULL;
160         }
161
162         /* Change all plusses back to spaces */
163
164         for(i=0; cgiinput[i]; i++) if (cgiinput[i]=='+') cgiinput[i] = ' ';
165
166         /* First, split on "&" to extract the name-value pairs into pairlist */
167         pairlist= malloc(256*sizeof(char *));
168         paircount=0;
169         nvpair=strtok(cgiinput, "&");
170         while (nvpair) {
171                 pairlist[paircount++]= strdup(nvpair) ;
172                 if (!(paircount%256)) {
173                         pairlist= realloc(pairlist,
174                                         (paircount+256)*sizeof(char *));
175                 }
176                 nvpair=strtok(NULL, "&") ;
177         }
178
179         pairlist[paircount]=0;          /* terminate the list with NULL */
180
181         /* Then, from the list of pairs, extract the names and values */
182         
183         cgivars= malloc((paircount*2+1)*sizeof(char *));
184         
185         for (i=0; i<paircount; i++) {
186                 if ((eqpos=strchr(pairlist[i], '='))!=NULL) {
187                         *eqpos='\0';
188                         unescape_url(cgivars[i*2+1]=strdup(eqpos+1));
189                 } else {
190                         unescape_url(cgivars[i*2+1]=strdup(""));
191                 }
192                 unescape_url(cgivars[i*2]= strdup(pairlist[i])) ;
193         }
194
195         cgivars[paircount*2]=NULL;      /* terminate the list with NULL */
196     
197         /* Free anything that needs to be freed */
198         free(cgiinput);
199         for (i=0; pairlist[i]; i++) free(pairlist[i]);
200         free(pairlist);
201
202         /* Return the list of name-value strings */
203         return cgivars;
204 }
205
206
207 /**
208  *      cleanupcgi - free the memory allocated for our CGI parameters.
209  *      @cgivars: The CGI parameter list to free.
210  *
211  *      Frees up the elements of the CGI parameter array and then frees the
212  *      array.
213  */
214 void cleanupcgi(char **cgivars)
215 {
216         int i;
217
218         if (cgivars != NULL) {
219                 for (i = 0; cgivars[i] != NULL; i++) {
220                         free(cgivars[i]);
221                         cgivars[i] = NULL;
222                 }
223                 free(cgivars);
224                 cgivars = NULL;
225         }
226
227         return;
228 }