]> code.citadel.org Git - citadel.git/blobdiff - webcit/webserver.c
* Brought over the newer string tokenizer from Citadel
[citadel.git] / webcit / webserver.c
index ce0c9159d81eedd63b903838fd8e2f778d14869e..266cd692998c77e3b88cf1700b943888fd38c329 100644 (file)
@@ -48,11 +48,11 @@ extern pthread_mutex_t SessionListMutex;
 extern pthread_key_t MyConKey;
 
 
+char *server_cookie = NULL;
 
 
-
-const char *defaulthost = DEFAULT_HOST;
-const char *defaultport = DEFAULT_PORT;
+char *defaulthost = DEFAULT_HOST;
+char *defaultport = DEFAULT_PORT;
 
 pthread_mutex_t AcceptQueue;
 
@@ -70,14 +70,15 @@ int ig_tcp_server(int port_number, int queue_len)
        sin.sin_addr.s_addr = INADDR_ANY;
 
        if (port_number == 0) {
-               printf("webcit: Cannot start: no port number specified.\n");
+               fprintf(stderr,
+                       "webcit: Cannot start: no port number specified.\n");
                exit(1);
        }
        sin.sin_port = htons((u_short) port_number);
 
        s = socket(PF_INET, SOCK_STREAM, (getprotobyname("tcp")->p_proto));
        if (s < 0) {
-               printf("webcit: Can't create a socket: %s\n",
+               fprintf(stderr, "webcit: Can't create a socket: %s\n",
                       strerror(errno));
                exit(errno);
        }
@@ -86,59 +87,23 @@ int ig_tcp_server(int port_number, int queue_len)
        setsockopt(s, SOL_SOCKET, SO_REUSEADDR, &i, sizeof(i));
 
        if (bind(s, (struct sockaddr *) &sin, sizeof(sin)) < 0) {
-               printf("webcit: Can't bind: %s\n", strerror(errno));
+               fprintf(stderr, "webcit: Can't bind: %s\n", strerror(errno));
                exit(errno);
        }
        if (listen(s, queue_len) < 0) {
-               printf("webcit: Can't listen: %s\n", strerror(errno));
+               fprintf(stderr, "webcit: Can't listen: %s\n", strerror(errno));
                exit(errno);
        }
        return (s);
 }
 
 
-/*
- * client_write()   ...    Send binary data to the client.
- */
-void client_write(int sock, char *buf, int nbytes)
-{
-       int bytes_written = 0;
-       int retval;
-       while (bytes_written < nbytes) {
-               retval = write(sock, &buf[bytes_written],
-                              nbytes - bytes_written);
-               if (retval < 1) {
-                       printf("client_write() failed: %s\n",
-                              strerror(errno));
-                       pthread_exit(NULL);
-               }
-               bytes_written = bytes_written + retval;
-       }
-}
-
-
-/*
- * cprintf()  ...   Send formatted printable data to the client.
- */
-void cprintf(int sock, const char *format,...)
-{
-       va_list arg_ptr;
-       char buf[256];
-
-       va_start(arg_ptr, format);
-       if (vsnprintf(buf, sizeof buf, format, arg_ptr) == -1)
-               buf[sizeof buf - 2] = '\n';
-       client_write(sock, buf, strlen(buf));
-       va_end(arg_ptr);
-}
-
-
 /*
  * Read data from the client socket.
  * Return values are:
  *      1       Requested number of bytes has been read.
  *      0       Request timed out.
- * If the socket breaks, the session is immediately terminated.
+ *     -1      Connection is broken, or other error.
  */
 int client_read_to(int sock, char *buf, int bytes, int timeout)
 {
@@ -161,9 +126,9 @@ int client_read_to(int sock, char *buf, int bytes, int timeout)
                }
                rlen = read(sock, &buf[len], bytes - len);
                if (rlen < 1) {
-                       printf("client_read() failed: %s\n",
+                       fprintf(stderr, "client_read() failed: %s\n",
                               strerror(errno));
-                       pthread_exit(NULL);
+                       return(-1);
                }
                len = len + rlen;
        }
@@ -205,7 +170,7 @@ int client_gets(int sock, char *buf)
                        retval = client_read(sock, &buf[i], 1);
 
        /*
-        * Strip any trailing not-printable characters.
+        * Strip any trailing non-printable characters.
         */
        buf[i] = 0;
        while ((strlen(buf) > 0) && (!isprint(buf[strlen(buf) - 1]))) {
@@ -232,6 +197,25 @@ void start_daemon(int do_close_stdio)
                exit(0);
 }
 
+void spawn_another_worker_thread() {
+       pthread_t SessThread;   /* Thread descriptor */
+       pthread_attr_t attr;    /* Thread attributes */
+
+       fprintf(stderr, "Creating a new thread\n");
+
+       /* set attributes for the new thread */
+       pthread_attr_init(&attr);
+       pthread_attr_setdetachstate(&attr, PTHREAD_CREATE_DETACHED);
+
+       /* now create the thread */
+       if (pthread_create(&SessThread, &attr,
+                       (void *(*)(void *)) worker_entry, NULL)
+                  != 0) {
+               fprintf(stderr, "webcit: can't create thread: %s\n",
+                       strerror(errno));
+       }
+}
+
 /*
  * Here's where it all begins.
  */
@@ -244,7 +228,7 @@ int main(int argc, char **argv)
        char tracefile[PATH_MAX];
 
        /* Parse command line */
-       while ((a = getopt(argc, argv, "hp:t:")) != EOF)
+       while ((a = getopt(argc, argv, "hp:t:c")) != EOF)
                switch (a) {
                case 'p':
                        port = atoi(optarg);
@@ -255,9 +239,22 @@ int main(int argc, char **argv)
                        freopen(tracefile, "w", stderr);
                        freopen(tracefile, "r", stdin);
                        break;
+               case 'c':
+                       server_cookie = malloc(SIZ);
+                       if (server_cookie != NULL) {
+                               strcpy(server_cookie, "Set-cookie: wcserver=");
+                               if (gethostname(
+                                  &server_cookie[strlen(server_cookie)],
+                                  200) != 0) {
+                                       fprintf(stderr, "gethostname: %s\n",
+                                               strerror(errno));
+                                       free(server_cookie);
+                               }
+                       }
+                       break;
                default:
                        fprintf(stderr, "usage: webserver [-p localport] "
-                               "[-t tracefile] "
+                               "[-t tracefile] [-c] "
                                "[remotehost [remoteport]]\n");
                        return 1;
                }
@@ -269,7 +266,9 @@ int main(int argc, char **argv)
        }
        /* Tell 'em who's in da house */
        fprintf(stderr, SERVER "\n"
-               "Copyright (C) 1996-1999.  All rights reserved.\n\n");
+               "Copyright (C) 1996-2001\n"
+               "This software is distributed under the terms of the GNU\n"
+               "General Public License.  All other rights reserved.\n\n");
 
        if (chdir(WEBCITDIR) != 0)
                perror("chdir");
@@ -288,9 +287,9 @@ int main(int argc, char **argv)
         * There is no need to check for errors, because ig_tcp_server()
         * exits if it doesn't succeed.
         */
-       printf("Attempting to bind to port %d...\n", port);
-       msock = ig_tcp_server(port, 5);
-       printf("Listening on socket %d\n", msock);
+       fprintf(stderr, "Attempting to bind to port %d...\n", port);
+       msock = ig_tcp_server(port, LISTEN_QUEUE_LENGTH);
+       fprintf(stderr, "Listening on socket %d\n", msock);
        signal(SIGPIPE, SIG_IGN);
 
        pthread_mutex_init(&SessionListMutex, NULL);
@@ -305,26 +304,14 @@ int main(int argc, char **argv)
                       (void *(*)(void *)) housekeeping_loop, NULL);
 
 
-
-       /* FIX make this variable */
-       for (i=0; i<10; ++i) {
-
-               /* set attributes for the new thread */
-               pthread_attr_init(&attr);
-               pthread_attr_setdetachstate(&attr, PTHREAD_CREATE_DETACHED);
-
-               /* now create the thread */
-               if (pthread_create(&SessThread, &attr,
-                               (void *(*)(void *)) worker_entry, NULL)
-                   != 0) {
-                       printf("webcit: can't create thread: %s\n",
-                              strerror(errno));
-               }
+       /* Start a few initial worker threads */
+       for (i=0; i<(INITIAL_WORKER_THREADS); ++i) {
+               spawn_another_worker_thread();
        }
 
-       /* now become a worker thread too */
-       worker_entry();
+       /* now the original thread can go away. */
        pthread_exit(NULL);
+       return 0;
 }
 
 
@@ -337,23 +324,35 @@ void worker_entry(void) {
        int alen;
        int i = 0;
        int time_to_die = 0;
+       time_t start_time, stop_time;
 
        do {
                /* Only one thread can accept at a time */
+               start_time = time(NULL);
                pthread_mutex_lock(&AcceptQueue);
                ssock = accept(msock, (struct sockaddr *) &fsin, &alen);
                pthread_mutex_unlock(&AcceptQueue);
+               stop_time = time(NULL);
+
+               /* Augment the thread pool if we're not blocking at all */
+               if ( (stop_time - start_time) == 0L) {
+                       spawn_another_worker_thread();
+               }
 
-               printf("New connection on socket %d\n", ssock);
                if (ssock < 0) {
-                       printf("webcit: accept() failed: %s\n",
+                       fprintf(stderr, "webcit: accept() failed: %s\n",
                        strerror(errno));
                } else {
                        /* Set the SO_REUSEADDR socket option */
                        i = 1;
                        setsockopt(ssock, SOL_SOCKET, SO_REUSEADDR,
                                &i, sizeof(i));
+
+                       /* Perform an HTTP transaction... */
                        context_loop(ssock);
+
+                       /* ...and close the socket. */
+                       lingering_close(ssock);
                }
 
        } while (!time_to_die);