2 * lws-minimal-http-server-eventlib
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>
21 #define LWS_PLUGIN_STATIC
22 #include "../../../plugins/protocol_lws_mirror.c"
23 #include "../../../plugins/protocol_lws_status.c"
24 #include "../../../plugins/protocol_dumb_increment.c"
25 #include "../../../plugins/protocol_post_demo.c"
27 static struct lws_context *context;
29 static struct lws_protocols protocols[] = {
30 /* first protocol must always be HTTP handler */
32 { "http-only", lws_callback_http_dummy, 0, 0, },
33 LWS_PLUGIN_PROTOCOL_DUMB_INCREMENT,
34 LWS_PLUGIN_PROTOCOL_MIRROR,
35 LWS_PLUGIN_PROTOCOL_LWS_STATUS,
36 LWS_PLUGIN_PROTOCOL_POST_DEMO,
37 { NULL, NULL, 0, 0 } /* terminator */
41 * mount handlers for sections of the URL space
44 static const struct lws_http_mount mount_ziptest = {
45 NULL, /* linked-list pointer to next*/
46 "/ziptest", /* mountpoint in URL namespace on this vhost */
47 "candide.zip", /* handler */
48 NULL, /* default filename if none given */
59 LWSMPRO_FILE, /* origin points to a callback */
60 8, /* strlen("/ziptest"), ie length of the mountpoint */
63 { NULL, NULL } // sentinel
66 static const struct lws_http_mount mount_post = {
67 (struct lws_http_mount *)&mount_ziptest, /* linked-list pointer to next*/
68 "/formtest", /* mountpoint in URL namespace on this vhost */
69 "protocol-post-demo", /* handler */
70 NULL, /* default filename if none given */
81 LWSMPRO_CALLBACK, /* origin points to a callback */
82 9, /* strlen("/formtest"), ie length of the mountpoint */
85 { NULL, NULL } // sentinel
89 static const struct lws_http_mount mount = {
90 /* .mount_next */ &mount_post, /* linked-list "next" */
91 /* .mountpoint */ "/", /* mountpoint URL */
92 /* .origin */ "./mount-origin", /* serve from dir */
93 /* .def */ "test.html", /* default filename */
96 /* .extra_mimetypes */ NULL,
97 /* .interpret */ NULL,
99 /* .cache_max_age */ 0,
101 /* .cache_reusable */ 0,
102 /* .cache_revalidate */ 0,
103 /* .cache_intermediaries */ 0,
104 /* .origin_protocol */ LWSMPRO_FILE, /* files in a dir */
105 /* .mountpoint_len */ 1, /* char count */
106 /* .basic_auth_login_file */ NULL,
109 void signal_cb(void *handle, int signum)
111 lwsl_err("%s: signal %d\n", __func__, signum);
121 lws_context_destroy(context);
124 void sigint_handler(int sig)
126 signal_cb(NULL, sig);
129 int main(int argc, const char **argv)
131 struct lws_context_creation_info info;
133 int logs = LLL_USER | LLL_ERR | LLL_WARN | LLL_NOTICE
134 /* for LLL_ verbosity above NOTICE to be built into lws,
135 * lws must have been configured and built with
136 * -DCMAKE_BUILD_TYPE=DEBUG instead of =RELEASE */
137 /* | LLL_INFO */ /* | LLL_PARSER */ /* | LLL_HEADER */
138 /* | LLL_EXT */ /* | LLL_CLIENT */ /* | LLL_LATENCY */
141 if ((p = lws_cmdline_option(argc, argv, "-d")))
144 lws_set_log_level(logs, NULL);
145 lwsl_user("LWS minimal http server eventlib | visit http://localhost:7681\n");
146 lwsl_user(" [-s (ssl)] [--uv (libuv)] [--ev (libev)] [--event (libevent)]\n");
148 memset(&info, 0, sizeof info); /* otherwise uninitialized garbage */
150 info.mounts = &mount;
151 info.error_document_404 = "/404.html";
152 info.pcontext = &context;
153 info.protocols = protocols;
154 info.signal_cb = signal_cb;
156 LWS_SERVER_OPTION_HTTP_HEADERS_SECURITY_BEST_PRACTICES_ENFORCE;
158 if (lws_cmdline_option(argc, argv, "-s")) {
159 info.options |= LWS_SERVER_OPTION_DO_SSL_GLOBAL_INIT;
160 info.ssl_cert_filepath = "localhost-100y.cert";
161 info.ssl_private_key_filepath = "localhost-100y.key";
164 if (lws_cmdline_option(argc, argv, "--uv"))
165 info.options |= LWS_SERVER_OPTION_LIBUV;
167 if (lws_cmdline_option(argc, argv, "--event"))
168 info.options |= LWS_SERVER_OPTION_LIBEVENT;
170 if (lws_cmdline_option(argc, argv, "--ev"))
171 info.options |= LWS_SERVER_OPTION_LIBEV;
173 signal(SIGINT, sigint_handler);
175 context = lws_create_context(&info);
177 lwsl_err("lws init failed\n");
181 while (!lws_service(context, 0))
184 lwsl_info("calling external context destroy\n");
185 lws_context_destroy(context);