remove deprecated files
This commit is contained in:
188
src/main.c
188
src/main.c
@@ -1,188 +0,0 @@
|
||||
/*
|
||||
* lws-minimal-http-server-eventlib
|
||||
*
|
||||
* 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 a minimal http[s] server that can work with any of the
|
||||
* supported event loop backends, or the default poll() one.
|
||||
*
|
||||
* 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>
|
||||
|
||||
#define LWS_PLUGIN_STATIC
|
||||
#include "./plugins/protocol_lws_mirror.c"
|
||||
#include "./plugins/protocol_lws_status.c"
|
||||
#include "./plugins/protocol_dumb_increment.c"
|
||||
#include "./plugins/protocol_post_demo.c"
|
||||
|
||||
static struct lws_context *context;
|
||||
|
||||
static struct lws_protocols protocols[] = {
|
||||
/* first protocol must always be HTTP handler */
|
||||
|
||||
{ "http-only", lws_callback_http_dummy, 0, 0, },
|
||||
LWS_PLUGIN_PROTOCOL_DUMB_INCREMENT,
|
||||
LWS_PLUGIN_PROTOCOL_MIRROR,
|
||||
LWS_PLUGIN_PROTOCOL_LWS_STATUS,
|
||||
LWS_PLUGIN_PROTOCOL_POST_DEMO,
|
||||
{ NULL, NULL, 0, 0 } /* terminator */
|
||||
};
|
||||
|
||||
/*
|
||||
* mount handlers for sections of the URL space
|
||||
*/
|
||||
|
||||
static const struct lws_http_mount mount_ziptest = {
|
||||
NULL, /* linked-list pointer to next*/
|
||||
"/ziptest", /* mountpoint in URL namespace on this vhost */
|
||||
"candide.zip", /* handler */
|
||||
NULL, /* default filename if none given */
|
||||
NULL,
|
||||
NULL,
|
||||
NULL,
|
||||
NULL,
|
||||
0,
|
||||
0,
|
||||
0,
|
||||
0,
|
||||
0,
|
||||
0,
|
||||
LWSMPRO_FILE, /* origin points to a callback */
|
||||
8, /* strlen("/ziptest"), ie length of the mountpoint */
|
||||
NULL,
|
||||
|
||||
{ NULL, NULL } // sentinel
|
||||
};
|
||||
|
||||
static const struct lws_http_mount mount_post = {
|
||||
(struct lws_http_mount *)&mount_ziptest, /* linked-list pointer to next*/
|
||||
"/formtest", /* mountpoint in URL namespace on this vhost */
|
||||
"protocol-post-demo", /* handler */
|
||||
NULL, /* default filename if none given */
|
||||
NULL,
|
||||
NULL,
|
||||
NULL,
|
||||
NULL,
|
||||
0,
|
||||
0,
|
||||
0,
|
||||
0,
|
||||
0,
|
||||
0,
|
||||
LWSMPRO_CALLBACK, /* origin points to a callback */
|
||||
9, /* strlen("/formtest"), ie length of the mountpoint */
|
||||
NULL,
|
||||
|
||||
{ NULL, NULL } // sentinel
|
||||
};
|
||||
|
||||
|
||||
static const struct lws_http_mount mount = {
|
||||
/* .mount_next */ &mount_post, /* linked-list "next" */
|
||||
/* .mountpoint */ "/", /* mountpoint URL */
|
||||
/* .origin */ "./mount-origin", /* serve from dir */
|
||||
/* .def */ "test.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 signal_cb(void *handle, int signum)
|
||||
{
|
||||
lwsl_err("%s: signal %d\n", __func__, signum);
|
||||
|
||||
switch (signum) {
|
||||
case SIGTERM:
|
||||
case SIGINT:
|
||||
break;
|
||||
default:
|
||||
|
||||
break;
|
||||
}
|
||||
lws_context_destroy(context);
|
||||
}
|
||||
|
||||
void sigint_handler(int sig)
|
||||
{
|
||||
signal_cb(NULL, sig);
|
||||
}
|
||||
|
||||
int main(int argc, const char **argv)
|
||||
{
|
||||
struct lws_context_creation_info info;
|
||||
const char *p;
|
||||
int 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 eventlib | visit http://localhost:7681\n");
|
||||
lwsl_user(" [-s (ssl)] [--uv (libuv)] [--ev (libev)] [--event (libevent)]\n");
|
||||
|
||||
memset(&info, 0, sizeof info); /* otherwise uninitialized garbage */
|
||||
info.port = 7681;
|
||||
info.mounts = &mount;
|
||||
info.error_document_404 = "/404.html";
|
||||
info.pcontext = &context;
|
||||
info.protocols = protocols;
|
||||
info.signal_cb = signal_cb;
|
||||
info.options =
|
||||
LWS_SERVER_OPTION_HTTP_HEADERS_SECURITY_BEST_PRACTICES_ENFORCE;
|
||||
|
||||
if (lws_cmdline_option(argc, argv, "-s")) {
|
||||
info.options |= LWS_SERVER_OPTION_DO_SSL_GLOBAL_INIT;
|
||||
info.ssl_cert_filepath = "localhost-100y.cert";
|
||||
info.ssl_private_key_filepath = "localhost-100y.key";
|
||||
}
|
||||
|
||||
if (lws_cmdline_option(argc, argv, "--uv"))
|
||||
info.options |= LWS_SERVER_OPTION_LIBUV;
|
||||
else
|
||||
if (lws_cmdline_option(argc, argv, "--event"))
|
||||
info.options |= LWS_SERVER_OPTION_LIBEVENT;
|
||||
else
|
||||
if (lws_cmdline_option(argc, argv, "--ev"))
|
||||
info.options |= LWS_SERVER_OPTION_LIBEV;
|
||||
else
|
||||
signal(SIGINT, sigint_handler);
|
||||
|
||||
context = lws_create_context(&info);
|
||||
if (!context) {
|
||||
lwsl_err("lws init failed\n");
|
||||
return 1;
|
||||
}
|
||||
|
||||
while (!lws_service(context, 0))
|
||||
;
|
||||
|
||||
lwsl_info("calling external context destroy\n");
|
||||
lws_context_destroy(context);
|
||||
|
||||
return 0;
|
||||
}
|
||||
55
src/main.cpp
Normal file
55
src/main.cpp
Normal file
@@ -0,0 +1,55 @@
|
||||
/*!
|
||||
* main.cpp
|
||||
*
|
||||
* Copyright (c) 2015-2019, NADAL Jean-Baptiste. All rights reserved.
|
||||
*
|
||||
* This library is free software; you can redistribute it and/or
|
||||
* modify it under the terms of the GNU Lesser General Public
|
||||
* License as published by the Free Software Foundation; either
|
||||
* version 2.1 of the License, or (at your option) any later version.
|
||||
*
|
||||
* This library is distributed in the hope that it will be useful,
|
||||
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
||||
* Lesser General Public License for more details.
|
||||
*
|
||||
* You should have received a copy of the GNU Lesser General Public
|
||||
* License along with this library; if not, write to the Free Software
|
||||
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
|
||||
* MA 02110-1301 USA
|
||||
*
|
||||
* @Author: NADAL Jean-Baptiste
|
||||
* @Date: 08/11/2019
|
||||
*
|
||||
*/
|
||||
|
||||
// This is an independent project of an individual developer. Dear PVS-Studio, please check it.
|
||||
// PVS-Studio Static Code Analyzer for C, C++, C#, and Java: http://www.viva64.com
|
||||
|
||||
/*-------------------------------- INCLUDES ---------------------------------*/
|
||||
|
||||
#include <cstdio>
|
||||
|
||||
#include "server/domo-server.h"
|
||||
|
||||
/*! ----------------------------------------------------------------------------
|
||||
* @fn main
|
||||
*
|
||||
* @brief Main function of domo-iot daemon.
|
||||
*/
|
||||
int main(int argc, char *argv[])
|
||||
{
|
||||
int the_return;
|
||||
|
||||
DomoServer the_server;
|
||||
|
||||
if (!the_server.setup())
|
||||
{
|
||||
fprintf(stderr, "Failed to setup the daemon.\n");
|
||||
return -1;
|
||||
}
|
||||
|
||||
the_return = the_server.loop();
|
||||
|
||||
return the_return;
|
||||
}
|
||||
@@ -1,148 +0,0 @@
|
||||
/*
|
||||
* ws protocol handler plugin for "dumb increment"
|
||||
*
|
||||
* 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.
|
||||
*
|
||||
* 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.
|
||||
*
|
||||
* These test plugins are intended to be adapted for use in your code, which
|
||||
* may be proprietary. So unlike the library itself, they are licensed
|
||||
* Public Domain.
|
||||
*/
|
||||
|
||||
#if !defined (LWS_PLUGIN_STATIC)
|
||||
#define LWS_DLL
|
||||
#define LWS_INTERNAL
|
||||
#include <libwebsockets.h>
|
||||
#endif
|
||||
|
||||
#include <string.h>
|
||||
|
||||
#define DUMB_PERIOD_US 50000
|
||||
|
||||
struct pss__dumb_increment {
|
||||
int number;
|
||||
};
|
||||
|
||||
struct vhd__dumb_increment {
|
||||
const unsigned int *options;
|
||||
};
|
||||
|
||||
static int
|
||||
callback_dumb_increment(struct lws *wsi, enum lws_callback_reasons reason,
|
||||
void *user, void *in, size_t len)
|
||||
{
|
||||
struct pss__dumb_increment *pss = (struct pss__dumb_increment *)user;
|
||||
struct vhd__dumb_increment *vhd =
|
||||
(struct vhd__dumb_increment *)
|
||||
lws_protocol_vh_priv_get(lws_get_vhost(wsi),
|
||||
lws_get_protocol(wsi));
|
||||
uint8_t buf[LWS_PRE + 20], *p = &buf[LWS_PRE];
|
||||
const struct lws_protocol_vhost_options *opt;
|
||||
int n, m;
|
||||
|
||||
switch (reason) {
|
||||
case LWS_CALLBACK_PROTOCOL_INIT:
|
||||
vhd = lws_protocol_vh_priv_zalloc(lws_get_vhost(wsi),
|
||||
lws_get_protocol(wsi),
|
||||
sizeof(struct vhd__dumb_increment));
|
||||
if (!vhd)
|
||||
return -1;
|
||||
if ((opt = lws_pvo_search(
|
||||
(const struct lws_protocol_vhost_options *)in,
|
||||
"options")))
|
||||
vhd->options = (unsigned int *)opt->value;
|
||||
break;
|
||||
|
||||
case LWS_CALLBACK_ESTABLISHED:
|
||||
pss->number = 0;
|
||||
if (!vhd->options || !((*vhd->options) & 1))
|
||||
lws_set_timer_usecs(wsi, DUMB_PERIOD_US);
|
||||
break;
|
||||
|
||||
case LWS_CALLBACK_SERVER_WRITEABLE:
|
||||
n = lws_snprintf((char *)p, sizeof(buf) - LWS_PRE, "%d",
|
||||
pss->number++);
|
||||
m = lws_write(wsi, p, n, LWS_WRITE_TEXT);
|
||||
if (m < n) {
|
||||
lwsl_err("ERROR %d writing to di socket\n", n);
|
||||
return -1;
|
||||
}
|
||||
break;
|
||||
|
||||
case LWS_CALLBACK_RECEIVE:
|
||||
if (len < 6)
|
||||
break;
|
||||
if (strncmp((const char *)in, "reset\n", 6) == 0)
|
||||
pss->number = 0;
|
||||
if (strncmp((const char *)in, "closeme\n", 8) == 0) {
|
||||
lwsl_notice("dumb_inc: closing as requested\n");
|
||||
lws_close_reason(wsi, LWS_CLOSE_STATUS_GOINGAWAY,
|
||||
(unsigned char *)"seeya", 5);
|
||||
return -1;
|
||||
}
|
||||
break;
|
||||
|
||||
case LWS_CALLBACK_TIMER:
|
||||
if (!vhd->options || !((*vhd->options) & 1)) {
|
||||
lws_callback_on_writable_all_protocol_vhost(
|
||||
lws_get_vhost(wsi), lws_get_protocol(wsi));
|
||||
lws_set_timer_usecs(wsi, DUMB_PERIOD_US);
|
||||
}
|
||||
break;
|
||||
|
||||
default:
|
||||
break;
|
||||
}
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
#define LWS_PLUGIN_PROTOCOL_DUMB_INCREMENT \
|
||||
{ \
|
||||
"dumb-increment-protocol", \
|
||||
callback_dumb_increment, \
|
||||
sizeof(struct pss__dumb_increment), \
|
||||
10, /* rx buf size must be >= permessage-deflate rx size */ \
|
||||
0, NULL, 0 \
|
||||
}
|
||||
|
||||
#if !defined (LWS_PLUGIN_STATIC)
|
||||
|
||||
static const struct lws_protocols protocols[] = {
|
||||
LWS_PLUGIN_PROTOCOL_DUMB_INCREMENT
|
||||
};
|
||||
|
||||
LWS_EXTERN LWS_VISIBLE int
|
||||
init_protocol_dumb_increment(struct lws_context *context,
|
||||
struct lws_plugin_capability *c)
|
||||
{
|
||||
if (c->api_magic != LWS_PLUGIN_API_MAGIC) {
|
||||
lwsl_err("Plugin API %d, library API %d", LWS_PLUGIN_API_MAGIC,
|
||||
c->api_magic);
|
||||
return 1;
|
||||
}
|
||||
|
||||
c->protocols = protocols;
|
||||
c->count_protocols = LWS_ARRAY_SIZE(protocols);
|
||||
c->extensions = NULL;
|
||||
c->count_extensions = 0;
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
LWS_EXTERN LWS_VISIBLE int
|
||||
destroy_protocol_dumb_increment(struct lws_context *context)
|
||||
{
|
||||
return 0;
|
||||
}
|
||||
|
||||
#endif
|
||||
@@ -1,497 +0,0 @@
|
||||
/*
|
||||
* libwebsockets-test-server - libwebsockets test implementation
|
||||
*
|
||||
* 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.
|
||||
*
|
||||
* 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.
|
||||
*
|
||||
* 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.
|
||||
*
|
||||
* Notice that the lws_pthread... locking apis are all zero-footprint
|
||||
* NOPs in the case LWS_MAX_SMP == 1, which is the default. When lws
|
||||
* is built for multiple service threads though, they resolve to their
|
||||
* pthreads equivalents.
|
||||
*/
|
||||
|
||||
#if !defined (LWS_PLUGIN_STATIC)
|
||||
#define LWS_DLL
|
||||
#define LWS_INTERNAL
|
||||
#include <libwebsockets.h>
|
||||
#endif
|
||||
|
||||
#include <string.h>
|
||||
#include <stdlib.h>
|
||||
|
||||
#define QUEUELEN 32
|
||||
/* queue free space below this, rx flow is disabled */
|
||||
#define RXFLOW_MIN (4)
|
||||
/* queue free space above this, rx flow is enabled */
|
||||
#define RXFLOW_MAX ((2 * QUEUELEN) / 3)
|
||||
|
||||
#define MAX_MIRROR_INSTANCES 3
|
||||
|
||||
struct mirror_instance;
|
||||
|
||||
struct per_session_data__lws_mirror {
|
||||
struct lws *wsi;
|
||||
struct mirror_instance *mi;
|
||||
struct per_session_data__lws_mirror *same_mi_pss_list;
|
||||
uint32_t tail;
|
||||
};
|
||||
|
||||
/* this is the element in the ring */
|
||||
struct a_message {
|
||||
void *payload;
|
||||
size_t len;
|
||||
};
|
||||
|
||||
struct mirror_instance {
|
||||
struct mirror_instance *next;
|
||||
lws_pthread_mutex(lock) /* protects all mirror instance data */
|
||||
struct per_session_data__lws_mirror *same_mi_pss_list;
|
||||
/**< must hold the the per_vhost_data__lws_mirror.lock as well
|
||||
* to change mi list membership */
|
||||
struct lws_ring *ring;
|
||||
int messages_allocated;
|
||||
char name[30];
|
||||
char rx_enabled;
|
||||
};
|
||||
|
||||
struct per_vhost_data__lws_mirror {
|
||||
lws_pthread_mutex(lock) /* protects mi_list membership changes */
|
||||
struct mirror_instance *mi_list;
|
||||
};
|
||||
|
||||
|
||||
/* enable or disable rx from all connections to this mirror instance */
|
||||
static void
|
||||
__mirror_rxflow_instance(struct mirror_instance *mi, int enable)
|
||||
{
|
||||
lws_start_foreach_ll(struct per_session_data__lws_mirror *,
|
||||
pss, mi->same_mi_pss_list) {
|
||||
lws_rx_flow_control(pss->wsi, enable);
|
||||
} lws_end_foreach_ll(pss, same_mi_pss_list);
|
||||
|
||||
mi->rx_enabled = enable;
|
||||
}
|
||||
|
||||
/*
|
||||
* Find out which connection to this mirror instance has the longest number
|
||||
* of still unread elements in the ringbuffer and update the lws_ring "oldest
|
||||
* tail" with it. Elements behind the "oldest tail" are freed and recycled for
|
||||
* new head content. Elements after the "oldest tail" are still waiting to be
|
||||
* read by somebody.
|
||||
*
|
||||
* If the oldest tail moved on from before, check if it created enough space
|
||||
* in the queue to re-enable RX flow control for the mirror instance.
|
||||
*
|
||||
* Mark connections that are at the oldest tail as being on a 3s timeout to
|
||||
* transmit something, otherwise the connection will be closed. Without this,
|
||||
* a choked or nonresponsive connection can block the FIFO from freeing up any
|
||||
* new space for new data.
|
||||
*
|
||||
* You can skip calling this if on your connection, before processing, the tail
|
||||
* was not equal to the current worst, ie, if the tail you will work on is !=
|
||||
* lws_ring_get_oldest_tail(ring) then no need to call this when the tail
|
||||
* has changed; it wasn't the oldest so it won't change the oldest.
|
||||
*
|
||||
* Returns 0 if oldest unchanged or 1 if oldest changed from this call.
|
||||
*/
|
||||
static int
|
||||
__mirror_update_worst_tail(struct mirror_instance *mi)
|
||||
{
|
||||
uint32_t wai, worst = 0, worst_tail = 0, oldest;
|
||||
struct per_session_data__lws_mirror *worst_pss = NULL;
|
||||
|
||||
oldest = lws_ring_get_oldest_tail(mi->ring);
|
||||
|
||||
lws_start_foreach_ll(struct per_session_data__lws_mirror *,
|
||||
pss, mi->same_mi_pss_list) {
|
||||
wai = (uint32_t)lws_ring_get_count_waiting_elements(mi->ring,
|
||||
&pss->tail);
|
||||
if (wai >= worst) {
|
||||
worst = wai;
|
||||
worst_tail = pss->tail;
|
||||
worst_pss = pss;
|
||||
}
|
||||
} lws_end_foreach_ll(pss, same_mi_pss_list);
|
||||
|
||||
if (!worst_pss)
|
||||
return 0;
|
||||
|
||||
lws_ring_update_oldest_tail(mi->ring, worst_tail);
|
||||
if (oldest == lws_ring_get_oldest_tail(mi->ring))
|
||||
return 0;
|
||||
/*
|
||||
* The oldest tail did move on. Check if we should re-enable rx flow
|
||||
* for the mirror instance since we made some space now.
|
||||
*/
|
||||
if (!mi->rx_enabled && /* rx is disabled */
|
||||
lws_ring_get_count_free_elements(mi->ring) >= RXFLOW_MAX)
|
||||
/* there is enough space, let's re-enable rx for our instance */
|
||||
__mirror_rxflow_instance(mi, 1);
|
||||
|
||||
/* if nothing in queue, no timeout needed */
|
||||
if (!worst)
|
||||
return 1;
|
||||
|
||||
/*
|
||||
* The guy(s) with the oldest tail block the ringbuffer from recycling
|
||||
* the FIFO entries he has not read yet. Don't allow those guys to
|
||||
* block the FIFO operation for very long.
|
||||
*/
|
||||
lws_start_foreach_ll(struct per_session_data__lws_mirror *,
|
||||
pss, mi->same_mi_pss_list) {
|
||||
if (pss->tail == worst_tail)
|
||||
/*
|
||||
* Our policy is if you are the slowest connection,
|
||||
* you had better transmit something to help with that
|
||||
* within 3s, or we will hang up on you to stop you
|
||||
* blocking the FIFO for everyone else.
|
||||
*/
|
||||
lws_set_timeout(pss->wsi,
|
||||
PENDING_TIMEOUT_USER_REASON_BASE, 3);
|
||||
} lws_end_foreach_ll(pss, same_mi_pss_list);
|
||||
|
||||
return 1;
|
||||
}
|
||||
|
||||
static void
|
||||
__mirror_callback_all_in_mi_on_writable(struct mirror_instance *mi)
|
||||
{
|
||||
/* ask for WRITABLE callback for every wsi on this mi */
|
||||
lws_start_foreach_ll(struct per_session_data__lws_mirror *,
|
||||
pss, mi->same_mi_pss_list) {
|
||||
lws_callback_on_writable(pss->wsi);
|
||||
} lws_end_foreach_ll(pss, same_mi_pss_list);
|
||||
}
|
||||
|
||||
static void
|
||||
__mirror_destroy_message(void *_msg)
|
||||
{
|
||||
struct a_message *msg = _msg;
|
||||
|
||||
free(msg->payload);
|
||||
msg->payload = NULL;
|
||||
msg->len = 0;
|
||||
}
|
||||
|
||||
static int
|
||||
callback_lws_mirror(struct lws *wsi, enum lws_callback_reasons reason,
|
||||
void *user, void *in, size_t len)
|
||||
{
|
||||
struct per_session_data__lws_mirror *pss =
|
||||
(struct per_session_data__lws_mirror *)user;
|
||||
struct per_vhost_data__lws_mirror *v =
|
||||
(struct per_vhost_data__lws_mirror *)
|
||||
lws_protocol_vh_priv_get(lws_get_vhost(wsi),
|
||||
lws_get_protocol(wsi));
|
||||
char name[300], update_worst, sent_something, *pn = name;
|
||||
struct mirror_instance *mi = NULL;
|
||||
const struct a_message *msg;
|
||||
struct a_message amsg;
|
||||
uint32_t oldest_tail;
|
||||
int n, count_mi = 0;
|
||||
|
||||
switch (reason) {
|
||||
case LWS_CALLBACK_ESTABLISHED:
|
||||
lwsl_info("%s: LWS_CALLBACK_ESTABLISHED\n", __func__);
|
||||
|
||||
/*
|
||||
* mirror instance name... defaults to "", but if URL includes
|
||||
* "?mirror=xxx", will be "xxx"
|
||||
*/
|
||||
name[0] = '\0';
|
||||
if (!lws_get_urlarg_by_name(wsi, "mirror", name,
|
||||
sizeof(name) - 1))
|
||||
lwsl_debug("get urlarg failed\n");
|
||||
if (strchr(name, '='))
|
||||
pn = strchr(name, '=') + 1;
|
||||
|
||||
//lwsl_notice("%s: mirror name '%s'\n", __func__, pn);
|
||||
|
||||
/* is there already a mirror instance of this name? */
|
||||
|
||||
lws_pthread_mutex_lock(&v->lock); /* vhost lock { */
|
||||
|
||||
lws_start_foreach_ll(struct mirror_instance *, mi1,
|
||||
v->mi_list) {
|
||||
count_mi++;
|
||||
if (!strcmp(pn, mi1->name)) {
|
||||
/* yes... we will join it */
|
||||
mi = mi1;
|
||||
break;
|
||||
}
|
||||
} lws_end_foreach_ll(mi1, next);
|
||||
|
||||
if (!mi) {
|
||||
|
||||
/* no existing mirror instance for name */
|
||||
if (count_mi == MAX_MIRROR_INSTANCES) {
|
||||
lws_pthread_mutex_unlock(&v->lock); /* } vh lock */
|
||||
return -1;
|
||||
}
|
||||
|
||||
/* create one with this name, and join it */
|
||||
mi = malloc(sizeof(*mi));
|
||||
if (!mi)
|
||||
goto bail1;
|
||||
memset(mi, 0, sizeof(*mi));
|
||||
mi->ring = lws_ring_create(sizeof(struct a_message),
|
||||
QUEUELEN,
|
||||
__mirror_destroy_message);
|
||||
if (!mi->ring) {
|
||||
free(mi);
|
||||
goto bail1;
|
||||
}
|
||||
|
||||
mi->next = v->mi_list;
|
||||
v->mi_list = mi;
|
||||
lws_snprintf(mi->name, sizeof(mi->name) - 1, "%s", pn);
|
||||
mi->rx_enabled = 1;
|
||||
|
||||
lws_pthread_mutex_init(&mi->lock);
|
||||
|
||||
lwsl_notice("Created new mi %p '%s'\n", mi, pn);
|
||||
}
|
||||
|
||||
/* add our pss to list of guys bound to this mi */
|
||||
|
||||
lws_ll_fwd_insert(pss, same_mi_pss_list, mi->same_mi_pss_list);
|
||||
|
||||
/* init the pss */
|
||||
|
||||
pss->mi = mi;
|
||||
pss->tail = lws_ring_get_oldest_tail(mi->ring);
|
||||
pss->wsi = wsi;
|
||||
|
||||
lws_pthread_mutex_unlock(&v->lock); /* } vhost lock */
|
||||
break;
|
||||
|
||||
bail1:
|
||||
lws_pthread_mutex_unlock(&v->lock); /* } vhost lock */
|
||||
return 1;
|
||||
|
||||
case LWS_CALLBACK_CLOSED:
|
||||
/* detach our pss from the mirror instance */
|
||||
mi = pss->mi;
|
||||
if (!mi)
|
||||
break;
|
||||
|
||||
lws_pthread_mutex_lock(&v->lock); /* vhost lock { */
|
||||
|
||||
/* remove our closing pss from its mirror instance list */
|
||||
lws_ll_fwd_remove(struct per_session_data__lws_mirror,
|
||||
same_mi_pss_list, pss, mi->same_mi_pss_list);
|
||||
pss->mi = NULL;
|
||||
|
||||
if (mi->same_mi_pss_list) {
|
||||
/*
|
||||
* Still other pss using the mirror instance. The pss
|
||||
* going away may have had the oldest tail, reconfirm
|
||||
* using the remaining pss what is the current oldest
|
||||
* tail. If the oldest tail moves on, this call also
|
||||
* will re-enable rx flow control when appropriate.
|
||||
*/
|
||||
lws_pthread_mutex_lock(&mi->lock); /* mi lock { */
|
||||
__mirror_update_worst_tail(mi);
|
||||
lws_pthread_mutex_unlock(&mi->lock); /* } mi lock */
|
||||
lws_pthread_mutex_unlock(&v->lock); /* } vhost lock */
|
||||
break;
|
||||
}
|
||||
|
||||
/* No more pss using the mirror instance... delete mi */
|
||||
|
||||
lws_start_foreach_llp(struct mirror_instance **,
|
||||
pmi, v->mi_list) {
|
||||
if (*pmi == mi) {
|
||||
*pmi = (*pmi)->next;
|
||||
|
||||
lws_ring_destroy(mi->ring);
|
||||
lws_pthread_mutex_destroy(&mi->lock);
|
||||
|
||||
free(mi);
|
||||
break;
|
||||
}
|
||||
} lws_end_foreach_llp(pmi, next);
|
||||
|
||||
lws_pthread_mutex_unlock(&v->lock); /* } vhost lock */
|
||||
break;
|
||||
|
||||
case LWS_CALLBACK_CONFIRM_EXTENSION_OKAY:
|
||||
return 1; /* disallow compression */
|
||||
|
||||
case LWS_CALLBACK_PROTOCOL_INIT: /* per vhost */
|
||||
lws_protocol_vh_priv_zalloc(lws_get_vhost(wsi),
|
||||
lws_get_protocol(wsi),
|
||||
sizeof(struct per_vhost_data__lws_mirror));
|
||||
v = (struct per_vhost_data__lws_mirror *)
|
||||
lws_protocol_vh_priv_get(lws_get_vhost(wsi),
|
||||
lws_get_protocol(wsi));
|
||||
lws_pthread_mutex_init(&v->lock);
|
||||
break;
|
||||
|
||||
case LWS_CALLBACK_PROTOCOL_DESTROY:
|
||||
lws_pthread_mutex_destroy(&v->lock);
|
||||
break;
|
||||
|
||||
case LWS_CALLBACK_SERVER_WRITEABLE:
|
||||
lws_pthread_mutex_lock(&pss->mi->lock); /* instance lock { */
|
||||
oldest_tail = lws_ring_get_oldest_tail(pss->mi->ring);
|
||||
update_worst = oldest_tail == pss->tail;
|
||||
sent_something = 0;
|
||||
|
||||
do {
|
||||
msg = lws_ring_get_element(pss->mi->ring, &pss->tail);
|
||||
if (!msg)
|
||||
break;
|
||||
|
||||
if (!msg->payload) {
|
||||
lwsl_err("%s: NULL payload: worst = %d,"
|
||||
" pss->tail = %d\n", __func__,
|
||||
oldest_tail, pss->tail);
|
||||
if (lws_ring_consume(pss->mi->ring, &pss->tail,
|
||||
NULL, 1))
|
||||
continue;
|
||||
break;
|
||||
}
|
||||
|
||||
n = lws_write(wsi, (unsigned char *)msg->payload +
|
||||
LWS_PRE, msg->len, LWS_WRITE_TEXT);
|
||||
if (n < 0) {
|
||||
lwsl_info("%s: WRITEABLE: %d\n", __func__, n);
|
||||
|
||||
goto bail2;
|
||||
}
|
||||
sent_something = 1;
|
||||
lws_ring_consume(pss->mi->ring, &pss->tail, NULL, 1);
|
||||
|
||||
} while (!lws_send_pipe_choked(wsi));
|
||||
|
||||
/* if any left for us to send, ask for writeable again */
|
||||
if (lws_ring_get_count_waiting_elements(pss->mi->ring,
|
||||
&pss->tail))
|
||||
lws_callback_on_writable(wsi);
|
||||
|
||||
if (!sent_something || !update_worst)
|
||||
goto done1;
|
||||
|
||||
/*
|
||||
* We are no longer holding the oldest tail (since we sent
|
||||
* something. So free us of the timeout related to hogging the
|
||||
* oldest tail.
|
||||
*/
|
||||
lws_set_timeout(pss->wsi, NO_PENDING_TIMEOUT, 0);
|
||||
/*
|
||||
* If we were originally at the oldest fifo position of
|
||||
* all the tails, now we used some up we may have
|
||||
* changed the oldest fifo position and made some space.
|
||||
*/
|
||||
__mirror_update_worst_tail(pss->mi);
|
||||
|
||||
done1:
|
||||
lws_pthread_mutex_unlock(&pss->mi->lock); /* } instance lock */
|
||||
break;
|
||||
|
||||
bail2:
|
||||
lws_pthread_mutex_unlock(&pss->mi->lock); /* } instance lock */
|
||||
|
||||
return -1;
|
||||
|
||||
case LWS_CALLBACK_RECEIVE:
|
||||
lws_pthread_mutex_lock(&pss->mi->lock); /* mi lock { */
|
||||
n = (int)lws_ring_get_count_free_elements(pss->mi->ring);
|
||||
if (!n) {
|
||||
lwsl_notice("dropping!\n");
|
||||
if (pss->mi->rx_enabled)
|
||||
__mirror_rxflow_instance(pss->mi, 0);
|
||||
goto req_writable;
|
||||
}
|
||||
|
||||
amsg.payload = malloc(LWS_PRE + len);
|
||||
amsg.len = len;
|
||||
if (!amsg.payload) {
|
||||
lwsl_notice("OOM: dropping\n");
|
||||
goto done2;
|
||||
}
|
||||
|
||||
memcpy((char *)amsg.payload + LWS_PRE, in, len);
|
||||
if (!lws_ring_insert(pss->mi->ring, &amsg, 1)) {
|
||||
__mirror_destroy_message(&amsg);
|
||||
lwsl_notice("dropping!\n");
|
||||
if (pss->mi->rx_enabled)
|
||||
__mirror_rxflow_instance(pss->mi, 0);
|
||||
goto req_writable;
|
||||
}
|
||||
|
||||
if (pss->mi->rx_enabled &&
|
||||
lws_ring_get_count_free_elements(pss->mi->ring) <
|
||||
RXFLOW_MIN)
|
||||
__mirror_rxflow_instance(pss->mi, 0);
|
||||
|
||||
req_writable:
|
||||
__mirror_callback_all_in_mi_on_writable(pss->mi);
|
||||
|
||||
done2:
|
||||
lws_pthread_mutex_unlock(&pss->mi->lock); /* } mi lock */
|
||||
break;
|
||||
|
||||
case LWS_CALLBACK_EVENT_WAIT_CANCELLED:
|
||||
lwsl_info("LWS_CALLBACK_EVENT_WAIT_CANCELLED\n");
|
||||
break;
|
||||
|
||||
default:
|
||||
break;
|
||||
}
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
#define LWS_PLUGIN_PROTOCOL_MIRROR { \
|
||||
"lws-mirror-protocol", \
|
||||
callback_lws_mirror, \
|
||||
sizeof(struct per_session_data__lws_mirror), \
|
||||
4096, /* rx buf size must be >= permessage-deflate rx size */ \
|
||||
0, NULL, 0 \
|
||||
}
|
||||
|
||||
#if !defined (LWS_PLUGIN_STATIC)
|
||||
|
||||
static const struct lws_protocols protocols[] = {
|
||||
LWS_PLUGIN_PROTOCOL_MIRROR
|
||||
};
|
||||
|
||||
LWS_EXTERN LWS_VISIBLE int
|
||||
init_protocol_lws_mirror(struct lws_context *context,
|
||||
struct lws_plugin_capability *c)
|
||||
{
|
||||
if (c->api_magic != LWS_PLUGIN_API_MAGIC) {
|
||||
lwsl_err("Plugin API %d, library API %d", LWS_PLUGIN_API_MAGIC,
|
||||
c->api_magic);
|
||||
return 1;
|
||||
}
|
||||
|
||||
c->protocols = protocols;
|
||||
c->count_protocols = LWS_ARRAY_SIZE(protocols);
|
||||
c->extensions = NULL;
|
||||
c->count_extensions = 0;
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
LWS_EXTERN LWS_VISIBLE int
|
||||
destroy_protocol_lws_mirror(struct lws_context *context)
|
||||
{
|
||||
return 0;
|
||||
}
|
||||
#endif
|
||||
@@ -1,271 +0,0 @@
|
||||
/*
|
||||
* libwebsockets-test-server - libwebsockets test implementation
|
||||
*
|
||||
* 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.
|
||||
*
|
||||
* 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.
|
||||
*
|
||||
* 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.
|
||||
*/
|
||||
|
||||
#if !defined (LWS_PLUGIN_STATIC)
|
||||
#define LWS_DLL
|
||||
#define LWS_INTERNAL
|
||||
#include <libwebsockets.h>
|
||||
#endif
|
||||
|
||||
#include <time.h>
|
||||
#include <string.h>
|
||||
#ifdef WIN32
|
||||
#include <io.h>
|
||||
#include <gettimeofday.h>
|
||||
#endif
|
||||
|
||||
|
||||
typedef enum {
|
||||
WALK_NONE,
|
||||
WALK_INITIAL,
|
||||
WALK_LIST,
|
||||
WALK_FINAL
|
||||
} e_walk;
|
||||
|
||||
struct per_session_data__lws_status {
|
||||
struct per_session_data__lws_status *next;
|
||||
struct lws *wsi;
|
||||
time_t time_est;
|
||||
char user_agent[256];
|
||||
|
||||
e_walk walk;
|
||||
struct per_session_data__lws_status *walk_next;
|
||||
unsigned char subsequent:1;
|
||||
unsigned char changed_partway:1;
|
||||
unsigned char wss_over_h2:1;
|
||||
};
|
||||
|
||||
struct per_vhost_data__lws_status {
|
||||
struct per_session_data__lws_status *live_pss_list;
|
||||
struct lws_context *context;
|
||||
struct lws_vhost *vhost;
|
||||
const struct lws_protocols *protocol;
|
||||
int count_live_pss;
|
||||
};
|
||||
|
||||
static void
|
||||
trigger_resend(struct per_vhost_data__lws_status *vhd)
|
||||
{
|
||||
lws_start_foreach_ll(struct per_session_data__lws_status *, pss,
|
||||
vhd->live_pss_list) {
|
||||
if (pss->walk == WALK_NONE) {
|
||||
pss->subsequent = 0;
|
||||
pss->walk_next = vhd->live_pss_list;
|
||||
pss->walk = WALK_INITIAL;
|
||||
} else
|
||||
pss->changed_partway = 1;
|
||||
} lws_end_foreach_ll(pss, next);
|
||||
|
||||
lws_callback_on_writable_all_protocol(vhd->context, vhd->protocol);
|
||||
}
|
||||
|
||||
/* lws-status protocol */
|
||||
|
||||
int
|
||||
callback_lws_status(struct lws *wsi, enum lws_callback_reasons reason,
|
||||
void *user, void *in, size_t len)
|
||||
{
|
||||
struct per_session_data__lws_status *pss =
|
||||
(struct per_session_data__lws_status *)user;
|
||||
struct per_vhost_data__lws_status *vhd =
|
||||
(struct per_vhost_data__lws_status *)
|
||||
lws_protocol_vh_priv_get(lws_get_vhost(wsi),
|
||||
lws_get_protocol(wsi));
|
||||
char buf[LWS_PRE + 384], ip[24], *start = buf + LWS_PRE - 1, *p = start,
|
||||
*end = buf + sizeof(buf) - 1;
|
||||
int n, m;
|
||||
|
||||
switch (reason) {
|
||||
|
||||
case LWS_CALLBACK_PROTOCOL_INIT:
|
||||
vhd = lws_protocol_vh_priv_zalloc(lws_get_vhost(wsi),
|
||||
lws_get_protocol(wsi),
|
||||
sizeof(struct per_vhost_data__lws_status));
|
||||
vhd->context = lws_get_context(wsi);
|
||||
vhd->protocol = lws_get_protocol(wsi);
|
||||
vhd->vhost = lws_get_vhost(wsi);
|
||||
break;
|
||||
|
||||
case LWS_CALLBACK_ESTABLISHED:
|
||||
|
||||
/*
|
||||
* This shows how to stage sending a single ws message in
|
||||
* multiple fragments. In this case, it lets us trade off
|
||||
* memory needed to make the data vs time to send it.
|
||||
*/
|
||||
|
||||
vhd->count_live_pss++;
|
||||
pss->next = vhd->live_pss_list;
|
||||
vhd->live_pss_list = pss;
|
||||
|
||||
pss->wss_over_h2 = !!len;
|
||||
|
||||
time(&pss->time_est);
|
||||
pss->wsi = wsi;
|
||||
|
||||
if (lws_hdr_copy(wsi, pss->user_agent, sizeof(pss->user_agent),
|
||||
WSI_TOKEN_HTTP_USER_AGENT) < 0) /* too big */
|
||||
strcpy(pss->user_agent, "unknown");
|
||||
trigger_resend(vhd);
|
||||
break;
|
||||
|
||||
case LWS_CALLBACK_SERVER_WRITEABLE:
|
||||
switch (pss->walk) {
|
||||
case WALK_INITIAL:
|
||||
n = LWS_WRITE_TEXT | LWS_WRITE_NO_FIN;
|
||||
p += lws_snprintf(p, end - p,
|
||||
"{ \"version\":\"%s\","
|
||||
" \"wss_over_h2\":\"%d\","
|
||||
" \"hostname\":\"%s\","
|
||||
" \"wsi\":\"%d\", \"conns\":[",
|
||||
lws_get_library_version(),
|
||||
pss->wss_over_h2,
|
||||
lws_canonical_hostname(vhd->context),
|
||||
vhd->count_live_pss);
|
||||
pss->walk = WALK_LIST;
|
||||
pss->walk_next = vhd->live_pss_list;
|
||||
break;
|
||||
case WALK_LIST:
|
||||
n = LWS_WRITE_CONTINUATION | LWS_WRITE_NO_FIN;
|
||||
if (!pss->walk_next)
|
||||
goto walk_final;
|
||||
|
||||
if (pss->subsequent)
|
||||
*p++ = ',';
|
||||
pss->subsequent = 1;
|
||||
|
||||
m = 0;
|
||||
lws_start_foreach_ll(struct per_session_data__lws_status *,
|
||||
pss2, vhd->live_pss_list) {
|
||||
if (pss2 == pss->walk_next) {
|
||||
m = 1;
|
||||
break;
|
||||
}
|
||||
} lws_end_foreach_ll(pss2, next);
|
||||
|
||||
if (!m) {
|
||||
/* our next guy went away */
|
||||
pss->walk = WALK_FINAL;
|
||||
pss->changed_partway = 1;
|
||||
break;
|
||||
}
|
||||
|
||||
strcpy(ip, "unknown");
|
||||
lws_get_peer_simple(pss->walk_next->wsi, ip, sizeof(ip));
|
||||
p += lws_snprintf(p, end - p,
|
||||
"{\"peer\":\"%s\",\"time\":\"%ld\","
|
||||
"\"ua\":\"%s\"}",
|
||||
ip, (unsigned long)pss->walk_next->time_est,
|
||||
pss->walk_next->user_agent);
|
||||
pss->walk_next = pss->walk_next->next;
|
||||
if (!pss->walk_next)
|
||||
pss->walk = WALK_FINAL;
|
||||
break;
|
||||
case WALK_FINAL:
|
||||
walk_final:
|
||||
n = LWS_WRITE_CONTINUATION;
|
||||
p += lws_snprintf(p, 4, "]}");
|
||||
if (pss->changed_partway) {
|
||||
pss->changed_partway = 0;
|
||||
pss->subsequent = 0;
|
||||
pss->walk_next = vhd->live_pss_list;
|
||||
pss->walk = WALK_INITIAL;
|
||||
} else
|
||||
pss->walk = WALK_NONE;
|
||||
break;
|
||||
default:
|
||||
return 0;
|
||||
}
|
||||
|
||||
m = lws_write(wsi, (unsigned char *)start, p - start, n);
|
||||
if (m < 0) {
|
||||
lwsl_err("ERROR %d writing to di socket\n", m);
|
||||
return -1;
|
||||
}
|
||||
|
||||
if (pss->walk != WALK_NONE)
|
||||
lws_callback_on_writable(wsi);
|
||||
break;
|
||||
|
||||
case LWS_CALLBACK_RECEIVE:
|
||||
lwsl_notice("pmd test: RX len %d\n", (int)len);
|
||||
break;
|
||||
|
||||
case LWS_CALLBACK_CLOSED:
|
||||
// lwsl_debug("****** LWS_CALLBACK_CLOSED\n");
|
||||
lws_start_foreach_llp(struct per_session_data__lws_status **,
|
||||
ppss, vhd->live_pss_list) {
|
||||
if (*ppss == pss) {
|
||||
*ppss = pss->next;
|
||||
break;
|
||||
}
|
||||
} lws_end_foreach_llp(ppss, next);
|
||||
|
||||
trigger_resend(vhd);
|
||||
break;
|
||||
|
||||
default:
|
||||
break;
|
||||
}
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
#define LWS_PLUGIN_PROTOCOL_LWS_STATUS \
|
||||
{ \
|
||||
"lws-status", \
|
||||
callback_lws_status, \
|
||||
sizeof(struct per_session_data__lws_status), \
|
||||
512, /* rx buf size must be >= permessage-deflate rx size */ \
|
||||
0, NULL, 0 \
|
||||
}
|
||||
|
||||
#if !defined (LWS_PLUGIN_STATIC)
|
||||
|
||||
static const struct lws_protocols protocols[] = {
|
||||
LWS_PLUGIN_PROTOCOL_LWS_STATUS
|
||||
};
|
||||
|
||||
|
||||
LWS_EXTERN LWS_VISIBLE int
|
||||
init_protocol_lws_status(struct lws_context *context,
|
||||
struct lws_plugin_capability *c)
|
||||
{
|
||||
if (c->api_magic != LWS_PLUGIN_API_MAGIC) {
|
||||
lwsl_err("Plugin API %d, library API %d", LWS_PLUGIN_API_MAGIC,
|
||||
c->api_magic);
|
||||
return 1;
|
||||
}
|
||||
|
||||
c->protocols = protocols;
|
||||
c->count_protocols = LWS_ARRAY_SIZE(protocols);
|
||||
c->extensions = NULL;
|
||||
c->count_extensions = 0;
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
LWS_EXTERN LWS_VISIBLE int
|
||||
destroy_protocol_lws_status(struct lws_context *context)
|
||||
{
|
||||
return 0;
|
||||
}
|
||||
|
||||
#endif
|
||||
@@ -1,314 +0,0 @@
|
||||
/*
|
||||
* ws protocol handler plugin for "POST demo"
|
||||
*
|
||||
* 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.
|
||||
*
|
||||
* 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.
|
||||
*
|
||||
* These test plugins are intended to be adapted for use in your code, which
|
||||
* may be proprietary. So unlike the library itself, they are licensed
|
||||
* Public Domain.
|
||||
*/
|
||||
|
||||
#if !defined (LWS_PLUGIN_STATIC)
|
||||
#define LWS_DLL
|
||||
#define LWS_INTERNAL
|
||||
#include <libwebsockets.h>
|
||||
#endif
|
||||
|
||||
#include <stdlib.h>
|
||||
#include <string.h>
|
||||
#include <fcntl.h>
|
||||
#include <sys/types.h>
|
||||
#include <sys/stat.h>
|
||||
#ifdef WIN32
|
||||
#include <io.h>
|
||||
#endif
|
||||
#include <stdio.h>
|
||||
|
||||
struct per_session_data__post_demo {
|
||||
struct lws_spa *spa;
|
||||
char result[LWS_PRE + LWS_RECOMMENDED_MIN_HEADER_SPACE];
|
||||
char filename[64];
|
||||
long file_length;
|
||||
#if !defined(LWS_WITH_ESP32)
|
||||
lws_filefd_type fd;
|
||||
#endif
|
||||
uint8_t completed:1;
|
||||
uint8_t sent_headers:1;
|
||||
uint8_t sent_body:1;
|
||||
};
|
||||
|
||||
static const char * const param_names[] = {
|
||||
"text",
|
||||
"send",
|
||||
"file",
|
||||
"upload",
|
||||
};
|
||||
|
||||
enum enum_param_names {
|
||||
EPN_TEXT,
|
||||
EPN_SEND,
|
||||
EPN_FILE,
|
||||
EPN_UPLOAD,
|
||||
};
|
||||
|
||||
static int
|
||||
file_upload_cb(void *data, const char *name, const char *filename,
|
||||
char *buf, int len, enum lws_spa_fileupload_states state)
|
||||
{
|
||||
struct per_session_data__post_demo *pss =
|
||||
(struct per_session_data__post_demo *)data;
|
||||
#if !defined(LWS_WITH_ESP32)
|
||||
int n;
|
||||
|
||||
(void)n;
|
||||
#endif
|
||||
|
||||
switch (state) {
|
||||
case LWS_UFS_OPEN:
|
||||
lws_strncpy(pss->filename, filename, sizeof(pss->filename));
|
||||
/* we get the original filename in @filename arg, but for
|
||||
* simple demo use a fixed name so we don't have to deal with
|
||||
* attacks */
|
||||
#if !defined(LWS_WITH_ESP32)
|
||||
pss->fd = (lws_filefd_type)(long long)lws_open("/tmp/post-file",
|
||||
O_CREAT | O_TRUNC | O_RDWR, 0600);
|
||||
#endif
|
||||
break;
|
||||
case LWS_UFS_FINAL_CONTENT:
|
||||
case LWS_UFS_CONTENT:
|
||||
if (len) {
|
||||
pss->file_length += len;
|
||||
|
||||
/* if the file length is too big, drop it */
|
||||
if (pss->file_length > 100000)
|
||||
return 1;
|
||||
|
||||
#if !defined(LWS_WITH_ESP32)
|
||||
n = write((int)(long long)pss->fd, buf, len);
|
||||
lwsl_info("%s: write %d says %d\n", __func__, len, n);
|
||||
#else
|
||||
lwsl_notice("%s: Received chunk size %d\n", __func__, len);
|
||||
#endif
|
||||
}
|
||||
if (state == LWS_UFS_CONTENT)
|
||||
break;
|
||||
#if !defined(LWS_WITH_ESP32)
|
||||
close((int)(long long)pss->fd);
|
||||
pss->fd = LWS_INVALID_FILE;
|
||||
#endif
|
||||
break;
|
||||
case LWS_UFS_CLOSE:
|
||||
break;
|
||||
}
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
/*
|
||||
* returns length in bytes
|
||||
*/
|
||||
|
||||
static int
|
||||
format_result(struct per_session_data__post_demo *pss)
|
||||
{
|
||||
unsigned char *p, *start, *end;
|
||||
int n;
|
||||
|
||||
p = (unsigned char *)pss->result + LWS_PRE;
|
||||
start = p;
|
||||
end = p + sizeof(pss->result) - LWS_PRE - 1;
|
||||
|
||||
p += lws_snprintf((char *)p, end -p,
|
||||
"<!DOCTYPE html><html lang=\"en\"><head>"
|
||||
"<meta charset=utf-8 http-equiv=\"Content-Language\" "
|
||||
"content=\"en\"/>"
|
||||
"<title>LWS Server Status</title>"
|
||||
"</head><body><h1>Form results (after urldecoding)</h1>"
|
||||
"<table><tr><td>Name</td><td>Length</td><td>Value</td></tr>");
|
||||
|
||||
for (n = 0; n < (int)LWS_ARRAY_SIZE(param_names); n++) {
|
||||
if (!lws_spa_get_string(pss->spa, n))
|
||||
p += lws_snprintf((char *)p, end - p,
|
||||
"<tr><td><b>%s</b></td><td>0"
|
||||
"</td><td>NULL</td></tr>",
|
||||
param_names[n]);
|
||||
else
|
||||
p += lws_snprintf((char *)p, end - p,
|
||||
"<tr><td><b>%s</b></td><td>%d"
|
||||
"</td><td>%s</td></tr>",
|
||||
param_names[n],
|
||||
lws_spa_get_length(pss->spa, n),
|
||||
lws_spa_get_string(pss->spa, n));
|
||||
}
|
||||
|
||||
p += lws_snprintf((char *)p, end - p,
|
||||
"</table><br><b>filename:</b> %s, "
|
||||
"<b>length</b> %ld",
|
||||
pss->filename, pss->file_length);
|
||||
|
||||
p += lws_snprintf((char *)p, end - p, "</body></html>");
|
||||
|
||||
return (int)lws_ptr_diff(p, start);
|
||||
}
|
||||
|
||||
static int
|
||||
callback_post_demo(struct lws *wsi, enum lws_callback_reasons reason,
|
||||
void *user, void *in, size_t len)
|
||||
{
|
||||
struct per_session_data__post_demo *pss =
|
||||
(struct per_session_data__post_demo *)user;
|
||||
unsigned char *p, *start, *end;
|
||||
int n;
|
||||
|
||||
switch (reason) {
|
||||
case LWS_CALLBACK_HTTP_BODY:
|
||||
/* create the POST argument parser if not already existing */
|
||||
if (!pss->spa) {
|
||||
pss->spa = lws_spa_create(wsi, param_names,
|
||||
LWS_ARRAY_SIZE(param_names), 1024,
|
||||
file_upload_cb, pss);
|
||||
if (!pss->spa)
|
||||
return -1;
|
||||
|
||||
pss->filename[0] = '\0';
|
||||
pss->file_length = 0;
|
||||
}
|
||||
|
||||
/* let it parse the POST data */
|
||||
if (lws_spa_process(pss->spa, in, (int)len))
|
||||
return -1;
|
||||
break;
|
||||
|
||||
case LWS_CALLBACK_HTTP_BODY_COMPLETION:
|
||||
lwsl_debug("LWS_CALLBACK_HTTP_BODY_COMPLETION: %p\n", wsi);
|
||||
/* call to inform no more payload data coming */
|
||||
lws_spa_finalize(pss->spa);
|
||||
|
||||
pss->completed = 1;
|
||||
lws_callback_on_writable(wsi);
|
||||
break;
|
||||
|
||||
case LWS_CALLBACK_HTTP_WRITEABLE:
|
||||
if (!pss->completed)
|
||||
break;
|
||||
|
||||
p = (unsigned char *)pss->result + LWS_PRE;
|
||||
start = p;
|
||||
end = p + sizeof(pss->result) - LWS_PRE - 1;
|
||||
|
||||
if (!pss->sent_headers) {
|
||||
n = format_result(pss);
|
||||
|
||||
if (lws_add_http_header_status(wsi, HTTP_STATUS_OK,
|
||||
&p, end))
|
||||
goto bail;
|
||||
|
||||
if (lws_add_http_header_by_token(wsi,
|
||||
WSI_TOKEN_HTTP_CONTENT_TYPE,
|
||||
(unsigned char *)"text/html", 9,
|
||||
&p, end))
|
||||
goto bail;
|
||||
if (lws_add_http_header_content_length(wsi, n, &p, end))
|
||||
goto bail;
|
||||
if (lws_finalize_http_header(wsi, &p, end))
|
||||
goto bail;
|
||||
|
||||
/* first send the headers ... */
|
||||
n = lws_write(wsi, start, lws_ptr_diff(p, start),
|
||||
LWS_WRITE_HTTP_HEADERS);
|
||||
if (n < 0)
|
||||
goto bail;
|
||||
|
||||
pss->sent_headers = 1;
|
||||
lws_callback_on_writable(wsi);
|
||||
break;
|
||||
}
|
||||
|
||||
if (!pss->sent_body) {
|
||||
n = format_result(pss);
|
||||
|
||||
n = lws_write(wsi, (unsigned char *)start, n,
|
||||
LWS_WRITE_HTTP_FINAL);
|
||||
|
||||
pss->sent_body = 1;
|
||||
if (n < 0)
|
||||
return 1;
|
||||
goto try_to_reuse;
|
||||
}
|
||||
break;
|
||||
|
||||
case LWS_CALLBACK_HTTP_DROP_PROTOCOL:
|
||||
/* called when our wsi user_space is going to be destroyed */
|
||||
if (pss->spa) {
|
||||
lws_spa_destroy(pss->spa);
|
||||
pss->spa = NULL;
|
||||
}
|
||||
break;
|
||||
|
||||
default:
|
||||
break;
|
||||
}
|
||||
|
||||
return 0;
|
||||
|
||||
bail:
|
||||
|
||||
return 1;
|
||||
|
||||
try_to_reuse:
|
||||
if (lws_http_transaction_completed(wsi))
|
||||
return -1;
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
#define LWS_PLUGIN_PROTOCOL_POST_DEMO \
|
||||
{ \
|
||||
"protocol-post-demo", \
|
||||
callback_post_demo, \
|
||||
sizeof(struct per_session_data__post_demo), \
|
||||
1024, \
|
||||
0, NULL, 0 \
|
||||
}
|
||||
|
||||
#if !defined (LWS_PLUGIN_STATIC)
|
||||
|
||||
static const struct lws_protocols protocols[] = {
|
||||
LWS_PLUGIN_PROTOCOL_POST_DEMO
|
||||
};
|
||||
|
||||
LWS_EXTERN LWS_VISIBLE int
|
||||
init_protocol_post_demo(struct lws_context *context,
|
||||
struct lws_plugin_capability *c)
|
||||
{
|
||||
if (c->api_magic != LWS_PLUGIN_API_MAGIC) {
|
||||
lwsl_err("Plugin API %d, library API %d", LWS_PLUGIN_API_MAGIC,
|
||||
c->api_magic);
|
||||
return 1;
|
||||
}
|
||||
|
||||
c->protocols = protocols;
|
||||
c->count_protocols = LWS_ARRAY_SIZE(protocols);
|
||||
c->extensions = NULL;
|
||||
c->count_extensions = 0;
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
LWS_EXTERN LWS_VISIBLE int
|
||||
destroy_protocol_post_demo(struct lws_context *context)
|
||||
{
|
||||
return 0;
|
||||
}
|
||||
|
||||
#endif
|
||||
@@ -1,161 +0,0 @@
|
||||
/*!
|
||||
* web-server.cpp
|
||||
*
|
||||
* Copyright (c) 2015-2019, NADAL Jean-Baptiste. All rights reserved.
|
||||
*
|
||||
* This library is free software; you can redistribute it and/or
|
||||
* modify it under the terms of the GNU Lesser General Public
|
||||
* License as published by the Free Software Foundation; either
|
||||
* version 2.1 of the License, or (at your option) any later version.
|
||||
*
|
||||
* This library is distributed in the hope that it will be useful,
|
||||
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
||||
* Lesser General Public License for more details.
|
||||
*
|
||||
* You should have received a copy of the GNU Lesser General Public
|
||||
* License along with this library; if not, write to the Free Software
|
||||
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
|
||||
* MA 02110-1301 USA
|
||||
*
|
||||
* @Author: NADAL Jean-Baptiste
|
||||
* @Date: 13/11/2019
|
||||
*
|
||||
*/
|
||||
|
||||
// This is an independent project of an individual developer. Dear PVS-Studio, please check it.
|
||||
// PVS-Studio Static Code Analyzer for C, C++, C#, and Java: http://www.viva64.com
|
||||
|
||||
/*------------------------------- INCLUDES ----------------------------------*/
|
||||
|
||||
#include <string.h>
|
||||
|
||||
#include "web-server.h"
|
||||
|
||||
//404前回调(未找到页面/文件时回调,此功能便于程序返回自定义功能);返回0表示没有适合的处理请求,需要发送404错误
|
||||
char on_request(void* data, uv_stream_t* client, tw_peerAddr* pa, tw_reqHeads* heads)
|
||||
{
|
||||
// struct sockaddr_in serveraddr, peeraddr;
|
||||
// char serv_ip[17],peer_ip[17], tmp[1024];
|
||||
// int addrlen = sizeof(struct sockaddr);
|
||||
// int r;
|
||||
//
|
||||
// //获取clientAddr: http://www.codes51.com/article/detail_113112.html
|
||||
// //本地接入地址
|
||||
// r = uv_tcp_getsockname((uv_tcp_t*)client, (struct sockaddr*)&serveraddr, &addrlen);
|
||||
// //网络字节序转换成主机字符序
|
||||
// uv_ip4_name(&serveraddr, (char*)serv_ip, sizeof(serv_ip));
|
||||
// //客户端的地址
|
||||
// r = uv_tcp_getpeername((uv_tcp_t*)client, (struct sockaddr*)&peeraddr, &addrlen);
|
||||
// //网络字节序转换成主机字符序
|
||||
// uv_ip4_name(&peeraddr, (char*)peer_ip, sizeof(peer_ip));
|
||||
//
|
||||
// sprintf(tmp, "<h1>Page not found:</h1><url>%s<br>%s<br></url><br><br><br><i>server:%s:%d\t\tpeer:%s:%d</i>\n", heads->path, (heads->query?heads->query:""), serv_ip, ntohs(serveraddr.sin_port), peer_ip, ntohs(peeraddr.sin_port));
|
||||
//#ifdef _MSC_VER //Windows下需要转换编码
|
||||
// size_t ll = strlen(tmp);
|
||||
// char *ch = GB2U8(tmp, &ll);
|
||||
// tw_send_200_OK(client, "text/html", ch, -1, 0);
|
||||
// free(ch);
|
||||
//#else //linux 下,系统是和源代码文件编码都是是utf8的,就不需要转换
|
||||
// tw_send_200_OK(client, "text/html", tmp, -1, 0);
|
||||
//#endif // _MSC_VER
|
||||
//
|
||||
printf(" sk:%zd Request:\n",pa->sk);
|
||||
printf(" Query: %s\n",heads->query);
|
||||
printf(" Path: %s\n",heads->path);
|
||||
printf(" Host: %s\n",heads->host);
|
||||
printf(" Cookie: %s\n", heads->cookie);
|
||||
printf(" Range: %lld-%lld\n",heads->Range_frm,heads->Range_to);
|
||||
printf(" data(%zd): %s\n", heads->len,heads->data);
|
||||
if (!heads->cookie)
|
||||
{
|
||||
char ck[512];
|
||||
tw_make_setcookie(ck, 255,"TINYSSID","FDSAFdfafdsafds", 3600 * 8, NULL, heads->path);
|
||||
tw_make_setcookie(ck + strlen(ck),255,"TINYSSID2","faFDSAF45dsafds", 0, heads->host, NULL);
|
||||
sprintf(ck + strlen(ck), "WWW-Authenticate: Basic realm=\".\"\r\n");
|
||||
size_t len;
|
||||
char* rp = tw_format_http_respone(client, "401 Unauthorized", ck, "text/plan", "", -1, &len);
|
||||
tw_send_data(client, rp, len, 0, 1);
|
||||
return 1;
|
||||
}
|
||||
return 0;
|
||||
}
|
||||
|
||||
char on_socket_data(void* data, uv_stream_t* client, tw_peerAddr* pa, membuf_t* buf)
|
||||
{
|
||||
if (buf->size < 1)
|
||||
return 1;//防止发生数据为空
|
||||
if (pa->flag & 0x2) { //WebSocket
|
||||
printf((const char*)buf->data, buf->size < 256 ? buf->size : 256);
|
||||
printf("-------------------------------------------ws:%zd dlen=%zd\n%s\n-------------------------------------------\n",pa->sk, buf->size, buf->data);
|
||||
ulong len = buf->size;
|
||||
char* p = WebSocketMakeFrame(buf->data, &len, 1);//文本帧
|
||||
tw_send_data(client, p, len, 0, 1);
|
||||
} else { //Socket
|
||||
printf("-------------------------------------------sk:%zd dlen=%zd\n%s\n-------------------------------------------\n",pa->sk, buf->size, buf->data);
|
||||
tw_send_data(client, buf->data, buf->size, 1, 0);
|
||||
}
|
||||
return 1;
|
||||
}
|
||||
|
||||
char on_close(void* data, uv_stream_t* client, tw_peerAddr* pa)
|
||||
{
|
||||
//printf("closed: sk=%zd [%s:%d] from:%s:%d cli:%d\n", pa->sk, pa->ip, pa->port, pa->fip, pa->fport, client->loop->active_tcp_streams);
|
||||
return 0;
|
||||
}
|
||||
|
||||
char on_connect(void* data, uv_stream_t* client, tw_peerAddr* pa)
|
||||
{
|
||||
//printf("connected: sk=%zd [%s:%d] from:%s:%d cli:%d\n",pa->sk,pa->ip,pa->port,pa->fip,pa->fport, client->loop->active_tcp_streams);
|
||||
return 0;
|
||||
}
|
||||
|
||||
char on_error(void* data, uv_stream_t* client, tw_peerAddr* pa, int errcode, char* errstr)
|
||||
{
|
||||
//printf("error: sk=%zd [%s:%d] from:%s:%d cli:%d %s\n", pa->sk, pa->ip, pa->port, pa->fip, pa->fport, client->loop->active_tcp_streams,errstr);
|
||||
return 0;
|
||||
}
|
||||
|
||||
/*! ----------------------------------------------------------------------------
|
||||
* @fn WebServer
|
||||
*
|
||||
* @brief Constructor of the Web Server Object.
|
||||
*/
|
||||
WebServer::WebServer(void)
|
||||
{
|
||||
}
|
||||
|
||||
/*! ----------------------------------------------------------------------------
|
||||
* @fn ~WebServer
|
||||
*
|
||||
* @brief Destructor of the Web Server Object.
|
||||
*/
|
||||
|
||||
WebServer::~WebServer(void)
|
||||
{
|
||||
}
|
||||
|
||||
/*! ----------------------------------------------------------------------------
|
||||
* @fn setup
|
||||
*
|
||||
* @brief Setup the Web server
|
||||
*/
|
||||
int WebServer::setup(char *a_document_root, int a_port, uv_loop_t *an_evt_loop)
|
||||
{
|
||||
memset(&m_conf, 0, sizeof(m_conf));
|
||||
m_conf.dirlist = 1;//目录列表
|
||||
//conf.ip = NULL;// "127.0.0.1";
|
||||
m_conf.port = a_port;
|
||||
m_conf.doc_dir = a_document_root;
|
||||
m_conf.doc_index = NULL;// Default homepage
|
||||
//m_conf.doc_dir = NULL;// The directory where the default program files are located
|
||||
|
||||
m_conf.on_request = on_request;
|
||||
m_conf.on_data = on_socket_data;
|
||||
m_conf.on_close = on_close;
|
||||
m_conf.on_connect = on_connect;
|
||||
m_conf.on_error = on_error;
|
||||
|
||||
tinyweb_start(an_evt_loop, &m_conf);
|
||||
return 0;
|
||||
}
|
||||
@@ -1,49 +0,0 @@
|
||||
/*!
|
||||
* web-server.h
|
||||
*
|
||||
* Copyright (c) 2015-2019, NADAL Jean-Baptiste. All rights reserved.
|
||||
*
|
||||
* This library is free software; you can redistribute it and/or
|
||||
* modify it under the terms of the GNU Lesser General Public
|
||||
* License as published by the Free Software Foundation; either
|
||||
* version 2.1 of the License, or (at your option) any later version.
|
||||
*
|
||||
* This library is distributed in the hope that it will be useful,
|
||||
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
||||
* Lesser General Public License for more details.
|
||||
*
|
||||
* You should have received a copy of the GNU Lesser General Public
|
||||
* License along with this library; if not, write to the Free Software
|
||||
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
|
||||
* MA 02110-1301 USA
|
||||
*
|
||||
* @Author: NADAL Jean-Baptiste
|
||||
* @Date: 13/11/2019
|
||||
*
|
||||
*/
|
||||
|
||||
#ifndef _WEB_SERVER_H
|
||||
#define _WEB_SERVER_H
|
||||
|
||||
/*------------------------------- INCLUDES ----------------------------------*/
|
||||
#include <uv.h>
|
||||
#include <tinyweb/tinyweb.h>
|
||||
|
||||
/*---------------------------------- Deps -----------------------------------*/
|
||||
|
||||
/*--------------------------------- CLASS ----------------------------------*/
|
||||
|
||||
class WebServer
|
||||
{
|
||||
public:
|
||||
WebServer(void);
|
||||
~WebServer(void);
|
||||
|
||||
int setup(char *a_document_root, int a_port, uv_loop_t *an_evt_loop);
|
||||
|
||||
private:
|
||||
tw_config m_conf;
|
||||
};
|
||||
|
||||
#endif /* __WEB_SERVER_H */
|
||||
Reference in New Issue
Block a user