• 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_pmd_bulk.c"
22 
23 static struct lws_protocols protocols[] = {
24 	{ "http", lws_callback_http_dummy, 0, 0 },
25 	LWS_PLUGIN_PROTOCOL_MINIMAL_PMD_BULK,
26 	{ NULL, NULL, 0, 0 } /* terminator */
27 };
28 
29 static int interrupted, options;
30 
31 /* pass pointers to shared vars to the protocol */
32 
33 static const struct lws_protocol_vhost_options pvo_options = {
34         NULL,
35         NULL,
36         "options",              /* pvo name */
37         (void *)&options        /* pvo value */
38 };
39 
40 static const struct lws_protocol_vhost_options pvo_interrupted = {
41         &pvo_options,
42         NULL,
43         "interrupted",          /* pvo name */
44         (void *)&interrupted    /* pvo value */
45 };
46 
47 static const struct lws_protocol_vhost_options pvo = {
48         NULL,           /* "next" pvo linked-list */
49         &pvo_interrupted,       /* "child" pvo linked-list */
50         "lws-minimal-pmd-bulk", /* protocol name we belong to on this vhost */
51         ""              /* ignored */
52 };
53 
54 static const struct lws_http_mount mount = {
55 	/* .mount_next */		NULL,		/* linked-list "next" */
56 	/* .mountpoint */		"/",		/* mountpoint URL */
57 	/* .origin */			"./mount-origin", /* serve from dir */
58 	/* .def */			"index.html",	/* default filename */
59 	/* .protocol */			NULL,
60 	/* .cgienv */			NULL,
61 	/* .extra_mimetypes */		NULL,
62 	/* .interpret */		NULL,
63 	/* .cgi_timeout */		0,
64 	/* .cache_max_age */		0,
65 	/* .auth_mask */		0,
66 	/* .cache_reusable */		0,
67 	/* .cache_revalidate */		0,
68 	/* .cache_intermediaries */	0,
69 	/* .origin_protocol */		LWSMPRO_FILE,	/* files in a dir */
70 	/* .mountpoint_len */		1,		/* char count */
71 	/* .basic_auth_login_file */	NULL,
72 };
73 
74 static const struct lws_extension extensions[] = {
75 	{
76 		"permessage-deflate",
77 		lws_extension_callback_pm_deflate,
78 		"permessage-deflate"
79 		 "; client_no_context_takeover"
80 		 "; client_max_window_bits"
81 	},
82 	{ NULL, NULL, NULL /* terminator */ }
83 };
84 
sigint_handler(int sig)85 void sigint_handler(int sig)
86 {
87 	interrupted = 1;
88 }
89 
main(int argc,const char ** argv)90 int main(int argc, const char **argv)
91 {
92 	struct lws_context_creation_info info;
93 	struct lws_context *context;
94 	const char *p;
95 	int n = 0, logs = LLL_USER | LLL_ERR | LLL_WARN | LLL_NOTICE
96 			/* for LLL_ verbosity above NOTICE to be built into lws,
97 			 * lws must have been configured and built with
98 			 * -DCMAKE_BUILD_TYPE=DEBUG instead of =RELEASE */
99 			/* | LLL_INFO */ /* | LLL_PARSER */ /* | LLL_HEADER */
100 			/* | LLL_EXT */ /* | LLL_CLIENT */ /* | LLL_LATENCY */
101 			/* | LLL_DEBUG */;
102 
103 	signal(SIGINT, sigint_handler);
104 
105 	if ((p = lws_cmdline_option(argc, argv, "-d")))
106 		logs = atoi(p);
107 
108 	lws_set_log_level(logs, NULL);
109 	lwsl_user("LWS minimal ws server + permessage-deflate | visit http://localhost:7681\n");
110 	lwsl_user("   %s [-n (no exts)] [-c (compressible)] [-b (blob)]\n", argv[0]);
111 
112 	memset(&info, 0, sizeof info); /* otherwise uninitialized garbage */
113 	info.port = 7681;
114 	info.mounts = &mount;
115 	info.protocols = protocols;
116 	info.pvo = &pvo;
117 	info.options =
118 		LWS_SERVER_OPTION_HTTP_HEADERS_SECURITY_BEST_PRACTICES_ENFORCE;
119 
120 	if (!lws_cmdline_option(argc, argv, "-n"))
121 		info.extensions = extensions;
122 
123 	if (lws_cmdline_option(argc, argv, "-c"))
124 		options |= 1; /* send compressible text */
125 
126 	if (lws_cmdline_option(argc, argv, "-b"))
127 		options |= 2; /* send in one giant blob */
128 
129 	info.pt_serv_buf_size = 32 * 1024;
130 
131 	context = lws_create_context(&info);
132 	if (!context) {
133 		lwsl_err("lws init failed\n");
134 		return 1;
135 	}
136 
137 	while (n >= 0 && !interrupted)
138 		n = lws_service(context, 0);
139 
140 	lws_context_destroy(context);
141 
142 	return 0;
143 }
144