1/*
2 * lws-minimal-http-server-eventlib
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
21static struct lws_context *context;
22
23static const struct lws_http_mount mount = {
24	/* .mount_next */		NULL,		/* linked-list "next" */
25	/* .mountpoint */		"/",		/* mountpoint URL */
26	/* .origin */			"./mount-origin", /* serve from dir */
27	/* .def */			"index.html",	/* default filename */
28	/* .protocol */			NULL,
29	/* .cgienv */			NULL,
30	/* .extra_mimetypes */		NULL,
31	/* .interpret */		NULL,
32	/* .cgi_timeout */		0,
33	/* .cache_max_age */		0,
34	/* .auth_mask */		0,
35	/* .cache_reusable */		0,
36	/* .cache_revalidate */		0,
37	/* .cache_intermediaries */	0,
38	/* .origin_protocol */		LWSMPRO_FILE,	/* files in a dir */
39	/* .mountpoint_len */		1,		/* char count */
40	/* .basic_auth_login_file */	NULL,
41};
42
43void signal_cb(void *handle, int signum)
44{
45	switch (signum) {
46	case SIGTERM:
47	case SIGINT:
48		break;
49	default:
50		lwsl_err("%s: signal %d\n", __func__, signum);
51		break;
52	}
53	lws_context_destroy(context);
54}
55
56void sigint_handler(int sig)
57{
58	signal_cb(NULL, sig);
59}
60
61int main(int argc, const char **argv)
62{
63	struct lws_context_creation_info info;
64	const char *p;
65	int logs = LLL_USER | LLL_ERR | LLL_WARN | LLL_NOTICE
66			/* for LLL_ verbosity above NOTICE to be built into lws,
67			 * lws must have been configured and built with
68			 * -DCMAKE_BUILD_TYPE=DEBUG instead of =RELEASE */
69			/* | LLL_INFO */ /* | LLL_PARSER */ /* | LLL_HEADER */
70			/* | LLL_EXT */ /* | LLL_CLIENT */ /* | LLL_LATENCY */
71			/* | LLL_DEBUG */;
72
73	if ((p = lws_cmdline_option(argc, argv, "-d")))
74		logs = atoi(p);
75
76	lws_set_log_level(logs, NULL);
77	lwsl_user("LWS minimal http server eventlib | visit http://localhost:7681\n");
78	lwsl_user(" [-s (ssl)] [--uv (libuv)] [--ev (libev)] [--event (libevent)]\n");
79
80	memset(&info, 0, sizeof info); /* otherwise uninitialized garbage */
81	info.port = 7681;
82	info.mounts = &mount;
83	info.error_document_404 = "/404.html";
84	info.pcontext = &context;
85	info.signal_cb = signal_cb;
86	info.options =
87		LWS_SERVER_OPTION_HTTP_HEADERS_SECURITY_BEST_PRACTICES_ENFORCE;
88
89#if defined(LWS_WITH_TLS)
90	if (lws_cmdline_option(argc, argv, "-s")) {
91		info.options |= LWS_SERVER_OPTION_DO_SSL_GLOBAL_INIT;
92		info.ssl_cert_filepath = "localhost-100y.cert";
93		info.ssl_private_key_filepath = "localhost-100y.key";
94	}
95#endif
96
97	if (lws_cmdline_option(argc, argv, "--uv"))
98		info.options |= LWS_SERVER_OPTION_LIBUV;
99	else
100		if (lws_cmdline_option(argc, argv, "--event"))
101			info.options |= LWS_SERVER_OPTION_LIBEVENT;
102		else
103			if (lws_cmdline_option(argc, argv, "--ev"))
104				info.options |= LWS_SERVER_OPTION_LIBEV;
105			else
106				if (lws_cmdline_option(argc, argv, "--glib"))
107					info.options |= LWS_SERVER_OPTION_GLIB;
108				else
109					signal(SIGINT, sigint_handler);
110
111	context = lws_create_context(&info);
112	if (!context) {
113		lwsl_err("lws init failed\n");
114		return 1;
115	}
116
117	while (!lws_service(context, 0))
118		;
119
120	lwsl_info("calling external context destroy\n");
121	lws_context_destroy(context);
122
123	return 0;
124}
125