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.
13 #include "webserver.h"
16 int vsnprintf(char *buf, size_t max, const char *fmt, va_list argp);
19 int verbosity = 9; /**< Logging level */
20 int msock; /**< master listening socket */
21 int is_https = 0; /**< Nonzero if I am an HTTPS service */
22 int follow_xff = 0; /**< Follow X-Forwarded-For: header */
23 extern void *context_loop(int);
24 extern void *housekeeping_loop(void);
25 extern pthread_mutex_t SessionListMutex;
26 extern pthread_key_t MyConKey;
29 char *server_cookie = NULL; /**< our Cookie connection to the client */
32 char *ctdlhost = DEFAULT_HOST; /**< our name */
33 char *ctdlport = DEFAULT_PORT; /**< our Port */
34 int setup_wizard = 0; /**< should we run the setup wizard? \todo */
35 char wizard_filename[PATH_MAX];/**< where's the setup wizard? */
38 * \brief This is a generic function to set up a master socket for listening on
39 * a TCP port. The server shuts down if the bind fails.
40 * \param ip_addr ip to bind to
41 * \param port_number the port to bind to
42 * \param queue_len the size of the input queue ????
44 int ig_tcp_server(char *ip_addr, int port_number, int queue_len)
46 struct sockaddr_in sin;
49 memset(&sin, 0, sizeof(sin));
50 sin.sin_family = AF_INET;
51 if (ip_addr == NULL) {
52 sin.sin_addr.s_addr = INADDR_ANY;
54 sin.sin_addr.s_addr = inet_addr(ip_addr);
57 if (sin.sin_addr.s_addr == INADDR_NONE) {
58 sin.sin_addr.s_addr = INADDR_ANY;
61 if (port_number == 0) {
62 lprintf(1, "Cannot start: no port number specified.\n");
65 sin.sin_port = htons((u_short) port_number);
67 s = socket(PF_INET, SOCK_STREAM, (getprotobyname("tcp")->p_proto));
69 lprintf(1, "Can't create a socket: %s\n", strerror(errno));
72 /** Set some socket options that make sense. */
74 setsockopt(s, SOL_SOCKET, SO_REUSEADDR, &i, sizeof(i));
76 if (bind(s, (struct sockaddr *) &sin, sizeof(sin)) < 0) {
77 lprintf(1, "Can't bind: %s\n", strerror(errno));
80 if (listen(s, queue_len) < 0) {
81 lprintf(1, "Can't listen: %s\n", strerror(errno));
90 * \brief Create a Unix domain socket and listen on it
91 * \param sockpath file name of the unix domain socket
92 * \param queue_len queue size of the kernel fifo????
94 int ig_uds_server(char *sockpath, int queue_len)
96 struct sockaddr_un addr;
101 actual_queue_len = queue_len;
102 if (actual_queue_len < 5) actual_queue_len = 5;
104 i = unlink(sockpath);
105 if (i != 0) if (errno != ENOENT) {
106 lprintf(1, "citserver: can't unlink %s: %s\n",
107 sockpath, strerror(errno));
111 memset(&addr, 0, sizeof(addr));
112 addr.sun_family = AF_UNIX;
113 safestrncpy(addr.sun_path, sockpath, sizeof addr.sun_path);
115 s = socket(AF_UNIX, SOCK_STREAM, 0);
117 lprintf(1, "citserver: Can't create a socket: %s\n",
122 if (bind(s, (struct sockaddr *)&addr, sizeof(addr)) < 0) {
123 lprintf(1, "citserver: Can't bind: %s\n",
128 if (listen(s, actual_queue_len) < 0) {
129 lprintf(1, "citserver: Can't listen: %s\n",
134 chmod(sockpath, 0777);
142 * \brief Read data from the client socket.
143 * \param sock socket fd to read from ???
144 * \param buf buffer to read into
145 * \param bytes how large is the read buffer?
146 * \param timeout how long should we wait for input?
147 * \return values are\
148 * 1 Requested number of bytes has been read.\
149 * 0 Request timed out.\
150 * -1 Connection is broken, or other error.
152 int client_read_to(int sock, char *buf, int bytes, int timeout)
162 return (client_read_ssl(buf, bytes, timeout));
167 while (len < bytes) {
173 retval = select((sock) + 1, &rfds, NULL, NULL, &tv);
174 if (FD_ISSET(sock, &rfds) == 0) {
178 rlen = read(sock, &buf[len], bytes - len);
181 lprintf(2, "client_read() failed: %s\n",
189 write(2, "\033[32m", 5);
190 write(2, buf, bytes);
191 write(2, "\033[30m", 5);
197 * \brief write data to the client
198 * \param buf data to write to the client
199 * \param count size of buffer
201 ssize_t client_write(const void *buf, size_t count)
204 if (WC->burst != NULL) {
206 realloc(WC->burst, (WC->burst_len + count + 2));
207 memcpy(&WC->burst[WC->burst_len], buf, count);
208 WC->burst_len += count;
213 client_write_ssl((char *) buf, count);
218 write(2, "\033[34m", 5);
219 write(2, buf, count);
220 write(2, "\033[30m", 5);
222 return (write(WC->http_sock, buf, count));
226 * \brief what burst???
228 void begin_burst(void)
230 if (WC->burst != NULL) {
235 WC->burst = malloc(SIZ);
240 * \brief uses the same calling syntax as compress2(), but it
241 * creates a stream compatible with HTTP "Content-encoding: gzip"
244 #define DEF_MEM_LEVEL 8 /**< memlevel??? */
245 #define OS_CODE 0x03 /**< unix */
246 int ZEXPORT compress_gzip(Bytef * dest, /**< compressed buffer*/
247 uLongf * destLen, /**< length of the compresed data */
248 const Bytef * source, /**< source to encode */
249 uLong sourceLen, /**< length of the source to encode */
250 int level) /**< what level??? */
252 const int gz_magic[2] = { 0x1f, 0x8b }; /** gzip magic header */
254 /** write gzip header */
255 sprintf((char *) dest, "%c%c%c%c%c%c%c%c%c%c",
256 gz_magic[0], gz_magic[1], Z_DEFLATED,
257 0 /*flags */ , 0, 0, 0, 0 /*time */ , 0 /** xflags */ ,
263 stream.next_in = (Bytef *) source;
264 stream.avail_in = (uInt) sourceLen;
265 stream.next_out = dest + 10L; // after header
266 stream.avail_out = (uInt) * destLen;
267 if ((uLong) stream.avail_out != *destLen)
270 stream.zalloc = (alloc_func) 0;
271 stream.zfree = (free_func) 0;
272 stream.opaque = (voidpf) 0;
274 err = deflateInit2(&stream, level, Z_DEFLATED, -MAX_WBITS,
275 DEF_MEM_LEVEL, Z_DEFAULT_STRATEGY);
279 err = deflate(&stream, Z_FINISH);
280 if (err != Z_STREAM_END) {
282 return err == Z_OK ? Z_BUF_ERROR : err;
284 *destLen = stream.total_out + 10L;
286 /* write CRC and Length */
287 uLong crc = crc32(0L, source, sourceLen);
289 for (n = 0; n < 4; ++n, ++*destLen) {
290 dest[*destLen] = (int) (crc & 0xff);
293 uLong len = stream.total_in;
294 for (n = 0; n < 4; ++n, ++*destLen) {
295 dest[*destLen] = (int) (len & 0xff);
298 err = deflateEnd(&stream);
304 * \brief what burst???
311 if (WC->burst == NULL)
314 the_len = WC->burst_len;
315 the_data = WC->burst;
321 /* Handle gzip compression */
323 char *compressed_data = NULL;
324 uLongf compressed_len;
326 compressed_len = (uLongf) ((the_len * 101) / 100) + 100;
327 compressed_data = malloc(compressed_len);
329 if (compress_gzip((Bytef *) compressed_data,
332 (uLongf) the_len, Z_BEST_SPEED) == Z_OK) {
333 wprintf("Content-encoding: gzip\r\n");
335 the_data = compressed_data;
336 the_len = compressed_len;
338 free(compressed_data);
341 #endif /* HAVE_ZLIB */
343 wprintf("Content-length: %d\r\n\r\n", the_len);
344 client_write(the_data, the_len);
352 * \brief Read data from the client socket with default timeout.
353 * (This is implemented in terms of client_read_to() and could be
354 * justifiably moved out of sysdep.c)
355 * \param sock the socket fd to read from???
356 * \param buf the buffer to write to
357 * \param bytes how large is the buffer
359 int client_read(int sock, char *buf, int bytes)
361 return (client_read_to(sock, buf, bytes, SLEEPING));
366 * \brief Get a LF-terminated line of text from the client.
367 * (This is implemented in terms of client_read() and could be
368 * justifiably moved out of sysdep.c)
369 * \param sock socket fd to get client line from???
370 * \param buf buffer to write read data to
371 * \param bufsiz how many bytes to read
372 * \return numer of bytes read???
374 int client_getln(int sock, char *buf, int bufsiz)
378 /** Read one character at a time.*/
380 retval = client_read(sock, &buf[i], 1);
381 if (retval != 1 || buf[i] == '\n' || i == (bufsiz-1))
385 /** If we got a long line, discard characters until the newline. */
387 while (buf[i] != '\n' && retval == 1)
388 retval = client_read(sock, &buf[i], 1);
391 * Strip any trailing non-printable characters.
394 while ((strlen(buf) > 0) && (!isprint(buf[strlen(buf) - 1]))) {
395 buf[strlen(buf) - 1] = 0;
402 * \brief Start running as a daemon.
403 * param do_close_stdio Only close stdio if set.
405 void start_daemon(int do_close_stdio)
407 if (do_close_stdio) {
412 signal(SIGHUP, SIG_IGN);
413 signal(SIGINT, SIG_IGN);
414 signal(SIGQUIT, SIG_IGN);
419 void spawn_another_worker_thread()
421 pthread_t SessThread; /**< Thread descriptor */
422 pthread_attr_t attr; /**< Thread attributes */
425 lprintf(3, "Creating a new thread\n");
427 /** set attributes for the new thread */
428 pthread_attr_init(&attr);
429 pthread_attr_setdetachstate(&attr, PTHREAD_CREATE_DETACHED);
432 * Our per-thread stacks need to be bigger than the default size, otherwise
433 * the MIME parser crashes on FreeBSD, and the IMAP service crashes on
436 if ((ret = pthread_attr_setstacksize(&attr, 1024 * 1024))) {
437 lprintf(1, "pthread_attr_setstacksize: %s\n",
439 pthread_attr_destroy(&attr);
442 /** now create the thread */
443 if (pthread_create(&SessThread, &attr,
444 (void *(*)(void *)) worker_entry, NULL)
446 lprintf(1, "Can't create thread: %s\n", strerror(errno));
449 /** free up the attributes */
450 pthread_attr_destroy(&attr);
454 * \brief Here's where it all begins.
455 * \param argc number of commandline args
456 * \param argv the commandline arguments
458 int main(int argc, char **argv)
460 pthread_t SessThread; /**< Thread descriptor */
461 pthread_attr_t attr; /**< Thread attributes */
462 int a, i; /**< General-purpose variables */
463 int port = PORT_NUM; /**< Port to listen on */
464 char tracefile[PATH_MAX];
466 char *webcitdir = WEBCITDIR;
470 #endif /* ENABLE_NLS */
471 char uds_listen_path[PATH_MAX]; /**< listen on a unix domain socket? */
473 strcpy(uds_listen_path, "");
475 /** Parse command line */
477 while ((a = getopt(argc, argv, "h:i:p:t:x:cfs")) != EOF)
479 while ((a = getopt(argc, argv, "h:i:p:t:x:cf")) != EOF)
483 webcitdir = strdup(optarg);
486 safestrncpy(ip_addr, optarg, sizeof ip_addr);
491 safestrncpy(uds_listen_path, optarg, sizeof uds_listen_path);
495 safestrncpy(tracefile, optarg, sizeof tracefile);
496 freopen(tracefile, "w", stdout);
497 freopen(tracefile, "w", stderr);
498 freopen(tracefile, "r", stdin);
501 verbosity = atoi(optarg);
507 server_cookie = malloc(256);
508 if (server_cookie != NULL) {
509 safestrncpy(server_cookie,
510 "Set-cookie: wcserver=",
513 (&server_cookie[strlen(server_cookie)],
515 lprintf(2, "gethostname: %s\n",
525 fprintf(stderr, "usage: webserver "
526 "[-i ip_addr] [-p http_port] "
527 "[-t tracefile] [-c] [-f] "
531 "[remotehost [remoteport]]\n");
536 ctdlhost = argv[optind];
538 ctdlport = argv[optind];
540 /** Tell 'em who's in da house */
541 lprintf(1, SERVER "\n");
542 lprintf(1, "Copyright (C) 1996-2005 by the Citadel development team.\n"
543 "This software is distributed under the terms of the "
544 "GNU General Public License.\n\n"
547 lprintf(9, "Changing directory to %s\n", webcitdir);
548 if (chdir(webcitdir) != 0) {
552 /** initialize the International Bright Young Thing */
555 initialize_locales();
557 locale = setlocale(LC_ALL, "");
559 mo = malloc(strlen(webcitdir) + 20);
560 sprintf(mo, "%s/locale", webcitdir);
561 lprintf(9, "Message catalog directory: %s\n",
562 bindtextdomain("webcit", mo)
565 lprintf(9, "Text domain: %s\n",
568 lprintf(9, "Text domain Charset: %s\n",
569 bind_textdomain_codeset("webcit","UTF8")
573 initialize_viewdefs();
577 * Set up a place to put thread-specific data.
578 * We only need a single pointer per thread - it points to the
579 * wcsession struct to which the thread is currently bound.
581 if (pthread_key_create(&MyConKey, NULL) != 0) {
582 lprintf(1, "Can't create TSD key: %s\n", strerror(errno));
586 * Set up a place to put thread-specific SSL data.
587 * We don't stick this in the wcsession struct because SSL starts
588 * up before the session is bound, and it gets torn down between
592 if (pthread_key_create(&ThreadSSL, NULL) != 0) {
593 lprintf(1, "Can't create TSD key: %s\n", strerror(errno));
598 * Bind the server to our favorite port.
599 * There is no need to check for errors, because ig_tcp_server()
600 * exits if it doesn't succeed.
603 if (strlen(uds_listen_path) > 0) {
604 lprintf(2, "Attempting to create listener socket at %s...\n", uds_listen_path);
605 msock = ig_uds_server(uds_listen_path, LISTEN_QUEUE_LENGTH);
608 lprintf(2, "Attempting to bind to port %d...\n", port);
609 msock = ig_tcp_server(ip_addr, port, LISTEN_QUEUE_LENGTH);
612 lprintf(2, "Listening on socket %d\n", msock);
613 signal(SIGPIPE, SIG_IGN);
615 pthread_mutex_init(&SessionListMutex, NULL);
618 * Start up the housekeeping thread
620 pthread_attr_init(&attr);
621 pthread_attr_setdetachstate(&attr, PTHREAD_CREATE_DETACHED);
622 pthread_create(&SessThread, &attr,
623 (void *(*)(void *)) housekeeping_loop, NULL);
627 * If this is an HTTPS server, fire up SSL
635 /** Start a few initial worker threads */
636 for (i = 0; i < (MIN_WORKER_THREADS); ++i) {
637 spawn_another_worker_thread();
640 /* now the original thread becomes another worker */
647 * Entry point for worker threads
649 void worker_entry(void)
654 int fail_this_transaction = 0;
657 /** Only one thread can accept at a time */
658 fail_this_transaction = 0;
659 ssock = accept(msock, NULL, 0);
661 lprintf(2, "accept() failed: %s\n",
664 /** Set the SO_REUSEADDR socket option */
666 setsockopt(ssock, SOL_SOCKET, SO_REUSEADDR,
669 /** If we are an HTTPS server, go crypto now. */
672 if (starttls(ssock) != 0) {
673 fail_this_transaction = 1;
679 if (fail_this_transaction == 0) {
680 /** Perform an HTTP transaction... */
682 /** ...and close the socket. */
683 lingering_close(ssock);
688 } while (!time_to_die);
694 * \brief logprintf. log messages
695 * logs to stderr if loglevel is lower than the verbosity set at startup
696 * \param loglevel level of the message
697 * \param format the printf like format string
698 * \param ... the strings to put into format
700 int lprintf(int loglevel, const char *format, ...)
704 if (loglevel <= verbosity) {
705 va_start(ap, format);
706 vfprintf(stderr, format, ap);