chiark / gitweb /
split out dcgi_get_cookie
[disorder] / server / cgimain.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 server/cgimain.c
21  * @brief DisOrder CGI
22  */
23
24 #include "disorder-cgi.h"
25
26 int main(int argc, char **argv) {
27   const char *conf;
28
29   if(argc > 0)
30     progname = argv[0];
31   /* RFC 3875 s8.2 recommends rejecting PATH_INFO if we don't make use of
32    * it. */
33   /* TODO we could make disorder/ACTION equivalent to disorder?action=ACTION */
34   if(getenv("PATH_INFO")) {
35     /* TODO it might be nice to link back to the right place... */
36     printf("Content-Type: text/html\n");
37     printf("Status: 404\n");
38     printf("\n");
39     printf("<p>Sorry, PATH_INFO not supported.</p>\n");
40     exit(0);
41   }
42   /* Parse CGI arguments */
43   cgi_init();
44   /* We allow various things to be overridden from the environment.  This is
45    * intended for debugging and is not a documented feature. */
46   if((conf = getenv("DISORDER_CONFIG")))
47     configfile = xstrdup(conf);
48   if(getenv("DISORDER_DEBUG"))
49     debugging = 1;
50   if(config_read(0))
51     exit(EXIT_FAILURE);
52   /* Figure out our URL.  This can still be overridden from the config file if
53    * necessary but it shouldn't be necessary in ordinary installations. */
54   if(!config->url)
55     config->url = infer_url();
56   /* Pick up the cookie, if there is one */
57   dcgi_get_cookie();
58   /* Register expansions */
59   mx_register_builtin();
60   dcgi_expansions();
61   /* Update search path.  We look in the config directory first and the data
62    * directory second, so that the latter overrides the former. */
63   mx_search_path(pkgconfdir);
64   mx_search_path(pkgdatadir);
65   /* Never cache anythging */
66   if(printf("Cache-Control: no-cache\n") < 0)
67     fatal(errno, "error writing to stdout");
68   /* Create the initial connection, trying the cookie if we found a suitable
69    * one. */
70   dcgi_login();
71   /* The main program... */
72   dcgi_action(NULL);
73   /* In practice if a write fails that probably means the web server went away,
74    * but we log it anyway. */
75   if(fclose(stdout) < 0)
76     fatal(errno, "error closing stdout");
77   return 0;
78 }
79
80 /*
81 Local Variables:
82 c-basic-offset:2
83 comment-column:40
84 fill-column:79
85 indent-tabs-mode:nil
86 End:
87 */