* due to demand of debian, we rename the webcit binary from webserver to webcit.
[citadel.git] / webcit / webserver.c
index 0b47ce87200dcc33d240a055f2bd82166ffd5254..9ecc7d5d413450be1dba07f492a06a4b466ab2ef 100644 (file)
@@ -39,10 +39,12 @@ char socket_dir[PATH_MAX];                  /* where to talk to our citadel server */
 static const char editor_absolut_dir[PATH_MAX]=EDITORDIR;      /* nailed to what configure gives us. */
 static char static_dir[PATH_MAX];              /* calculated on startup */
 static char static_local_dir[PATH_MAX];                /* calculated on startup */
+static char static_icon_dir[PATH_MAX];          /* where should we find our mime icons? */
 char  *static_dirs[]={                         /* needs same sort order as the web mapping */
        (char*)static_dir,                      /* our templates on disk */
        (char*)static_local_dir,                /* user provided templates disk */
-       (char*)editor_absolut_dir               /* the editor on disk */
+       (char*)editor_absolut_dir,              /* the editor on disk */
+       (char*)static_icon_dir                  /* our icons... */
 };
 
 /*
@@ -108,7 +110,10 @@ int ig_tcp_server(char *ip_addr, int port_number, int queue_len)
        i = 1;
        setsockopt(s, SOL_SOCKET, SO_REUSEADDR, &i, sizeof(i));
 
-       fcntl(s, F_SETFL, O_NONBLOCK);/// TODO
+       fcntl(s, F_SETFL, O_NONBLOCK); /* maide: this statement is incorrect
+                                         there should be a preceding F_GETFL
+                                         and a bitwise OR with the previous
+                                         fd flags */
        
        if (bind(s, (struct sockaddr *) &sin, sizeof(sin)) < 0) {
                lprintf(1, "Can't bind: %s\n", strerror(errno));
@@ -140,7 +145,7 @@ int ig_uds_server(char *sockpath, int queue_len)
 
        i = unlink(sockpath);
        if (i != 0) if (errno != ENOENT) {
-               lprintf(1, "webserver: can't unlink %s: %s\n",
+               lprintf(1, "webcit: can't unlink %s: %s\n",
                        sockpath, strerror(errno));
                exit(WC_EXIT_BIND);
        }
@@ -151,19 +156,19 @@ int ig_uds_server(char *sockpath, int queue_len)
 
        s = socket(AF_UNIX, SOCK_STREAM, 0);
        if (s < 0) {
-               lprintf(1, "webserver: Can't create a socket: %s\n",
+               lprintf(1, "webcit: Can't create a socket: %s\n",
                        strerror(errno));
                exit(WC_EXIT_BIND);
        }
 
        if (bind(s, (struct sockaddr *)&addr, sizeof(addr)) < 0) {
-               lprintf(1, "webserver: Can't bind: %s\n",
+               lprintf(1, "webcit: Can't bind: %s\n",
                        strerror(errno));
                exit(WC_EXIT_BIND);
        }
 
        if (listen(s, actual_queue_len) < 0) {
-               lprintf(1, "webserver: Can't listen: %s\n",
+               lprintf(1, "webcit: Can't listen: %s\n",
                        strerror(errno));
                exit(WC_EXIT_BIND);
        }
@@ -237,8 +242,12 @@ int client_read_to(int sock, char *buf, int bytes, int timeout)
  */
 ssize_t client_write(const void *buf, size_t count)
 {
-       char *newptr;
-       size_t newalloc;
+        char *newptr;
+        size_t newalloc;
+        size_t bytesWritten = 0;
+        ssize_t res;
+        fd_set wset;
+        int fdflags;
 
        if (WC->burst != NULL) {
                if ((WC->burst_len + count) >= WC->burst_alloc) {
@@ -272,7 +281,27 @@ ssize_t client_write(const void *buf, size_t count)
        write(2, buf, count);
        write(2, "\033[30m", 5);
 #endif
-       return (write(WC->http_sock, buf, count));
+       fdflags = fcntl(WC->http_sock, F_GETFL);
+
+        while (bytesWritten < count) {
+                if ((fdflags & O_NONBLOCK) == O_NONBLOCK) {
+                        FD_ZERO(&wset);
+                        FD_SET(WC->http_sock, &wset);
+                        if (select(1, NULL, &wset, NULL, NULL) == -1) {
+                                lprintf(2, "client_write: Socket select failed (%s)\n", strerror(errno));
+                                return -1;
+                        }
+                }
+
+                if ((res = write(WC->http_sock, (char*)buf + bytesWritten,
+                  count - bytesWritten)) == -1) {
+                        lprintf(2, "client_write: Socket write failed (%s)\n", strerror(errno));
+                        return res;
+                }
+                bytesWritten += res;
+        }
+
+       return bytesWritten;
 }
 
 /*
@@ -670,9 +699,6 @@ int main(int argc, char **argv)
 
        strcpy(uds_listen_path, "");
 
-       if (getenv("TZ") == NULL)
-               putenv("TZ=UTC");
-
        /* Parse command line */
 #ifdef HAVE_OPENSSL
        while ((a = getopt(argc, argv, "h:i:p:t:x:dD:cfs")) != EOF)
@@ -739,7 +765,7 @@ int main(int argc, char **argv)
                        is_https = 1;
                        break;
                default:
-                       fprintf(stderr, "usage: webserver "
+                       fprintf(stderr, "usage: webcit "
                                "[-i ip_addr] [-p http_port] "
                                "[-t tracefile] [-c] [-f] "
                                "[-d] "
@@ -776,12 +802,15 @@ int main(int argc, char **argv)
        /* initialize the International Bright Young Thing */
 #ifdef ENABLE_NLS
        initialize_locales();
+
        locale = setlocale(LC_ALL, "");
+
        mo = malloc(strlen(webcitdir) + 20);
        lprintf(9, "Message catalog directory: %s\n", bindtextdomain("webcit", LOCALEDIR"/locale"));
        free(mo);
        lprintf(9, "Text domain: %s\n", textdomain("webcit"));
        lprintf(9, "Text domain Charset: %s\n", bind_textdomain_codeset("webcit","UTF8"));
+       preset_locale();
 #endif
 
 
@@ -801,6 +830,8 @@ int main(int argc, char **argv)
        COMPUTE_DIRECTORY(socket_dir);
        basedir=WWWDIR "/static";
        COMPUTE_DIRECTORY(static_dir);
+       basedir=WWWDIR "/static/icons";
+       COMPUTE_DIRECTORY(static_icon_dir);
        basedir=WWWDIR "/static.local";
        COMPUTE_DIRECTORY(static_local_dir);
 
@@ -821,8 +852,8 @@ int main(int argc, char **argv)
        lprintf(9, "Changing directory to %s\n", socket_dir);
        if (chdir(webcitdir) != 0) {
                perror("chdir");
-       }       
-       CtdlInitBase64Table();
+       }
+       LoadIconDir(static_icon_dir);
        initialize_viewdefs();
        initialize_axdefs();
 
@@ -963,12 +994,10 @@ void worker_entry(void)
                                sleeeeeeeeeep(1); // wait so some others might finish...
                                lprintf(2, "master shutdown: cleaning up sessions\n");
                                do_housekeeping();
-#ifdef WEBCIT_WITH_CALENDAR_SERVICE
                                lprintf(2, "master shutdown: cleaning up libical\n");
                                free_zone_directory ();
                                icaltimezone_release_zone_tab ();
                                icalmemory_free_ring ();
-#endif
                                lprintf(2, "master shutdown exiting!.\n");                              
                                exit(0);
                        }