• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * lws-minimal-http-server-cgi
3  *
4  * Written in 2010-2020 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 from the subdirectory
12  * "./mount-origin" of the directory it was started in.
13  * You can change that by changing mount.origin below.
14  */
15 
16 #include <libwebsockets.h>
17 #include <string.h>
18 #include <signal.h>
19 
20 static int interrupted;
21 static char cgi_script_fullpath[256];
22 
23 static const struct lws_http_mount mount = {
24 	/* .mount_next */		NULL,		/* linked-list "next" */
25 	/* .mountpoint */		"/",		/* mountpoint URL */
26 	/* .origin */			cgi_script_fullpath, /* cgi script */
27 	/* .def */			"/",	/* default filename */
28 	/* .protocol */			NULL,
29 	/* .cgienv */			NULL,
30 	/* .extra_mimetypes */		NULL,
31 	/* .interpret */		NULL,
32 	/* .cgi_timeout */		0,
33 	/* .cache_max_age */		0,
34 	/* .auth_mask */		0,
35 	/* .cache_reusable */		0,
36 	/* .cache_revalidate */		0,
37 	/* .cache_intermediaries */	0,
38 	/* .origin_protocol */		LWSMPRO_CGI,	/* files in a dir */
39 	/* .mountpoint_len */		1,		/* char count */
40 	/* .basic_auth_login_file */	NULL,
41 };
42 
sigint_handler(int sig)43 void sigint_handler(int sig)
44 {
45 	interrupted = 1;
46 }
47 
main(int argc,const char ** argv)48 int main(int argc, const char **argv)
49 {
50 	struct lws_context_creation_info info;
51 	struct lws_context *context;
52 	const char *p;
53 	int n = 0, logs = LLL_USER | LLL_ERR | LLL_WARN | LLL_NOTICE
54 			/* for LLL_ verbosity above NOTICE to be built into lws,
55 			 * lws must have been configured and built with
56 			 * -DCMAKE_BUILD_TYPE=DEBUG instead of =RELEASE */
57 			/* | LLL_INFO */ /* | LLL_PARSER */ /* | LLL_HEADER */
58 			/* | LLL_EXT */ /* | LLL_CLIENT */ /* | LLL_LATENCY */
59 			/* | LLL_DEBUG */;
60 
61 	signal(SIGINT, sigint_handler);
62 
63 	if ((p = lws_cmdline_option(argc, argv, "-d")))
64 		logs = atoi(p);
65 
66 	lws_set_log_level(logs, NULL);
67 	lwsl_user("LWS minimal http server | visit http://localhost:7681\n");
68 
69 	{
70 		char cwd[128];
71 		cwd[0] = '\0';
72 		getcwd(cwd, sizeof(cwd));
73 
74 		lws_snprintf(cgi_script_fullpath, sizeof(cgi_script_fullpath),
75 				"%s/my-cgi-script.sh", cwd);
76 	}
77 
78 	memset(&info, 0, sizeof info); /* otherwise uninitialized garbage */
79 	info.port = 7681;
80 	info.mounts = &mount;
81 	info.error_document_404 = "/404.html";
82 	info.options =
83 		LWS_SERVER_OPTION_HTTP_HEADERS_SECURITY_BEST_PRACTICES_ENFORCE;
84 
85 	if (lws_cmdline_option(argc, argv, "-s")) {
86 		info.options |= LWS_SERVER_OPTION_DO_SSL_GLOBAL_INIT;
87 		info.ssl_cert_filepath = "localhost-100y.cert";
88 		info.ssl_private_key_filepath = "localhost-100y.key";
89 	}
90 
91 	context = lws_create_context(&info);
92 	if (!context) {
93 		lwsl_err("lws init failed\n");
94 		return 1;
95 	}
96 
97 	while (n >= 0 && !interrupted)
98 		n = lws_service(context, 1000);
99 
100 	lws_context_destroy(context);
101 
102 	return 0;
103 }
104