2 * lws-minimal-http-server-eventlib-smp
4 * Written in 2010-2019 by Andy Green <andy@warmcat.com>
6 * This file is made available under the Creative Commons CC0 1.0
7 * Universal Public Domain Dedication.
9 * This demonstrates a minimal http[s] server that can work with any of the
10 * supported event loop backends, or the default poll() one.
12 * To keep it simple, it serves stuff from the subdirectory
13 * "./mount-origin" of the directory it was started in.
14 * You can change that by changing mount.origin below.
17 #include <libwebsockets.h>
23 #define COUNT_THREADS 8
25 static struct lws_context *context;
26 static volatile int interrupted;
28 static const struct lws_http_mount mount = {
29 /* .mount_next */ NULL, /* linked-list "next" */
30 /* .mountpoint */ "/", /* mountpoint URL */
31 /* .origin */ "./mount-origin", /* serve from dir */
32 /* .def */ "index.html", /* default filename */
35 /* .extra_mimetypes */ NULL,
36 /* .interpret */ NULL,
38 /* .cache_max_age */ 0,
40 /* .cache_reusable */ 0,
41 /* .cache_revalidate */ 0,
42 /* .cache_intermediaries */ 0,
43 /* .origin_protocol */ LWSMPRO_FILE, /* files in a dir */
44 /* .mountpoint_len */ 1, /* char count */
45 /* .basic_auth_login_file */ NULL,
48 void *thread_service(void *threadid)
50 while (lws_service_tsi(context, 10000,
51 (int)(lws_intptr_t)threadid) >= 0 &&
60 void signal_cb(void *handle, int signum)
69 lwsl_err("%s: signal %d\n", __func__, signum);
72 lws_context_destroy(context);
75 void sigint_handler(int sig)
80 int main(int argc, const char **argv)
82 pthread_t pthread_service[COUNT_THREADS];
83 struct lws_context_creation_info info;
86 int n, logs = LLL_USER | LLL_ERR | LLL_WARN | LLL_NOTICE
87 /* for LLL_ verbosity above NOTICE to be built into lws,
88 * lws must have been configured and built with
89 * -DCMAKE_BUILD_TYPE=DEBUG instead of =RELEASE */
90 /* | LLL_INFO */ /* | LLL_PARSER */ /* | LLL_HEADER */
91 /* | LLL_EXT */ /* | LLL_CLIENT */ /* | LLL_LATENCY */
94 if ((p = lws_cmdline_option(argc, argv, "-d")))
97 lws_set_log_level(logs, NULL);
98 lwsl_user("LWS minimal http server eventlib SMP | visit http://localhost:7681\n");
99 lwsl_user(" [-s (ssl)] [--uv (libuv)] [--ev (libev)] [--event (libevent)]\n");
100 lwsl_user("WARNING: Not stable, under development!\n");
102 memset(&info, 0, sizeof info); /* otherwise uninitialized garbage */
104 info.mounts = &mount;
105 info.error_document_404 = "/404.html";
106 info.pcontext = &context;
107 info.signal_cb = signal_cb;
109 LWS_SERVER_OPTION_HTTP_HEADERS_SECURITY_BEST_PRACTICES_ENFORCE;
111 if ((p = lws_cmdline_option(argc, argv, "-t"))) {
112 info.count_threads = atoi(p);
113 if (info.count_threads < 1 || info.count_threads > LWS_MAX_SMP)
116 info.count_threads = COUNT_THREADS;
118 if (lws_cmdline_option(argc, argv, "-s")) {
119 info.options |= LWS_SERVER_OPTION_DO_SSL_GLOBAL_INIT;
120 info.ssl_cert_filepath = "localhost-100y.cert";
121 info.ssl_private_key_filepath = "localhost-100y.key";
124 if (lws_cmdline_option(argc, argv, "--uv"))
125 info.options |= LWS_SERVER_OPTION_LIBUV;
127 if (lws_cmdline_option(argc, argv, "--event"))
128 info.options |= LWS_SERVER_OPTION_LIBEVENT;
130 if (lws_cmdline_option(argc, argv, "--ev"))
131 info.options |= LWS_SERVER_OPTION_LIBEV;
133 signal(SIGINT, sigint_handler);
135 context = lws_create_context(&info);
137 lwsl_err("lws init failed\n");
141 lwsl_notice(" Service threads: %d\n", lws_get_count_threads(context));
143 /* start all the service threads */
145 for (n = 0; n < lws_get_count_threads(context); n++)
146 if (pthread_create(&pthread_service[n], NULL, thread_service,
147 (void *)(lws_intptr_t)n))
148 lwsl_err("Failed to start service thread\n");
150 /* wait for all the service threads to exit */
153 pthread_join(pthread_service[n], &retval);
155 lwsl_notice("%s: calling external context destroy\n", __func__);
156 lws_context_destroy(context);