• 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  * with an added websocket chat server.
11  *
12  * To keep it simple, it serves stuff in the subdirectory "./mount-origin" of
13  * the directory it was started in.
14  * You can change that by changing mount.origin.
15  */
16 
17 #include <libwebsockets.h>
18 #include <string.h>
19 #include <signal.h>
20 
21 #define LWS_PLUGIN_STATIC
22 #include "protocol_lws_minimal.c"
23 
24 static struct lws_protocols protocols[] = {
25 	{ "http", lws_callback_http_dummy, 0, 0, 0, NULL, 0},
26 	LWS_PLUGIN_PROTOCOL_MINIMAL,
27 	LWS_PROTOCOL_LIST_TERM
28 };
29 
30 static const lws_retry_bo_t retry = {
31 	.secs_since_valid_ping = 3,
32 	.secs_since_valid_hangup = 10,
33 };
34 
35 static int interrupted;
36 
37 static const struct lws_http_mount mount = {
38 	/* .mount_next */		NULL,		/* linked-list "next" */
39 	/* .mountpoint */		"/",		/* mountpoint URL */
40 	/* .origin */			"./mount-origin",  /* serve from dir */
41 	/* .def */			"index.html",	/* default filename */
42 	/* .protocol */			NULL,
43 	/* .cgienv */			NULL,
44 	/* .extra_mimetypes */		NULL,
45 	/* .interpret */		NULL,
46 	/* .cgi_timeout */		0,
47 	/* .cache_max_age */		0,
48 	/* .auth_mask */		0,
49 	/* .cache_reusable */		0,
50 	/* .cache_revalidate */		0,
51 	/* .cache_intermediaries */	0,
52 	/* .origin_protocol */		LWSMPRO_FILE,	/* files in a dir */
53 	/* .mountpoint_len */		1,		/* char count */
54 	/* .basic_auth_login_file */	NULL,
55 };
56 
57 #if defined(LWS_WITH_PLUGINS)
58 /* if plugins enabled, only protocols explicitly named in pvo bind to vhost */
59 static struct lws_protocol_vhost_options pvo = { NULL, NULL, "lws-minimal", "" };
60 #endif
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 | visit http://localhost:7681 (-s = use TLS / https)\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.vhost_name = "localhost";
93 #if defined(LWS_WITH_PLUGINS)
94 	info.pvo = &pvo;
95 #endif
96 	info.options =
97 		LWS_SERVER_OPTION_HTTP_HEADERS_SECURITY_BEST_PRACTICES_ENFORCE;
98 
99 #if defined(LWS_WITH_TLS)
100 	if (lws_cmdline_option(argc, argv, "-s")) {
101 		lwsl_user("Server using TLS\n");
102 		info.options |= LWS_SERVER_OPTION_DO_SSL_GLOBAL_INIT;
103 		info.ssl_cert_filepath = "localhost-100y.cert";
104 		info.ssl_private_key_filepath = "localhost-100y.key";
105 	}
106 #endif
107 
108 	if (lws_cmdline_option(argc, argv, "-h"))
109 		info.options |= LWS_SERVER_OPTION_VHOST_UPG_STRICT_HOST_CHECK;
110 
111 	if (lws_cmdline_option(argc, argv, "-v"))
112 		info.retry_and_idle_policy = &retry;
113 
114 	context = lws_create_context(&info);
115 	if (!context) {
116 		lwsl_err("lws init failed\n");
117 		return 1;
118 	}
119 
120 	while (n >= 0 && !interrupted)
121 		n = lws_service(context, 0);
122 
123 	lws_context_destroy(context);
124 
125 	return 0;
126 }
127