2 * $Id: webserver.c 7847 2009-10-03 17:57:14Z dothebart $
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.
8 * Copyright (c) 1996-2009 by the citadel.org developers.
9 * This program is released under the terms of the GNU General Public License v3.
13 #include "../webcit.h"
14 #include "../webserver.h"
15 #include "../modules_init.h"
18 #include <CUnit/CUnit.h>
19 #include <CUnit/Basic.h>
20 #include <CUnit/TestDB.h>
23 CU_EXPORT void CU_automated_run_tests(void);
24 CU_EXPORT CU_ErrorCode CU_list_tests_to_file(void);
25 CU_EXPORT void CU_set_output_filename(const char* szFilenameRoot);
27 extern int msock; /* master listening socket */
28 extern int verbosity; /* Logging level */
29 extern char static_icon_dir[PATH_MAX]; /* where should we find our mime icons */
31 int is_https = 0; /* Nonzero if I am an HTTPS service */
32 int follow_xff = 0; /* Follow X-Forwarded-For: header */
33 int home_specified = 0; /* did the user specify a homedir? */
35 extern pthread_mutex_t SessionListMutex;
36 extern pthread_key_t MyConKey;
38 extern void run_tests(void);
39 extern int ig_tcp_server(char *ip_addr, int port_number, int queue_len);
40 extern int ig_uds_server(char *sockpath, int queue_len);
41 extern void webcit_calc_dirs_n_files(int relh, const char *basedir, int home, char *webcitdir, char *relhome);
44 char socket_dir[PATH_MAX]; /* where to talk to our citadel server */
46 char *server_cookie = NULL; /* our Cookie connection to the client */
47 int http_port = PORT_NUM; /* Port to listen on */
48 char *ctdlhost = DEFAULT_HOST; /* our name */
49 char *ctdlport = DEFAULT_PORT; /* our Port */
50 int running_as_daemon = 0; /* should we deamonize on startup? */
51 char wizard_filename[PATH_MAX];
53 StrBuf *Username = NULL; /* the test user... */
54 StrBuf *Passvoid = NULL; /* the test user... */
57 extern int dbg_analyze_msg;
58 extern int dbg_bactrace_template_errors;
59 extern int DumpTemplateI18NStrings;
60 extern StrBuf *I18nDump;
61 void InitTemplateCache(void);
62 extern int LoadTemplates;
67 * Here's where it all begins.
69 int main(int argc, char **argv)
71 size_t basesize = 2; /* how big should strbufs be on creation? */
72 pthread_attr_t attr; /* Thread attributes */
73 int a; /* General-purpose variables */
74 char tracefile[PATH_MAX];
75 char ip_addr[256]="0.0.0.0";
79 char relhome[PATH_MAX]="";
80 char webcitdir[PATH_MAX] = DATADIR;
82 const char *basedir = NULL;
83 char uds_listen_path[PATH_MAX]; /* listen on a unix domain socket? */
86 WildFireInitBacktrace(argv[0], 2);
90 /* Ensure that we are linked to the correct version of libcitadel */
91 if (libcitadel_version_number() < LIBCITADEL_VERSION_NUMBER) {
92 fprintf(stderr, " You are running libcitadel version %d.%02d\n",
93 (libcitadel_version_number() / 100), (libcitadel_version_number() % 100));
94 fprintf(stderr, "WebCit was compiled against version %d.%02d\n",
95 (LIBCITADEL_VERSION_NUMBER / 100), (LIBCITADEL_VERSION_NUMBER % 100));
99 strcpy(uds_listen_path, "");
101 /* Parse command line */
102 while ((a = getopt(argc, argv, "h:i:p:t:T:B:x:U:P:cf:Z")) != EOF)
105 Username = NewStrBufPlain(optarg, -1);
108 Passvoid = NewStrBufPlain(optarg, -1);
111 hdir = strdup(optarg);
114 safestrncpy(webcitdir, hdir, sizeof webcitdir);
117 safestrncpy(relhome, relhome, sizeof relhome);
119 /* free(hdir); TODO: SHOULD WE DO THIS? */
123 case 'B': /* Basesize */
124 basesize = atoi(optarg);
126 StartLibCitadel(basesize);
129 safestrncpy(ip_addr, optarg, sizeof ip_addr);
132 http_port = atoi(optarg);
133 if (http_port == 0) {
134 safestrncpy(uds_listen_path, optarg, sizeof uds_listen_path);
138 safestrncpy(tracefile, optarg, sizeof tracefile);
139 rvfp = freopen(tracefile, "w", stdout);
140 rvfp = freopen(tracefile, "w", stderr);
141 rvfp = freopen(tracefile, "r", stdin);
144 LoadTemplates = atoi(optarg);
145 dbg_analyze_msg = (LoadTemplates && (1<<1)) != 0;
146 dbg_bactrace_template_errors = (LoadTemplates && (1<<2)) != 0;
152 verbosity = atoi(optarg);
158 server_cookie = malloc(256);
159 if (server_cookie != NULL) {
160 safestrncpy(server_cookie,
161 "Set-cookie: wcserver=",
164 (&server_cookie[strlen(server_cookie)],
166 lprintf(2, "gethostname: %s\n",
173 fprintf(stderr, "usage: webcit "
174 "[-i ip_addr] [-p http_port] "
175 "[-t tracefile] [-c] [-f] "
176 "[-T Templatedebuglevel] "
177 "[-Z] [-G i18ndumpfile] "
179 "[-s] [-S cipher_suites]"
181 "[-U Username -P Password]"
183 "[remotehost [remoteport]]\n");
188 ctdlhost = argv[optind];
190 ctdlport = argv[optind];
193 webcit_calc_dirs_n_files(relh, basedir, home, webcitdir, relhome);
194 LoadIconDir(static_icon_dir);
196 /* Tell 'em who's in da house */
197 lprintf(1, PACKAGE_STRING "\n");
198 lprintf(1, "Copyright (C) 1996-2009 by the Citadel development team.\n"
199 "This software is distributed under the terms of the "
200 "GNU General Public License.\n\n"
204 /* initialize the International Bright Young Thing */
206 initialise_modules();
207 initialize_viewdefs();
212 /* Use our own prefix on tzid's generated from system tzdata */
213 icaltimezone_set_tzid_prefix("/citadel.org/");
216 * Set up a place to put thread-specific data.
217 * We only need a single pointer per thread - it points to the
218 * wcsession struct to which the thread is currently bound.
220 if (pthread_key_create(&MyConKey, NULL) != 0) {
221 lprintf(1, "Can't create TSD key: %s\n", strerror(errno));
223 InitialiseSemaphores ();
227 * Start up the housekeeping thread
229 pthread_attr_init(&attr);
230 pthread_attr_setdetachstate(&attr, PTHREAD_CREATE_DETACHED);
235 FreeStrBuf(&Username);
236 FreeStrBuf(&Passvoid);