5 * \defgroup Webserver This contains a simple multithreaded TCP server manager. It sits around
6 * waiting on the specified port for incoming HTTP connections. When a
7 * connection is established, it calls context_loop() from context_loop.c.
8 * \ingroup WebcitHttpServer
13 #include "webserver.h"
20 int vsnprintf(char *buf, size_t max, const char *fmt, va_list argp);
23 int verbosity = 9; /**< Logging level */
24 int msock; /**< master listening socket */
25 int is_https = 0; /**< Nonzero if I am an HTTPS service */
26 int follow_xff = 0; /**< Follow X-Forwarded-For: header */
27 int home_specified = 0; /**< did the user specify a homedir? */
28 extern void *context_loop(int);
29 extern void *housekeeping_loop(void);
30 extern pthread_mutex_t SessionListMutex;
31 extern pthread_key_t MyConKey;
33 char socket_dir[PATH_MAX]; /**< where to talk to our citadel server */
34 static const char editor_absolut_dir[PATH_MAX]=EDITORDIR; /**< nailed to what configure gives us. */
35 static char static_dir[PATH_MAX]; /**< calculated on startup */
36 char *static_dirs[]={ /**< needs same sort order as the web mapping */
37 (char*)static_dir, /** our templates on disk */
38 (char*)editor_absolut_dir /** the editor on disk */
40 int ndirs=2; //sizeof(static_content_dirs);//sizeof(char *);
43 * Subdirectories from which the client may request static content
45 char *static_content_dirs[] = {
46 "static", /** static templates */
47 "tiny_mce" /** the JS editor */
52 char *server_cookie = NULL; /**< our Cookie connection to the client */
54 int http_port = PORT_NUM; /**< Port to listen on */
56 char *ctdlhost = DEFAULT_HOST; /**< our name */
57 char *ctdlport = DEFAULT_PORT; /**< our Port */
58 int setup_wizard = 0; /**< should we run the setup wizard? \todo */
59 char wizard_filename[PATH_MAX];/**< where's the setup wizard? */
60 int running_as_daemon = 0; /**< should we deamonize on startup? */
64 * \brief This is a generic function to set up a master socket for listening on
65 * a TCP port. The server shuts down if the bind fails.
66 * \param ip_addr ip to bind to
67 * \param port_number the port to bind to
68 * \param queue_len the size of the input queue ????
70 int ig_tcp_server(char *ip_addr, int port_number, int queue_len)
72 struct sockaddr_in sin;
75 memset(&sin, 0, sizeof(sin));
76 sin.sin_family = AF_INET;
77 if (ip_addr == NULL) {
78 sin.sin_addr.s_addr = INADDR_ANY;
80 sin.sin_addr.s_addr = inet_addr(ip_addr);
83 if (sin.sin_addr.s_addr == INADDR_NONE) {
84 sin.sin_addr.s_addr = INADDR_ANY;
87 if (port_number == 0) {
88 lprintf(1, "Cannot start: no port number specified.\n");
91 sin.sin_port = htons((u_short) port_number);
93 s = socket(PF_INET, SOCK_STREAM, (getprotobyname("tcp")->p_proto));
95 lprintf(1, "Can't create a socket: %s\n", strerror(errno));
98 /** Set some socket options that make sense. */
100 setsockopt(s, SOL_SOCKET, SO_REUSEADDR, &i, sizeof(i));
102 if (bind(s, (struct sockaddr *) &sin, sizeof(sin)) < 0) {
103 lprintf(1, "Can't bind: %s\n", strerror(errno));
106 if (listen(s, queue_len) < 0) {
107 lprintf(1, "Can't listen: %s\n", strerror(errno));
116 * \brief Create a Unix domain socket and listen on it
117 * \param sockpath file name of the unix domain socket
118 * \param queue_len queue size of the kernel fifo????
120 int ig_uds_server(char *sockpath, int queue_len)
122 struct sockaddr_un addr;
125 int actual_queue_len;
127 actual_queue_len = queue_len;
128 if (actual_queue_len < 5) actual_queue_len = 5;
130 i = unlink(sockpath);
131 if (i != 0) if (errno != ENOENT) {
132 lprintf(1, "citserver: can't unlink %s: %s\n",
133 sockpath, strerror(errno));
137 memset(&addr, 0, sizeof(addr));
138 addr.sun_family = AF_UNIX;
139 safestrncpy(addr.sun_path, sockpath, sizeof addr.sun_path);
141 s = socket(AF_UNIX, SOCK_STREAM, 0);
143 lprintf(1, "citserver: Can't create a socket: %s\n",
148 if (bind(s, (struct sockaddr *)&addr, sizeof(addr)) < 0) {
149 lprintf(1, "citserver: Can't bind: %s\n",
154 if (listen(s, actual_queue_len) < 0) {
155 lprintf(1, "citserver: Can't listen: %s\n",
160 chmod(sockpath, 0777);
168 * \brief Read data from the client socket.
169 * \param sock socket fd to read from ???
170 * \param buf buffer to read into
171 * \param bytes how large is the read buffer?
172 * \param timeout how long should we wait for input?
173 * \return values are\
174 * 1 Requested number of bytes has been read.\
175 * 0 Request timed out.\
176 * -1 Connection is broken, or other error.
178 int client_read_to(int sock, char *buf, int bytes, int timeout)
188 return (client_read_ssl(buf, bytes, timeout));
193 while (len < bytes) {
199 retval = select((sock) + 1, &rfds, NULL, NULL, &tv);
200 if (FD_ISSET(sock, &rfds) == 0) {
204 rlen = read(sock, &buf[len], bytes - len);
207 lprintf(2, "client_read() failed: %s\n",
215 write(2, "\033[32m", 5);
216 write(2, buf, bytes);
217 write(2, "\033[30m", 5);
223 * \brief write data to the client
224 * \param buf data to write to the client
225 * \param count size of buffer
227 ssize_t client_write(const void *buf, size_t count)
232 if (WC->burst != NULL) {
233 if ((WC->burst_len + count) >= WC->burst_alloc) {
234 newalloc = (WC->burst_alloc * 2);
235 if ((WC->burst_len + count) >= newalloc) {
238 newptr = realloc(WC->burst, newalloc);
239 if (newptr != NULL) {
241 WC->burst_alloc = newalloc;
244 if ((WC->burst_len + count) < WC->burst_alloc) {
245 memcpy(&WC->burst[WC->burst_len], buf, count);
246 WC->burst_len += count;
255 client_write_ssl((char *) buf, count);
260 write(2, "\033[34m", 5);
261 write(2, buf, count);
262 write(2, "\033[30m", 5);
264 return (write(WC->http_sock, buf, count));
268 * \brief what burst???
270 void begin_burst(void)
272 if (WC->burst != NULL) {
277 WC->burst_alloc = 32768;
278 WC->burst = malloc(WC->burst_alloc);
283 * \brief uses the same calling syntax as compress2(), but it
284 * creates a stream compatible with HTTP "Content-encoding: gzip"
287 #define DEF_MEM_LEVEL 8 /**< memlevel??? */
288 #define OS_CODE 0x03 /**< unix */
289 int ZEXPORT compress_gzip(Bytef * dest, /**< compressed buffer*/
290 uLongf * destLen, /**< length of the compresed data */
291 const Bytef * source, /**< source to encode */
292 uLong sourceLen, /**< length of the source to encode */
293 int level) /**< what level??? */
295 const int gz_magic[2] = { 0x1f, 0x8b }; /** gzip magic header */
297 /** write gzip header */
298 sprintf((char *) dest, "%c%c%c%c%c%c%c%c%c%c",
299 gz_magic[0], gz_magic[1], Z_DEFLATED,
300 0 /*flags */ , 0, 0, 0, 0 /*time */ , 0 /** xflags */ ,
306 stream.next_in = (Bytef *) source;
307 stream.avail_in = (uInt) sourceLen;
308 stream.next_out = dest + 10L; // after header
309 stream.avail_out = (uInt) * destLen;
310 if ((uLong) stream.avail_out != *destLen)
313 stream.zalloc = (alloc_func) 0;
314 stream.zfree = (free_func) 0;
315 stream.opaque = (voidpf) 0;
317 err = deflateInit2(&stream, level, Z_DEFLATED, -MAX_WBITS,
318 DEF_MEM_LEVEL, Z_DEFAULT_STRATEGY);
322 err = deflate(&stream, Z_FINISH);
323 if (err != Z_STREAM_END) {
325 return err == Z_OK ? Z_BUF_ERROR : err;
327 *destLen = stream.total_out + 10L;
329 /* write CRC and Length */
330 uLong crc = crc32(0L, source, sourceLen);
332 for (n = 0; n < 4; ++n, ++*destLen) {
333 dest[*destLen] = (int) (crc & 0xff);
336 uLong len = stream.total_in;
337 for (n = 0; n < 4; ++n, ++*destLen) {
338 dest[*destLen] = (int) (len & 0xff);
341 err = deflateEnd(&stream);
347 * \brief what burst???
354 if (WC->burst == NULL)
357 the_len = WC->burst_len;
358 the_data = WC->burst;
365 /* Handle gzip compression */
367 char *compressed_data = NULL;
368 uLongf compressed_len;
370 compressed_len = (uLongf) ((the_len * 101) / 100) + 100;
371 compressed_data = malloc(compressed_len);
373 if (compress_gzip((Bytef *) compressed_data,
376 (uLongf) the_len, Z_BEST_SPEED) == Z_OK) {
377 wprintf("Content-encoding: gzip\r\n");
379 the_data = compressed_data;
380 the_len = compressed_len;
382 free(compressed_data);
385 #endif /* HAVE_ZLIB */
387 wprintf("Content-length: %d\r\n\r\n", the_len);
388 client_write(the_data, the_len);
396 * \brief Read data from the client socket with default timeout.
397 * (This is implemented in terms of client_read_to() and could be
398 * justifiably moved out of sysdep.c)
399 * \param sock the socket fd to read from???
400 * \param buf the buffer to write to
401 * \param bytes how large is the buffer
403 int client_read(int sock, char *buf, int bytes)
405 return (client_read_to(sock, buf, bytes, SLEEPING));
410 * \brief Get a LF-terminated line of text from the client.
411 * (This is implemented in terms of client_read() and could be
412 * justifiably moved out of sysdep.c)
413 * \param sock socket fd to get client line from???
414 * \param buf buffer to write read data to
415 * \param bufsiz how many bytes to read
416 * \return numer of bytes read???
418 int client_getln(int sock, char *buf, int bufsiz)
422 /** Read one character at a time.*/
424 retval = client_read(sock, &buf[i], 1);
425 if (retval != 1 || buf[i] == '\n' || i == (bufsiz-1))
427 if ( (!isspace(buf[i])) && (!isprint(buf[i])) ) {
428 /** Non printable character recieved from client */
433 /** If we got a long line, discard characters until the newline. */
435 while (buf[i] != '\n' && retval == 1)
436 retval = client_read(sock, &buf[i], 1);
439 * Strip any trailing non-printable characters.
442 while ((strlen(buf) > 0) && (!isprint(buf[strlen(buf) - 1]))) {
443 buf[strlen(buf) - 1] = 0;
450 * \brief Start running as a daemon.
452 * param do_close_stdio Only close stdio if set.
454 void start_daemon(int do_close_stdio)
456 if (do_close_stdio) {
461 signal(SIGHUP, SIG_IGN);
462 signal(SIGINT, SIG_IGN);
463 signal(SIGQUIT, SIG_IGN);
470 * \brief Spawn an additional worker thread into the pool.
472 void spawn_another_worker_thread()
474 pthread_t SessThread; /**< Thread descriptor */
475 pthread_attr_t attr; /**< Thread attributes */
478 lprintf(3, "Creating a new thread\n");
480 /** set attributes for the new thread */
481 pthread_attr_init(&attr);
482 pthread_attr_setdetachstate(&attr, PTHREAD_CREATE_DETACHED);
485 * Our per-thread stacks need to be bigger than the default size, otherwise
486 * the MIME parser crashes on FreeBSD, and the IMAP service crashes on
489 if ((ret = pthread_attr_setstacksize(&attr, 1024 * 1024))) {
490 lprintf(1, "pthread_attr_setstacksize: %s\n",
492 pthread_attr_destroy(&attr);
495 /** now create the thread */
496 if (pthread_create(&SessThread, &attr,
497 (void *(*)(void *)) worker_entry, NULL)
499 lprintf(1, "Can't create thread: %s\n", strerror(errno));
502 /** free up the attributes */
503 pthread_attr_destroy(&attr);
507 * \brief Here's where it all begins.
508 * \param argc number of commandline args
509 * \param argv the commandline arguments
511 int main(int argc, char **argv)
513 pthread_t SessThread; /**< Thread descriptor */
514 pthread_attr_t attr; /**< Thread attributes */
515 int a, i; /**< General-purpose variables */
516 char tracefile[PATH_MAX];
518 char dirbuffer[PATH_MAX]="";
521 int home_specified=0;
522 char relhome[PATH_MAX]="";
523 char webcitdir[PATH_MAX] = DATADIR;
529 #endif /* ENABLE_NLS */
530 char uds_listen_path[PATH_MAX]; /**< listen on a unix domain socket? */
532 strcpy(uds_listen_path, "");
534 /** Parse command line */
536 while ((a = getopt(argc, argv, "h:i:p:t:x:dcfs")) != EOF)
538 while ((a = getopt(argc, argv, "h:i:p:t:x:dcf")) != EOF)
542 hdir = strdup(optarg);
544 if (!relh) safestrncpy(webcitdir, hdir,
547 safestrncpy(relhome, relhome,
549 /* free(hdir); TODO: SHOULD WE DO THIS? */
554 running_as_daemon = 1;
557 safestrncpy(ip_addr, optarg, sizeof ip_addr);
560 http_port = atoi(optarg);
561 if (http_port == 0) {
562 safestrncpy(uds_listen_path, optarg, sizeof uds_listen_path);
566 safestrncpy(tracefile, optarg, sizeof tracefile);
567 freopen(tracefile, "w", stdout);
568 freopen(tracefile, "w", stderr);
569 freopen(tracefile, "r", stdin);
572 verbosity = atoi(optarg);
578 server_cookie = malloc(256);
579 if (server_cookie != NULL) {
580 safestrncpy(server_cookie,
581 "Set-cookie: wcserver=",
584 (&server_cookie[strlen(server_cookie)],
586 lprintf(2, "gethostname: %s\n",
596 fprintf(stderr, "usage: webserver "
597 "[-i ip_addr] [-p http_port] "
598 "[-t tracefile] [-c] [-f] "
603 "[remotehost [remoteport]]\n");
608 ctdlhost = argv[optind];
610 ctdlport = argv[optind];
613 /* daemonize, if we were asked to */
614 if (running_as_daemon) {
618 /** Tell 'em who's in da house */
619 lprintf(1, SERVER "\n");
620 lprintf(1, "Copyright (C) 1996-2006 by the Citadel development team.\n"
621 "This software is distributed under the terms of the "
622 "GNU General Public License.\n\n"
626 /** initialize the International Bright Young Thing */
628 initialize_locales();
629 locale = setlocale(LC_ALL, "");
630 mo = malloc(strlen(webcitdir) + 20);
631 lprintf(9, "Message catalog directory: %s\n", bindtextdomain("webcit", LOCALEDIR));
633 lprintf(9, "Text domain: %s\n", textdomain("webcit"));
634 lprintf(9, "Text domain Charset: %s\n", bind_textdomain_codeset("webcit","UTF8"));
638 /* calculate all our path on a central place */
639 /* where to keep our config */
641 #define COMPUTE_DIRECTORY(SUBDIR) memcpy(dirbuffer,SUBDIR, sizeof dirbuffer);\
642 snprintf(SUBDIR,sizeof SUBDIR, "%s%s%s%s%s%s%s", \
643 (home&!relh)?webcitdir:basedir, \
644 ((basedir!=webcitdir)&(home&!relh))?basedir:"/", \
645 ((basedir!=webcitdir)&(home&!relh))?"/":"", \
647 (relhome[0]!='\0')?"/":"",\
649 (dirbuffer[0]!='\0')?"/":"");
651 COMPUTE_DIRECTORY(socket_dir);
653 COMPUTE_DIRECTORY(static_dir);
654 /** we should go somewhere we can leave our coredump, if enabled... */
655 lprintf(9, "Changing directory to %s\n", socket_dir);
656 if (chdir(webcitdir) != 0) {
659 initialize_viewdefs();
663 * Set up a place to put thread-specific data.
664 * We only need a single pointer per thread - it points to the
665 * wcsession struct to which the thread is currently bound.
667 if (pthread_key_create(&MyConKey, NULL) != 0) {
668 lprintf(1, "Can't create TSD key: %s\n", strerror(errno));
672 * Set up a place to put thread-specific SSL data.
673 * We don't stick this in the wcsession struct because SSL starts
674 * up before the session is bound, and it gets torn down between
678 if (pthread_key_create(&ThreadSSL, NULL) != 0) {
679 lprintf(1, "Can't create TSD key: %s\n", strerror(errno));
684 * Bind the server to our favorite port.
685 * There is no need to check for errors, because ig_tcp_server()
686 * exits if it doesn't succeed.
689 if (strlen(uds_listen_path) > 0) {
690 lprintf(2, "Attempting to create listener socket at %s...\n", uds_listen_path);
691 msock = ig_uds_server(uds_listen_path, LISTEN_QUEUE_LENGTH);
694 lprintf(2, "Attempting to bind to port %d...\n", http_port);
695 msock = ig_tcp_server(ip_addr, http_port, LISTEN_QUEUE_LENGTH);
698 lprintf(2, "Listening on socket %d\n", msock);
699 signal(SIGPIPE, SIG_IGN);
701 pthread_mutex_init(&SessionListMutex, NULL);
704 * Start up the housekeeping thread
706 pthread_attr_init(&attr);
707 pthread_attr_setdetachstate(&attr, PTHREAD_CREATE_DETACHED);
708 pthread_create(&SessThread, &attr,
709 (void *(*)(void *)) housekeeping_loop, NULL);
713 * If this is an HTTPS server, fire up SSL
721 /** Start a few initial worker threads */
722 for (i = 0; i < (MIN_WORKER_THREADS); ++i) {
723 spawn_another_worker_thread();
726 /* now the original thread becomes another worker */
733 * Entry point for worker threads
735 void worker_entry(void)
740 int fail_this_transaction = 0;
743 /** Only one thread can accept at a time */
744 fail_this_transaction = 0;
745 ssock = accept(msock, NULL, 0);
747 lprintf(2, "accept() failed: %s\n",
750 /** Set the SO_REUSEADDR socket option */
752 setsockopt(ssock, SOL_SOCKET, SO_REUSEADDR,
755 /** If we are an HTTPS server, go crypto now. */
758 if (starttls(ssock) != 0) {
759 fail_this_transaction = 1;
765 if (fail_this_transaction == 0) {
767 /** Perform an HTTP transaction... */
770 /** Shut down SSL/TLS if required... */
777 /** ...and close the socket. */
778 lingering_close(ssock);
783 } while (!time_to_die);
789 * \brief logprintf. log messages
790 * logs to stderr if loglevel is lower than the verbosity set at startup
791 * \param loglevel level of the message
792 * \param format the printf like format string
793 * \param ... the strings to put into format
795 int lprintf(int loglevel, const char *format, ...)
799 if (loglevel <= verbosity) {
800 va_start(ap, format);
801 vfprintf(stderr, format, ap);
810 * \brief print the actual stack frame.
812 void wc_backtrace(void)
814 #ifdef HAVE_BACKTRACE
815 void *stack_frames[50];
820 size = backtrace(stack_frames, sizeof(stack_frames) / sizeof(void*));
821 strings = backtrace_symbols(stack_frames, size);
822 for (i = 0; i < size; i++) {
824 lprintf(1, "%s\n", strings[i]);
826 lprintf(1, "%p\n", stack_frames[i]);