1 /*
2 * lws-minimal-http-server-tls
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 * with three extra lines giving it tls (ssl) capabilities, which in
11 * turn allow operation with HTTP/2 if lws was configured for it.
12 *
13 * To keep it simple, it serves stuff from the subdirectory
14 * "./mount-origin" of the directory it was started in.
15 *
16 * You can change that by changing mount.origin below.
17 */
18
19 #include <libwebsockets.h>
20 #include <string.h>
21 #include <signal.h>
22
23 static int interrupted;
24
25 static const struct lws_http_mount mount = {
26 /* .mount_next */ NULL, /* linked-list "next" */
27 /* .mountpoint */ "/", /* mountpoint URL */
28 /* .origin */ "./mount-origin", /* serve from dir */
29 /* .def */ "index.html", /* default filename */
30 /* .protocol */ NULL,
31 /* .cgienv */ NULL,
32 /* .extra_mimetypes */ NULL,
33 /* .interpret */ NULL,
34 /* .cgi_timeout */ 0,
35 /* .cache_max_age */ 0,
36 /* .auth_mask */ 0,
37 /* .cache_reusable */ 0,
38 /* .cache_revalidate */ 0,
39 /* .cache_intermediaries */ 0,
40 /* .origin_protocol */ LWSMPRO_FILE, /* files in a dir */
41 /* .mountpoint_len */ 1, /* char count */
42 /* .basic_auth_login_file */ NULL,
43 };
44
sigint_handler(int sig)45 void sigint_handler(int sig)
46 {
47 interrupted = 1;
48 }
49
main(int argc,const char ** argv)50 int main(int argc, const char **argv)
51 {
52 struct lws_context_creation_info info;
53 struct lws_context *context;
54 const char *p;
55 int n = 0, logs = LLL_USER | LLL_ERR | LLL_WARN | LLL_NOTICE
56 /* for LLL_ verbosity above NOTICE to be built into lws,
57 * lws must have been configured and built with
58 * -DCMAKE_BUILD_TYPE=DEBUG instead of =RELEASE */
59 /* | LLL_INFO */ /* | LLL_PARSER */ /* | LLL_HEADER */
60 /* | LLL_EXT */ /* | LLL_CLIENT */ /* | LLL_LATENCY */
61 /* | LLL_DEBUG */;
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 TLS | visit https://localhost:7681\n");
68
69 signal(SIGINT, sigint_handler);
70
71 memset(&info, 0, sizeof info); /* otherwise uninitialized garbage */
72 info.port = 7681;
73 info.mounts = &mount;
74 info.error_document_404 = "/404.html";
75 info.options = LWS_SERVER_OPTION_DO_SSL_GLOBAL_INIT |
76 LWS_SERVER_OPTION_HTTP_HEADERS_SECURITY_BEST_PRACTICES_ENFORCE;
77 info.ssl_cert_filepath = "localhost-100y.cert";
78 info.ssl_private_key_filepath = "localhost-100y.key";
79
80 if (lws_cmdline_option(argc, argv, "-h"))
81 info.options |= LWS_SERVER_OPTION_VHOST_UPG_STRICT_HOST_CHECK;
82
83 context = lws_create_context(&info);
84 if (!context) {
85 lwsl_err("lws init failed\n");
86 return 1;
87 }
88
89 while (n >= 0 && !interrupted)
90 n = lws_service(context, 0);
91
92 lws_context_destroy(context);
93
94 return 0;
95 }
96