test-server-v2.0.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476
  1. /*
  2. * libwebsockets-test-server-v2.0 - libwebsockets test implementation
  3. *
  4. * Copyright (C) 2010-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 <libwebsockets.h>
  21. #include <getopt.h>
  22. #ifndef WIN32
  23. #include <syslog.h>
  24. #endif
  25. int debug_level = 7;
  26. struct lws_context *context;
  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. * This test server is ONLY this .c file, it's radically simpler than the
  35. * pre-v2.0 test servers. For example it has no user callback content or
  36. * defines any protocols.
  37. *
  38. * To achieve that, it uses the LWS protocol plugins. Those in turn
  39. * use libuv. So you must configure with LWS_WITH_PLUGINS (which implies
  40. * libuv) to get this to build.
  41. *
  42. * You can find the individual protocol plugin sources in ../plugins
  43. */
  44. void sighandler(int sig)
  45. {
  46. lws_cancel_service(context);
  47. }
  48. static const struct lws_extension exts[] = {
  49. {
  50. "permessage-deflate",
  51. lws_extension_callback_pm_deflate,
  52. "permessage-deflate"
  53. },
  54. {
  55. "deflate-frame",
  56. lws_extension_callback_pm_deflate,
  57. "deflate_frame"
  58. },
  59. { NULL, NULL, NULL /* terminator */ }
  60. };
  61. /*
  62. * mount handlers for sections of the URL space
  63. */
  64. static const struct lws_http_mount mount_ziptest = {
  65. NULL, /* linked-list pointer to next*/
  66. "/ziptest", /* mountpoint in URL namespace on this vhost */
  67. LOCAL_RESOURCE_PATH"/candide.zip", /* handler */
  68. NULL, /* default filename if none given */
  69. NULL,
  70. NULL,
  71. NULL,
  72. NULL,
  73. 0,
  74. 0,
  75. 0,
  76. 0,
  77. 0,
  78. 0,
  79. LWSMPRO_FILE, /* origin points to a callback */
  80. 8, /* strlen("/ziptest"), ie length of the mountpoint */
  81. NULL,
  82. { NULL, NULL } // sentinel
  83. };
  84. static const struct lws_http_mount mount_post = {
  85. (struct lws_http_mount *)&mount_ziptest, /* linked-list pointer to next*/
  86. "/formtest", /* mountpoint in URL namespace on this vhost */
  87. "protocol-post-demo", /* handler */
  88. NULL, /* default filename if none given */
  89. NULL,
  90. NULL,
  91. NULL,
  92. NULL,
  93. 0,
  94. 0,
  95. 0,
  96. 0,
  97. 0,
  98. 0,
  99. LWSMPRO_CALLBACK, /* origin points to a callback */
  100. 9, /* strlen("/formtest"), ie length of the mountpoint */
  101. NULL,
  102. { NULL, NULL } // sentinel
  103. };
  104. /*
  105. * mount a filesystem directory into the URL space at /
  106. * point it to our /usr/share directory with our assets in
  107. * stuff from here is autoserved by the library
  108. */
  109. static const struct lws_http_mount mount = {
  110. (struct lws_http_mount *)&mount_post, /* linked-list pointer to next*/
  111. "/", /* mountpoint in URL namespace on this vhost */
  112. LOCAL_RESOURCE_PATH, /* where to go on the filesystem for that */
  113. "test.html", /* default filename if none given */
  114. NULL,
  115. NULL,
  116. NULL,
  117. NULL,
  118. 0,
  119. 0,
  120. 0,
  121. 0,
  122. 0,
  123. 0,
  124. LWSMPRO_FILE, /* mount type is a directory in a filesystem */
  125. 1, /* strlen("/"), ie length of the mountpoint */
  126. NULL,
  127. { NULL, NULL } // sentinel
  128. };
  129. /*
  130. * this sets a per-vhost, per-protocol option name:value pair
  131. * the effect is to set this protocol to be the default one for the vhost,
  132. * ie, selected if no Protocol: header is sent with the ws upgrade.
  133. */
  134. static const struct lws_protocol_vhost_options pvo_opt = {
  135. NULL,
  136. NULL,
  137. "default",
  138. "1"
  139. };
  140. static const struct lws_protocol_vhost_options pvo_opt4a = {
  141. NULL,
  142. NULL,
  143. "raw", /* indicate we are the protocol that gets raw connections */
  144. "1"
  145. };
  146. static const struct lws_protocol_vhost_options pvo_opt4 = {
  147. &pvo_opt4a,
  148. NULL,
  149. "fifo-path", /* tell the raw test plugin to open a raw file here */
  150. "/tmp/lws-test-raw"
  151. };
  152. /*
  153. * We must enable the plugin protocols we want into our vhost with a
  154. * linked-list. We can also give the plugin per-vhost options here.
  155. */
  156. static const struct lws_protocol_vhost_options pvo_4 = {
  157. NULL,
  158. &pvo_opt4, /* set us as the protocol who gets raw connections */
  159. "protocol-lws-raw-test",
  160. "" /* ignored, just matches the protocol name above */
  161. };
  162. static const struct lws_protocol_vhost_options pvo_3 = {
  163. &pvo_4,
  164. NULL,
  165. "protocol-post-demo",
  166. "" /* ignored, just matches the protocol name above */
  167. };
  168. static const struct lws_protocol_vhost_options pvo_2 = {
  169. &pvo_3,
  170. NULL,
  171. "lws-status",
  172. "" /* ignored, just matches the protocol name above */
  173. };
  174. static const struct lws_protocol_vhost_options pvo_1 = {
  175. &pvo_2,
  176. NULL,
  177. "lws-mirror-protocol",
  178. ""
  179. };
  180. static const struct lws_protocol_vhost_options pvo = {
  181. &pvo_1,
  182. &pvo_opt,
  183. "dumb-increment-protocol",
  184. ""
  185. };
  186. static void signal_cb(uv_signal_t *watcher, int signum)
  187. {
  188. lwsl_err("Signal %d caught, exiting...\n", watcher->signum);
  189. switch (watcher->signum) {
  190. case SIGTERM:
  191. case SIGINT:
  192. break;
  193. default:
  194. signal(SIGABRT, SIG_DFL);
  195. abort();
  196. break;
  197. }
  198. lws_libuv_stop(context);
  199. }
  200. static const struct option options[] = {
  201. { "help", no_argument, NULL, 'h' },
  202. { "debug", required_argument, NULL, 'd' },
  203. { "port", required_argument, NULL, 'p' },
  204. { "ssl", no_argument, NULL, 's' },
  205. { "allow-non-ssl", no_argument, NULL, 'a' },
  206. { "interface", required_argument, NULL, 'i' },
  207. { "ssl-cert", required_argument, NULL, 'C' },
  208. { "ssl-key", required_argument, NULL, 'K' },
  209. { "ssl-ca", required_argument, NULL, 'A' },
  210. #if defined(LWS_OPENSSL_SUPPORT)
  211. { "ssl-verify-client", no_argument, NULL, 'v' },
  212. #if defined(LWS_HAVE_SSL_CTX_set1_param)
  213. { "ssl-crl", required_argument, NULL, 'R' },
  214. #endif
  215. #endif
  216. #ifndef LWS_NO_DAEMONIZE
  217. { "daemonize", no_argument, NULL, 'D' },
  218. #endif
  219. { "resource_path", required_argument, NULL, 'r' },
  220. { NULL, 0, 0, 0 }
  221. };
  222. static const char * const plugin_dirs[] = {
  223. INSTALL_DATADIR"/libwebsockets-test-server/plugins/",
  224. NULL
  225. };
  226. int main(int argc, char **argv)
  227. {
  228. struct lws_context_creation_info info;
  229. char interface_name[128] = "";
  230. const char *iface = NULL;
  231. char cert_path[1024] = "";
  232. char key_path[1024] = "";
  233. char ca_path[1024] = "";
  234. int uid = -1, gid = -1;
  235. int use_ssl = 0;
  236. int opts = 0;
  237. int n = 0;
  238. #ifndef _WIN32
  239. int syslog_options = LOG_PID | LOG_PERROR;
  240. #endif
  241. #ifndef LWS_NO_DAEMONIZE
  242. int daemonize = 0;
  243. #endif
  244. /*
  245. * take care to zero down the info struct, he contains random garbaage
  246. * from the stack otherwise
  247. */
  248. memset(&info, 0, sizeof info);
  249. info.port = 7681;
  250. while (n >= 0) {
  251. n = getopt_long(argc, argv, "i:hsap:d:Dr:C:K:A:R:vu:g:",
  252. (struct option *)options, NULL);
  253. if (n < 0)
  254. continue;
  255. switch (n) {
  256. #ifndef LWS_NO_DAEMONIZE
  257. case 'D':
  258. daemonize = 1;
  259. #ifndef _WIN32
  260. syslog_options &= ~LOG_PERROR;
  261. #endif
  262. break;
  263. #endif
  264. case 'u':
  265. uid = atoi(optarg);
  266. break;
  267. case 'g':
  268. gid = atoi(optarg);
  269. break;
  270. case 'd':
  271. debug_level = atoi(optarg);
  272. break;
  273. case 's':
  274. use_ssl = 1;
  275. break;
  276. case 'a':
  277. opts |= LWS_SERVER_OPTION_ALLOW_NON_SSL_ON_SSL_PORT;
  278. break;
  279. case 'p':
  280. info.port = atoi(optarg);
  281. break;
  282. case 'i':
  283. strncpy(interface_name, optarg, sizeof interface_name);
  284. interface_name[(sizeof interface_name) - 1] = '\0';
  285. iface = interface_name;
  286. break;
  287. case 'r':
  288. resource_path = optarg;
  289. printf("Setting resource path to \"%s\"\n", resource_path);
  290. break;
  291. case 'C':
  292. strncpy(cert_path, optarg, sizeof(cert_path) - 1);
  293. cert_path[sizeof(cert_path) - 1] = '\0';
  294. break;
  295. case 'K':
  296. strncpy(key_path, optarg, sizeof(key_path) - 1);
  297. key_path[sizeof(key_path) - 1] = '\0';
  298. break;
  299. case 'A':
  300. strncpy(ca_path, optarg, sizeof(ca_path) - 1);
  301. ca_path[sizeof(ca_path) - 1] = '\0';
  302. break;
  303. #if defined(LWS_OPENSSL_SUPPORT)
  304. case 'v':
  305. use_ssl = 1;
  306. opts |= LWS_SERVER_OPTION_REQUIRE_VALID_OPENSSL_CLIENT_CERT;
  307. break;
  308. #if defined(LWS_HAVE_SSL_CTX_set1_param)
  309. case 'R':
  310. strncpy(crl_path, optarg, sizeof(crl_path) - 1);
  311. crl_path[sizeof(crl_path) - 1] = '\0';
  312. break;
  313. #endif
  314. #endif
  315. case 'h':
  316. fprintf(stderr, "Usage: test-server "
  317. "[--port=<p>] [--ssl] "
  318. "[-d <log bitfield>] "
  319. "[--resource_path <path>]\n");
  320. exit(1);
  321. }
  322. }
  323. #if !defined(LWS_NO_DAEMONIZE) && !defined(WIN32)
  324. /*
  325. * normally lock path would be /var/lock/lwsts or similar, to
  326. * simplify getting started without having to take care about
  327. * permissions or running as root, set to /tmp/.lwsts-lock
  328. */
  329. if (daemonize && lws_daemonize("/tmp/.lwsts-lock")) {
  330. fprintf(stderr, "Failed to daemonize\n");
  331. return 10;
  332. }
  333. #endif
  334. signal(SIGINT, sighandler);
  335. #ifndef _WIN32
  336. /* we will only try to log things according to our debug_level */
  337. setlogmask(LOG_UPTO (LOG_DEBUG));
  338. openlog("lwsts", syslog_options, LOG_DAEMON);
  339. #endif
  340. /* tell the library what debug level to emit and to send it to syslog */
  341. lws_set_log_level(debug_level, lwsl_emit_syslog);
  342. lwsl_notice("libwebsockets test server - license LGPL2.1+SLE\n");
  343. lwsl_notice("(C) Copyright 2010-2016 Andy Green <andy@warmcat.com>\n");
  344. lwsl_notice(" Using resource path \"%s\"\n", resource_path);
  345. info.iface = iface;
  346. info.protocols = NULL; /* all protocols from lib / plugins */
  347. info.ssl_cert_filepath = NULL;
  348. info.ssl_private_key_filepath = NULL;
  349. info.gid = gid;
  350. info.uid = uid;
  351. info.max_http_header_pool = 16;
  352. info.options = opts | LWS_SERVER_OPTION_FALLBACK_TO_RAW |
  353. LWS_SERVER_OPTION_VALIDATE_UTF8 |
  354. LWS_SERVER_OPTION_LIBUV; /* plugins require this */
  355. if (use_ssl) {
  356. if (strlen(resource_path) > sizeof(cert_path) - 32) {
  357. lwsl_err("resource path too long\n");
  358. return -1;
  359. }
  360. if (!cert_path[0])
  361. sprintf(cert_path, "%s/libwebsockets-test-server.pem",
  362. resource_path);
  363. if (strlen(resource_path) > sizeof(key_path) - 32) {
  364. lwsl_err("resource path too long\n");
  365. return -1;
  366. }
  367. if (!key_path[0])
  368. sprintf(key_path, "%s/libwebsockets-test-server.key.pem",
  369. resource_path);
  370. info.ssl_cert_filepath = cert_path;
  371. info.ssl_private_key_filepath = key_path;
  372. if (ca_path[0])
  373. info.ssl_ca_filepath = ca_path;
  374. /* redirect guys coming on http */
  375. info.options |= LWS_SERVER_OPTION_REDIRECT_HTTP_TO_HTTPS;
  376. }
  377. info.extensions = exts;
  378. info.timeout_secs = 5;
  379. info.ssl_cipher_list = "ECDHE-ECDSA-AES256-GCM-SHA384:"
  380. "ECDHE-RSA-AES256-GCM-SHA384:"
  381. "DHE-RSA-AES256-GCM-SHA384:"
  382. "ECDHE-RSA-AES256-SHA384:"
  383. "HIGH:!aNULL:!eNULL:!EXPORT:"
  384. "!DES:!MD5:!PSK:!RC4:!HMAC_SHA1:"
  385. "!SHA1:!DHE-RSA-AES128-GCM-SHA256:"
  386. "!DHE-RSA-AES128-SHA256:"
  387. "!AES128-GCM-SHA256:"
  388. "!AES128-SHA256:"
  389. "!DHE-RSA-AES256-SHA256:"
  390. "!AES256-GCM-SHA384:"
  391. "!AES256-SHA256";
  392. /* tell lws to look for protocol plugins here */
  393. info.plugin_dirs = plugin_dirs;
  394. /* tell lws about our mount we want */
  395. info.mounts = &mount;
  396. /*
  397. * give it our linked-list of Per-Vhost Options, these control
  398. * which protocols (from plugins) are allowed to be enabled on
  399. * our vhost
  400. */
  401. info.pvo = &pvo;
  402. /*
  403. * As it is, this creates the context and a single Vhost at the same
  404. * time. You can use LWS_SERVER_OPTION_EXPLICIT_VHOSTS option above
  405. * to just create the context, and call lws_create_vhost() afterwards
  406. * multiple times with different info to get multiple listening vhosts.
  407. */
  408. context = lws_create_context(&info);
  409. if (context == NULL) {
  410. lwsl_err("libwebsocket init failed\n");
  411. return -1;
  412. }
  413. /* libuv event loop */
  414. lws_uv_sigint_cfg(context, 1, signal_cb);
  415. if (lws_uv_initloop(context, NULL, 0))
  416. lwsl_err("lws_uv_initloop failed\n");
  417. else
  418. lws_libuv_run(context, 0);
  419. /* when we decided to exit the event loop */
  420. lws_context_destroy(context);
  421. lwsl_notice("libwebsockets-test-server exited cleanly\n");
  422. #ifndef _WIN32
  423. closelog();
  424. #endif
  425. return 0;
  426. }