2016-03-28 10:12:37 +08:00
|
|
|
/*
|
|
|
|
* libwebsockets web server application
|
|
|
|
*
|
|
|
|
* Copyright (C) 2010-2016 Andy Green <andy@warmcat.com>
|
|
|
|
*
|
2016-09-19 19:16:47 +08:00
|
|
|
* This file is made available under the Creative Commons CC0 1.0
|
|
|
|
* Universal Public Domain Dedication.
|
2016-03-28 10:12:37 +08:00
|
|
|
*
|
2016-09-19 19:16:47 +08:00
|
|
|
* The person who associated a work with this deed has dedicated
|
|
|
|
* the work to the public domain by waiving all of his or her rights
|
|
|
|
* to the work worldwide under copyright law, including all related
|
|
|
|
* and neighboring rights, to the extent allowed by law. You can copy,
|
|
|
|
* modify, distribute and perform the work, even for commercial purposes,
|
|
|
|
* all without asking permission.
|
2016-03-28 10:12:37 +08:00
|
|
|
*
|
2016-09-19 19:16:47 +08:00
|
|
|
* The test apps are intended to be adapted for use in your code, which
|
|
|
|
* may be proprietary. So unlike the library itself, they are licensed
|
|
|
|
* Public Domain.
|
2016-03-28 10:12:37 +08:00
|
|
|
*/
|
2016-05-26 21:12:11 +08:00
|
|
|
#include "lws_config.h"
|
2016-03-28 10:12:37 +08:00
|
|
|
|
2016-05-26 21:12:11 +08:00
|
|
|
#include <stdio.h>
|
|
|
|
#include <stdlib.h>
|
|
|
|
#include <getopt.h>
|
|
|
|
#include <signal.h>
|
|
|
|
#include <string.h>
|
|
|
|
#include <sys/stat.h>
|
|
|
|
#include <fcntl.h>
|
|
|
|
#include <assert.h>
|
|
|
|
#ifndef _WIN32
|
|
|
|
#include <dirent.h>
|
|
|
|
#include <syslog.h>
|
|
|
|
#include <sys/time.h>
|
|
|
|
#include <unistd.h>
|
|
|
|
#else
|
|
|
|
#include <io.h>
|
|
|
|
#include "gettimeofday.h"
|
|
|
|
#endif
|
|
|
|
|
|
|
|
#include "../lib/libwebsockets.h"
|
2016-03-28 10:12:37 +08:00
|
|
|
|
2016-05-10 10:16:52 +08:00
|
|
|
static struct lws_context *context;
|
2016-03-28 10:12:37 +08:00
|
|
|
|
|
|
|
#define LWSWS_CONFIG_STRING_SIZE (32 * 1024)
|
|
|
|
|
|
|
|
static const struct lws_extension exts[] = {
|
|
|
|
{
|
|
|
|
"permessage-deflate",
|
|
|
|
lws_extension_callback_pm_deflate,
|
|
|
|
"permessage-deflate"
|
|
|
|
},
|
|
|
|
{ NULL, NULL, NULL /* terminator */ }
|
|
|
|
};
|
|
|
|
|
2016-05-02 10:03:25 +08:00
|
|
|
static const char * const plugin_dirs[] = {
|
2016-05-10 10:16:52 +08:00
|
|
|
INSTALL_DATADIR"/libwebsockets-test-server/plugins/",
|
|
|
|
NULL
|
2016-05-02 10:03:25 +08:00
|
|
|
};
|
|
|
|
|
2016-03-28 10:12:37 +08:00
|
|
|
static struct option options[] = {
|
|
|
|
{ "help", no_argument, NULL, 'h' },
|
|
|
|
{ "debug", required_argument, NULL, 'd' },
|
|
|
|
{ "configdir", required_argument, NULL, 'c' },
|
|
|
|
#ifndef LWS_NO_DAEMONIZE
|
|
|
|
{ "daemonize", no_argument, NULL, 'D' },
|
|
|
|
#endif
|
|
|
|
{ NULL, 0, 0, 0 }
|
|
|
|
};
|
|
|
|
|
|
|
|
void signal_cb(uv_signal_t *watcher, int signum)
|
|
|
|
{
|
|
|
|
lwsl_err("Signal %d caught, exiting...\n", watcher->signum);
|
|
|
|
switch (watcher->signum) {
|
|
|
|
case SIGTERM:
|
|
|
|
case SIGINT:
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
signal(SIGABRT, SIG_DFL);
|
|
|
|
abort();
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
lws_libuv_stop(context);
|
|
|
|
}
|
2016-05-02 10:03:25 +08:00
|
|
|
|
2016-03-28 10:12:37 +08:00
|
|
|
int main(int argc, char **argv)
|
|
|
|
{
|
|
|
|
struct lws_context_creation_info info;
|
2016-05-10 10:42:19 +08:00
|
|
|
char *cs;
|
|
|
|
int opts = 0, cs_len = LWSWS_CONFIG_STRING_SIZE - 1;
|
2016-03-28 10:12:37 +08:00
|
|
|
int n = 0;
|
|
|
|
#ifndef _WIN32
|
|
|
|
int syslog_options = LOG_PID | LOG_PERROR;
|
|
|
|
#endif
|
|
|
|
#ifndef LWS_NO_DAEMONIZE
|
|
|
|
int daemonize = 0;
|
|
|
|
#endif
|
2016-05-10 10:16:52 +08:00
|
|
|
int debug_level = 7;
|
2016-05-22 07:01:35 +08:00
|
|
|
char config_dir[128];
|
2016-05-10 10:16:52 +08:00
|
|
|
char *config_strings;
|
2016-03-28 10:12:37 +08:00
|
|
|
|
|
|
|
memset(&info, 0, sizeof info);
|
2016-05-22 07:01:35 +08:00
|
|
|
strcpy(config_dir, "/etc/lwsws");
|
2016-03-28 10:12:37 +08:00
|
|
|
while (n >= 0) {
|
|
|
|
n = getopt_long(argc, argv, "hd:c:D", options, NULL);
|
|
|
|
if (n < 0)
|
|
|
|
continue;
|
|
|
|
switch (n) {
|
|
|
|
#ifndef LWS_NO_DAEMONIZE
|
|
|
|
case 'D':
|
|
|
|
daemonize = 1;
|
|
|
|
#ifndef _WIN32
|
|
|
|
syslog_options &= ~LOG_PERROR;
|
|
|
|
#endif
|
2016-04-10 09:33:54 +08:00
|
|
|
printf("Daemonizing...\n");
|
2016-03-28 10:12:37 +08:00
|
|
|
break;
|
|
|
|
#endif
|
|
|
|
case 'd':
|
|
|
|
debug_level = atoi(optarg);
|
|
|
|
break;
|
|
|
|
case 'c':
|
|
|
|
strncpy(config_dir, optarg, sizeof(config_dir) - 1);
|
|
|
|
config_dir[sizeof(config_dir) - 1] = '\0';
|
|
|
|
break;
|
|
|
|
case 'h':
|
|
|
|
fprintf(stderr, "Usage: lwsws [-c <config dir>] "
|
|
|
|
"[-d <log bitfield>] [-D] [--help]\n");
|
|
|
|
exit(1);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
#if !defined(LWS_NO_DAEMONIZE) && !defined(WIN32)
|
|
|
|
/*
|
|
|
|
* normally lock path would be /var/lock/lwsts or similar, to
|
|
|
|
* simplify getting started without having to take care about
|
|
|
|
* permissions or running as root, set to /tmp/.lwsts-lock
|
|
|
|
*/
|
|
|
|
if (daemonize && lws_daemonize("/tmp/.lwsts-lock")) {
|
|
|
|
fprintf(stderr, "Failed to daemonize\n");
|
|
|
|
return 10;
|
|
|
|
}
|
2016-04-10 09:33:54 +08:00
|
|
|
if (daemonize)
|
|
|
|
lwsl_notice("Daemonized\n");
|
2016-03-28 10:12:37 +08:00
|
|
|
#endif
|
|
|
|
|
|
|
|
#ifndef _WIN32
|
|
|
|
/* we will only try to log things according to our debug_level */
|
|
|
|
setlogmask(LOG_UPTO (LOG_DEBUG));
|
|
|
|
openlog("lwsws", syslog_options, LOG_DAEMON);
|
|
|
|
#endif
|
|
|
|
|
|
|
|
lws_set_log_level(debug_level, lwsl_emit_syslog);
|
|
|
|
|
2016-09-19 19:16:47 +08:00
|
|
|
lwsl_notice("lwsws libwebsockets web server - license CC0 + LGPL2.1\n");
|
2016-03-28 10:12:37 +08:00
|
|
|
lwsl_notice("(C) Copyright 2010-2016 Andy Green <andy@warmcat.com>\n");
|
|
|
|
|
2016-05-10 10:42:19 +08:00
|
|
|
cs = config_strings = malloc(LWSWS_CONFIG_STRING_SIZE);
|
|
|
|
if (!config_strings) {
|
|
|
|
lwsl_err("Unable to allocate config strings heap\n");
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
2016-03-28 10:12:37 +08:00
|
|
|
memset(&info, 0, sizeof(info));
|
|
|
|
|
|
|
|
info.max_http_header_pool = 16;
|
|
|
|
info.options = opts | LWS_SERVER_OPTION_VALIDATE_UTF8 |
|
2016-04-06 16:15:40 +08:00
|
|
|
LWS_SERVER_OPTION_EXPLICIT_VHOSTS |
|
|
|
|
LWS_SERVER_OPTION_LIBUV;
|
|
|
|
|
2016-05-02 10:03:25 +08:00
|
|
|
info.plugin_dirs = plugin_dirs;
|
2016-03-28 10:12:37 +08:00
|
|
|
lwsl_notice("Using config dir: \"%s\"\n", config_dir);
|
|
|
|
|
|
|
|
/*
|
|
|
|
* first go through the config for creating the outer context
|
|
|
|
*/
|
|
|
|
if (lwsws_get_config_globals(&info, config_dir, &cs, &cs_len))
|
2016-05-10 10:42:19 +08:00
|
|
|
goto init_failed;
|
2016-03-28 10:12:37 +08:00
|
|
|
|
|
|
|
context = lws_create_context(&info);
|
|
|
|
if (context == NULL) {
|
|
|
|
lwsl_err("libwebsocket init failed\n");
|
2016-05-10 10:42:19 +08:00
|
|
|
goto init_failed;
|
2016-03-28 10:12:37 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
2016-05-10 10:16:52 +08:00
|
|
|
* then create the vhosts... protocols are entirely coming from
|
|
|
|
* plugins, so we leave it NULL
|
2016-03-28 10:12:37 +08:00
|
|
|
*/
|
|
|
|
|
|
|
|
info.extensions = exts;
|
|
|
|
|
2016-05-10 10:42:19 +08:00
|
|
|
if (!lwsws_get_config_vhosts(context, &info, config_dir,
|
|
|
|
&cs, &cs_len)) {
|
2016-03-28 10:12:37 +08:00
|
|
|
|
2016-05-10 10:42:19 +08:00
|
|
|
/* run the server */
|
2016-03-28 10:12:37 +08:00
|
|
|
|
2016-05-10 10:42:19 +08:00
|
|
|
lws_uv_sigint_cfg(context, 1, signal_cb);
|
|
|
|
lws_uv_initloop(context, NULL, 0);
|
|
|
|
|
|
|
|
lws_libuv_run(context, 0);
|
|
|
|
}
|
2016-03-28 10:12:37 +08:00
|
|
|
|
|
|
|
lws_context_destroy(context);
|
2016-05-10 10:42:19 +08:00
|
|
|
free(config_strings);
|
2016-05-10 10:16:52 +08:00
|
|
|
|
2016-04-06 16:15:40 +08:00
|
|
|
fprintf(stderr, "lwsws exited cleanly\n");
|
2016-03-28 10:12:37 +08:00
|
|
|
|
|
|
|
#ifndef _WIN32
|
|
|
|
closelog();
|
|
|
|
#endif
|
|
|
|
|
|
|
|
return 0;
|
2016-05-10 10:42:19 +08:00
|
|
|
|
|
|
|
init_failed:
|
|
|
|
free(config_strings);
|
|
|
|
|
|
|
|
return 1;
|
2016-03-28 10:12:37 +08:00
|
|
|
}
|