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