chiark / gitweb /
Avoid namespace clash detected by picky compilers
[disorder] / server / cgimain.c
index ec63bf8188ed0ab2e55d453263f4bbad23ef7e28..b377c8e996614c19f3744bc2a65ec60b56f9848d 100644 (file)
@@ -33,7 +33,6 @@
 #include "client.h"
 #include "sink.h"
 #include "cgi.h"
-#include "dcgi.h"
 #include "mem.h"
 #include "log.h"
 #include "configuration.h"
 #include "api-client.h"
 #include "mime.h"
 #include "printf.h"
-
-/** @brief Infer the base URL for the web interface if it's not set
- *
- * See <a href="http://tools.ietf.org/html/rfc3875">RFC 3875</a>.
- */
-static void infer_url(void) {
-  if(!config->url) {
-    const char *scheme = "http", *server, *script, *e;
-    int port;
-
-    /* Figure out the server.  'MUST' be set and we don't cope if it
-     * is not. */
-    if(!(server = getenv("SERVER_NAME")))
-      fatal(0, "SERVER_NAME is not set");
-    server = xstrdup(server);
-
-    /* Figure out the port.  'MUST' be set but we cope if it is not. */
-    if((e = getenv("SERVER_PORT")))
-      port = atoi(e);
-    else
-      port = 80;
-
-    /* Figure out path to ourselves */
-    if(!(script = getenv("SCRIPT_NAME")))
-      fatal(0, "SCRIPT_NAME is not set");
-    if(script[0] != '/')
-      fatal(0, "SCRIPT_NAME does not start with a '/'");
-    script = xstrdup(script);
-
-    if(port == 80)
-      byte_xasprintf(&config->url, "%s://%s%s",
-                    scheme, server, script);
-    else
-      byte_xasprintf(&config->url, "%s://%s:%d%s",
-                    scheme, server, port, script);
-  }
-}
+#include "dcgi.h"
+#include "url.h"
 
 int main(int argc, char **argv) {
   const char *cookie_env, *conf;
@@ -92,7 +56,8 @@ int main(int argc, char **argv) {
   if((conf = getenv("DISORDER_CONFIG"))) configfile = xstrdup(conf);
   if(getenv("DISORDER_DEBUG")) debugging = 1;
   if(config_read(0)) exit(EXIT_FAILURE);
-  infer_url();
+  if(!config->url)
+    config->url = infer_url();
   memset(&g, 0, sizeof g);
   memset(&s, 0, sizeof s);
   s.g = &g;
@@ -111,14 +76,7 @@ int main(int argc, char **argv) {
        login_cookie = cd.cookies[n].value;
     }
   }
-  /* Log in with the cookie if possible otherwise as guest */
-  if(disorder_connect_cookie(g.client, login_cookie)) {
-    disorder_cgi_error(&output, &s, "connect");
-    return 0;
-  }
-  /* If there was a cookie but it went bad, we forget it */
-  if(login_cookie && !strcmp(disorder_user(g.client), "guest"))
-    login_cookie = 0;
+  disorder_cgi_login(&s, &output);
   /* TODO RFC 3875 s8.2 recommendations e.g. concerning PATH_INFO */
   disorder_cgi(&output, &s);
   if(fclose(stdout) < 0) fatal(errno, "error closing stdout");