2010-11-08 17:12:19 +00:00
|
|
|
/*
|
2016-02-08 08:44:21 +08:00
|
|
|
* libwebsockets-test-servet - libwebsockets test implementation
|
2010-11-13 10:03:47 +00:00
|
|
|
*
|
2016-02-08 08:44:21 +08:00
|
|
|
* Copyright (C) 2010-2016 Andy Green <andy@warmcat.com>
|
2010-11-08 17:12:19 +00:00
|
|
|
*
|
2016-02-08 08:44:21 +08:00
|
|
|
* This file is made available under the Creative Commons CC0 1.0
|
|
|
|
* Universal Public Domain Dedication.
|
2010-11-08 17:12:19 +00:00
|
|
|
*
|
2016-02-08 08:44:21 +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.
|
2010-11-08 17:12:19 +00:00
|
|
|
*
|
2016-02-08 08:44:21 +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.
|
2010-11-08 17:12:19 +00:00
|
|
|
*/
|
|
|
|
|
2015-11-19 13:55:47 +08:00
|
|
|
#include "test-server.h"
|
2013-02-13 09:29:26 +08:00
|
|
|
|
2015-11-19 13:55:47 +08:00
|
|
|
int close_testing;
|
2013-01-20 20:51:14 +08:00
|
|
|
int max_poll_elements;
|
2016-01-26 20:56:56 +08:00
|
|
|
int debug_level = 7;
|
2013-01-15 19:44:33 +08:00
|
|
|
|
2015-03-05 17:06:16 +08:00
|
|
|
#ifdef EXTERNAL_POLL
|
2015-12-04 11:08:32 +08:00
|
|
|
struct lws_pollfd *pollfds;
|
2013-01-20 20:51:14 +08:00
|
|
|
int *fd_lookup;
|
2013-01-17 11:16:15 +08:00
|
|
|
int count_pollfds;
|
2015-03-05 17:06:16 +08:00
|
|
|
#endif
|
2015-11-19 13:55:47 +08:00
|
|
|
volatile int force_exit = 0;
|
2015-12-04 11:08:32 +08:00
|
|
|
struct lws_context *context;
|
lws_plat_fd implement platform default handlers
This is a rewrite of the patch from Soapyman here
https://github.com/warmcat/libwebsockets/pull/363
The main changes compared to Soapyman's original patch are
- There's no new stuff in the info struct user code does any overrides
it may want to do explicitly after lws_context_create returns
- User overrides for file ops can call through (subclass) to the original
platform implementation using lws_get_fops_plat()
- A typedef is provided for plat-specific fd type
- Public helpers are provided to allow user code to be platform-independent
about file access, using the lws platform file operations underneath:
static inline lws_filefd_type
lws_plat_file_open(struct lws_plat_file_ops *fops, const char *filename,
unsigned long *filelen, int flags)
static inline int
lws_plat_file_close(struct lws_plat_file_ops *fops, lws_filefd_type fd)
static inline unsigned long
lws_plat_file_seek_cur(struct lws_plat_file_ops *fops, lws_filefd_type fd,
long offset_from_cur_pos)
static inline int
lws_plat_file_read(struct lws_plat_file_ops *fops, lws_filefd_type fd,
unsigned long *amount, unsigned char *buf, unsigned long len)
static inline int
lws_plat_file_write(struct lws_plat_file_ops *fops, lws_filefd_type fd,
unsigned long *amount, unsigned char *buf, unsigned long len)
There's example documentation and implementation in the test server.
Signed-off-by: Andy Green <andy.green@linaro.org>
2015-12-10 07:58:58 +08:00
|
|
|
struct lws_plat_file_ops fops_plat;
|
2015-11-19 13:55:47 +08:00
|
|
|
|
|
|
|
/* http server gets files from this path */
|
|
|
|
#define LOCAL_RESOURCE_PATH INSTALL_DATADIR"/libwebsockets-test-server"
|
|
|
|
char *resource_path = LOCAL_RESOURCE_PATH;
|
2013-01-19 11:11:42 +08:00
|
|
|
|
2015-11-20 09:33:02 +08:00
|
|
|
/* singlethreaded version --> no locks */
|
|
|
|
|
|
|
|
void test_server_lock(int care)
|
|
|
|
{
|
|
|
|
}
|
|
|
|
void test_server_unlock(int care)
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
2010-11-12 13:10:40 +00:00
|
|
|
/*
|
|
|
|
* This demo server shows how to use libwebsockets for one or more
|
|
|
|
* websocket protocols in the same server
|
|
|
|
*
|
|
|
|
* It defines the following websocket protocols:
|
|
|
|
*
|
|
|
|
* dumb-increment-protocol: once the socket is opened, an incrementing
|
|
|
|
* ascii string is sent down it every 50ms.
|
2011-01-27 06:26:52 +00:00
|
|
|
* If you send "reset\n" on the websocket, then
|
|
|
|
* the incrementing number is reset to 0.
|
2010-11-12 14:12:13 +00:00
|
|
|
*
|
|
|
|
* lws-mirror-protocol: copies any received packet to every connection also
|
2011-01-27 06:26:52 +00:00
|
|
|
* using this protocol, including the sender
|
2010-11-12 13:10:40 +00:00
|
|
|
*/
|
|
|
|
|
2011-01-18 15:29:04 +00:00
|
|
|
enum demo_protocols {
|
|
|
|
/* always first */
|
|
|
|
PROTOCOL_HTTP = 0,
|
|
|
|
|
|
|
|
PROTOCOL_DUMB_INCREMENT,
|
|
|
|
PROTOCOL_LWS_MIRROR,
|
2016-01-11 11:34:01 +08:00
|
|
|
PROTOCOL_LWS_ECHOGEN,
|
2016-02-21 13:44:07 +08:00
|
|
|
PROTOCOL_LWS_STATUS,
|
2011-01-18 15:29:04 +00:00
|
|
|
|
|
|
|
/* always last */
|
|
|
|
DEMO_PROTOCOL_COUNT
|
|
|
|
};
|
|
|
|
|
2010-11-12 10:44:16 +00:00
|
|
|
/* list of supported protocols and callbacks */
|
|
|
|
|
2015-12-04 11:08:32 +08:00
|
|
|
static struct lws_protocols protocols[] = {
|
2010-12-19 20:50:01 +00:00
|
|
|
/* first protocol must always be HTTP handler */
|
2011-03-02 22:03:47 +00:00
|
|
|
|
|
|
|
{
|
|
|
|
"http-only", /* name */
|
|
|
|
callback_http, /* callback */
|
2013-02-14 23:06:37 +08:00
|
|
|
sizeof (struct per_session_data__http), /* per_session_data_size */
|
2013-02-06 21:10:16 +09:00
|
|
|
0, /* max frame size / rx buffer */
|
2010-11-12 10:44:16 +00:00
|
|
|
},
|
2011-03-02 22:03:47 +00:00
|
|
|
{
|
|
|
|
"dumb-increment-protocol",
|
|
|
|
callback_dumb_increment,
|
|
|
|
sizeof(struct per_session_data__dumb_increment),
|
2016-04-01 09:30:09 +08:00
|
|
|
10, /* rx buf size must be >= permessage-deflate rx size */
|
2010-11-12 10:44:16 +00:00
|
|
|
},
|
2011-03-02 22:03:47 +00:00
|
|
|
{
|
|
|
|
"lws-mirror-protocol",
|
|
|
|
callback_lws_mirror,
|
2013-02-06 21:10:16 +09:00
|
|
|
sizeof(struct per_session_data__lws_mirror),
|
2016-04-01 09:30:09 +08:00
|
|
|
128, /* rx buf size must be >= permessage-deflate rx size */
|
2010-11-12 13:10:40 +00:00
|
|
|
},
|
2016-01-11 11:34:01 +08:00
|
|
|
{
|
|
|
|
"lws-echogen",
|
|
|
|
callback_lws_echogen,
|
|
|
|
sizeof(struct per_session_data__echogen),
|
2016-04-01 09:30:09 +08:00
|
|
|
128, /* rx buf size must be >= permessage-deflate rx size */
|
2016-01-11 11:34:01 +08:00
|
|
|
},
|
2016-02-21 13:44:07 +08:00
|
|
|
{
|
|
|
|
"lws-status",
|
|
|
|
callback_lws_status,
|
|
|
|
sizeof(struct per_session_data__lws_status),
|
2016-04-01 09:30:09 +08:00
|
|
|
128, /* rx buf size must be >= permessage-deflate rx size */
|
2016-02-21 13:44:07 +08:00
|
|
|
},
|
2013-02-06 21:10:16 +09:00
|
|
|
{ NULL, NULL, 0, 0 } /* terminator */
|
2010-11-12 10:44:16 +00:00
|
|
|
};
|
|
|
|
|
lws_plat_fd implement platform default handlers
This is a rewrite of the patch from Soapyman here
https://github.com/warmcat/libwebsockets/pull/363
The main changes compared to Soapyman's original patch are
- There's no new stuff in the info struct user code does any overrides
it may want to do explicitly after lws_context_create returns
- User overrides for file ops can call through (subclass) to the original
platform implementation using lws_get_fops_plat()
- A typedef is provided for plat-specific fd type
- Public helpers are provided to allow user code to be platform-independent
about file access, using the lws platform file operations underneath:
static inline lws_filefd_type
lws_plat_file_open(struct lws_plat_file_ops *fops, const char *filename,
unsigned long *filelen, int flags)
static inline int
lws_plat_file_close(struct lws_plat_file_ops *fops, lws_filefd_type fd)
static inline unsigned long
lws_plat_file_seek_cur(struct lws_plat_file_ops *fops, lws_filefd_type fd,
long offset_from_cur_pos)
static inline int
lws_plat_file_read(struct lws_plat_file_ops *fops, lws_filefd_type fd,
unsigned long *amount, unsigned char *buf, unsigned long len)
static inline int
lws_plat_file_write(struct lws_plat_file_ops *fops, lws_filefd_type fd,
unsigned long *amount, unsigned char *buf, unsigned long len)
There's example documentation and implementation in the test server.
Signed-off-by: Andy Green <andy.green@linaro.org>
2015-12-10 07:58:58 +08:00
|
|
|
|
|
|
|
/* this shows how to override the lws file operations. You don't need
|
|
|
|
* to do any of this unless you have a reason (eg, want to serve
|
|
|
|
* compressed files without decompressing the whole archive)
|
|
|
|
*/
|
|
|
|
static lws_filefd_type
|
2015-12-11 13:12:58 +08:00
|
|
|
test_server_fops_open(struct lws *wsi, const char *filename,
|
|
|
|
unsigned long *filelen, int flags)
|
lws_plat_fd implement platform default handlers
This is a rewrite of the patch from Soapyman here
https://github.com/warmcat/libwebsockets/pull/363
The main changes compared to Soapyman's original patch are
- There's no new stuff in the info struct user code does any overrides
it may want to do explicitly after lws_context_create returns
- User overrides for file ops can call through (subclass) to the original
platform implementation using lws_get_fops_plat()
- A typedef is provided for plat-specific fd type
- Public helpers are provided to allow user code to be platform-independent
about file access, using the lws platform file operations underneath:
static inline lws_filefd_type
lws_plat_file_open(struct lws_plat_file_ops *fops, const char *filename,
unsigned long *filelen, int flags)
static inline int
lws_plat_file_close(struct lws_plat_file_ops *fops, lws_filefd_type fd)
static inline unsigned long
lws_plat_file_seek_cur(struct lws_plat_file_ops *fops, lws_filefd_type fd,
long offset_from_cur_pos)
static inline int
lws_plat_file_read(struct lws_plat_file_ops *fops, lws_filefd_type fd,
unsigned long *amount, unsigned char *buf, unsigned long len)
static inline int
lws_plat_file_write(struct lws_plat_file_ops *fops, lws_filefd_type fd,
unsigned long *amount, unsigned char *buf, unsigned long len)
There's example documentation and implementation in the test server.
Signed-off-by: Andy Green <andy.green@linaro.org>
2015-12-10 07:58:58 +08:00
|
|
|
{
|
2015-12-14 11:34:00 +08:00
|
|
|
lws_filefd_type n;
|
lws_plat_fd implement platform default handlers
This is a rewrite of the patch from Soapyman here
https://github.com/warmcat/libwebsockets/pull/363
The main changes compared to Soapyman's original patch are
- There's no new stuff in the info struct user code does any overrides
it may want to do explicitly after lws_context_create returns
- User overrides for file ops can call through (subclass) to the original
platform implementation using lws_get_fops_plat()
- A typedef is provided for plat-specific fd type
- Public helpers are provided to allow user code to be platform-independent
about file access, using the lws platform file operations underneath:
static inline lws_filefd_type
lws_plat_file_open(struct lws_plat_file_ops *fops, const char *filename,
unsigned long *filelen, int flags)
static inline int
lws_plat_file_close(struct lws_plat_file_ops *fops, lws_filefd_type fd)
static inline unsigned long
lws_plat_file_seek_cur(struct lws_plat_file_ops *fops, lws_filefd_type fd,
long offset_from_cur_pos)
static inline int
lws_plat_file_read(struct lws_plat_file_ops *fops, lws_filefd_type fd,
unsigned long *amount, unsigned char *buf, unsigned long len)
static inline int
lws_plat_file_write(struct lws_plat_file_ops *fops, lws_filefd_type fd,
unsigned long *amount, unsigned char *buf, unsigned long len)
There's example documentation and implementation in the test server.
Signed-off-by: Andy Green <andy.green@linaro.org>
2015-12-10 07:58:58 +08:00
|
|
|
|
|
|
|
/* call through to original platform implementation */
|
2015-12-11 13:12:58 +08:00
|
|
|
n = fops_plat.open(wsi, filename, filelen, flags);
|
lws_plat_fd implement platform default handlers
This is a rewrite of the patch from Soapyman here
https://github.com/warmcat/libwebsockets/pull/363
The main changes compared to Soapyman's original patch are
- There's no new stuff in the info struct user code does any overrides
it may want to do explicitly after lws_context_create returns
- User overrides for file ops can call through (subclass) to the original
platform implementation using lws_get_fops_plat()
- A typedef is provided for plat-specific fd type
- Public helpers are provided to allow user code to be platform-independent
about file access, using the lws platform file operations underneath:
static inline lws_filefd_type
lws_plat_file_open(struct lws_plat_file_ops *fops, const char *filename,
unsigned long *filelen, int flags)
static inline int
lws_plat_file_close(struct lws_plat_file_ops *fops, lws_filefd_type fd)
static inline unsigned long
lws_plat_file_seek_cur(struct lws_plat_file_ops *fops, lws_filefd_type fd,
long offset_from_cur_pos)
static inline int
lws_plat_file_read(struct lws_plat_file_ops *fops, lws_filefd_type fd,
unsigned long *amount, unsigned char *buf, unsigned long len)
static inline int
lws_plat_file_write(struct lws_plat_file_ops *fops, lws_filefd_type fd,
unsigned long *amount, unsigned char *buf, unsigned long len)
There's example documentation and implementation in the test server.
Signed-off-by: Andy Green <andy.green@linaro.org>
2015-12-10 07:58:58 +08:00
|
|
|
|
2015-12-14 11:34:00 +08:00
|
|
|
lwsl_notice("%s: opening %s, ret %ld, len %lu\n", __func__, filename,
|
|
|
|
(long)n, *filelen);
|
lws_plat_fd implement platform default handlers
This is a rewrite of the patch from Soapyman here
https://github.com/warmcat/libwebsockets/pull/363
The main changes compared to Soapyman's original patch are
- There's no new stuff in the info struct user code does any overrides
it may want to do explicitly after lws_context_create returns
- User overrides for file ops can call through (subclass) to the original
platform implementation using lws_get_fops_plat()
- A typedef is provided for plat-specific fd type
- Public helpers are provided to allow user code to be platform-independent
about file access, using the lws platform file operations underneath:
static inline lws_filefd_type
lws_plat_file_open(struct lws_plat_file_ops *fops, const char *filename,
unsigned long *filelen, int flags)
static inline int
lws_plat_file_close(struct lws_plat_file_ops *fops, lws_filefd_type fd)
static inline unsigned long
lws_plat_file_seek_cur(struct lws_plat_file_ops *fops, lws_filefd_type fd,
long offset_from_cur_pos)
static inline int
lws_plat_file_read(struct lws_plat_file_ops *fops, lws_filefd_type fd,
unsigned long *amount, unsigned char *buf, unsigned long len)
static inline int
lws_plat_file_write(struct lws_plat_file_ops *fops, lws_filefd_type fd,
unsigned long *amount, unsigned char *buf, unsigned long len)
There's example documentation and implementation in the test server.
Signed-off-by: Andy Green <andy.green@linaro.org>
2015-12-10 07:58:58 +08:00
|
|
|
|
|
|
|
return n;
|
|
|
|
}
|
|
|
|
|
2013-01-30 08:02:26 +08:00
|
|
|
void sighandler(int sig)
|
|
|
|
{
|
|
|
|
force_exit = 1;
|
2015-12-04 08:43:54 +08:00
|
|
|
lws_cancel_service(context);
|
2013-01-30 08:02:26 +08:00
|
|
|
}
|
|
|
|
|
2016-01-11 11:34:01 +08:00
|
|
|
static const struct lws_extension exts[] = {
|
|
|
|
{
|
|
|
|
"permessage-deflate",
|
|
|
|
lws_extension_callback_pm_deflate,
|
|
|
|
"permessage-deflate"
|
|
|
|
},
|
|
|
|
{
|
|
|
|
"deflate-frame",
|
|
|
|
lws_extension_callback_pm_deflate,
|
|
|
|
"deflate_frame"
|
|
|
|
},
|
|
|
|
{ NULL, NULL, NULL /* terminator */ }
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
|
2010-10-31 07:40:33 +00:00
|
|
|
static struct option options[] = {
|
2011-01-27 06:26:52 +00:00
|
|
|
{ "help", no_argument, NULL, 'h' },
|
2013-01-10 22:28:59 +08:00
|
|
|
{ "debug", required_argument, NULL, 'd' },
|
2011-01-27 06:26:52 +00:00
|
|
|
{ "port", required_argument, NULL, 'p' },
|
|
|
|
{ "ssl", no_argument, NULL, 's' },
|
2015-11-19 13:55:47 +08:00
|
|
|
{ "allow-non-ssl", no_argument, NULL, 'a' },
|
2012-04-09 15:09:01 +08:00
|
|
|
{ "interface", required_argument, NULL, 'i' },
|
2011-03-07 07:08:07 +00:00
|
|
|
{ "closetest", no_argument, NULL, 'c' },
|
2016-02-17 12:00:40 +08:00
|
|
|
{ "ssl-cert", required_argument, NULL, 'C' },
|
|
|
|
{ "ssl-key", required_argument, NULL, 'K' },
|
|
|
|
{ "ssl-ca", required_argument, NULL, 'A' },
|
2014-04-11 13:14:37 +08:00
|
|
|
{ "libev", no_argument, NULL, 'e' },
|
2015-11-19 13:55:47 +08:00
|
|
|
#ifndef LWS_NO_DAEMONIZE
|
2013-01-19 11:11:42 +08:00
|
|
|
{ "daemonize", no_argument, NULL, 'D' },
|
|
|
|
#endif
|
2015-11-19 13:55:47 +08:00
|
|
|
{ "resource_path", required_argument, NULL, 'r' },
|
2010-10-31 07:40:33 +00:00
|
|
|
{ NULL, 0, 0, 0 }
|
|
|
|
};
|
2010-10-29 14:15:22 +01:00
|
|
|
|
2010-10-31 07:40:33 +00:00
|
|
|
int main(int argc, char **argv)
|
2010-10-29 14:15:22 +01:00
|
|
|
{
|
2015-11-19 13:55:47 +08:00
|
|
|
struct lws_context_creation_info info;
|
|
|
|
char interface_name[128] = "";
|
|
|
|
unsigned int ms, oldms = 0;
|
|
|
|
const char *iface = NULL;
|
2016-02-17 12:00:40 +08:00
|
|
|
char cert_path[1024] = "";
|
|
|
|
char key_path[1024] = "";
|
|
|
|
char ca_path[1024] = "";
|
2016-02-20 08:03:51 +08:00
|
|
|
int uid = -1, gid = -1;
|
2010-12-19 20:50:01 +00:00
|
|
|
int use_ssl = 0;
|
2011-01-30 20:57:25 +00:00
|
|
|
int opts = 0;
|
2015-11-19 13:55:47 +08:00
|
|
|
int n = 0;
|
2015-10-23 08:10:55 +02:00
|
|
|
#ifndef _WIN32
|
2013-01-19 11:11:42 +08:00
|
|
|
int syslog_options = LOG_PID | LOG_PERROR;
|
2013-02-06 15:26:58 +09:00
|
|
|
#endif
|
2013-01-21 12:58:04 +08:00
|
|
|
#ifndef LWS_NO_DAEMONIZE
|
2015-11-29 19:32:02 +08:00
|
|
|
int daemonize = 0;
|
2013-01-19 11:11:42 +08:00
|
|
|
#endif
|
2010-11-13 10:03:47 +00:00
|
|
|
|
2015-12-14 08:52:03 +08:00
|
|
|
/*
|
2015-11-19 13:55:47 +08:00
|
|
|
* take care to zero down the info struct, he contains random garbaage
|
|
|
|
* from the stack otherwise
|
|
|
|
*/
|
2013-02-09 14:01:09 +08:00
|
|
|
memset(&info, 0, sizeof info);
|
|
|
|
info.port = 7681;
|
|
|
|
|
2010-10-31 07:40:33 +00:00
|
|
|
while (n >= 0) {
|
2016-02-20 08:03:51 +08:00
|
|
|
n = getopt_long(argc, argv, "eci:hsap:d:Dr:C:K:A:u:g:", options, NULL);
|
2010-10-31 07:40:33 +00:00
|
|
|
if (n < 0)
|
|
|
|
continue;
|
|
|
|
switch (n) {
|
2014-04-11 13:14:37 +08:00
|
|
|
case 'e':
|
|
|
|
opts |= LWS_SERVER_OPTION_LIBEV;
|
|
|
|
break;
|
2013-01-21 12:58:04 +08:00
|
|
|
#ifndef LWS_NO_DAEMONIZE
|
2013-01-19 11:11:42 +08:00
|
|
|
case 'D':
|
|
|
|
daemonize = 1;
|
2015-10-23 08:10:55 +02:00
|
|
|
#ifndef _WIN32
|
2013-01-19 11:11:42 +08:00
|
|
|
syslog_options &= ~LOG_PERROR;
|
2013-02-06 15:26:58 +09:00
|
|
|
#endif
|
2013-01-19 11:11:42 +08:00
|
|
|
break;
|
|
|
|
#endif
|
2016-02-20 08:03:51 +08:00
|
|
|
case 'u':
|
|
|
|
uid = atoi(optarg);
|
|
|
|
break;
|
|
|
|
case 'g':
|
|
|
|
gid = atoi(optarg);
|
|
|
|
break;
|
2013-01-10 22:28:59 +08:00
|
|
|
case 'd':
|
2013-01-19 11:32:18 +08:00
|
|
|
debug_level = atoi(optarg);
|
2013-01-10 22:28:59 +08:00
|
|
|
break;
|
2010-11-08 17:03:03 +00:00
|
|
|
case 's':
|
|
|
|
use_ssl = 1;
|
|
|
|
break;
|
2013-12-14 11:41:29 +08:00
|
|
|
case 'a':
|
|
|
|
opts |= LWS_SERVER_OPTION_ALLOW_NON_SSL_ON_SSL_PORT;
|
|
|
|
break;
|
2010-10-31 07:40:33 +00:00
|
|
|
case 'p':
|
2013-02-09 14:01:09 +08:00
|
|
|
info.port = atoi(optarg);
|
2010-10-31 07:40:33 +00:00
|
|
|
break;
|
2011-02-19 08:32:53 +00:00
|
|
|
case 'i':
|
|
|
|
strncpy(interface_name, optarg, sizeof interface_name);
|
|
|
|
interface_name[(sizeof interface_name) - 1] = '\0';
|
2013-02-11 17:52:23 +01:00
|
|
|
iface = interface_name;
|
2011-02-19 08:32:53 +00:00
|
|
|
break;
|
2011-03-07 07:08:07 +00:00
|
|
|
case 'c':
|
|
|
|
close_testing = 1;
|
|
|
|
fprintf(stderr, " Close testing mode -- closes on "
|
|
|
|
"client after 50 dumb increments"
|
|
|
|
"and suppresses lws_mirror spam\n");
|
|
|
|
break;
|
2013-03-18 17:08:25 +00:00
|
|
|
case 'r':
|
|
|
|
resource_path = optarg;
|
|
|
|
printf("Setting resource path to \"%s\"\n", resource_path);
|
|
|
|
break;
|
2016-02-17 12:00:40 +08:00
|
|
|
case 'C':
|
|
|
|
strncpy(cert_path, optarg, sizeof cert_path);
|
|
|
|
break;
|
|
|
|
case 'K':
|
|
|
|
strncpy(key_path, optarg, sizeof key_path);
|
|
|
|
break;
|
|
|
|
case 'A':
|
|
|
|
strncpy(ca_path, optarg, sizeof ca_path);
|
|
|
|
break;
|
2010-10-31 07:40:33 +00:00
|
|
|
case 'h':
|
2010-10-31 12:42:52 +00:00
|
|
|
fprintf(stderr, "Usage: test-server "
|
2013-01-10 22:28:59 +08:00
|
|
|
"[--port=<p>] [--ssl] "
|
2013-03-18 17:08:25 +00:00
|
|
|
"[-d <log bitfield>] "
|
|
|
|
"[--resource_path <path>]\n");
|
2010-10-31 07:40:33 +00:00
|
|
|
exit(1);
|
|
|
|
}
|
|
|
|
}
|
2010-11-08 17:03:03 +00:00
|
|
|
|
2013-02-06 15:26:58 +09:00
|
|
|
#if !defined(LWS_NO_DAEMONIZE) && !defined(WIN32)
|
2015-12-14 08:52:03 +08:00
|
|
|
/*
|
2013-01-19 11:11:42 +08:00
|
|
|
* 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");
|
2016-02-20 08:04:32 +08:00
|
|
|
return 10;
|
2013-01-19 11:11:42 +08:00
|
|
|
}
|
2013-01-19 13:56:10 +08:00
|
|
|
#endif
|
2013-01-30 08:02:26 +08:00
|
|
|
|
|
|
|
signal(SIGINT, sighandler);
|
|
|
|
|
2015-10-23 08:10:55 +02:00
|
|
|
#ifndef _WIN32
|
2013-01-19 11:11:42 +08:00
|
|
|
/* we will only try to log things according to our debug_level */
|
|
|
|
setlogmask(LOG_UPTO (LOG_DEBUG));
|
|
|
|
openlog("lwsts", syslog_options, LOG_DAEMON);
|
2013-02-06 15:26:58 +09:00
|
|
|
#endif
|
2013-01-19 11:32:18 +08:00
|
|
|
|
|
|
|
/* tell the library what debug level to emit and to send it to syslog */
|
|
|
|
lws_set_log_level(debug_level, lwsl_emit_syslog);
|
|
|
|
|
2016-01-29 09:35:58 +08:00
|
|
|
lwsl_notice("libwebsockets test server - license LGPL2.1+SLE\n");
|
|
|
|
lwsl_notice("(C) Copyright 2010-2016 Andy Green <andy@warmcat.com>\n");
|
2014-10-14 15:24:31 +02:00
|
|
|
|
|
|
|
printf("Using resource path \"%s\"\n", resource_path);
|
2013-01-20 20:51:14 +08:00
|
|
|
#ifdef EXTERNAL_POLL
|
|
|
|
max_poll_elements = getdtablesize();
|
2015-12-04 11:08:32 +08:00
|
|
|
pollfds = malloc(max_poll_elements * sizeof (struct lws_pollfd));
|
2013-01-20 20:51:14 +08:00
|
|
|
fd_lookup = malloc(max_poll_elements * sizeof (int));
|
|
|
|
if (pollfds == NULL || fd_lookup == NULL) {
|
|
|
|
lwsl_err("Out of memory pollfds=%d\n", max_poll_elements);
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
#endif
|
2010-11-13 10:03:47 +00:00
|
|
|
|
2013-02-11 17:52:23 +01:00
|
|
|
info.iface = iface;
|
2013-02-09 14:01:09 +08:00
|
|
|
info.protocols = protocols;
|
2015-11-19 13:55:47 +08:00
|
|
|
info.ssl_cert_filepath = NULL;
|
|
|
|
info.ssl_private_key_filepath = NULL;
|
|
|
|
|
|
|
|
if (use_ssl) {
|
2013-05-02 08:15:53 +08:00
|
|
|
if (strlen(resource_path) > sizeof(cert_path) - 32) {
|
|
|
|
lwsl_err("resource path too long\n");
|
|
|
|
return -1;
|
|
|
|
}
|
2016-02-17 12:00:40 +08:00
|
|
|
if (!cert_path[0])
|
|
|
|
sprintf(cert_path, "%s/libwebsockets-test-server.pem",
|
2013-05-02 08:15:53 +08:00
|
|
|
resource_path);
|
|
|
|
if (strlen(resource_path) > sizeof(key_path) - 32) {
|
|
|
|
lwsl_err("resource path too long\n");
|
|
|
|
return -1;
|
|
|
|
}
|
2016-02-17 12:00:40 +08:00
|
|
|
if (!key_path[0])
|
|
|
|
sprintf(key_path, "%s/libwebsockets-test-server.key.pem",
|
2013-05-02 08:15:53 +08:00
|
|
|
resource_path);
|
2013-03-18 17:08:25 +00:00
|
|
|
|
|
|
|
info.ssl_cert_filepath = cert_path;
|
|
|
|
info.ssl_private_key_filepath = key_path;
|
2016-02-17 12:00:40 +08:00
|
|
|
if (ca_path[0])
|
|
|
|
info.ssl_ca_filepath = ca_path;
|
2013-02-09 14:01:09 +08:00
|
|
|
}
|
2016-02-20 08:03:51 +08:00
|
|
|
info.gid = gid;
|
|
|
|
info.uid = uid;
|
|
|
|
info.max_http_header_pool = 16;
|
2016-01-11 11:34:01 +08:00
|
|
|
info.options = opts | LWS_SERVER_OPTION_VALIDATE_UTF8;
|
|
|
|
info.extensions = exts;
|
2016-02-20 08:03:51 +08:00
|
|
|
info.timeout_secs = 5;
|
2016-02-18 19:20:02 +08:00
|
|
|
info.ssl_cipher_list = "ECDHE-ECDSA-AES256-GCM-SHA384:"
|
|
|
|
"ECDHE-RSA-AES256-GCM-SHA384:"
|
|
|
|
"DHE-RSA-AES256-GCM-SHA384:"
|
|
|
|
"ECDHE-RSA-AES256-SHA384:"
|
|
|
|
"HIGH:!aNULL:!eNULL:!EXPORT:"
|
|
|
|
"!DES:!MD5:!PSK:!RC4:!HMAC_SHA1:"
|
|
|
|
"!SHA1:!DHE-RSA-AES128-GCM-SHA256:"
|
|
|
|
"!DHE-RSA-AES128-SHA256:"
|
|
|
|
"!AES128-GCM-SHA256:"
|
|
|
|
"!AES128-SHA256:"
|
|
|
|
"!DHE-RSA-AES256-SHA256:"
|
|
|
|
"!AES256-GCM-SHA384:"
|
|
|
|
"!AES256-SHA256";
|
2015-12-04 08:43:54 +08:00
|
|
|
context = lws_create_context(&info);
|
2011-01-22 12:51:57 +00:00
|
|
|
if (context == NULL) {
|
2013-01-19 11:32:18 +08:00
|
|
|
lwsl_err("libwebsocket init failed\n");
|
2010-10-29 14:15:22 +01:00
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
lws_plat_fd implement platform default handlers
This is a rewrite of the patch from Soapyman here
https://github.com/warmcat/libwebsockets/pull/363
The main changes compared to Soapyman's original patch are
- There's no new stuff in the info struct user code does any overrides
it may want to do explicitly after lws_context_create returns
- User overrides for file ops can call through (subclass) to the original
platform implementation using lws_get_fops_plat()
- A typedef is provided for plat-specific fd type
- Public helpers are provided to allow user code to be platform-independent
about file access, using the lws platform file operations underneath:
static inline lws_filefd_type
lws_plat_file_open(struct lws_plat_file_ops *fops, const char *filename,
unsigned long *filelen, int flags)
static inline int
lws_plat_file_close(struct lws_plat_file_ops *fops, lws_filefd_type fd)
static inline unsigned long
lws_plat_file_seek_cur(struct lws_plat_file_ops *fops, lws_filefd_type fd,
long offset_from_cur_pos)
static inline int
lws_plat_file_read(struct lws_plat_file_ops *fops, lws_filefd_type fd,
unsigned long *amount, unsigned char *buf, unsigned long len)
static inline int
lws_plat_file_write(struct lws_plat_file_ops *fops, lws_filefd_type fd,
unsigned long *amount, unsigned char *buf, unsigned long len)
There's example documentation and implementation in the test server.
Signed-off-by: Andy Green <andy.green@linaro.org>
2015-12-10 07:58:58 +08:00
|
|
|
/* this shows how to override the lws file operations. You don't need
|
|
|
|
* to do any of this unless you have a reason (eg, want to serve
|
|
|
|
* compressed files without decompressing the whole archive)
|
|
|
|
*/
|
|
|
|
/* stash original platform fops */
|
|
|
|
fops_plat = *(lws_get_fops(context));
|
|
|
|
/* override the active fops */
|
|
|
|
lws_get_fops(context)->open = test_server_fops_open;
|
|
|
|
|
2012-07-20 12:58:38 +08:00
|
|
|
n = 0;
|
2013-01-30 08:02:26 +08:00
|
|
|
while (n >= 0 && !force_exit) {
|
2011-01-27 06:26:52 +00:00
|
|
|
struct timeval tv;
|
|
|
|
|
|
|
|
gettimeofday(&tv, NULL);
|
2010-12-18 15:13:50 +00:00
|
|
|
|
|
|
|
/*
|
2013-01-29 17:57:39 +08:00
|
|
|
* This provokes the LWS_CALLBACK_SERVER_WRITEABLE for every
|
|
|
|
* live websocket connection using the DUMB_INCREMENT protocol,
|
|
|
|
* as soon as it can take more packets (usually immediately)
|
2010-12-18 15:13:50 +00:00
|
|
|
*/
|
|
|
|
|
2014-07-05 11:52:58 +08:00
|
|
|
ms = (tv.tv_sec * 1000) + (tv.tv_usec / 1000);
|
|
|
|
if ((ms - oldms) > 50) {
|
2015-12-11 10:45:35 +08:00
|
|
|
lws_callback_on_writable_all_protocol(context,
|
2015-11-19 13:55:47 +08:00
|
|
|
&protocols[PROTOCOL_DUMB_INCREMENT]);
|
2014-07-05 11:52:58 +08:00
|
|
|
oldms = ms;
|
2011-01-27 06:26:52 +00:00
|
|
|
}
|
2011-01-19 13:11:55 +00:00
|
|
|
|
2013-01-15 12:39:48 +08:00
|
|
|
#ifdef EXTERNAL_POLL
|
|
|
|
/*
|
|
|
|
* this represents an existing server's single poll action
|
|
|
|
* which also includes libwebsocket sockets
|
|
|
|
*/
|
2011-01-19 13:11:55 +00:00
|
|
|
|
2013-01-15 12:39:48 +08:00
|
|
|
n = poll(pollfds, count_pollfds, 50);
|
|
|
|
if (n < 0)
|
|
|
|
continue;
|
|
|
|
|
|
|
|
if (n)
|
|
|
|
for (n = 0; n < count_pollfds; n++)
|
|
|
|
if (pollfds[n].revents)
|
|
|
|
/*
|
|
|
|
* returns immediately if the fd does not
|
|
|
|
* match anything under libwebsockets
|
|
|
|
* control
|
|
|
|
*/
|
2015-12-04 08:43:54 +08:00
|
|
|
if (lws_service_fd(context,
|
2013-01-15 12:39:48 +08:00
|
|
|
&pollfds[n]) < 0)
|
|
|
|
goto done;
|
|
|
|
#else
|
2013-02-02 23:02:56 +08:00
|
|
|
/*
|
|
|
|
* If libwebsockets sockets are all we care about,
|
|
|
|
* you can use this api which takes care of the poll()
|
|
|
|
* and looping through finding who needed service.
|
|
|
|
*
|
|
|
|
* If no socket needs service, it'll return anyway after
|
|
|
|
* the number of ms in the second argument.
|
|
|
|
*/
|
|
|
|
|
2015-12-04 08:43:54 +08:00
|
|
|
n = lws_service(context, 50);
|
2013-01-15 12:39:48 +08:00
|
|
|
#endif
|
2010-12-18 15:13:50 +00:00
|
|
|
}
|
|
|
|
|
2013-01-15 12:39:48 +08:00
|
|
|
#ifdef EXTERNAL_POLL
|
|
|
|
done:
|
2011-01-20 10:23:50 +00:00
|
|
|
#endif
|
|
|
|
|
2015-12-04 08:43:54 +08:00
|
|
|
lws_context_destroy(context);
|
2011-01-23 16:50:33 +00:00
|
|
|
|
2013-01-19 11:32:18 +08:00
|
|
|
lwsl_notice("libwebsockets-test-server exited cleanly\n");
|
|
|
|
|
2015-10-23 08:10:55 +02:00
|
|
|
#ifndef _WIN32
|
2013-01-19 11:32:18 +08:00
|
|
|
closelog();
|
2013-02-06 15:26:58 +09:00
|
|
|
#endif
|
2013-01-19 11:32:18 +08:00
|
|
|
|
2010-10-29 14:15:22 +01:00
|
|
|
return 0;
|
|
|
|
}
|