1/* 2 * lws-minimal-ws-broker 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 publish / broker / subscribe ws 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#define LWS_PLUGIN_STATIC 22#include "protocol_lws_minimal.c" 23 24static struct lws_protocols protocols[] = { 25 { "http", lws_callback_http_dummy, 0, 0, 0, NULL, 0 }, 26 LWS_PLUGIN_PROTOCOL_MINIMAL, 27 LWS_PROTOCOL_LIST_TERM 28}; 29 30static int interrupted; 31 32static const struct lws_http_mount mount = { 33 /* .mount_next */ NULL, /* linked-list "next" */ 34 /* .mountpoint */ "/", /* mountpoint URL */ 35 /* .origin */ "./mount-origin", /* serve from dir */ 36 /* .def */ "index.html", /* default filename */ 37 /* .protocol */ NULL, 38 /* .cgienv */ NULL, 39 /* .extra_mimetypes */ NULL, 40 /* .interpret */ NULL, 41 /* .cgi_timeout */ 0, 42 /* .cache_max_age */ 0, 43 /* .auth_mask */ 0, 44 /* .cache_reusable */ 0, 45 /* .cache_revalidate */ 0, 46 /* .cache_intermediaries */ 0, 47 /* .origin_protocol */ LWSMPRO_FILE, /* files in a dir */ 48 /* .mountpoint_len */ 1, /* char count */ 49 /* .basic_auth_login_file */ NULL, 50}; 51 52void sigint_handler(int sig) 53{ 54 interrupted = 1; 55} 56 57int main(int argc, const char **argv) 58{ 59 struct lws_context_creation_info info; 60 struct lws_context *context; 61 const char *p; 62 int n = 0, logs = LLL_USER | LLL_ERR | LLL_WARN | LLL_NOTICE 63 /* for LLL_ verbosity above NOTICE to be built into lws, 64 * lws must have been configured and built with 65 * -DCMAKE_BUILD_TYPE=DEBUG instead of =RELEASE */ 66 /* | LLL_INFO */ /* | LLL_PARSER */ /* | LLL_HEADER */ 67 /* | LLL_EXT */ /* | LLL_CLIENT */ /* | LLL_LATENCY */ 68 /* | LLL_DEBUG */; 69 70 signal(SIGINT, sigint_handler); 71 72 if ((p = lws_cmdline_option(argc, argv, "-d"))) 73 logs = atoi(p); 74 75 lws_set_log_level(logs, NULL); 76 lwsl_user("LWS minimal ws broker | visit http://localhost:7681\n"); 77 78 memset(&info, 0, sizeof info); /* otherwise uninitialized garbage */ 79 info.port = 7681; 80 info.mounts = &mount; 81 info.protocols = protocols; 82 info.options = 83 LWS_SERVER_OPTION_HTTP_HEADERS_SECURITY_BEST_PRACTICES_ENFORCE; 84 85 context = lws_create_context(&info); 86 if (!context) { 87 lwsl_err("lws init failed\n"); 88 return 1; 89 } 90 91 while (n >= 0 && !interrupted) 92 n = lws_service(context, 0); 93 94 lws_context_destroy(context); 95 96 return 0; 97} 98