chiark / gitweb /
Shift many cgi_ functions into lib/, and add a test for them. The
[disorder] / lib / cgi.c
1 /*
2  * This file is part of DisOrder.
3  * Copyright (C) 2004, 2005, 2007, 2008 Richard Kettlewell
4  *
5  * This program is free software; you can redistribute it and/or modify
6  * it under the terms of the GNU General Public License as published by
7  * the Free Software Foundation; either version 2 of the License, or
8  * (at your option) any later version.
9  *
10  * This program is distributed in the hope that it will be useful, but
11  * WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
13  * General Public License for more details.
14  *
15  * You should have received a copy of the GNU General Public License
16  * along with this program; if not, write to the Free Software
17  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
18  * USA
19  */
20 /** @file lib/cgi.c
21  * @brief CGI tools
22  */
23
24 #include <config.h>
25 #include "types.h"
26
27 #include <stdlib.h>
28 #include <string.h>
29 #include <assert.h>
30 #include <unistd.h>
31 #include <errno.h>
32 #include <stdio.h>
33
34 #include "cgi.h"
35 #include "mem.h"
36 #include "log.h"
37 #include "vector.h"
38 #include "hash.h"
39 #include "kvp.h"
40 #include "mime.h"
41 #include "unicode.h"
42 #include "sink.h"
43
44 /** @brief Hash of arguments */
45 static hash *cgi_args;
46
47 /** @brief Get CGI arguments from a GET request's query string */
48 static struct kvp *cgi__init_get(void) {
49   const char *q;
50
51   if((q = getenv("QUERY_STRING")))
52     return kvp_urldecode(q, strlen(q));
53   error(0, "QUERY_STRING not set, assuming empty");
54   return NULL;
55 }
56
57 /** @brief Read the HTTP request body */
58 static void cgi__input(char **ptrp, size_t *np) {
59   const char *cl;
60   char *q;
61   size_t n, m = 0;
62   int r;
63
64   if(!(cl = getenv("CONTENT_LENGTH")))
65     fatal(0, "CONTENT_LENGTH not set");
66   n = atol(cl);
67   /* We check for overflow and also limit the input to 16MB. Lower
68    * would probably do.  */
69   if(!(n+1) || n > 16 * 1024 * 1024)
70     fatal(0, "input is much too large");
71   q = xmalloc_noptr(n + 1);
72   while(m < n) {
73     r = read(0, q + m, n - m);
74     if(r > 0)
75       m += r;
76     else if(r == 0)
77       fatal(0, "unexpected end of file reading request body");
78     else switch(errno) {
79     case EINTR: break;
80     default: fatal(errno, "error reading request body");
81     }
82   }
83   if(memchr(q, 0, n))
84     fatal(0, "null character in request body");
85   q[n + 1] = 0;
86   *ptrp = q;
87   if(np)
88     *np = n;
89 }
90
91 /** @brief Called for each part header field (see cgi__part_callback()) */
92 static int cgi__field_callback(const char *name, const char *value,
93                                void *u) {
94   char *disposition, *pname, *pvalue;
95   char **namep = u;
96
97   if(!strcmp(name, "content-disposition")) {
98     if(mime_rfc2388_content_disposition(value,
99                                         &disposition,
100                                         &pname,
101                                         &pvalue))
102       fatal(0, "error parsing Content-Disposition field");
103     if(!strcmp(disposition, "form-data")
104        && pname
105        && !strcmp(pname, "name")) {
106       if(*namep)
107         fatal(0, "duplicate Content-Disposition field");
108       *namep = pvalue;
109     }
110   }
111   return 0;
112 }
113
114 /** @brief Called for each part (see cgi__init_multipart()) */
115 static int cgi__part_callback(const char *s,
116                               void *u) {
117   char *name = 0;
118   struct kvp *k, **head = u;
119   
120   if(!(s = mime_parse(s, cgi__field_callback, &name)))
121     fatal(0, "error parsing part header");
122   if(!name)
123     fatal(0, "no name found");
124   k = xmalloc(sizeof *k);
125   k->next = *head;
126   k->name = name;
127   k->value = s;
128   *head = k;
129   return 0;
130 }
131
132 /** @brief Initialize CGI arguments from a multipart/form-data request body */
133 static struct kvp *cgi__init_multipart(const char *boundary) {
134   char *q;
135   struct kvp *head = 0;
136   
137   cgi__input(&q, 0);
138   if(mime_multipart(q, cgi__part_callback, boundary, &head))
139     fatal(0, "invalid multipart object");
140   return head;
141 }
142
143 /** @brief Initialize CGI arguments from a POST request */
144 static struct kvp *cgi__init_post(void) {
145   const char *ct, *boundary;
146   char *q, *type;
147   size_t n;
148   struct kvp *k;
149
150   if(!(ct = getenv("CONTENT_TYPE")))
151     ct = "application/x-www-form-urlencoded";
152   if(mime_content_type(ct, &type, &k))
153     fatal(0, "invalid content type '%s'", ct);
154   if(!strcmp(type, "application/x-www-form-urlencoded")) {
155     cgi__input(&q, &n);
156     return kvp_urldecode(q, n);
157   }
158   if(!strcmp(type, "multipart/form-data")) {
159     if(!(boundary = kvp_get(k, "boundary")))
160       fatal(0, "no boundary parameter found");
161     return cgi__init_multipart(boundary);
162   }
163   fatal(0, "unrecognized content type '%s'", type);
164 }
165
166 /** @brief Initialize CGI arguments
167  *
168  * Must be called before other cgi_ functions are used.
169  *
170  * This function can be called more than once, in which case it
171  * revisits the environment and (perhaps) standard input.  This is
172  * only intended to be used for testing, actual CGI applications
173  * should call it exactly once.
174  */
175 void cgi_init(void) {
176   const char *p;
177   struct kvp *k;
178
179   cgi_args = hash_new(sizeof (char *));
180   if(!(p = getenv("REQUEST_METHOD")))
181     error(0, "REQUEST_METHOD not set, assuming GET");
182   if(!p || !strcmp(p, "GET"))
183     k = cgi__init_get();
184   else if(!strcmp(p, "POST"))
185     k = cgi__init_post();
186   else
187     fatal(0, "unknown request method %s", p);
188   /* Validate the arguments and put them in a hash */
189   for(; k; k = k->next) {
190     if(!utf8_valid(k->name, strlen(k->name))
191        || !utf8_valid(k->value, strlen(k->value)))
192       error(0, "invalid UTF-8 sequence in cgi argument %s", k->name);
193     else
194       hash_add(cgi_args, k->name, &k->value, HASH_INSERT_OR_REPLACE);
195     /* We just drop bogus arguments. */
196   }
197 }
198
199 /** @brief Get a CGI argument by name
200  *
201  * cgi_init() must be called first.  Names and values are all valid
202  * UTF-8 strings (and this is enforced at initialization time).
203  */
204 const char *cgi_get(const char *name) {
205   const char **v = hash_find(cgi_args, name);
206
207   return v ? *v : NULL;
208 }
209
210 /** @brief Add SGML-style quoting
211  * @param src String to quote (UTF-8)
212  * @return Quoted string
213  *
214  * Quotes characters for insertion into HTML output.  Anything that is
215  * not a printable ASCII character will be converted to a numeric
216  * character references, as will '"', '&', '<' and '>' (since those
217  * have special meanings).
218  *
219  * Quoting everything down to ASCII means we don't care what the
220  * content encoding really is (as long as it's not anything insane
221  * like EBCDIC).
222  */
223 char *cgi_sgmlquote(const char *src) {
224   uint32_t *ucs, c;
225   int n;
226   struct dynstr d[1];
227   struct sink *s;
228
229   if(!(ucs = utf8_to_utf32(src, strlen(src), 0)))
230     exit(1);
231   dynstr_init(d);
232   s = sink_dynstr(d);
233   n = 1;
234   /* format the string */
235   while((c = *ucs++)) {
236     switch(c) {
237     default:
238       if(c > 126 || c < 32) {
239       case '"':
240       case '&':
241       case '<':
242       case '>':
243         /* For simplicity we always use numeric character references
244          * even if a named reference is available. */
245         sink_printf(s, "&#%"PRIu32";", c);
246         break;
247       } else
248         sink_writec(s, (char)c);
249     }
250   }
251   dynstr_terminate(d);
252   return d->vec;
253 }
254
255 /** @brief Write a CGI attribute
256  * @param output Where to send output
257  * @param name Attribute name
258  * @param value Attribute value
259  */
260 void cgi_attr(struct sink *output, const char *name, const char *value) {
261   /* Try to avoid needless quoting */
262   if(!value[strspn(value, "abcdefghijklmnopqrstuvwxyz"
263                    "ABCDEFGHIJKLMNOPQRSTUVWXYZ"
264                    "0123456789")])
265     sink_printf(output, "%s=%s", name, value);
266   else
267     sink_printf(output, "%s=\"%s\"", name, cgi_sgmlquote(value));
268 }
269
270 /** @brief Write an open tag
271  * @param output Where to send output
272  * @param name Element name
273  * @param ... Attribute name/value pairs
274  *
275  * The name/value pair list is terminated by a single (char *)0.
276  */
277 void cgi_opentag(struct sink *output, const char *name, ...) {
278   va_list ap;
279   const char *n, *v;
280    
281   sink_printf(output, "<%s", name);
282   va_start(ap, name);
283   while((n = va_arg(ap, const char *))) {
284     sink_printf(output, " ");
285     v = va_arg(ap, const char *);
286     if(v)
287       cgi_attr(output, n, v);
288     else
289       sink_printf(output, n);
290   }
291   va_end(ap);
292   sink_printf(output, ">");
293 }
294
295 /** @brief Write a close tag
296  * @param output Where to send output
297  * @param name Element name
298  */
299 void cgi_closetag(struct sink *output, const char *name) {
300   sink_printf(output, "</%s>", name);
301 }
302
303 /** @brief Construct a URL
304  * @param url Base URL
305  * @param ... Name/value pairs for constructed query string
306  * @return Constructed URL
307  *
308  * The name/value pair list is terminated by a single (char *)0.
309  */
310 char *cgi_makeurl(const char *url, ...) {
311   va_list ap;
312   struct kvp *kvp, *k, **kk = &kvp;
313   struct dynstr d;
314   const char *n, *v;
315   
316   dynstr_init(&d);
317   dynstr_append_string(&d, url);
318   va_start(ap, url);
319   while((n = va_arg(ap, const char *))) {
320     v = va_arg(ap, const char *);
321     *kk = k = xmalloc(sizeof *k);
322     kk = &k->next;
323     k->name = n;
324     k->value = v;
325   }
326   va_end(ap);
327   *kk = 0;
328   if(kvp) {
329     dynstr_append(&d, '?');
330     dynstr_append_string(&d, kvp_urlencode(kvp, 0));
331   }
332   dynstr_terminate(&d);
333   return d.vec;
334 }
335
336 /*
337 Local Variables:
338 c-basic-offset:2
339 comment-column:40
340 fill-column:79
341 indent-tabs-mode:nil
342 End:
343 */