eaad5805d5930755a702736e7091113299ebfc34
[platform/upstream/libwebsockets.git] /
1 /*
2  * lws-minimal-http-server-eventlib
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 a minimal http[s] server that can work with any of the
10  * supported event loop backends, or the default poll() one.
11  *
12  * To keep it simple, it serves stuff from the subdirectory 
13  * "./mount-origin" of the directory it was started in.
14  * You can change that by changing mount.origin below.
15  */
16
17 #include <libwebsockets.h>
18 #include <string.h>
19 #include <signal.h>
20
21 #define LWS_PLUGIN_STATIC
22 #include "../../../plugins/protocol_lws_mirror.c"
23 #include "../../../plugins/protocol_lws_status.c"
24 #include "../../../plugins/protocol_dumb_increment.c"
25 #include "../../../plugins/protocol_post_demo.c"
26
27 static struct lws_context *context;
28
29 static struct lws_protocols protocols[] = {
30         /* first protocol must always be HTTP handler */
31
32         { "http-only", lws_callback_http_dummy, 0, 0, },
33         LWS_PLUGIN_PROTOCOL_DUMB_INCREMENT,
34         LWS_PLUGIN_PROTOCOL_MIRROR,
35         LWS_PLUGIN_PROTOCOL_LWS_STATUS,
36         LWS_PLUGIN_PROTOCOL_POST_DEMO,
37         { NULL, NULL, 0, 0 } /* terminator */
38 };
39
40 /*
41  * mount handlers for sections of the URL space
42  */
43
44 static const struct lws_http_mount mount_ziptest = {
45         NULL,                   /* linked-list pointer to next*/
46         "/ziptest",             /* mountpoint in URL namespace on this vhost */
47         "candide.zip",  /* handler */
48         NULL,   /* default filename if none given */
49         NULL,
50         NULL,
51         NULL,
52         NULL,
53         0,
54         0,
55         0,
56         0,
57         0,
58         0,
59         LWSMPRO_FILE,   /* origin points to a callback */
60         8,                      /* strlen("/ziptest"), ie length of the mountpoint */
61         NULL,
62
63         { NULL, NULL } // sentinel
64 };
65
66 static const struct lws_http_mount mount_post = {
67         (struct lws_http_mount *)&mount_ziptest, /* linked-list pointer to next*/
68         "/formtest",            /* mountpoint in URL namespace on this vhost */
69         "protocol-post-demo",   /* handler */
70         NULL,   /* default filename if none given */
71         NULL,
72         NULL,
73         NULL,
74         NULL,
75         0,
76         0,
77         0,
78         0,
79         0,
80         0,
81         LWSMPRO_CALLBACK,       /* origin points to a callback */
82         9,                      /* strlen("/formtest"), ie length of the mountpoint */
83         NULL,
84
85         { NULL, NULL } // sentinel
86 };
87
88
89 static const struct lws_http_mount mount = {
90         /* .mount_next */               &mount_post,    /* linked-list "next" */
91         /* .mountpoint */               "/",            /* mountpoint URL */
92         /* .origin */                   "./mount-origin", /* serve from dir */
93         /* .def */                      "test.html",    /* default filename */
94         /* .protocol */                 NULL,
95         /* .cgienv */                   NULL,
96         /* .extra_mimetypes */          NULL,
97         /* .interpret */                NULL,
98         /* .cgi_timeout */              0,
99         /* .cache_max_age */            0,
100         /* .auth_mask */                0,
101         /* .cache_reusable */           0,
102         /* .cache_revalidate */         0,
103         /* .cache_intermediaries */     0,
104         /* .origin_protocol */          LWSMPRO_FILE,   /* files in a dir */
105         /* .mountpoint_len */           1,              /* char count */
106         /* .basic_auth_login_file */    NULL,
107 };
108
109 void signal_cb(void *handle, int signum)
110 {
111         lwsl_err("%s: signal %d\n", __func__, signum);
112
113         switch (signum) {
114         case SIGTERM:
115         case SIGINT:
116                 break;
117         default:
118
119                 break;
120         }
121         lws_context_destroy(context);
122 }
123
124 void sigint_handler(int sig)
125 {
126         signal_cb(NULL, sig);
127 }
128
129 int main(int argc, const char **argv)
130 {
131         struct lws_context_creation_info info;
132         const char *p;
133         int logs = LLL_USER | LLL_ERR | LLL_WARN | LLL_NOTICE
134                         /* for LLL_ verbosity above NOTICE to be built into lws,
135                          * lws must have been configured and built with
136                          * -DCMAKE_BUILD_TYPE=DEBUG instead of =RELEASE */
137                         /* | LLL_INFO */ /* | LLL_PARSER */ /* | LLL_HEADER */
138                         /* | LLL_EXT */ /* | LLL_CLIENT */ /* | LLL_LATENCY */
139                         /* | LLL_DEBUG */;
140
141         if ((p = lws_cmdline_option(argc, argv, "-d")))
142                 logs = atoi(p);
143
144         lws_set_log_level(logs, NULL);
145         lwsl_user("LWS minimal http server eventlib | visit http://localhost:7681\n");
146         lwsl_user(" [-s (ssl)] [--uv (libuv)] [--ev (libev)] [--event (libevent)]\n");
147
148         memset(&info, 0, sizeof info); /* otherwise uninitialized garbage */
149         info.port = 7681;
150         info.mounts = &mount;
151         info.error_document_404 = "/404.html";
152         info.pcontext = &context;
153         info.protocols = protocols;
154         info.signal_cb = signal_cb;
155         info.options =
156                 LWS_SERVER_OPTION_HTTP_HEADERS_SECURITY_BEST_PRACTICES_ENFORCE;
157
158         if (lws_cmdline_option(argc, argv, "-s")) {
159                 info.options |= LWS_SERVER_OPTION_DO_SSL_GLOBAL_INIT;
160                 info.ssl_cert_filepath = "localhost-100y.cert";
161                 info.ssl_private_key_filepath = "localhost-100y.key";
162         }
163
164         if (lws_cmdline_option(argc, argv, "--uv"))
165                 info.options |= LWS_SERVER_OPTION_LIBUV;
166         else
167                 if (lws_cmdline_option(argc, argv, "--event"))
168                         info.options |= LWS_SERVER_OPTION_LIBEVENT;
169                 else
170                         if (lws_cmdline_option(argc, argv, "--ev"))
171                                 info.options |= LWS_SERVER_OPTION_LIBEV;
172                         else
173                                 signal(SIGINT, sigint_handler);
174
175         context = lws_create_context(&info);
176         if (!context) {
177                 lwsl_err("lws init failed\n");
178                 return 1;
179         }
180
181         while (!lws_service(context, 0))
182                 ;
183
184         lwsl_info("calling external context destroy\n");
185         lws_context_destroy(context);
186
187         return 0;
188 }