1/*
2 * lws-minimal-http-server-eventlib-smp
3 *
4 * Written in 2010-2019 by Andy Green <andy@warmcat.com>
5 *
6 * This file is made available under the Creative Commons CC0 1.0
7 * Universal Public Domain Dedication.
8 *
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.
11 *
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.
15 */
16
17#include <libwebsockets.h>
18#include <string.h>
19#include <signal.h>
20
21#if defined(WIN32)
22#define HAVE_STRUCT_TIMESPEC
23#if defined(pid_t)
24#undef pid_t
25#endif
26#endif
27
28#include <pthread.h>
29
30#define COUNT_THREADS 8
31
32static struct lws_context *context;
33static volatile int interrupted;
34
35static const struct lws_http_mount mount = {
36	/* .mount_next */		NULL,		/* linked-list "next" */
37	/* .mountpoint */		"/",		/* mountpoint URL */
38	/* .origin */			"./mount-origin", /* serve from dir */
39	/* .def */			"index.html",	/* default filename */
40	/* .protocol */			NULL,
41	/* .cgienv */			NULL,
42	/* .extra_mimetypes */		NULL,
43	/* .interpret */		NULL,
44	/* .cgi_timeout */		0,
45	/* .cache_max_age */		0,
46	/* .auth_mask */		0,
47	/* .cache_reusable */		0,
48	/* .cache_revalidate */		0,
49	/* .cache_intermediaries */	0,
50	/* .origin_protocol */		LWSMPRO_FILE,	/* files in a dir */
51	/* .mountpoint_len */		1,		/* char count */
52	/* .basic_auth_login_file */	NULL,
53};
54
55void *thread_service(void *threadid)
56{
57	while (lws_service_tsi(context, 10000,
58			       (int)(lws_intptr_t)threadid) >= 0 &&
59	       !interrupted)
60		;
61
62	pthread_exit(NULL);
63
64	return NULL;
65}
66
67void signal_cb(void *handle, int signum)
68{
69	interrupted = 1;
70
71	switch (signum) {
72	case SIGTERM:
73	case SIGINT:
74		break;
75	default:
76		lwsl_err("%s: signal %d\n", __func__, signum);
77		break;
78	}
79	lws_context_destroy(context);
80}
81
82void sigint_handler(int sig)
83{
84	signal_cb(NULL, sig);
85}
86
87int main(int argc, const char **argv)
88{
89	pthread_t pthread_service[COUNT_THREADS];
90	struct lws_context_creation_info info;
91	const char *p;
92	void *retval;
93	int n, logs = LLL_USER | LLL_ERR | LLL_WARN | LLL_NOTICE
94			/* for LLL_ verbosity above NOTICE to be built into lws,
95			 * lws must have been configured and built with
96			 * -DCMAKE_BUILD_TYPE=DEBUG instead of =RELEASE */
97			/* | LLL_INFO */ /* | LLL_PARSER */ /* | LLL_HEADER */
98			/* | LLL_EXT */ /* | LLL_CLIENT */ /* | LLL_LATENCY */
99			/* | LLL_DEBUG */;
100
101	if ((p = lws_cmdline_option(argc, argv, "-d")))
102		logs = atoi(p);
103
104	lws_set_log_level(logs, NULL);
105	lwsl_user("LWS minimal http server eventlib SMP | visit http://localhost:7681\n");
106	lwsl_user(" [-s (ssl)] [--uv (libuv)] [--ev (libev)] [--event (libevent)]\n");
107	lwsl_user("WARNING: Not stable, under development!\n");
108
109	memset(&info, 0, sizeof info); /* otherwise uninitialized garbage */
110	info.port = 7681;
111	info.mounts = &mount;
112	info.error_document_404 = "/404.html";
113	info.pcontext = &context;
114	info.signal_cb = signal_cb;
115	info.options =
116		LWS_SERVER_OPTION_HTTP_HEADERS_SECURITY_BEST_PRACTICES_ENFORCE;
117
118	if ((p = lws_cmdline_option(argc, argv, "-t"))) {
119		info.count_threads = (unsigned int)atoi(p);
120		if (info.count_threads < 1 || info.count_threads > LWS_MAX_SMP)
121			return 1;
122	} else
123		info.count_threads = COUNT_THREADS;
124
125#if defined(LWS_WITH_TLS)
126	if (lws_cmdline_option(argc, argv, "-s")) {
127		info.options |= LWS_SERVER_OPTION_DO_SSL_GLOBAL_INIT;
128		info.ssl_cert_filepath = "localhost-100y.cert";
129		info.ssl_private_key_filepath = "localhost-100y.key";
130	}
131#endif
132
133	if (lws_cmdline_option(argc, argv, "--uv"))
134		info.options |= LWS_SERVER_OPTION_LIBUV;
135	else
136		if (lws_cmdline_option(argc, argv, "--event"))
137			info.options |= LWS_SERVER_OPTION_LIBEVENT;
138		else
139			if (lws_cmdline_option(argc, argv, "--ev"))
140				info.options |= LWS_SERVER_OPTION_LIBEV;
141			else
142				if (lws_cmdline_option(argc, argv, "--glib"))
143					info.options |= LWS_SERVER_OPTION_GLIB;
144				else
145					signal(SIGINT, sigint_handler);
146
147	context = lws_create_context(&info);
148	if (!context) {
149		lwsl_err("lws init failed\n");
150		return 1;
151	}
152
153	lwsl_notice("  Service threads: %d\n", lws_get_count_threads(context));
154
155	/* start all the service threads */
156
157	for (n = 0; n < lws_get_count_threads(context); n++)
158		if (pthread_create(&pthread_service[n], NULL, thread_service,
159				   (void *)(lws_intptr_t)n))
160			lwsl_err("Failed to start service thread\n");
161
162	/* wait for all the service threads to exit */
163
164	while ((--n) >= 0)
165		pthread_join(pthread_service[n], &retval);
166
167	lwsl_notice("%s: calling external context destroy\n", __func__);
168	lws_context_destroy(context);
169
170	return 0;
171}
172