• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * lws-minimal-http-server-eventlib
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[s] server that can work with any of the
10  * supported event loop backends, or the default poll() one.
11  *
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.
15  */
16 
17 #include <libwebsockets.h>
18 #include <string.h>
19 #include <signal.h>
20 
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"
26 
27 static struct lws_context *context;
28 
29 static struct lws_protocols protocols[] = {
30 	/* first protocol must always be HTTP handler */
31 
32 	{ "http-only", lws_callback_http_dummy, 0, 0, 0, NULL, 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 	LWS_PROTOCOL_LIST_TERM
38 };
39 
40 /*
41  * mount handlers for sections of the URL space
42  */
43 
44 static const struct lws_http_mount mount_ziptest_uncomm = {
45 	NULL,			/* linked-list pointer to next*/
46 	"/uncommziptest",		/* mountpoint in URL namespace on this vhost */
47 	"./mount-origin/candide-uncompressed.zip",	/* handler */
48 	NULL,	/* default filename if none given */
49 	NULL,
50 	NULL,
51 	NULL,
52 	NULL,
53 	0,
54 	0,
55 	0,
56 	0,
57 	0,
58 	0,
59 	LWSMPRO_FILE,	/* origin points to a callback */
60 	14,			/* strlen("/ziptest"), ie length of the mountpoint */
61 	NULL,
62 }, mount_ziptest = {
63 	(struct lws_http_mount *)&mount_ziptest_uncomm,			/* linked-list pointer to next*/
64 	"/ziptest",		/* mountpoint in URL namespace on this vhost */
65 	"./mount-origin/candide.zip",	/* handler */
66 	NULL,	/* default filename if none given */
67 	NULL,
68 	NULL,
69 	NULL,
70 	NULL,
71 	0,
72 	0,
73 	0,
74 	0,
75 	0,
76 	0,
77 	LWSMPRO_FILE,	/* origin points to a callback */
78 	8,			/* strlen("/ziptest"), ie length of the mountpoint */
79 	NULL,
80 
81 }, mount_post = {
82 	(struct lws_http_mount *)&mount_ziptest, /* linked-list pointer to next*/
83 	"/formtest",		/* mountpoint in URL namespace on this vhost */
84 	"protocol-post-demo",	/* handler */
85 	NULL,	/* default filename if none given */
86 	NULL,
87 	NULL,
88 	NULL,
89 	NULL,
90 	0,
91 	0,
92 	0,
93 	0,
94 	0,
95 	0,
96 	LWSMPRO_CALLBACK,	/* origin points to a callback */
97 	9,			/* strlen("/formtest"), ie length of the mountpoint */
98 	NULL,
99 
100 }, mount = {
101 	/* .mount_next */		&mount_post,	/* linked-list "next" */
102 	/* .mountpoint */		"/",		/* mountpoint URL */
103 	/* .origin */			"./mount-origin", /* serve from dir */
104 	/* .def */			"test.html",	/* default filename */
105 	/* .protocol */			NULL,
106 	/* .cgienv */			NULL,
107 	/* .extra_mimetypes */		NULL,
108 	/* .interpret */		NULL,
109 	/* .cgi_timeout */		0,
110 	/* .cache_max_age */		0,
111 	/* .auth_mask */		0,
112 	/* .cache_reusable */		0,
113 	/* .cache_revalidate */		0,
114 	/* .cache_intermediaries */	0,
115 	/* .origin_protocol */		LWSMPRO_FILE,	/* files in a dir */
116 	/* .mountpoint_len */		1,		/* char count */
117 	/* .basic_auth_login_file */	NULL,
118 };
119 
signal_cb(void * handle,int signum)120 void signal_cb(void *handle, int signum)
121 {
122 	lwsl_err("%s: signal %d\n", __func__, signum);
123 
124 	switch (signum) {
125 	case SIGTERM:
126 	case SIGINT:
127 		break;
128 	default:
129 
130 		break;
131 	}
132 	lws_context_destroy(context);
133 }
134 
sigint_handler(int sig)135 void sigint_handler(int sig)
136 {
137 	signal_cb(NULL, sig);
138 }
139 
main(int argc,const char ** argv)140 int main(int argc, const char **argv)
141 {
142 	struct lws_context_creation_info info;
143 	const char *p;
144 	int logs = LLL_USER | LLL_ERR | LLL_WARN | LLL_NOTICE
145 			/* for LLL_ verbosity above NOTICE to be built into lws,
146 			 * lws must have been configured and built with
147 			 * -DCMAKE_BUILD_TYPE=DEBUG instead of =RELEASE */
148 			/* | LLL_INFO */ /* | LLL_PARSER */ /* | LLL_HEADER */
149 			/* | LLL_EXT */ /* | LLL_CLIENT */ /* | LLL_LATENCY */
150 			/* | LLL_DEBUG */;
151 
152 	if ((p = lws_cmdline_option(argc, argv, "-d")))
153 		logs = atoi(p);
154 
155 	lws_set_log_level(logs, NULL);
156 	lwsl_user("LWS minimal http server eventlib | visit http://localhost:7681\n");
157 	lwsl_user(" [-s (ssl)] [--uv (libuv)] [--ev (libev)] [--event (libevent)]\n");
158 
159 	memset(&info, 0, sizeof info); /* otherwise uninitialized garbage */
160 	info.port = 7681;
161 	info.mounts = &mount;
162 	info.error_document_404 = "/404.html";
163 	info.pcontext = &context;
164 	info.protocols = protocols;
165 	info.signal_cb = signal_cb;
166 	info.options =
167 		LWS_SERVER_OPTION_HTTP_HEADERS_SECURITY_BEST_PRACTICES_ENFORCE;
168 
169 	if (lws_cmdline_option(argc, argv, "-s")) {
170 		info.options |= LWS_SERVER_OPTION_DO_SSL_GLOBAL_INIT;
171 #if defined(LWS_WITH_TLS)
172 		info.ssl_cert_filepath = "localhost-100y.cert";
173 		info.ssl_private_key_filepath = "localhost-100y.key";
174 #endif
175 	}
176 
177 	if (lws_cmdline_option(argc, argv, "--uv"))
178 		info.options |= LWS_SERVER_OPTION_LIBUV;
179 	else
180 		if (lws_cmdline_option(argc, argv, "--event"))
181 			info.options |= LWS_SERVER_OPTION_LIBEVENT;
182 		else
183 			if (lws_cmdline_option(argc, argv, "--ev"))
184 				info.options |= LWS_SERVER_OPTION_LIBEV;
185 			else
186 				if (lws_cmdline_option(argc, argv, "--glib"))
187 					info.options |= LWS_SERVER_OPTION_GLIB;
188 				else
189 					signal(SIGINT, sigint_handler);
190 
191 	context = lws_create_context(&info);
192 	if (!context) {
193 		lwsl_err("lws init failed\n");
194 		return 1;
195 	}
196 
197 	while (!lws_service(context, 0))
198 		;
199 
200 	lwsl_info("calling external context destroy\n");
201 	lws_context_destroy(context);
202 
203 	return 0;
204 }
205