2 * This file is part of DisOrder.
3 * Copyright (C) 2004, 2005, 2007, 2008 Richard Kettlewell
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 3 of the License, or
8 * (at your option) any later version.
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.
15 * You should have received a copy of the GNU General Public License
16 * along with this program. If not, see <http://www.gnu.org/licenses/>.
37 /** @brief Hash of arguments */
38 static hash *cgi_args;
40 /** @brief Get CGI arguments from a GET request's query string */
41 static struct kvp *cgi__init_get(void) {
44 if((q = getenv("QUERY_STRING")))
45 return kvp_urldecode(q, strlen(q));
46 disorder_error(0, "QUERY_STRING not set, assuming empty");
50 /** @brief Read the HTTP request body */
51 static void cgi__input(char **ptrp, size_t *np) {
57 if(!(cl = getenv("CONTENT_LENGTH")))
58 disorder_fatal(0, "CONTENT_LENGTH not set");
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)
63 disorder_fatal(0, "input is much too large");
64 q = xmalloc_noptr(n + 1);
66 r = read(0, q + m, n - m);
70 disorder_fatal(0, "unexpected end of file reading request body");
73 default: disorder_fatal(errno, "error reading request body");
77 disorder_fatal(0, "null character in request body");
84 /** @brief Called for each part header field (see cgi__part_callback()) */
85 static int cgi__field_callback(const char *name, const char *value,
87 char *disposition, *pname, *pvalue;
90 if(!strcmp(name, "content-disposition")) {
91 if(mime_rfc2388_content_disposition(value,
95 disorder_fatal(0, "error parsing Content-Disposition field");
96 if(!strcmp(disposition, "form-data")
98 && !strcmp(pname, "name")) {
100 disorder_fatal(0, "duplicate Content-Disposition field");
107 /** @brief Called for each part (see cgi__init_multipart()) */
108 static int cgi__part_callback(const char *s,
111 struct kvp *k, **head = u;
113 if(!(s = mime_parse(s, cgi__field_callback, &name)))
114 disorder_fatal(0, "error parsing part header");
116 disorder_fatal(0, "no name found");
117 k = xmalloc(sizeof *k);
125 /** @brief Initialize CGI arguments from a multipart/form-data request body */
126 static struct kvp *cgi__init_multipart(const char *boundary) {
128 struct kvp *head = 0;
131 if(mime_multipart(q, cgi__part_callback, boundary, &head))
132 disorder_fatal(0, "invalid multipart object");
136 /** @brief Initialize CGI arguments from a POST request */
137 static struct kvp *cgi__init_post(void) {
138 const char *ct, *boundary;
143 if(!(ct = getenv("CONTENT_TYPE")))
144 ct = "application/x-www-form-urlencoded";
145 if(mime_content_type(ct, &type, &k))
146 disorder_fatal(0, "invalid content type '%s'", ct);
147 if(!strcmp(type, "application/x-www-form-urlencoded")) {
149 return kvp_urldecode(q, n);
151 if(!strcmp(type, "multipart/form-data")) {
152 if(!(boundary = kvp_get(k, "boundary")))
153 disorder_fatal(0, "no boundary parameter found");
154 return cgi__init_multipart(boundary);
156 disorder_fatal(0, "unrecognized content type '%s'", type);
159 /** @brief Initialize CGI arguments
161 * Must be called before other cgi_ functions are used.
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.
168 void cgi_init(void) {
172 cgi_args = hash_new(sizeof (char *));
173 if(!(p = getenv("REQUEST_METHOD")))
174 disorder_error(0, "REQUEST_METHOD not set, assuming GET");
175 if(!p || !strcmp(p, "GET"))
177 else if(!strcmp(p, "POST"))
178 k = cgi__init_post();
180 disorder_fatal(0, "unknown request method %s", p);
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)))
185 disorder_error(0, "invalid UTF-8 sequence in cgi argument %s", k->name);
187 hash_add(cgi_args, k->name, &k->value, HASH_INSERT_OR_REPLACE);
188 /* We just drop bogus arguments. */
192 /** @brief Get a CGI argument by name
194 * cgi_init() must be called first. Names and values are all valid
195 * UTF-8 strings (and this is enforced at initialization time).
197 const char *cgi_get(const char *name) {
198 const char **v = hash_find(cgi_args, name);
200 return v ? *v : NULL;
203 /** @brief Set a CGI argument */
204 void cgi_set(const char *name, const char *value) {
205 value = xstrdup(value);
206 hash_add(cgi_args, name, &value, HASH_INSERT_OR_REPLACE);
209 /** @brief Clear CGI arguments */
210 void cgi_clear(void) {
211 cgi_args = hash_new(sizeof (char *));
214 /** @brief Add SGML-style quoting
215 * @param src String to quote (UTF-8)
216 * @return Quoted string
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).
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
227 char *cgi_sgmlquote(const char *src) {
232 if(!(ucs = utf8_to_utf32(src, strlen(src), 0)))
236 /* format the string */
237 while((c = *ucs++)) {
240 if(c > 126 || c < 32) {
245 /* For simplicity we always use numeric character references
246 * even if a named reference is available. */
247 sink_printf(s, "&#%"PRIu32";", c);
250 sink_writec(s, (char)c);
257 /** @brief Construct a URL
258 * @param url Base URL
259 * @param ... Name/value pairs for constructed query string
260 * @return Constructed URL
262 * The name/value pair list is terminated by a single (char *)0.
264 char *cgi_makeurl(const char *url, ...) {
266 struct kvp *kvp, *k, **kk = &kvp;
271 dynstr_append_string(&d, url);
273 while((n = va_arg(ap, const char *))) {
274 v = va_arg(ap, const char *);
275 *kk = k = xmalloc(sizeof *k);
283 dynstr_append(&d, '?');
284 dynstr_append_string(&d, kvp_urlencode(kvp, 0));
286 dynstr_terminate(&d);
290 /** @brief Construct a URL from current parameters
291 * @param url Base URL
292 * @return Constructed URL
294 char *cgi_thisurl(const char *url) {
296 char **keys = hash_keys(cgi_args);
300 dynstr_append_string(d, url);
301 for(n = 0; keys[n]; ++n) {
302 dynstr_append(d, n ? '&' : '?');
303 dynstr_append_string(d, urlencodestring(keys[n]));
304 dynstr_append(d, '=');
305 dynstr_append_string(d, urlencodestring(cgi_get(keys[n])));