2 * lws-minimal-http-server-basicauth
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 server with a second mount that
10 * is protected using a password file and basic auth.
12 * To keep it simple, it serves the static stuff from the subdirectory
13 * "./mount-origin" of the directory it was started in.
15 * You can change that by changing mount.origin below.
18 #include <libwebsockets.h>
23 static int interrupted;
25 /* override the default mount for /secret in the URL space */
27 static const struct lws_http_mount mount_secret = {
28 /* .mount_next */ NULL, /* linked-list "next" */
29 /* .mountpoint */ "/secret", /* mountpoint URL */
30 /* .origin */ "./mount-secret-origin",
31 /* .def */ "index.html",
34 /* .extra_mimetypes */ NULL,
35 /* .interpret */ NULL,
37 /* .cache_max_age */ 0,
39 /* .cache_reusable */ 0,
40 /* .cache_revalidate */ 0,
41 /* .cache_intermediaries */ 0,
42 /* .origin_protocol */ LWSMPRO_FILE, /* dynamic */
43 /* .mountpoint_len */ 7, /* char count */
44 /* .basic_auth_login_file */ "./ba-passwords",
47 /* default mount serves the URL space from ./mount-origin */
49 static const struct lws_http_mount mount = {
50 /* .mount_next */ &mount_secret, /* linked-list "next" */
51 /* .mountpoint */ "/", /* mountpoint URL */
52 /* .origin */ "./mount-origin", /* serve from dir */
53 /* .def */ "index.html", /* default filename */
56 /* .extra_mimetypes */ NULL,
57 /* .interpret */ NULL,
59 /* .cache_max_age */ 0,
61 /* .cache_reusable */ 0,
62 /* .cache_revalidate */ 0,
63 /* .cache_intermediaries */ 0,
64 /* .origin_protocol */ LWSMPRO_FILE, /* files in a dir */
65 /* .mountpoint_len */ 1, /* char count */
66 /* .basic_auth_login_file */ NULL,
69 void sigint_handler(int sig)
74 int main(int argc, const char **argv)
76 struct lws_context_creation_info info;
77 struct lws_context *context;
79 int n = 0, logs = LLL_USER | LLL_ERR | LLL_WARN | LLL_NOTICE
80 /* for LLL_ verbosity above NOTICE to be built into lws,
81 * lws must have been configured and built with
82 * -DCMAKE_BUILD_TYPE=DEBUG instead of =RELEASE */
83 /* | LLL_INFO */ /* | LLL_PARSER */ /* | LLL_HEADER */
84 /* | LLL_EXT */ /* | LLL_CLIENT */ /* | LLL_LATENCY */
87 signal(SIGINT, sigint_handler);
89 if ((p = lws_cmdline_option(argc, argv, "-d")))
92 lws_set_log_level(logs, NULL);
93 lwsl_user("LWS minimal http server basic auth | visit http://localhost:7681\n");
95 memset(&info, 0, sizeof info); /* otherwise uninitialized garbage */
99 LWS_SERVER_OPTION_HTTP_HEADERS_SECURITY_BEST_PRACTICES_ENFORCE;
101 context = lws_create_context(&info);
103 lwsl_err("lws init failed\n");
107 while (n >= 0 && !interrupted)
108 n = lws_service(context, 0);
110 lws_context_destroy(context);