• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * lws-minimal-http-server-smp
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 multithreaded 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  * Also for simplicity the number of threads is set in the code... note that
16  * the real number of threads possible is decided by the LWS_MAX_SMP that lws
17  * was configured with, by default that is 1.  Lws will limit the number of
18  * requested threads to the number possible.
19  */
20 
21 #include <libwebsockets.h>
22 #include <string.h>
23 #include <signal.h>
24 #if defined(WIN32)
25 #define HAVE_STRUCT_TIMESPEC
26 #if defined(pid_t)
27 #undef pid_t
28 #endif
29 #endif
30 #include <pthread.h>
31 
32 #define COUNT_THREADS 8
33 
34 static struct lws_context *context;
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 
thread_service(void * threadid)57 void *thread_service(void *threadid)
58 {
59 	while (lws_service_tsi(context, 10000,
60 			       (int)(lws_intptr_t)threadid) >= 0 &&
61 	       !interrupted)
62 		;
63 
64 	pthread_exit(NULL);
65 
66 	return NULL;
67 }
68 
sigint_handler(int sig)69 void sigint_handler(int sig)
70 {
71 	interrupted = 1;
72 	lws_cancel_service(context);
73 }
74 
main(int argc,const char ** argv)75 int main(int argc, const char **argv)
76 {
77 	pthread_t pthread_service[COUNT_THREADS];
78 	struct lws_context_creation_info info;
79 	void *retval;
80 	const char *p;
81 	int n = 0, logs = LLL_USER | LLL_ERR | LLL_WARN | LLL_NOTICE
82 			/* for LLL_ verbosity above NOTICE to be built into lws,
83 			 * lws must have been configured and built with
84 			 * -DCMAKE_BUILD_TYPE=DEBUG instead of =RELEASE */
85 			/* | LLL_INFO */ /* | LLL_PARSER */ /* | LLL_HEADER */
86 			/* | LLL_EXT */ /* | LLL_CLIENT */ /* | LLL_LATENCY */
87 			/* | LLL_DEBUG */;
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 SMP | visit http://127.0.0.1:7681\n");
94 
95 	signal(SIGINT, sigint_handler);
96 
97 	memset(&info, 0, sizeof info); /* otherwise uninitialized garbage */
98 	info.port = 7681;
99 	info.mounts = &mount;
100 	info.options =
101 		LWS_SERVER_OPTION_HTTP_HEADERS_SECURITY_BEST_PRACTICES_ENFORCE;
102 	if ((p = lws_cmdline_option(argc, argv, "-t"))) {
103 		info.count_threads = (unsigned int)atoi(p);
104 		if (info.count_threads < 1 || info.count_threads > LWS_MAX_SMP)
105 			return 1;
106 	} else
107 		info.count_threads = COUNT_THREADS;
108 
109 #if defined(LWS_WITH_TLS)
110 	if (lws_cmdline_option(argc, argv, "-s")) {
111 		info.options = LWS_SERVER_OPTION_DO_SSL_GLOBAL_INIT;
112 		info.ssl_cert_filepath = "localhost-100y.cert";
113 		info.ssl_private_key_filepath = "localhost-100y.key";
114 	}
115 #endif
116 
117 	context = lws_create_context(&info);
118 	if (!context) {
119 		lwsl_err("lws init failed\n");
120 		return 1;
121 	}
122 
123 	lwsl_notice("  Service threads: %d\n", lws_get_count_threads(context));
124 
125 	/* start all the service threads */
126 
127 	for (n = 0; n < lws_get_count_threads(context); n++)
128 		if (pthread_create(&pthread_service[n], NULL, thread_service,
129 				   (void *)(lws_intptr_t)n))
130 			lwsl_err("Failed to start service thread\n");
131 
132 	/* wait for all the service threads to exit */
133 
134 	while ((--n) >= 0)
135 		pthread_join(pthread_service[n], &retval);
136 
137 	lws_context_destroy(context);
138 
139 	return 0;
140 }
141