chiark / gitweb /
lib/addr.c, etc.: Return plain addresses from `netaddress_resolve'.
[disorder] / cgi / cgimain.c
CommitLineData
460b9539 1/*
2 * This file is part of DisOrder.
ac169f8a 3 * Copyright (C) 2004, 2005, 2007, 2008 Richard Kettlewell
460b9539 4 *
e7eb3a27 5 * This program is free software: you can redistribute it and/or modify
460b9539 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
460b9539 8 * (at your option) any later version.
9 *
e7eb3a27
RK
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 *
460b9539 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/>.
460b9539 17 */
59cf25c4 18/** @file cgi/cgimain.c
9faa7a88
RK
19 * @brief DisOrder CGI
20 */
460b9539 21
1e97629d 22#include "disorder-cgi.h"
9faa7a88 23
460b9539 24int main(int argc, char **argv) {
d0b6635e 25 const char *conf;
460b9539 26
9faa7a88
RK
27 if(argc > 0)
28 progname = argv[0];
ad9bae0b 29 /* RFC 3875 s8.2 recommends rejecting PATH_INFO if we don't make use of
30 * it. */
c8d29a75 31 if(!setlocale(LC_CTYPE, "")) disorder_error(errno, "error calling setlocale");
d0b6635e 32 /* TODO we could make disorder/ACTION equivalent to disorder?action=ACTION */
ad9bae0b 33 if(getenv("PATH_INFO")) {
9faa7a88 34 /* TODO it might be nice to link back to the right place... */
10921eba 35 printf("Content-Type: text/html; charset=UTF-8\n");
ad9bae0b 36 printf("Status: 404\n");
37 printf("\n");
cc5b0a8e
RK
38 printf("<p>Sorry, is PATH_INFO not supported."
39 "<a href=\"%s\">Try here instead.</a></p>\n",
40 cgi_sgmlquote(infer_url(0/*!include_path_info*/)));
ad9bae0b 41 exit(0);
42 }
d0b6635e 43 /* Parse CGI arguments */
71634563 44 cgi_init();
9faa7a88
RK
45 /* We allow various things to be overridden from the environment. This is
46 * intended for debugging and is not a documented feature. */
47 if((conf = getenv("DISORDER_CONFIG")))
48 configfile = xstrdup(conf);
49 if(getenv("DISORDER_DEBUG"))
50 debugging = 1;
2257512d 51 /* Read configuration */
02ba7921 52 if(config_read(0/*!server*/, NULL))
9faa7a88
RK
53 exit(EXIT_FAILURE);
54 /* Figure out our URL. This can still be overridden from the config file if
55 * necessary but it shouldn't be necessary in ordinary installations. */
36bde473 56 if(!config->url)
cc5b0a8e 57 config->url = infer_url(1/*include_path_info*/);
d0b6635e
RK
58 /* Pick up the cookie, if there is one */
59 dcgi_get_cookie();
9faa7a88
RK
60 /* Register expansions */
61 mx_register_builtin();
1e97629d 62 dcgi_expansions();
9faa7a88
RK
63 /* Update search path. We look in the config directory first and the data
64 * directory second, so that the latter overrides the former. */
65 mx_search_path(pkgconfdir);
66 mx_search_path(pkgdatadir);
bca4e2b7 67 /* Never cache anythging */
71634563 68 if(printf("Cache-Control: no-cache\n") < 0)
2e9ba080 69 disorder_fatal(errno, "error writing to stdout");
9faa7a88
RK
70 /* Create the initial connection, trying the cookie if we found a suitable
71 * one. */
1e97629d 72 dcgi_login();
2257512d 73 /* Do whatever the user wanted */
1e97629d 74 dcgi_action(NULL);
9faa7a88
RK
75 /* In practice if a write fails that probably means the web server went away,
76 * but we log it anyway. */
77 if(fclose(stdout) < 0)
2e9ba080 78 disorder_fatal(errno, "error closing stdout");
460b9539 79 return 0;
80}
81
82/*
83Local Variables:
84c-basic-offset:2
85comment-column:40
ac169f8a 86fill-column:79
87indent-tabs-mode:nil
460b9539 88End:
89*/