• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * lws-minimal-http-server-basicauth
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 server with a second mount that
10  * is protected using a password file and basic auth.
11  *
12  * To keep it simple, it serves the static stuff from the subdirectory
13  * "./mount-origin" of the directory it was started in.
14  *
15  * You can change that by changing mount.origin below.
16  */
17 
18 #include <libwebsockets.h>
19 #include <string.h>
20 #include <signal.h>
21 #include <time.h>
22 
23 static int interrupted;
24 
25 /* override the default mount for /secret in the URL space */
26 
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",
32 	/* .protocol */			NULL,
33 	/* .cgienv */			NULL,
34 	/* .extra_mimetypes */		NULL,
35 	/* .interpret */		NULL,
36 	/* .cgi_timeout */		0,
37 	/* .cache_max_age */		0,
38 	/* .auth_mask */		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",
45 };
46 
47 /* default mount serves the URL space from ./mount-origin */
48 
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 */
54 	/* .protocol */			NULL,
55 	/* .cgienv */			NULL,
56 	/* .extra_mimetypes */		NULL,
57 	/* .interpret */		NULL,
58 	/* .cgi_timeout */		0,
59 	/* .cache_max_age */		0,
60 	/* .auth_mask */		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,
67 };
68 
sigint_handler(int sig)69 void sigint_handler(int sig)
70 {
71 	interrupted = 1;
72 }
73 
main(int argc,const char ** argv)74 int main(int argc, const char **argv)
75 {
76 	struct lws_context_creation_info info;
77 	struct lws_context *context;
78 	const char *p;
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 */
85 			/* | LLL_DEBUG */;
86 
87 	signal(SIGINT, sigint_handler);
88 
89 	if ((p = lws_cmdline_option(argc, argv, "-d")))
90 		logs = atoi(p);
91 
92 	lws_set_log_level(logs, NULL);
93 	lwsl_user("LWS minimal http server basic auth | visit http://localhost:7681\n");
94 
95 	memset(&info, 0, sizeof info); /* otherwise uninitialized garbage */
96 	info.port = 7681;
97 	info.mounts = &mount;
98 	info.options =
99 		LWS_SERVER_OPTION_HTTP_HEADERS_SECURITY_BEST_PRACTICES_ENFORCE;
100 
101 	context = lws_create_context(&info);
102 	if (!context) {
103 		lwsl_err("lws init failed\n");
104 		return 1;
105 	}
106 
107 	while (n >= 0 && !interrupted)
108 		n = lws_service(context, 0);
109 
110 	lws_context_destroy(context);
111 
112 	return 0;
113 }
114