1
0
Fork 0
mirror of https://github.com/warmcat/libwebsockets.git synced 2025-03-16 00:00:07 +01:00
libwebsockets/minimal-examples/http-server/minimal-http-server-multivhost/minimal-http-server.c
Andy Green 498a4e2bd7 sul: all timed objects use a single pt sul list
wsi timeout, wsi hrtimer, sequencer timeout and vh-protocol timer
all now participate on a single sorted us list.

The whole idea of polling wakes is thrown out, poll waits ignore the
timeout field and always use infinite timeouts.

Introduce a public api that can schedule its own callback from the event
loop with us resolution (usually ms is all the platform can do).

Upgrade timeouts and sequencer timeouts to also be able to use us resolution.

Introduce a prepared fakewsi in the pt, so we don't have to allocate
one on the heap when we need it.

Directly handle vh-protocol timer if LWS_MAX_SMP == 1
2019-08-09 10:12:09 +01:00

179 lines
5.2 KiB
C

/*
* lws-minimal-http-server-multivhost
*
* Written in 2010-2019 by Andy Green <andy@warmcat.com>
*
* This file is made available under the Creative Commons CC0 1.0
* Universal Public Domain Dedication.
*
* This demonstrates the most minimal http server you can make with lws.
*
* To keep it simple, it serves stuff from the subdirectory
* "./mount-origin" of the directory it was started in.
* You can change that by changing mount.origin below.
*/
#include <libwebsockets.h>
#include <string.h>
#include <signal.h>
static int interrupted;
static const struct lws_http_mount mount_localhost1 = {
/* .mount_next */ NULL, /* linked-list "next" */
/* .mountpoint */ "/", /* mountpoint URL */
/* .origin */ "./mount-origin-localhost1",
/* .def */ "index.html", /* default filename */
/* .protocol */ NULL,
/* .cgienv */ NULL,
/* .extra_mimetypes */ NULL,
/* .interpret */ NULL,
/* .cgi_timeout */ 0,
/* .cache_max_age */ 0,
/* .auth_mask */ 0,
/* .cache_reusable */ 0,
/* .cache_revalidate */ 0,
/* .cache_intermediaries */ 0,
/* .origin_protocol */ LWSMPRO_FILE, /* files in a dir */
/* .mountpoint_len */ 1, /* char count */
/* .basic_auth_login_file */ NULL,
}, mount_localhost2 = {
/* .mount_next */ NULL, /* linked-list "next" */
/* .mountpoint */ "/", /* mountpoint URL */
/* .origin */ "./mount-origin-localhost2",
/* .def */ "index.html", /* default filename */
/* .protocol */ NULL,
/* .cgienv */ NULL,
/* .extra_mimetypes */ NULL,
/* .interpret */ NULL,
/* .cgi_timeout */ 0,
/* .cache_max_age */ 0,
/* .auth_mask */ 0,
/* .cache_reusable */ 0,
/* .cache_revalidate */ 0,
/* .cache_intermediaries */ 0,
/* .origin_protocol */ LWSMPRO_FILE, /* files in a dir */
/* .mountpoint_len */ 1, /* char count */
/* .basic_auth_login_file */ NULL,
}, mount_localhost3 = {
/* .mount_next */ NULL, /* linked-list "next" */
/* .mountpoint */ "/", /* mountpoint URL */
/* .origin */ "./mount-origin-localhost3",
/* .def */ "index.html", /* default filename */
/* .protocol */ NULL,
/* .cgienv */ NULL,
/* .extra_mimetypes */ NULL,
/* .interpret */ NULL,
/* .cgi_timeout */ 0,
/* .cache_max_age */ 0,
/* .auth_mask */ 0,
/* .cache_reusable */ 0,
/* .cache_revalidate */ 0,
/* .cache_intermediaries */ 0,
/* .origin_protocol */ LWSMPRO_FILE, /* files in a dir */
/* .mountpoint_len */ 1, /* char count */
/* .basic_auth_login_file */ NULL,
};
void sigint_handler(int sig)
{
interrupted = 1;
}
void vh_destruction_notification(struct lws_vhost *vh, void *arg)
{
lwsl_user("%s: called, arg: %p\n", __func__, arg);
}
int main(int argc, const char **argv)
{
struct lws_context_creation_info info;
struct lws_context *context;
const char *p;
int n = 0, logs = LLL_USER | LLL_ERR | LLL_WARN | LLL_NOTICE
/* for LLL_ verbosity above NOTICE to be built into lws,
* lws must have been configured and built with
* -DCMAKE_BUILD_TYPE=DEBUG instead of =RELEASE */
/* | LLL_INFO */ /* | LLL_PARSER */ /* | LLL_HEADER */
/* | LLL_EXT */ /* | LLL_CLIENT */ /* | LLL_LATENCY */
/* | LLL_DEBUG */;
if ((p = lws_cmdline_option(argc, argv, "-d")))
logs = atoi(p);
lws_set_log_level(logs, NULL);
lwsl_user("LWS minimal http server-multivhost | visit http://localhost:7681 / 7682\n");
signal(SIGINT, sigint_handler);
memset(&info, 0, sizeof info); /* otherwise uninitialized garbage */
info.options = LWS_SERVER_OPTION_EXPLICIT_VHOSTS |
LWS_SERVER_OPTION_HTTP_HEADERS_SECURITY_BEST_PRACTICES_ENFORCE;
/*
* Because of LWS_SERVER_OPTION_EXPLICIT_VHOSTS, this only creates
* the context and no longer creates a default vhost
*/
context = lws_create_context(&info);
if (!context) {
lwsl_err("lws init failed\n");
return 1;
}
/* it's our job now to create the vhosts we want:
*
* - "localhost1" listen on 7681 and serve ./mount-origin-localhost1/
* - "localhost2" listen on 7682 and serve ./mount-origin-localhost2/
* - "localhost3" share 7682 and serve ./mount-origin-localhost3/
*
* Note lws supports dynamic vhost creation and destruction at runtime.
* When using multi-vhost with your own protocols, you must provide a
* pvo for each vhost naming each protocol you want enabled on it.
* minimal-ws-server-threads demonstrates how to provide pvos.
*/
info.port = 7681;
info.mounts = &mount_localhost1;
info.error_document_404 = "/404.html";
info.vhost_name = "localhost1";
if (!lws_create_vhost(context, &info)) {
lwsl_err("Failed to create first vhost\n");
goto bail;
}
info.port = 7682;
info.mounts = &mount_localhost2;
info.error_document_404 = "/404.html";
info.vhost_name = "localhost2";
if (!lws_create_vhost(context, &info)) {
lwsl_err("Failed to create second vhost\n");
goto bail;
}
/* a second vhost listens on port 7682 */
info.mounts = &mount_localhost3;
info.error_document_404 = "/404.html";
info.vhost_name = "localhost3";
info.finalize = vh_destruction_notification;
info.finalize_arg = NULL;
if (!lws_create_vhost(context, &info)) {
lwsl_err("Failed to create third vhost\n");
goto bail;
}
if (lws_cmdline_option(argc, argv, "--die-after-vhost")) {
lwsl_warn("bailing after creating vhosts\n");
goto bail;
}
while (n >= 0 && !interrupted)
n = lws_service(context, 0);
bail:
lws_context_destroy(context);
return 0;
}