* Mercilessly ripped out all of the gratuitously complex GNU libintl
[citadel.git] / webcit / webserver.c
index 70078b4c61e1f74960b8a834c4362eff746adde0..04ac7c3a3872604583c42fcb6a54e6710c767fb5 100644 (file)
@@ -7,39 +7,7 @@
  *
  */
 
-/*
- * Uncomment to dump an HTTP trace to stderr
-#define HTTP_TRACING 1
- */
 
-#include <ctype.h>
-#include <stdlib.h>
-#ifdef HAVE_UNISTD_H
-#include <unistd.h>
-#endif
-#include <stdio.h>
-#ifdef HAVE_FCNTL_H
-#include <fcntl.h>
-#endif
-#include <signal.h>
-#include <sys/types.h>
-#include <sys/wait.h>
-#include <sys/socket.h>
-#ifdef HAVE_SYS_TIME_H
-#include <sys/time.h>
-#endif
-#ifdef HAVE_LIMITS_H
-#include <limits.h>
-#endif
-#include <netinet/in.h>
-#include <arpa/inet.h>
-#include <netdb.h>
-#include <string.h>
-#include <pwd.h>
-#include <errno.h>
-#include <stdarg.h>
-#include <pthread.h>
-#include <signal.h>
 #include "webcit.h"
 #include "webserver.h"
 
@@ -50,6 +18,7 @@ int vsnprintf(char *buf, size_t max, const char *fmt, va_list argp);
 int verbosity = 9;             /* Logging level */
 int msock;                     /* master listening socket */
 int is_https = 0;              /* Nonzero if I am an HTTPS service */
+int follow_xff = 0;            /* Follow X-Forwarded-For: header */
 extern void *context_loop(int);
 extern void *housekeeping_loop(void);
 extern pthread_mutex_t SessionListMutex;
@@ -318,11 +287,11 @@ int client_read(int sock, char *buf, int bytes)
 
 
 /*
- * client_gets()   ...   Get a LF-terminated line of text from the client.
+ * client_getln()   ...   Get a LF-terminated line of text from the client.
  * (This is implemented in terms of client_read() and could be
  * justifiably moved out of sysdep.c)
  */
-int client_gets(int sock, char *buf)
+int client_getln(int sock, char *buf, int bufsiz)
 {
        int i, retval;
 
@@ -330,13 +299,13 @@ int client_gets(int sock, char *buf)
         */
        for (i = 0;; i++) {
                retval = client_read(sock, &buf[i], 1);
-               if (retval != 1 || buf[i] == '\n' || i == 255)
+               if (retval != 1 || buf[i] == '\n' || i == (bufsiz-1))
                        break;
        }
 
        /* If we got a long line, discard characters until the newline.
         */
-       if (i == 255)
+       if (i == (bufsiz-1))
                while (buf[i] != '\n' && retval == 1)
                        retval = client_read(sock, &buf[i], 1);
 
@@ -412,22 +381,30 @@ int main(int argc, char **argv)
        int port = PORT_NUM;    /* Port to listen on */
        char tracefile[PATH_MAX];
        char ip_addr[256];
+       char *webcitdir = WEBCITDIR;
+#ifdef ENABLE_NLS
+       char *locale = NULL;
+       char *mo = NULL;
+#endif /* ENABLE_NLS */
 
        /* Parse command line */
 #ifdef HAVE_OPENSSL
-       while ((a = getopt(argc, argv, "hi:p:t:x:cs")) != EOF)
+       while ((a = getopt(argc, argv, "h:i:p:t:x:cfs")) != EOF)
 #else
-       while ((a = getopt(argc, argv, "hi:p:t:x:c")) != EOF)
+       while ((a = getopt(argc, argv, "h:i:p:t:x:cf")) != EOF)
 #endif
                switch (a) {
+               case 'h':
+                       webcitdir = strdup(optarg);
+                       break;
                case 'i':
-                       strcpy(ip_addr, optarg);
+                       safestrncpy(ip_addr, optarg, sizeof ip_addr);
                        break;
                case 'p':
                        port = atoi(optarg);
                        break;
                case 't':
-                       strcpy(tracefile, optarg);
+                       safestrncpy(tracefile, optarg, sizeof tracefile);
                        freopen(tracefile, "w", stdout);
                        freopen(tracefile, "w", stderr);
                        freopen(tracefile, "r", stdin);
@@ -435,11 +412,15 @@ int main(int argc, char **argv)
                case 'x':
                        verbosity = atoi(optarg);
                        break;
+               case 'f':
+                       follow_xff = 1;
+                       break;
                case 'c':
-                       server_cookie = malloc(SIZ);
+                       server_cookie = malloc(256);
                        if (server_cookie != NULL) {
-                               strcpy(server_cookie,
-                                      "Set-cookie: wcserver=");
+                               safestrncpy(server_cookie,
+                                      "Set-cookie: wcserver=",
+                                       256);
                                if (gethostname
                                    (&server_cookie[strlen(server_cookie)],
                                     200) != 0) {
@@ -455,7 +436,7 @@ int main(int argc, char **argv)
                default:
                        fprintf(stderr, "usage: webserver "
                                "[-i ip_addr] [-p http_port] "
-                               "[-t tracefile] [-c] "
+                               "[-t tracefile] [-c] [-f] "
 #ifdef HAVE_OPENSSL
                                "[-s] "
 #endif
@@ -469,13 +450,40 @@ int main(int argc, char **argv)
                        ctdlport = argv[optind];
        }
        /* Tell 'em who's in da house */
-       lprintf(1, SERVER "\n"
-               "Copyright (C) 1996-2005 by the Citadel/UX development team.\n"
-               "This software is distributed under the terms of the GNU General Public\n"
-               "License.  If you paid for this software, someone is ripping you off.\n\n");
-
-       if (chdir(WEBCITDIR) != 0)
+       lprintf(1, SERVER "\n");
+       lprintf(1, "Copyright (C) 1996-2005 by the Citadel development team.\n"
+               "This software is distributed under the terms of the "
+               "GNU General Public License.\n\n"
+       );
+
+       lprintf(9, "Changing directory to %s\n", webcitdir);
+       if (chdir(webcitdir) != 0) {
                perror("chdir");
+       }
+
+       /* initialize the International Bright Young Thing */
+#ifdef ENABLE_NLS
+
+       initialize_locales();
+
+       locale = setlocale(LC_ALL, "");
+
+       mo = malloc(strlen(webcitdir) + 20);
+       sprintf(mo, "%s/locale", webcitdir);
+       lprintf(9, "Message catalog directory: %s\n",
+               bindtextdomain("webcit", mo)
+       );
+       free(mo);
+       lprintf(9, "Text domain: %s\n",
+               textdomain("webcit")
+       );
+       lprintf(9, "Text domain Charset: %s\n",
+                       bind_textdomain_codeset("webcit","UTF8")
+       );
+#endif
+
+       initialize_viewdefs();
+       initialize_axdefs();
 
        /*
         * Set up a place to put thread-specific data.