1 /*
2 * lws-minimal-ws-server-timer
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 an added websocket chat server.
11 *
12 * To keep it simple, it serves stuff in the subdirectory "./mount-origin" of
13 * the directory it was started in.
14 * You can change that by changing mount.origin.
15 */
16
17 #include <libwebsockets.h>
18 #include <string.h>
19 #include <signal.h>
20
21 static int
callback_protocol(struct lws * wsi,enum lws_callback_reasons reason,void * user,void * in,size_t len)22 callback_protocol(struct lws *wsi, enum lws_callback_reasons reason,
23 void *user, void *in, size_t len)
24 {
25 switch (reason) {
26
27 case LWS_CALLBACK_ESTABLISHED:
28 lwsl_user("LWS_CALLBACK_ESTABLISHED\n");
29 lws_set_timer_usecs(wsi, 20 * LWS_USEC_PER_SEC);
30 lws_set_timeout(wsi, 1, 60);
31 break;
32
33 case LWS_CALLBACK_TIMER:
34 lwsl_user("LWS_CALLBACK_TIMER\n");
35 lws_set_timer_usecs(wsi, 20 * LWS_USEC_PER_SEC);
36 lws_set_timeout(wsi, 1, 60);
37 break;
38
39 case LWS_CALLBACK_CLOSED:
40 lwsl_user("LWS_CALLBACK_CLOSED\n");
41 break;
42
43 default:
44 break;
45 }
46
47 return 0;
48 }
49
50 static struct lws_protocols protocols[] = {
51 { "http", lws_callback_http_dummy, 0, 0, 0, NULL, 0 },
52 { "timer", callback_protocol, 0, 0, 0, NULL, 0 },
53 LWS_PROTOCOL_LIST_TERM
54 };
55
56 static const lws_retry_bo_t retry = {
57 .secs_since_valid_ping = 3,
58 .secs_since_valid_hangup = 10,
59 };
60
61 static int interrupted;
62
63 static const struct lws_http_mount mount = {
64 /* .mount_next */ NULL, /* linked-list "next" */
65 /* .mountpoint */ "/", /* mountpoint URL */
66 /* .origin */ "./mount-origin", /* serve from dir */
67 /* .def */ "index.html", /* default filename */
68 /* .protocol */ NULL,
69 /* .cgienv */ NULL,
70 /* .extra_mimetypes */ NULL,
71 /* .interpret */ NULL,
72 /* .cgi_timeout */ 0,
73 /* .cache_max_age */ 0,
74 /* .auth_mask */ 0,
75 /* .cache_reusable */ 0,
76 /* .cache_revalidate */ 0,
77 /* .cache_intermediaries */ 0,
78 /* .origin_protocol */ LWSMPRO_FILE, /* files in a dir */
79 /* .mountpoint_len */ 1, /* char count */
80 /* .basic_auth_login_file */ NULL,
81 };
82
sigint_handler(int sig)83 void sigint_handler(int sig)
84 {
85 interrupted = 1;
86 }
87
main(int argc,const char ** argv)88 int main(int argc, const char **argv)
89 {
90 struct lws_context_creation_info info;
91 struct lws_context *context;
92 const char *p;
93 int n = 0, logs = LLL_USER | LLL_ERR | LLL_WARN | LLL_NOTICE
94 /* for LLL_ verbosity above NOTICE to be built into lws,
95 * lws must have been configured and built with
96 * -DCMAKE_BUILD_TYPE=DEBUG instead of =RELEASE */
97 /* | LLL_INFO */ /* | LLL_PARSER */ /* | LLL_HEADER */
98 /* | LLL_EXT */ /* | LLL_CLIENT */ /* | LLL_LATENCY */
99 /* | LLL_DEBUG */;
100
101 signal(SIGINT, sigint_handler);
102
103 if ((p = lws_cmdline_option(argc, argv, "-d")))
104 logs = atoi(p);
105
106 lws_set_log_level(logs, NULL);
107 lwsl_user("LWS minimal ws server | visit http://localhost:7681 (-s = use TLS / https)\n");
108
109 memset(&info, 0, sizeof info); /* otherwise uninitialized garbage */
110 info.port = 7681;
111 info.mounts = &mount;
112 info.protocols = protocols;
113 info.vhost_name = "localhost";
114 info.options =
115 LWS_SERVER_OPTION_HTTP_HEADERS_SECURITY_BEST_PRACTICES_ENFORCE;
116
117 #if defined(LWS_WITH_TLS)
118 if (lws_cmdline_option(argc, argv, "-s")) {
119 lwsl_user("Server using TLS\n");
120 info.options |= LWS_SERVER_OPTION_DO_SSL_GLOBAL_INIT;
121 info.ssl_cert_filepath = "localhost-100y.cert";
122 info.ssl_private_key_filepath = "localhost-100y.key";
123 }
124 #endif
125
126 if (lws_cmdline_option(argc, argv, "-h"))
127 info.options |= LWS_SERVER_OPTION_VHOST_UPG_STRICT_HOST_CHECK;
128
129 if (lws_cmdline_option(argc, argv, "-v"))
130 info.retry_and_idle_policy = &retry;
131
132 context = lws_create_context(&info);
133 if (!context) {
134 lwsl_err("lws init failed\n");
135 return 1;
136 }
137
138 while (n >= 0 && !interrupted)
139 n = lws_service(context, 0);
140
141 lws_context_destroy(context);
142
143 return 0;
144 }
145