2 * This file is part of DisOrder.
3 * Copyright (C) 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 2 of the License, or
8 * (at your option) any later version.
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.
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
21 #include "disorder-cgi.h"
23 /** @brief Client used by CGI
25 * The caller should arrange for this to be created before any of
26 * these expansions are used (if it cannot connect then it's safe to
29 disorder_client *dcgi_client;
31 /** @brief Login cookie */
34 /** @brief Return a Cookie: header */
35 char *dcgi_cookie_header(void) {
40 memset(&u, 0, sizeof u);
42 parse_url(config->url, &u);
44 dynstr_append_string(d, "disorder=");
45 dynstr_append_string(d, dcgi_cookie);
47 /* Force browser to discard cookie */
48 dynstr_append_string(d, "disorder=none;Max-Age=0");
51 /* The default domain matches the request host, so we need not override
52 * that. But the default path only goes up to the rightmost /, which would
53 * cause the browser to expose the cookie to other CGI programs on the same
55 dynstr_append_string(d, ";Version=1;Path=");
56 /* Formally we are supposed to quote the path, since it invariably has a
57 * slash in it. However Safari does not parse quoted paths correctly, so
58 * this won't work. Fortunately nothing else seems to care about proper
59 * quoting of paths, so in practice we get with it. (See also
60 * parse_cookie() where we are liberal about cookie paths on the way back
62 dynstr_append_string(d, u.path);
65 byte_xasprintf(&s, "Set-Cookie: %s", d->vec);
69 /** @brief Log in as the current user or guest if none */
70 void dcgi_login(void) {
73 /* Junk the old connection if there is one */
75 disorder_close(dcgi_client);
76 /* Create a new connection */
77 dcgi_client = disorder_new(0);
79 if(disorder_connect_cookie(dcgi_client, dcgi_cookie)) {
80 dcgi_error("Cannot connect to server");
83 /* If there was a cookie but it went bad, we forget it */
84 if(dcgi_cookie && !strcmp(disorder_user(dcgi_client), "guest"))