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
20static int interrupted;
21static char cgi_script_fullpath[256];
22
23static 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
43void sigint_handler(int sig)
44{
45	interrupted = 1;
46}
47
48int 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 defined(LWS_WITH_TLS)
86	if (lws_cmdline_option(argc, argv, "-s")) {
87		info.options |= LWS_SERVER_OPTION_DO_SSL_GLOBAL_INIT;
88		info.ssl_cert_filepath = "localhost-100y.cert";
89		info.ssl_private_key_filepath = "localhost-100y.key";
90	}
91#endif
92
93	context = lws_create_context(&info);
94	if (!context) {
95		lwsl_err("lws init failed\n");
96		return 1;
97	}
98
99	while (n >= 0 && !interrupted)
100		n = lws_service(context, 1000);
101
102	lws_context_destroy(context);
103
104	return 0;
105}
106