4 * This contains a simple multithreaded TCP server manager. It sits around
5 * waiting on the specified port for incoming HTTP connections. When a
6 * connection is established, it calls context_loop() from context_loop.c.
11 #include "webserver.h"
18 int vsnprintf(char *buf, size_t max, const char *fmt, va_list argp);
21 int verbosity = 9; /* Logging level */
22 int msock; /* master listening socket */
23 int is_https = 0; /* Nonzero if I am an HTTPS service */
24 int follow_xff = 0; /* Follow X-Forwarded-For: header */
25 int home_specified = 0; /* did the user specify a homedir? */
26 int time_to_die = 0; /* Nonzero if server is shutting down */
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 ctdl_key_dir[PATH_MAX]=SSL_DIR;
34 char file_crpt_file_key[PATH_MAX]="";
35 char file_crpt_file_csr[PATH_MAX]="";
36 char file_crpt_file_cer[PATH_MAX]="";
38 char socket_dir[PATH_MAX]; /* where to talk to our citadel server */
39 static const char editor_absolut_dir[PATH_MAX]=EDITORDIR; /* nailed to what configure gives us. */
40 static char static_dir[PATH_MAX]; /* calculated on startup */
41 static char static_local_dir[PATH_MAX]; /* calculated on startup */
42 char *static_dirs[]={ /* needs same sort order as the web mapping */
43 (char*)static_dir, /* our templates on disk */
44 (char*)static_local_dir, /* user provided templates disk */
45 (char*)editor_absolut_dir /* the editor on disk */
49 * Subdirectories from which the client may request static content
51 * (If you add more, remember to increment 'ndirs' below)
53 char *static_content_dirs[] = {
54 "static", /** static templates */
55 "static.local", /** site local static templates */
56 "tiny_mce" /** rich text editor */
62 char *server_cookie = NULL; /* our Cookie connection to the client */
63 int http_port = PORT_NUM; /* Port to listen on */
64 char *ctdlhost = DEFAULT_HOST; /* our name */
65 char *ctdlport = DEFAULT_PORT; /* our Port */
66 int setup_wizard = 0; /* should we run the setup wizard? \todo */
67 char wizard_filename[PATH_MAX]; /* where's the setup wizard? */
68 int running_as_daemon = 0; /* should we deamonize on startup? */
72 * This is a generic function to set up a master socket for listening on
73 * a TCP port. The server shuts down if the bind fails.
75 * ip_addr IP address to bind
76 * port_number port number to bind
77 * queue_len number of incoming connections to allow in the queue
79 int ig_tcp_server(char *ip_addr, int port_number, int queue_len)
81 struct sockaddr_in sin;
84 memset(&sin, 0, sizeof(sin));
85 sin.sin_family = AF_INET;
86 if (ip_addr == NULL) {
87 sin.sin_addr.s_addr = INADDR_ANY;
89 sin.sin_addr.s_addr = inet_addr(ip_addr);
92 if (sin.sin_addr.s_addr == INADDR_NONE) {
93 sin.sin_addr.s_addr = INADDR_ANY;
96 if (port_number == 0) {
97 lprintf(1, "Cannot start: no port number specified.\n");
100 sin.sin_port = htons((u_short) port_number);
102 s = socket(PF_INET, SOCK_STREAM, (getprotobyname("tcp")->p_proto));
104 lprintf(1, "Can't create a socket: %s\n", strerror(errno));
107 /* Set some socket options that make sense. */
109 setsockopt(s, SOL_SOCKET, SO_REUSEADDR, &i, sizeof(i));
111 fcntl(s, F_SETFL, O_NONBLOCK); /* maide: this statement is incorrect
112 there should be a preceding F_GETFL
113 and a bitwise OR with the previous
116 if (bind(s, (struct sockaddr *) &sin, sizeof(sin)) < 0) {
117 lprintf(1, "Can't bind: %s\n", strerror(errno));
120 if (listen(s, queue_len) < 0) {
121 lprintf(1, "Can't listen: %s\n", strerror(errno));
130 * \brief Create a Unix domain socket and listen on it
131 * \param sockpath file name of the unix domain socket
132 * \param queue_len Number of incoming connections to allow in the queue
134 int ig_uds_server(char *sockpath, int queue_len)
136 struct sockaddr_un addr;
139 int actual_queue_len;
141 actual_queue_len = queue_len;
142 if (actual_queue_len < 5) actual_queue_len = 5;
144 i = unlink(sockpath);
145 if (i != 0) if (errno != ENOENT) {
146 lprintf(1, "webserver: can't unlink %s: %s\n",
147 sockpath, strerror(errno));
151 memset(&addr, 0, sizeof(addr));
152 addr.sun_family = AF_UNIX;
153 safestrncpy(addr.sun_path, sockpath, sizeof addr.sun_path);
155 s = socket(AF_UNIX, SOCK_STREAM, 0);
157 lprintf(1, "webserver: Can't create a socket: %s\n",
162 if (bind(s, (struct sockaddr *)&addr, sizeof(addr)) < 0) {
163 lprintf(1, "webserver: Can't bind: %s\n",
168 if (listen(s, actual_queue_len) < 0) {
169 lprintf(1, "webserver: Can't listen: %s\n",
174 chmod(sockpath, 0777);
182 * \brief Read data from the client socket.
183 * \param sock socket fd to read from
184 * \param buf buffer to read into
185 * \param bytes number of bytes to read
186 * \param timeout Number of seconds to wait before timing out
187 * \return values are\
188 * 1 Requested number of bytes has been read.\
189 * 0 Request timed out.\
190 * -1 Connection is broken, or other error.
192 int client_read_to(int sock, char *buf, int bytes, int timeout)
202 return (client_read_ssl(buf, bytes, timeout));
207 while (len < bytes) {
213 retval = select((sock) + 1, &rfds, NULL, NULL, &tv);
214 if (FD_ISSET(sock, &rfds) == 0) {
218 rlen = read(sock, &buf[len], bytes - len);
221 lprintf(2, "client_read() failed: %s\n",
229 write(2, "\033[32m", 5);
230 write(2, buf, bytes);
231 write(2, "\033[30m", 5);
237 * \brief write data to the client
238 * \param buf data to write to the client
239 * \param count size of buffer
241 ssize_t client_write(const void *buf, size_t count)
245 size_t bytesWritten = 0;
250 if (WC->burst != NULL) {
251 if ((WC->burst_len + count) >= WC->burst_alloc) {
252 newalloc = (WC->burst_alloc * 2);
253 if ((WC->burst_len + count) >= newalloc) {
256 newptr = realloc(WC->burst, newalloc);
257 if (newptr != NULL) {
259 WC->burst_alloc = newalloc;
262 if ((WC->burst_len + count) < WC->burst_alloc) {
263 memcpy(&WC->burst[WC->burst_len], buf, count);
264 WC->burst_len += count;
273 client_write_ssl((char *) buf, count);
278 write(2, "\033[34m", 5);
279 write(2, buf, count);
280 write(2, "\033[30m", 5);
283 while (bytesWritten < count) {
284 if ((fdflags & O_NONBLOCK) == O_NONBLOCK) {
286 FD_SET(WC->http_sock, &wset);
287 if (select(1, NULL, &wset, NULL, NULL) == -1) {
288 lprintf(2, "client_write: Socket select failed (%s)\n", strerror(errno));
293 if ((res = write(WC->http_sock, (char*)buf + bytesWritten,
294 count - bytesWritten)) == -1) {
295 lprintf(2, "client_write: Socket write failed (%s)\n", strerror(errno));
305 * \brief Begin buffering HTTP output so we can transmit it all in one write operation later.
307 void begin_burst(void)
309 if (WC->burst != NULL) {
314 WC->burst_alloc = 32768;
315 WC->burst = malloc(WC->burst_alloc);
320 * \brief uses the same calling syntax as compress2(), but it
321 * creates a stream compatible with HTTP "Content-encoding: gzip"
324 #define DEF_MEM_LEVEL 8 /*< memlevel??? */
325 #define OS_CODE 0x03 /*< unix */
326 int ZEXPORT compress_gzip(Bytef * dest, /*< compressed buffer*/
327 size_t * destLen, /*< length of the compresed data */
328 const Bytef * source, /*< source to encode */
329 uLong sourceLen, /*< length of source to encode */
330 int level) /*< compression level */
332 const int gz_magic[2] = { 0x1f, 0x8b }; /* gzip magic header */
334 /* write gzip header */
335 snprintf((char *) dest, *destLen,
336 "%c%c%c%c%c%c%c%c%c%c",
337 gz_magic[0], gz_magic[1], Z_DEFLATED,
338 0 /*flags */ , 0, 0, 0, 0 /*time */ , 0 /* xflags */ ,
344 stream.next_in = (Bytef *) source;
345 stream.avail_in = (uInt) sourceLen;
346 stream.next_out = dest + 10L; // after header
347 stream.avail_out = (uInt) * destLen;
348 if ((uLong) stream.avail_out != *destLen)
351 stream.zalloc = (alloc_func) 0;
352 stream.zfree = (free_func) 0;
353 stream.opaque = (voidpf) 0;
355 err = deflateInit2(&stream, level, Z_DEFLATED, -MAX_WBITS,
356 DEF_MEM_LEVEL, Z_DEFAULT_STRATEGY);
360 err = deflate(&stream, Z_FINISH);
361 if (err != Z_STREAM_END) {
363 return err == Z_OK ? Z_BUF_ERROR : err;
365 *destLen = stream.total_out + 10L;
367 /* write CRC and Length */
368 uLong crc = crc32(0L, source, sourceLen);
370 for (n = 0; n < 4; ++n, ++*destLen) {
371 dest[*destLen] = (int) (crc & 0xff);
374 uLong len = stream.total_in;
375 for (n = 0; n < 4; ++n, ++*destLen) {
376 dest[*destLen] = (int) (len & 0xff);
379 err = deflateEnd(&stream);
385 * \brief Finish buffering HTTP output. [Compress using zlib and] output with a Content-Length: header.
392 if (WC->burst == NULL)
395 the_len = WC->burst_len;
396 the_data = WC->burst;
403 /* Perform gzip compression, if enabled and supported by client */
405 char *compressed_data = NULL;
406 size_t compressed_len;
408 compressed_len = ((the_len * 101) / 100) + 100;
409 compressed_data = malloc(compressed_len);
411 if (compress_gzip((Bytef *) compressed_data,
414 (uLongf) the_len, Z_BEST_SPEED) == Z_OK) {
415 wprintf("Content-encoding: gzip\r\n");
417 the_data = compressed_data;
418 the_len = compressed_len;
420 free(compressed_data);
423 #endif /* HAVE_ZLIB */
425 wprintf("Content-length: %d\r\n\r\n", the_len);
426 client_write(the_data, the_len);
434 * \brief Read data from the client socket with default timeout.
435 * (This is implemented in terms of client_read_to() and could be
436 * justifiably moved out of sysdep.c)
437 * \param sock the socket fd to read from
438 * \param buf the buffer to write to
439 * \param bytes Number of bytes to read
441 int client_read(int sock, char *buf, int bytes)
443 return (client_read_to(sock, buf, bytes, SLEEPING));
448 * \brief Get a LF-terminated line of text from the client.
449 * (This is implemented in terms of client_read() and could be
450 * justifiably moved out of sysdep.c)
451 * \param sock socket fd to get client line from
452 * \param buf buffer to write read data to
453 * \param bufsiz how many bytes to read
454 * \return number of bytes read???
456 int client_getln(int sock, char *buf, int bufsiz)
460 /* Read one character at a time.*/
462 retval = client_read(sock, &buf[i], 1);
463 if (retval != 1 || buf[i] == '\n' || i == (bufsiz-1))
465 if ( (!isspace(buf[i])) && (!isprint(buf[i])) ) {
466 /* Non printable character recieved from client */
471 /* If we got a long line, discard characters until the newline. */
473 while (buf[i] != '\n' && retval == 1)
474 retval = client_read(sock, &buf[i], 1);
477 * Strip any trailing non-printable characters.
480 while ((i > 0) && (!isprint(buf[i - 1]))) {
487 * \brief shut us down the regular way.
488 * param signum the signal we want to forward
491 void graceful_shutdown_watcher(int signum) {
492 lprintf (1, "bye; shutting down watcher.");
493 kill(current_child, signum);
494 if (signum != SIGHUP)
499 * \brief shut us down the regular way.
500 * param signum the signal we want to forward
503 void graceful_shutdown(int signum) {
504 // kill(current_child, signum);
509 lprintf (1, "bye going down gracefull.[%d][%s]\n", signum, wd);
521 * \brief Start running as a daemon.
523 * param do_close_stdio Only close stdio if set.
527 * Start running as a daemon.
529 void start_daemon(char *pid_file)
538 /* Close stdin/stdout/stderr and replace them with /dev/null.
539 * We don't just call close() because we don't want these fd's
540 * to be reused for other files.
544 signal(SIGHUP, SIG_IGN);
545 signal(SIGINT, SIG_IGN);
546 signal(SIGQUIT, SIG_IGN);
555 freopen("/dev/null", "r", stdin);
556 freopen("/dev/null", "w", stdout);
557 freopen("/dev/null", "w", stderr);
558 signal(SIGTERM, graceful_shutdown_watcher);
559 signal(SIGHUP, graceful_shutdown_watcher);
562 current_child = fork();
565 if (current_child < 0) {
570 else if (current_child == 0) { // child process
571 // signal(SIGTERM, graceful_shutdown);
572 signal(SIGHUP, graceful_shutdown);
574 return; /* continue starting webcit. */
577 else { // watcher process
578 // signal(SIGTERM, SIG_IGN);
579 // signal(SIGHUP, SIG_IGN);
581 fp = fopen(pid_file, "w");
583 fprintf(fp, "%d\n", getpid());
587 waitpid(current_child, &status, 0);
592 /* Did the main process exit with an actual exit code? */
593 if (WIFEXITED(status)) {
595 /* Exit code 0 means the watcher should exit */
596 if (WEXITSTATUS(status) == 0) {
600 /* Exit code 101-109 means the watcher should exit */
601 else if ( (WEXITSTATUS(status) >= 101) && (WEXITSTATUS(status) <= 109) ) {
605 /* Any other exit code means we should restart. */
611 /* Any other type of termination (signals, etc.) should also restart. */
616 } while (do_restart);
621 exit(WEXITSTATUS(status));
625 * \brief Spawn an additional worker thread into the pool.
627 void spawn_another_worker_thread()
629 pthread_t SessThread; /*< Thread descriptor */
630 pthread_attr_t attr; /*< Thread attributes */
633 lprintf(3, "Creating a new thread\n");
635 /* set attributes for the new thread */
636 pthread_attr_init(&attr);
637 pthread_attr_setdetachstate(&attr, PTHREAD_CREATE_DETACHED);
640 * Our per-thread stacks need to be bigger than the default size, otherwise
641 * the MIME parser crashes on FreeBSD, and the IMAP service crashes on
644 if ((ret = pthread_attr_setstacksize(&attr, 1024 * 1024))) {
645 lprintf(1, "pthread_attr_setstacksize: %s\n",
647 pthread_attr_destroy(&attr);
650 /* now create the thread */
651 if (pthread_create(&SessThread, &attr,
652 (void *(*)(void *)) worker_entry, NULL)
654 lprintf(1, "Can't create thread: %s\n", strerror(errno));
657 /* free up the attributes */
658 pthread_attr_destroy(&attr);
662 * \brief Here's where it all begins.
663 * \param argc number of commandline args
664 * \param argv the commandline arguments
666 int main(int argc, char **argv)
668 pthread_t SessThread; /*< Thread descriptor */
669 pthread_attr_t attr; /*< Thread attributes */
670 int a, i; /*< General-purpose variables */
671 char tracefile[PATH_MAX];
672 char ip_addr[256]="0.0.0.0";
673 char dirbuffer[PATH_MAX]="";
676 int home_specified=0;
677 char relhome[PATH_MAX]="";
678 char webcitdir[PATH_MAX] = DATADIR;
679 char *pidfile = NULL;
685 #endif /* ENABLE_NLS */
686 char uds_listen_path[PATH_MAX]; /*< listen on a unix domain socket? */
688 /* Ensure that we are linked to the correct version of libcitadel */
689 if (libcitadel_version_number() < LIBCITADEL_VERSION_NUMBER) {
690 fprintf(stderr, " You are running libcitadel version %d.%02d\n",
691 (libcitadel_version_number() / 100), (libcitadel_version_number() % 100));
692 fprintf(stderr, "WebCit was compiled against version %d.%02d\n",
693 (LIBCITADEL_VERSION_NUMBER / 100), (LIBCITADEL_VERSION_NUMBER % 100));
697 strcpy(uds_listen_path, "");
699 /* Parse command line */
701 while ((a = getopt(argc, argv, "h:i:p:t:x:dD:cfs")) != EOF)
703 while ((a = getopt(argc, argv, "h:i:p:t:x:dD:cf")) != EOF)
707 hdir = strdup(optarg);
709 if (!relh) safestrncpy(webcitdir, hdir,
712 safestrncpy(relhome, relhome,
714 /* free(hdir); TODO: SHOULD WE DO THIS? */
719 running_as_daemon = 1;
722 pidfile = strdup(optarg);
723 running_as_daemon = 1;
726 safestrncpy(ip_addr, optarg, sizeof ip_addr);
729 http_port = atoi(optarg);
730 if (http_port == 0) {
731 safestrncpy(uds_listen_path, optarg, sizeof uds_listen_path);
735 safestrncpy(tracefile, optarg, sizeof tracefile);
736 freopen(tracefile, "w", stdout);
737 freopen(tracefile, "w", stderr);
738 freopen(tracefile, "r", stdin);
741 verbosity = atoi(optarg);
747 server_cookie = malloc(256);
748 if (server_cookie != NULL) {
749 safestrncpy(server_cookie,
750 "Set-cookie: wcserver=",
753 (&server_cookie[strlen(server_cookie)],
755 lprintf(2, "gethostname: %s\n",
765 fprintf(stderr, "usage: webserver "
766 "[-i ip_addr] [-p http_port] "
767 "[-t tracefile] [-c] [-f] "
772 "[remotehost [remoteport]]\n");
777 ctdlhost = argv[optind];
779 ctdlport = argv[optind];
782 /* daemonize, if we were asked to */
783 if (running_as_daemon) {
784 start_daemon(pidfile);
787 /// signal(SIGTERM, graceful_shutdown);
788 signal(SIGHUP, graceful_shutdown);
791 /* Tell 'em who's in da house */
792 lprintf(1, PACKAGE_STRING "\n");
793 lprintf(1, "Copyright (C) 1996-2008 by the Citadel development team.\n"
794 "This software is distributed under the terms of the "
795 "GNU General Public License.\n\n"
799 /* initialize the International Bright Young Thing */
801 initialize_locales();
802 locale = setlocale(LC_ALL, "");
803 mo = malloc(strlen(webcitdir) + 20);
804 lprintf(9, "Message catalog directory: %s\n", bindtextdomain("webcit", LOCALEDIR"/locale"));
806 lprintf(9, "Text domain: %s\n", textdomain("webcit"));
807 lprintf(9, "Text domain Charset: %s\n", bind_textdomain_codeset("webcit","UTF8"));
811 /* calculate all our path on a central place */
812 /* where to keep our config */
814 #define COMPUTE_DIRECTORY(SUBDIR) memcpy(dirbuffer,SUBDIR, sizeof dirbuffer);\
815 snprintf(SUBDIR,sizeof SUBDIR, "%s%s%s%s%s%s%s", \
816 (home&!relh)?webcitdir:basedir, \
817 ((basedir!=webcitdir)&(home&!relh))?basedir:"/", \
818 ((basedir!=webcitdir)&(home&!relh))?"/":"", \
820 (relhome[0]!='\0')?"/":"",\
822 (dirbuffer[0]!='\0')?"/":"");
824 COMPUTE_DIRECTORY(socket_dir);
825 basedir=WWWDIR "/static";
826 COMPUTE_DIRECTORY(static_dir);
827 basedir=WWWDIR "/static.local";
828 COMPUTE_DIRECTORY(static_local_dir);
830 snprintf(file_crpt_file_key,
831 sizeof file_crpt_file_key,
834 snprintf(file_crpt_file_csr,
835 sizeof file_crpt_file_csr,
838 snprintf(file_crpt_file_cer,
839 sizeof file_crpt_file_cer,
843 /* we should go somewhere we can leave our coredump, if enabled... */
844 lprintf(9, "Changing directory to %s\n", socket_dir);
845 if (chdir(webcitdir) != 0) {
848 initialize_viewdefs();
852 * Set up a place to put thread-specific data.
853 * We only need a single pointer per thread - it points to the
854 * wcsession struct to which the thread is currently bound.
856 if (pthread_key_create(&MyConKey, NULL) != 0) {
857 lprintf(1, "Can't create TSD key: %s\n", strerror(errno));
859 InitialiseSemaphores ();
862 * Set up a place to put thread-specific SSL data.
863 * We don't stick this in the wcsession struct because SSL starts
864 * up before the session is bound, and it gets torn down between
868 if (pthread_key_create(&ThreadSSL, NULL) != 0) {
869 lprintf(1, "Can't create TSD key: %s\n", strerror(errno));
874 * Bind the server to our favorite port.
875 * There is no need to check for errors, because ig_tcp_server()
876 * exits if it doesn't succeed.
879 if (!IsEmptyStr(uds_listen_path)) {
880 lprintf(2, "Attempting to create listener socket at %s...\n", uds_listen_path);
881 msock = ig_uds_server(uds_listen_path, LISTEN_QUEUE_LENGTH);
884 lprintf(2, "Attempting to bind to port %d...\n", http_port);
885 msock = ig_tcp_server(ip_addr, http_port, LISTEN_QUEUE_LENGTH);
888 lprintf(2, "Listening on socket %d\n", msock);
889 signal(SIGPIPE, SIG_IGN);
891 pthread_mutex_init(&SessionListMutex, NULL);
894 * Start up the housekeeping thread
896 pthread_attr_init(&attr);
897 pthread_attr_setdetachstate(&attr, PTHREAD_CREATE_DETACHED);
898 pthread_create(&SessThread, &attr,
899 (void *(*)(void *)) housekeeping_loop, NULL);
903 * If this is an HTTPS server, fire up SSL
911 /* Start a few initial worker threads */
912 for (i = 0; i < (MIN_WORKER_THREADS); ++i) {
913 spawn_another_worker_thread();
916 /* now the original thread becomes another worker */
923 * Entry point for worker threads
925 void worker_entry(void)
929 int fail_this_transaction = 0;
932 fd_set readset, tempset;
937 FD_SET(msock, &readset);
940 /* Only one thread can accept at a time */
941 fail_this_transaction = 0;
945 ret = -1; /* just one at once should select... */
946 begin_critical_section(S_SELECT);
949 if (msock > 0) FD_SET(msock, &tempset);
952 if (msock > 0) ret = select(msock+1, &tempset, NULL, NULL, &tv);
953 end_critical_section(S_SELECT);
954 if ((ret < 0) && (errno != EINTR) && (errno != EAGAIN))
955 {// EINTR and EAGAIN are thrown but not of interest.
956 lprintf(2, "accept() failed:%d %s\n",
957 errno, strerror(errno));
959 else if ((ret > 0) && (msock > 0) && FD_ISSET(msock, &tempset))
960 {// Successfully selected, and still not shutting down? Accept!
961 ssock = accept(msock, NULL, 0);
964 } while ((msock > 0) && (ssock < 0) && (time_to_die == 0));
966 if ((msock == -1)||(time_to_die))
967 {// ok, we're going down.
970 /* the first to come here will have to do the cleanup.
971 * make shure its realy just one.
973 begin_critical_section(S_SHUTDOWN);
979 end_critical_section(S_SHUTDOWN);
981 {// we're the one to cleanup the mess.
982 lprintf(2, "I'm master shutdown: tagging sessions to be killed.\n");
984 lprintf(2, "master shutdown: waiting for others\n");
985 sleeeeeeeeeep(1); // wait so some others might finish...
986 lprintf(2, "master shutdown: cleaning up sessions\n");
988 #ifdef WEBCIT_WITH_CALENDAR_SERVICE
989 lprintf(2, "master shutdown: cleaning up libical\n");
990 free_zone_directory ();
991 icaltimezone_release_zone_tab ();
992 icalmemory_free_ring ();
994 lprintf(2, "master shutdown exiting!.\n");
999 if (ssock < 0 ) continue;
1002 if (ssock > 0) close (ssock);
1003 lprintf(2, "inbetween.");
1005 } else { // Got it? do some real work!
1006 /* Set the SO_REUSEADDR socket option */
1008 setsockopt(ssock, SOL_SOCKET, SO_REUSEADDR,
1011 /* If we are an HTTPS server, go crypto now. */
1014 if (starttls(ssock) != 0) {
1015 fail_this_transaction = 1;
1021 if (fail_this_transaction == 0) {
1023 /* Perform an HTTP transaction... */
1024 context_loop(ssock);
1026 /* Shut down SSL/TLS if required... */
1033 /* ...and close the socket. */
1034 lingering_close(ssock);
1039 } while (!time_to_die);
1041 lprintf (1, "bye\n");
1046 * \brief logprintf. log messages
1047 * logs to stderr if loglevel is lower than the verbosity set at startup
1048 * \param loglevel level of the message
1049 * \param format the printf like format string
1050 * \param ... the strings to put into format
1052 int lprintf(int loglevel, const char *format, ...)
1056 if (loglevel <= verbosity) {
1057 va_start(ap, format);
1058 vfprintf(stderr, format, ap);
1067 * \brief print the actual stack frame.
1069 void wc_backtrace(void)
1071 #ifdef HAVE_BACKTRACE
1072 void *stack_frames[50];
1077 size = backtrace(stack_frames, sizeof(stack_frames) / sizeof(void*));
1078 strings = backtrace_symbols(stack_frames, size);
1079 for (i = 0; i < size; i++) {
1080 if (strings != NULL)
1081 lprintf(1, "%s\n", strings[i]);
1083 lprintf(1, "%p\n", stack_frames[i]);