2 * lws-minimal-http-server-generic-sessions
4 * Copyright (C) 2019 Andy Green <andy@warmcat.com>
6 * This file is made available under the Creative Commons CC0 1.0
7 * Universal Public Domain Dedication.
9 * This demonstrates setting up and using generic sessions
12 #include <libwebsockets.h>
16 static int interrupted;
17 struct lws_context *context;
19 static const struct lws_protocol_vhost_options
21 NULL, NULL, "message-db", (void *)"/var/www/sessions/messageboard.sqlite3"
23 NULL, &pvo_mm1, "protocol-lws-messageboard", ""
27 NULL, NULL, "email-confirm-url-base", (void *)"https://localhost:7681/"
29 &pvo13, NULL, "urlroot", (void *)"https://127.0.0.1:7681/"
31 &pvo12, NULL, "email-contact-person", (void *)"andy@warmcat.com"
33 &pvo11, NULL, "email-helo", (void *)"warmcat.com"
35 &pvo10, NULL, "email-expire", (void *)"3600"
37 &pvo9, NULL, "email-smtp-ip", (void *)"127.0.0.1"
39 &pvo8, NULL, "email-from", (void *)"noreply@warmcat.com"
41 &pvo7, NULL, "confounder", (void *)"some kind of secret confounder"
43 &pvo6, NULL, "timeout-anon-idle-secs", (void *)"1200"
45 &pvo5, NULL, "timeout-idle-secs", (void *)"6000"
47 &pvo4, NULL, "session-db", (void *)"/var/www/sessions/lws.sqlite3"
49 &pvo3, NULL, "admin-password-sha256",
50 (void *)"25d08521d996bad92605f5a40fe71179dc968e70f669cb1db6190dcd53258200" /* pvo value */
52 &pvo2, NULL, "admin-user", (void *)"admin"
54 &pvo_m1, &pvo1, "protocol-generic-sessions", ""
58 NULL, NULL, ".js", "protocol-lws-messageboard"
62 NULL, NULL, "referrer-policy:", "no-referrer"
64 &pvo_hsbph[0], NULL, "x-xss-protection:", "1; mode=block"
66 &pvo_hsbph[1], NULL, "x-content-type-options:", "nosniff"
68 &pvo_hsbph[2], NULL, "content-security-policy:",
69 "default-src 'self'; "
70 "img-src https://www.gravatar.com 'self' data: ; "
74 "connect-src 'self'; "
75 "frame-ancestors 'self'; "
80 static const struct lws_http_mount mount2 = {
81 /* .mount_next */ NULL, /* linked-list "next" */
82 /* .mountpoint */ "/needadmin", /* mountpoint URL */
83 /* .origin */ "./mount-origin/needadmin", /* serve from dir */
84 /* .def */ "index.html", /* default filename */
85 /* .protocol */ "protocol-lws-messageboard",
87 /* .extra_mimetypes */ NULL,
88 /* .interpret */ &interpret1,
90 /* .cache_max_age */ 0,
92 /* .cache_reusable */ 0,
93 /* .cache_revalidate */ 0,
94 /* .cache_intermediaries */ 0,
95 /* .origin_protocol */ LWSMPRO_FILE, /* files in a dir */
96 /* .mountpoint_len */ 1, /* char count */
97 /* .basic_auth_login_file */ NULL,
100 static const struct lws_http_mount mount1 = {
101 /* .mount_next */ &mount2, /* linked-list "next" */
102 /* .mountpoint */ "/needauth", /* mountpoint URL */
103 /* .origin */ "./mount-origin/needauth", /* serve from dir */
104 /* .def */ "index.html", /* default filename */
105 /* .protocol */ "protocol-lws-messageboard",
107 /* .extra_mimetypes */ NULL,
108 /* .interpret */ &interpret1,
109 /* .cgi_timeout */ 0,
110 /* .cache_max_age */ 0,
112 /* .cache_reusable */ 0,
113 /* .cache_revalidate */ 0,
114 /* .cache_intermediaries */ 0,
115 /* .origin_protocol */ LWSMPRO_FILE, /* files in a dir */
116 /* .mountpoint_len */ 1, /* char count */
117 /* .basic_auth_login_file */ NULL,
120 static const struct lws_http_mount mount = {
121 /* .mount_next */ &mount1, /* linked-list "next" */
122 /* .mountpoint */ "/", /* mountpoint URL */
123 /* .origin */ "./mount-origin", /* serve from dir */
124 /* .def */ "index.html", /* default filename */
125 /* .protocol */ "protocol-lws-messageboard",
127 /* .extra_mimetypes */ NULL,
128 /* .interpret */ &interpret1,
129 /* .cgi_timeout */ 0,
130 /* .cache_max_age */ 0,
132 /* .cache_reusable */ 0,
133 /* .cache_revalidate */ 0,
134 /* .cache_intermediaries */ 0,
135 /* .origin_protocol */ LWSMPRO_FILE, /* files in a dir */
136 /* .mountpoint_len */ 1, /* char count */
137 /* .basic_auth_login_file */ NULL,
140 void sigint_handler(int sig)
142 lws_context_destroy(context);
147 int main(int argc, const char **argv)
149 struct lws_context_creation_info info;
150 const char *p, *plugin_dirs[] = {
151 "/usr/local/share/libwebsockets-test-server/plugins",
153 int n = 0, logs = LLL_USER | LLL_ERR | LLL_WARN | LLL_NOTICE
154 /* for LLL_ verbosity above NOTICE to be built into lws,
155 * lws must have been configured and built with
156 * -DCMAKE_BUILD_TYPE=DEBUG instead of =RELEASE */
157 /* | LLL_INFO */ /* | LLL_PARSER */ /* | LLL_HEADER */
158 /* | LLL_EXT */ /* | LLL_CLIENT */ /* | LLL_LATENCY */
161 if ((p = lws_cmdline_option(argc, argv, "-d")))
164 lws_set_log_level(logs, NULL);
165 lwsl_user("LWS minimal http server TLS | visit https://localhost:7681\n");
167 signal(SIGINT, sigint_handler);
169 memset(&info, 0, sizeof info); /* otherwise uninitialized garbage */
171 info.mounts = &mount;
172 info.error_document_404 = "/404.html";
173 info.options = LWS_SERVER_OPTION_DO_SSL_GLOBAL_INIT |
174 LWS_SERVER_OPTION_EXPLICIT_VHOSTS;
175 info.ssl_cert_filepath = "localhost-100y.cert";
176 info.ssl_private_key_filepath = "localhost-100y.key";
177 info.plugin_dirs = plugin_dirs;
180 if (lws_cmdline_option(argc, argv, "-h"))
181 info.options |= LWS_SERVER_OPTION_VHOST_UPG_STRICT_HOST_CHECK;
183 context = lws_create_context(&info);
185 lwsl_err("lws init failed\n");
189 info.headers = &pvo_hsbph[3];
191 if (!lws_create_vhost(context, &info)) {
192 lwsl_err("lws init failed\n");
196 while (n >= 0 && !interrupted)
197 n = lws_service(context, 0);
199 lws_context_destroy(context);