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