• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * lws-minimal-ws-server
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 the most minimal http server you can make with lws.
10  *
11  * To keep it simple, it serves stuff in the subdirectory "./mount-origin" of
12  * the directory it was started in.
13  * You can change that by changing mount.origin.
14  */
15 
16 #include <libwebsockets.h>
17 #include <string.h>
18 #include <signal.h>
19 
20 #define LWS_PLUGIN_STATIC
21 #include "protocol_lws_minimal.c"
22 
23 static struct lws_protocols protocols[] = {
24 	{ "http", lws_callback_http_dummy, 0, 0, 0, NULL, 0 },
25 	LWS_PLUGIN_PROTOCOL_MINIMAL,
26 	LWS_PROTOCOL_LIST_TERM
27 };
28 
29 static int interrupted;
30 
31 static const struct lws_http_mount mount = {
32 	/* .mount_next */		NULL,		/* linked-list "next" */
33 	/* .mountpoint */		"/",		/* mountpoint URL */
34 	/* .origin */			"./mount-origin",  /* serve from dir */
35 	/* .def */			"index.html",	/* default filename */
36 	/* .protocol */			NULL,
37 	/* .cgienv */			NULL,
38 	/* .extra_mimetypes */		NULL,
39 	/* .interpret */		NULL,
40 	/* .cgi_timeout */		0,
41 	/* .cache_max_age */		0,
42 	/* .auth_mask */		0,
43 	/* .cache_reusable */		0,
44 	/* .cache_revalidate */		0,
45 	/* .cache_intermediaries */	0,
46 	/* .origin_protocol */		LWSMPRO_FILE,	/* files in a dir */
47 	/* .mountpoint_len */		1,		/* char count */
48 	/* .basic_auth_login_file */	NULL,
49 };
50 
51 static const struct lws_extension extensions[] = {
52 	{
53 		"permessage-deflate",
54 		lws_extension_callback_pm_deflate,
55 		"permessage-deflate"
56 		 "; client_no_context_takeover"
57 		 "; client_max_window_bits"
58 	},
59 	{ NULL, NULL, NULL /* terminator */ }
60 };
61 
sigint_handler(int sig)62 void sigint_handler(int sig)
63 {
64 	interrupted = 1;
65 }
66 
main(int argc,const char ** argv)67 int main(int argc, const char **argv)
68 {
69 	struct lws_context_creation_info info;
70 	struct lws_context *context;
71 	const char *p;
72 	int n = 0, logs = LLL_USER | LLL_ERR | LLL_WARN | LLL_NOTICE
73 			/* for LLL_ verbosity above NOTICE to be built into lws,
74 			 * lws must have been configured and built with
75 			 * -DCMAKE_BUILD_TYPE=DEBUG instead of =RELEASE */
76 			/* | LLL_INFO */ /* | LLL_PARSER */ /* | LLL_HEADER */
77 			/* | LLL_EXT */ /* | LLL_CLIENT */ /* | LLL_LATENCY */
78 			/* | LLL_DEBUG */;
79 
80 	signal(SIGINT, sigint_handler);
81 
82 	if ((p = lws_cmdline_option(argc, argv, "-d")))
83 		logs = atoi(p);
84 
85 	lws_set_log_level(logs, NULL);
86 	lwsl_user("LWS minimal ws server + permessage-deflate | visit http://localhost:7681\n");
87 
88 	memset(&info, 0, sizeof info); /* otherwise uninitialized garbage */
89 	info.port = 7681;
90 	info.mounts = &mount;
91 	info.protocols = protocols;
92 	info.extensions = extensions;
93 	info.options =
94 		LWS_SERVER_OPTION_HTTP_HEADERS_SECURITY_BEST_PRACTICES_ENFORCE;
95 
96 	context = lws_create_context(&info);
97 	if (!context) {
98 		lwsl_err("lws init failed\n");
99 		return 1;
100 	}
101 
102 	while (n >= 0 && !interrupted)
103 		n = lws_service(context, 0);
104 
105 	lws_context_destroy(context);
106 
107 	return 0;
108 }
109