test-server-libev.c 9.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379
  1. /*
  2. * libwebsockets-test-server - libwebsockets test implementation
  3. *
  4. * Copyright (C) 2011-2016 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. * The person who associated a work with this deed has dedicated
  10. * the work to the public domain by waiving all of his or her rights
  11. * to the work worldwide under copyright law, including all related
  12. * and neighboring rights, to the extent allowed by law. You can copy,
  13. * modify, distribute and perform the work, even for commercial purposes,
  14. * all without asking permission.
  15. *
  16. * The test apps are intended to be adapted for use in your code, which
  17. * may be proprietary. So unlike the library itself, they are licensed
  18. * Public Domain.
  19. */
  20. #include "test-server.h"
  21. int close_testing;
  22. int max_poll_elements;
  23. int debug_level = 7;
  24. volatile int force_exit = 0;
  25. struct lws_context *context;
  26. struct lws_plat_file_ops fops_plat;
  27. /* http server gets files from this path */
  28. #define LOCAL_RESOURCE_PATH INSTALL_DATADIR"/libwebsockets-test-server"
  29. char *resource_path = LOCAL_RESOURCE_PATH;
  30. #if defined(LWS_OPENSSL_SUPPORT) && defined(LWS_HAVE_SSL_CTX_set1_param)
  31. char crl_path[1024] = "";
  32. #endif
  33. /*
  34. * libev dumps their hygiene problems on their users blaming compiler
  35. * http://lists.schmorp.de/pipermail/libev/2008q4/000442.html
  36. */
  37. #if EV_MINPRI == EV_MAXPRI
  38. # define _ev_set_priority(ev, pri) ((ev), (pri))
  39. #else
  40. # define _ev_set_priority(ev, pri) { \
  41. ev_watcher *evw = (ev_watcher *)(void *)ev; \
  42. evw->priority = pri; \
  43. }
  44. #endif
  45. #define _ev_init(ev,cb_) { \
  46. ev_watcher *evw = (ev_watcher *)(void *)ev; \
  47. \
  48. evw->active = evw->pending = 0; \
  49. _ev_set_priority((ev), 0); \
  50. ev_set_cb((ev), cb_); \
  51. }
  52. #define _ev_timer_init(ev, cb, after, _repeat) { \
  53. ev_watcher_time *evwt = (ev_watcher_time *)(void *)ev; \
  54. \
  55. _ev_init(ev, cb); \
  56. evwt->at = after; \
  57. (ev)->repeat = _repeat; \
  58. }
  59. /* singlethreaded version --> no locks */
  60. void test_server_lock(int care)
  61. {
  62. }
  63. void test_server_unlock(int care)
  64. {
  65. }
  66. /*
  67. * This demo server shows how to use libwebsockets for one or more
  68. * websocket protocols in the same server
  69. *
  70. * It defines the following websocket protocols:
  71. *
  72. * dumb-increment-protocol: once the socket is opened, an incrementing
  73. * ascii string is sent down it every 50ms.
  74. * If you send "reset\n" on the websocket, then
  75. * the incrementing number is reset to 0.
  76. *
  77. * lws-mirror-protocol: copies any received packet to every connection also
  78. * using this protocol, including the sender
  79. */
  80. enum demo_protocols {
  81. /* always first */
  82. PROTOCOL_HTTP = 0,
  83. PROTOCOL_DUMB_INCREMENT,
  84. PROTOCOL_LWS_MIRROR,
  85. /* always last */
  86. DEMO_PROTOCOL_COUNT
  87. };
  88. /* list of supported protocols and callbacks */
  89. static struct lws_protocols protocols[] = {
  90. /* first protocol must always be HTTP handler */
  91. {
  92. "http-only", /* name */
  93. callback_http, /* callback */
  94. sizeof (struct per_session_data__http), /* per_session_data_size */
  95. 0, /* max frame size / rx buffer */
  96. },
  97. {
  98. "dumb-increment-protocol",
  99. callback_dumb_increment,
  100. sizeof(struct per_session_data__dumb_increment),
  101. 10,
  102. },
  103. {
  104. "lws-mirror-protocol",
  105. callback_lws_mirror,
  106. sizeof(struct per_session_data__lws_mirror),
  107. 128,
  108. },
  109. {
  110. "lws-status",
  111. callback_lws_status,
  112. sizeof(struct per_session_data__lws_status),
  113. 128,
  114. },
  115. { NULL, NULL, 0, 0 } /* terminator */
  116. };
  117. static const struct lws_extension exts[] = {
  118. {
  119. "permessage-deflate",
  120. lws_extension_callback_pm_deflate,
  121. "permessage-deflate; client_no_context_takeover; client_max_window_bits"
  122. },
  123. {
  124. "deflate-frame",
  125. lws_extension_callback_pm_deflate,
  126. "deflate_frame"
  127. },
  128. { NULL, NULL, NULL /* terminator */ }
  129. };
  130. /* this shows how to override the lws file operations. You don't need
  131. * to do any of this unless you have a reason (eg, want to serve
  132. * compressed files without decompressing the whole archive)
  133. */
  134. static lws_fop_fd_t
  135. test_server_fops_open(const struct lws_plat_file_ops *fops,
  136. const char *vfs_path, const char *vpath,
  137. lws_fop_flags_t *flags)
  138. {
  139. lws_fop_fd_t n;
  140. /* call through to original platform implementation */
  141. n = fops_plat.open(fops, vfs_path, vpath, flags);
  142. lwsl_notice("%s: opening %s, ret %p\n", __func__, vfs_path, n);
  143. return n;
  144. }
  145. void signal_cb(struct ev_loop *loop, struct ev_signal* watcher, int revents)
  146. {
  147. lwsl_notice("Signal caught, exiting...\n");
  148. force_exit = 1;
  149. switch (watcher->signum) {
  150. case SIGTERM:
  151. case SIGINT:
  152. ev_break(loop, EVBREAK_ALL);
  153. break;
  154. default:
  155. signal(SIGABRT, SIG_DFL);
  156. abort();
  157. break;
  158. }
  159. }
  160. static void
  161. ev_timeout_cb (EV_P_ ev_timer *w, int revents)
  162. {
  163. lws_callback_on_writable_all_protocol(context,
  164. &protocols[PROTOCOL_DUMB_INCREMENT]);
  165. }
  166. static struct option options[] = {
  167. { "help", no_argument, NULL, 'h' },
  168. { "debug", required_argument, NULL, 'd' },
  169. { "port", required_argument, NULL, 'p' },
  170. { "ssl", no_argument, NULL, 's' },
  171. { "allow-non-ssl", no_argument, NULL, 'a' },
  172. { "interface", required_argument, NULL, 'i' },
  173. { "closetest", no_argument, NULL, 'c' },
  174. { "libev", no_argument, NULL, 'e' },
  175. #ifndef LWS_NO_DAEMONIZE
  176. { "daemonize", no_argument, NULL, 'D' },
  177. #endif
  178. { "resource_path", required_argument, NULL, 'r' },
  179. { NULL, 0, 0, 0 }
  180. };
  181. int main(int argc, char **argv)
  182. {
  183. int sigs[] = { SIGINT, SIGKILL, SIGTERM, SIGSEGV, SIGFPE };
  184. struct ev_signal signals[ARRAY_SIZE(sigs)];
  185. struct ev_loop *loop = ev_default_loop(0);
  186. struct lws_context_creation_info info;
  187. char interface_name[128] = "";
  188. const char *iface = NULL;
  189. ev_timer timeout_watcher;
  190. char cert_path[1024];
  191. char key_path[1024];
  192. int use_ssl = 0;
  193. int opts = 0;
  194. int n = 0;
  195. #ifndef _WIN32
  196. int syslog_options = LOG_PID | LOG_PERROR;
  197. #endif
  198. #ifndef LWS_NO_DAEMONIZE
  199. int daemonize = 0;
  200. #endif
  201. /*
  202. * take care to zero down the info struct, he contains random garbaage
  203. * from the stack otherwise
  204. */
  205. memset(&info, 0, sizeof info);
  206. info.port = 7681;
  207. while (n >= 0) {
  208. n = getopt_long(argc, argv, "eci:hsap:d:Dr:", options, NULL);
  209. if (n < 0)
  210. continue;
  211. switch (n) {
  212. case 'e':
  213. opts |= LWS_SERVER_OPTION_LIBEV;
  214. break;
  215. #ifndef LWS_NO_DAEMONIZE
  216. case 'D':
  217. daemonize = 1;
  218. #ifndef _WIN32
  219. syslog_options &= ~LOG_PERROR;
  220. #endif
  221. break;
  222. #endif
  223. case 'd':
  224. debug_level = atoi(optarg);
  225. break;
  226. case 's':
  227. use_ssl = 1;
  228. break;
  229. case 'a':
  230. opts |= LWS_SERVER_OPTION_ALLOW_NON_SSL_ON_SSL_PORT;
  231. break;
  232. case 'p':
  233. info.port = atoi(optarg);
  234. break;
  235. case 'i':
  236. strncpy(interface_name, optarg, sizeof interface_name);
  237. interface_name[(sizeof interface_name) - 1] = '\0';
  238. iface = interface_name;
  239. break;
  240. case 'c':
  241. close_testing = 1;
  242. fprintf(stderr, " Close testing mode -- closes on "
  243. "client after 50 dumb increments"
  244. "and suppresses lws_mirror spam\n");
  245. break;
  246. case 'r':
  247. resource_path = optarg;
  248. printf("Setting resource path to \"%s\"\n", resource_path);
  249. break;
  250. case 'h':
  251. fprintf(stderr, "Usage: test-server "
  252. "[--port=<p>] [--ssl] "
  253. "[-d <log bitfield>] "
  254. "[--resource_path <path>]\n");
  255. exit(1);
  256. }
  257. }
  258. #if !defined(LWS_NO_DAEMONIZE) && !defined(WIN32)
  259. /*
  260. * normally lock path would be /var/lock/lwsts or similar, to
  261. * simplify getting started without having to take care about
  262. * permissions or running as root, set to /tmp/.lwsts-lock
  263. */
  264. if (daemonize && lws_daemonize("/tmp/.lwsts-lock")) {
  265. fprintf(stderr, "Failed to daemonize\n");
  266. return 1;
  267. }
  268. #endif
  269. for (n = 0; n < ARRAY_SIZE(sigs); n++) {
  270. _ev_init(&signals[n], signal_cb);
  271. ev_signal_set(&signals[n], sigs[n]);
  272. ev_signal_start(loop, &signals[n]);
  273. }
  274. #ifndef _WIN32
  275. /* we will only try to log things according to our debug_level */
  276. setlogmask(LOG_UPTO (LOG_DEBUG));
  277. openlog("lwsts", syslog_options, LOG_DAEMON);
  278. #endif
  279. /* tell the library what debug level to emit and to send it to syslog */
  280. lws_set_log_level(debug_level, lwsl_emit_syslog);
  281. lwsl_notice("libwebsockets test server libev - license LGPL2.1+SLE\n");
  282. lwsl_notice("(C) Copyright 2010-2016 Andy Green <andy@warmcat.com>\n");
  283. printf("Using resource path \"%s\"\n", resource_path);
  284. info.iface = iface;
  285. info.protocols = protocols;
  286. info.extensions = exts;
  287. info.ssl_cert_filepath = NULL;
  288. info.ssl_private_key_filepath = NULL;
  289. if (use_ssl) {
  290. if (strlen(resource_path) > sizeof(cert_path) - 32) {
  291. lwsl_err("resource path too long\n");
  292. return -1;
  293. }
  294. sprintf(cert_path, "%s/libwebsockets-test-server.pem",
  295. resource_path);
  296. if (strlen(resource_path) > sizeof(key_path) - 32) {
  297. lwsl_err("resource path too long\n");
  298. return -1;
  299. }
  300. sprintf(key_path, "%s/libwebsockets-test-server.key.pem",
  301. resource_path);
  302. info.ssl_cert_filepath = cert_path;
  303. info.ssl_private_key_filepath = key_path;
  304. opts |= LWS_SERVER_OPTION_DO_SSL_GLOBAL_INIT;
  305. }
  306. info.gid = -1;
  307. info.uid = -1;
  308. info.max_http_header_pool = 1;
  309. info.options = opts | LWS_SERVER_OPTION_LIBEV;
  310. context = lws_create_context(&info);
  311. if (context == NULL) {
  312. lwsl_err("libwebsocket init failed\n");
  313. return -1;
  314. }
  315. /*
  316. * this shows how to override the lws file operations. You don't need
  317. * to do any of this unless you have a reason (eg, want to serve
  318. * compressed files without decompressing the whole archive)
  319. */
  320. /* stash original platform fops */
  321. fops_plat = *(lws_get_fops(context));
  322. /* override the active fops */
  323. lws_get_fops(context)->open = test_server_fops_open;
  324. lws_ev_initloop(context, loop, 0);
  325. _ev_timer_init(&timeout_watcher, ev_timeout_cb, 0.05, 0.05);
  326. ev_timer_start(loop, &timeout_watcher);
  327. ev_run(loop, 0);
  328. lws_context_destroy(context);
  329. lwsl_notice("libwebsockets-test-server exited cleanly\n");
  330. #ifndef _WIN32
  331. closelog();
  332. #endif
  333. return 0;
  334. }