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 extern void *context_loop(int);
28 extern void *housekeeping_loop(void);
29 extern pthread_mutex_t SessionListMutex;
30 extern pthread_key_t MyConKey;
33 char *server_cookie = NULL; /**< our Cookie connection to the client */
35 int http_port = PORT_NUM; /**< Port to listen on */
37 char *ctdlhost = DEFAULT_HOST; /**< our name */
38 char *ctdlport = DEFAULT_PORT; /**< our Port */
39 int setup_wizard = 0; /**< should we run the setup wizard? \todo */
40 char wizard_filename[PATH_MAX];/**< where's the setup wizard? */
43 * \brief This is a generic function to set up a master socket for listening on
44 * a TCP port. The server shuts down if the bind fails.
45 * \param ip_addr ip to bind to
46 * \param port_number the port to bind to
47 * \param queue_len the size of the input queue ????
49 int ig_tcp_server(char *ip_addr, int port_number, int queue_len)
51 struct sockaddr_in sin;
54 memset(&sin, 0, sizeof(sin));
55 sin.sin_family = AF_INET;
56 if (ip_addr == NULL) {
57 sin.sin_addr.s_addr = INADDR_ANY;
59 sin.sin_addr.s_addr = inet_addr(ip_addr);
62 if (sin.sin_addr.s_addr == INADDR_NONE) {
63 sin.sin_addr.s_addr = INADDR_ANY;
66 if (port_number == 0) {
67 lprintf(1, "Cannot start: no port number specified.\n");
70 sin.sin_port = htons((u_short) port_number);
72 s = socket(PF_INET, SOCK_STREAM, (getprotobyname("tcp")->p_proto));
74 lprintf(1, "Can't create a socket: %s\n", strerror(errno));
77 /** Set some socket options that make sense. */
79 setsockopt(s, SOL_SOCKET, SO_REUSEADDR, &i, sizeof(i));
81 if (bind(s, (struct sockaddr *) &sin, sizeof(sin)) < 0) {
82 lprintf(1, "Can't bind: %s\n", strerror(errno));
85 if (listen(s, queue_len) < 0) {
86 lprintf(1, "Can't listen: %s\n", strerror(errno));
95 * \brief Create a Unix domain socket and listen on it
96 * \param sockpath file name of the unix domain socket
97 * \param queue_len queue size of the kernel fifo????
99 int ig_uds_server(char *sockpath, int queue_len)
101 struct sockaddr_un addr;
104 int actual_queue_len;
106 actual_queue_len = queue_len;
107 if (actual_queue_len < 5) actual_queue_len = 5;
109 i = unlink(sockpath);
110 if (i != 0) if (errno != ENOENT) {
111 lprintf(1, "citserver: can't unlink %s: %s\n",
112 sockpath, strerror(errno));
116 memset(&addr, 0, sizeof(addr));
117 addr.sun_family = AF_UNIX;
118 safestrncpy(addr.sun_path, sockpath, sizeof addr.sun_path);
120 s = socket(AF_UNIX, SOCK_STREAM, 0);
122 lprintf(1, "citserver: Can't create a socket: %s\n",
127 if (bind(s, (struct sockaddr *)&addr, sizeof(addr)) < 0) {
128 lprintf(1, "citserver: Can't bind: %s\n",
133 if (listen(s, actual_queue_len) < 0) {
134 lprintf(1, "citserver: Can't listen: %s\n",
139 chmod(sockpath, 0777);
147 * \brief Read data from the client socket.
148 * \param sock socket fd to read from ???
149 * \param buf buffer to read into
150 * \param bytes how large is the read buffer?
151 * \param timeout how long should we wait for input?
152 * \return values are\
153 * 1 Requested number of bytes has been read.\
154 * 0 Request timed out.\
155 * -1 Connection is broken, or other error.
157 int client_read_to(int sock, char *buf, int bytes, int timeout)
167 return (client_read_ssl(buf, bytes, timeout));
172 while (len < bytes) {
178 retval = select((sock) + 1, &rfds, NULL, NULL, &tv);
179 if (FD_ISSET(sock, &rfds) == 0) {
183 rlen = read(sock, &buf[len], bytes - len);
186 lprintf(2, "client_read() failed: %s\n",
194 write(2, "\033[32m", 5);
195 write(2, buf, bytes);
196 write(2, "\033[30m", 5);
202 * \brief write data to the client
203 * \param buf data to write to the client
204 * \param count size of buffer
206 ssize_t client_write(const void *buf, size_t count)
211 if (WC->burst != NULL) {
212 if ((WC->burst_len + count) >= WC->burst_alloc) {
213 newalloc = (WC->burst_alloc * 2);
214 if ((WC->burst_len + count) >= newalloc) {
217 newptr = realloc(WC->burst, newalloc);
218 if (newptr != NULL) {
220 WC->burst_alloc = newalloc;
223 if ((WC->burst_len + count) < WC->burst_alloc) {
224 memcpy(&WC->burst[WC->burst_len], buf, count);
225 WC->burst_len += count;
234 client_write_ssl((char *) buf, count);
239 write(2, "\033[34m", 5);
240 write(2, buf, count);
241 write(2, "\033[30m", 5);
243 return (write(WC->http_sock, buf, count));
247 * \brief what burst???
249 void begin_burst(void)
251 if (WC->burst != NULL) {
256 WC->burst_alloc = 32768;
257 WC->burst = malloc(WC->burst_alloc);
262 * \brief uses the same calling syntax as compress2(), but it
263 * creates a stream compatible with HTTP "Content-encoding: gzip"
266 #define DEF_MEM_LEVEL 8 /**< memlevel??? */
267 #define OS_CODE 0x03 /**< unix */
268 int ZEXPORT compress_gzip(Bytef * dest, /**< compressed buffer*/
269 uLongf * destLen, /**< length of the compresed data */
270 const Bytef * source, /**< source to encode */
271 uLong sourceLen, /**< length of the source to encode */
272 int level) /**< what level??? */
274 const int gz_magic[2] = { 0x1f, 0x8b }; /** gzip magic header */
276 /** write gzip header */
277 sprintf((char *) dest, "%c%c%c%c%c%c%c%c%c%c",
278 gz_magic[0], gz_magic[1], Z_DEFLATED,
279 0 /*flags */ , 0, 0, 0, 0 /*time */ , 0 /** xflags */ ,
285 stream.next_in = (Bytef *) source;
286 stream.avail_in = (uInt) sourceLen;
287 stream.next_out = dest + 10L; // after header
288 stream.avail_out = (uInt) * destLen;
289 if ((uLong) stream.avail_out != *destLen)
292 stream.zalloc = (alloc_func) 0;
293 stream.zfree = (free_func) 0;
294 stream.opaque = (voidpf) 0;
296 err = deflateInit2(&stream, level, Z_DEFLATED, -MAX_WBITS,
297 DEF_MEM_LEVEL, Z_DEFAULT_STRATEGY);
301 err = deflate(&stream, Z_FINISH);
302 if (err != Z_STREAM_END) {
304 return err == Z_OK ? Z_BUF_ERROR : err;
306 *destLen = stream.total_out + 10L;
308 /* write CRC and Length */
309 uLong crc = crc32(0L, source, sourceLen);
311 for (n = 0; n < 4; ++n, ++*destLen) {
312 dest[*destLen] = (int) (crc & 0xff);
315 uLong len = stream.total_in;
316 for (n = 0; n < 4; ++n, ++*destLen) {
317 dest[*destLen] = (int) (len & 0xff);
320 err = deflateEnd(&stream);
326 * \brief what burst???
333 if (WC->burst == NULL)
336 the_len = WC->burst_len;
337 the_data = WC->burst;
344 /* Handle gzip compression */
346 char *compressed_data = NULL;
347 uLongf compressed_len;
349 compressed_len = (uLongf) ((the_len * 101) / 100) + 100;
350 compressed_data = malloc(compressed_len);
352 if (compress_gzip((Bytef *) compressed_data,
355 (uLongf) the_len, Z_BEST_SPEED) == Z_OK) {
356 wprintf("Content-encoding: gzip\r\n");
358 the_data = compressed_data;
359 the_len = compressed_len;
361 free(compressed_data);
364 #endif /* HAVE_ZLIB */
366 wprintf("Content-length: %d\r\n\r\n", the_len);
367 client_write(the_data, the_len);
375 * \brief Read data from the client socket with default timeout.
376 * (This is implemented in terms of client_read_to() and could be
377 * justifiably moved out of sysdep.c)
378 * \param sock the socket fd to read from???
379 * \param buf the buffer to write to
380 * \param bytes how large is the buffer
382 int client_read(int sock, char *buf, int bytes)
384 return (client_read_to(sock, buf, bytes, SLEEPING));
389 * \brief Get a LF-terminated line of text from the client.
390 * (This is implemented in terms of client_read() and could be
391 * justifiably moved out of sysdep.c)
392 * \param sock socket fd to get client line from???
393 * \param buf buffer to write read data to
394 * \param bufsiz how many bytes to read
395 * \return numer of bytes read???
397 int client_getln(int sock, char *buf, int bufsiz)
401 /** Read one character at a time.*/
403 retval = client_read(sock, &buf[i], 1);
404 if (retval != 1 || buf[i] == '\n' || i == (bufsiz-1))
406 if ( (!isspace(buf[i])) && (!isprint(buf[i])) ) {
407 /** Non printable character recieved from client */
412 /** If we got a long line, discard characters until the newline. */
414 while (buf[i] != '\n' && retval == 1)
415 retval = client_read(sock, &buf[i], 1);
418 * Strip any trailing non-printable characters.
421 while ((strlen(buf) > 0) && (!isprint(buf[strlen(buf) - 1]))) {
422 buf[strlen(buf) - 1] = 0;
429 * \brief Start running as a daemon.
431 * param do_close_stdio Only close stdio if set.
433 void start_daemon(int do_close_stdio)
435 if (do_close_stdio) {
440 signal(SIGHUP, SIG_IGN);
441 signal(SIGINT, SIG_IGN);
442 signal(SIGQUIT, SIG_IGN);
449 * \brief Spawn an additional worker thread into the pool.
451 void spawn_another_worker_thread()
453 pthread_t SessThread; /**< Thread descriptor */
454 pthread_attr_t attr; /**< Thread attributes */
457 lprintf(3, "Creating a new thread\n");
459 /** set attributes for the new thread */
460 pthread_attr_init(&attr);
461 pthread_attr_setdetachstate(&attr, PTHREAD_CREATE_DETACHED);
464 * Our per-thread stacks need to be bigger than the default size, otherwise
465 * the MIME parser crashes on FreeBSD, and the IMAP service crashes on
468 if ((ret = pthread_attr_setstacksize(&attr, 1024 * 1024))) {
469 lprintf(1, "pthread_attr_setstacksize: %s\n",
471 pthread_attr_destroy(&attr);
474 /** now create the thread */
475 if (pthread_create(&SessThread, &attr,
476 (void *(*)(void *)) worker_entry, NULL)
478 lprintf(1, "Can't create thread: %s\n", strerror(errno));
481 /** free up the attributes */
482 pthread_attr_destroy(&attr);
486 * \brief Here's where it all begins.
487 * \param argc number of commandline args
488 * \param argv the commandline arguments
490 int main(int argc, char **argv)
492 pthread_t SessThread; /**< Thread descriptor */
493 pthread_attr_t attr; /**< Thread attributes */
494 int a, i; /**< General-purpose variables */
495 char tracefile[PATH_MAX];
497 char *webcitdir = PREFIX;
501 #endif /* ENABLE_NLS */
502 char uds_listen_path[PATH_MAX]; /**< listen on a unix domain socket? */
504 strcpy(uds_listen_path, "");
506 /** Parse command line */
508 while ((a = getopt(argc, argv, "h:i:p:t:x:cfs")) != EOF)
510 while ((a = getopt(argc, argv, "h:i:p:t:x:cf")) != EOF)
514 webcitdir = strdup(optarg);
517 safestrncpy(ip_addr, optarg, sizeof ip_addr);
520 http_port = atoi(optarg);
521 if (http_port == 0) {
522 safestrncpy(uds_listen_path, optarg, sizeof uds_listen_path);
526 safestrncpy(tracefile, optarg, sizeof tracefile);
527 freopen(tracefile, "w", stdout);
528 freopen(tracefile, "w", stderr);
529 freopen(tracefile, "r", stdin);
532 verbosity = atoi(optarg);
538 server_cookie = malloc(256);
539 if (server_cookie != NULL) {
540 safestrncpy(server_cookie,
541 "Set-cookie: wcserver=",
544 (&server_cookie[strlen(server_cookie)],
546 lprintf(2, "gethostname: %s\n",
556 fprintf(stderr, "usage: webserver "
557 "[-i ip_addr] [-p http_port] "
558 "[-t tracefile] [-c] [-f] "
562 "[remotehost [remoteport]]\n");
567 ctdlhost = argv[optind];
569 ctdlport = argv[optind];
571 /** Tell 'em who's in da house */
572 lprintf(1, SERVER "\n");
573 lprintf(1, "Copyright (C) 1996-2006 by the Citadel development team.\n"
574 "This software is distributed under the terms of the "
575 "GNU General Public License.\n\n"
579 /** initialize the International Bright Young Thing */
582 initialize_locales();
584 locale = setlocale(LC_ALL, "");
586 mo = malloc(strlen(webcitdir) + 20);
587 lprintf(9, "Message catalog directory: %s\n",
588 bindtextdomain("webcit", PREFIX"/share/locale/")
591 lprintf(9, "Text domain: %s\n",
594 lprintf(9, "Text domain Charset: %s\n",
595 bind_textdomain_codeset("webcit","UTF8")
599 lprintf(9, "Changing directory to %s\n", webcitdir);
600 if (chdir(webcitdir) != 0) {
603 initialize_viewdefs();
607 * Set up a place to put thread-specific data.
608 * We only need a single pointer per thread - it points to the
609 * wcsession struct to which the thread is currently bound.
611 if (pthread_key_create(&MyConKey, NULL) != 0) {
612 lprintf(1, "Can't create TSD key: %s\n", strerror(errno));
616 * Set up a place to put thread-specific SSL data.
617 * We don't stick this in the wcsession struct because SSL starts
618 * up before the session is bound, and it gets torn down between
622 if (pthread_key_create(&ThreadSSL, NULL) != 0) {
623 lprintf(1, "Can't create TSD key: %s\n", strerror(errno));
628 * Bind the server to our favorite port.
629 * There is no need to check for errors, because ig_tcp_server()
630 * exits if it doesn't succeed.
633 if (strlen(uds_listen_path) > 0) {
634 lprintf(2, "Attempting to create listener socket at %s...\n", uds_listen_path);
635 msock = ig_uds_server(uds_listen_path, LISTEN_QUEUE_LENGTH);
638 lprintf(2, "Attempting to bind to port %d...\n", http_port);
639 msock = ig_tcp_server(ip_addr, http_port, LISTEN_QUEUE_LENGTH);
642 lprintf(2, "Listening on socket %d\n", msock);
643 signal(SIGPIPE, SIG_IGN);
645 pthread_mutex_init(&SessionListMutex, NULL);
648 * Start up the housekeeping thread
650 pthread_attr_init(&attr);
651 pthread_attr_setdetachstate(&attr, PTHREAD_CREATE_DETACHED);
652 pthread_create(&SessThread, &attr,
653 (void *(*)(void *)) housekeeping_loop, NULL);
657 * If this is an HTTPS server, fire up SSL
665 /** Start a few initial worker threads */
666 for (i = 0; i < (MIN_WORKER_THREADS); ++i) {
667 spawn_another_worker_thread();
670 /* now the original thread becomes another worker */
677 * Entry point for worker threads
679 void worker_entry(void)
684 int fail_this_transaction = 0;
687 /** Only one thread can accept at a time */
688 fail_this_transaction = 0;
689 ssock = accept(msock, NULL, 0);
691 lprintf(2, "accept() failed: %s\n",
694 /** Set the SO_REUSEADDR socket option */
696 setsockopt(ssock, SOL_SOCKET, SO_REUSEADDR,
699 /** If we are an HTTPS server, go crypto now. */
702 if (starttls(ssock) != 0) {
703 fail_this_transaction = 1;
709 if (fail_this_transaction == 0) {
710 /** Perform an HTTP transaction... */
712 /** ...and close the socket. */
713 lingering_close(ssock);
718 } while (!time_to_die);
724 * \brief logprintf. log messages
725 * logs to stderr if loglevel is lower than the verbosity set at startup
726 * \param loglevel level of the message
727 * \param format the printf like format string
728 * \param ... the strings to put into format
730 int lprintf(int loglevel, const char *format, ...)
734 if (loglevel <= verbosity) {
735 va_start(ap, format);
736 vfprintf(stderr, format, ap);
745 * \brief print the actual stack frame.
747 void wc_backtrace(void)
749 #ifdef HAVE_BACKTRACE
750 void *stack_frames[50];
755 size = backtrace(stack_frames, sizeof(stack_frames) / sizeof(void*));
756 strings = backtrace_symbols(stack_frames, size);
757 for (i = 0; i < size; i++) {
759 lprintf(1, "%s\n", strings[i]);
761 lprintf(1, "%p\n", stack_frames[i]);